Skip to content

1.0.0 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ Manages the callback [`function`][js-function] of a [`ResultCallback`](#resultca

**Instance methods:**

| Callback.prototype. | Description |
| :------------------------------------------------ | :---------- |
| [`getCallback()`][callback-getcallback] | Gets from the storage specified by-name callback [`function`][js-function] of a [`ResultCallback`](#resultcallback) type |
| [`setErrorCallback()`][callback-seterrorcallback] | Sets a callback [`function`][js-function] of a [`ResultCallback`](#resultcallback) type that throws [`ValidationError`][error-validationerror] with a specified message on a state from the provided `throwOnState` to the storage under the given allowed name restricted by `AllowNames` |
| [`setResultCallback()`][callback-setcallback] | Sets a callback of a [`ResultCallback`](#resultcallback) type to the storage under the given allowed `name`, which is restricted by `AllowNames` |
| Callback.prototype. | Description |
| :-------------------------------------------------- | :---------- |
| [`getCallback()`][callback-getcallback] | Gets from the storage specified by-name callback [`function`][js-function] of a [`ResultCallback`](#resultcallback) type |
| [`setErrorCallback()`][callback-seterrorcallback] | Sets a callback [`function`][js-function] of a [`ResultCallback`](#resultcallback) type that throws [`ValidationError`][error-validationerror] with a specified message on a state from the provided `throwOnState` to the storage under the given allowed name restricted by `AllowNames` |
| [`setResultCallback()`][callback-setresultcallback] | Sets a callback of a [`ResultCallback`](#resultcallback) type to the storage under the given allowed `name`, which is restricted by `AllowNames` |

[callback-getcallback]: #callbackprototypegetcallback
[callback-setcallback]: #callbackprototypesetcallback
[callback-seterrorcallback]: #callbackprototypeseterrorcallback
[callback-setresultcallback]: #callbackprototypesetresultcallback

<br>

Expand Down Expand Up @@ -177,13 +177,13 @@ static defineResultCallback<Payload extends object>(

| Name | Description |
| :----------------------- | :---------- |
| `Payload extends object` | A generic `Payload` variable that is constrained by the [`object`][js-object] type, is used by the type of `resultHandler` parameter and is linked with the return type `ResultCallback<Payload>`. Its value can be captured from the provided `capturePayload` optional parameter |
| `Payload extends object` | The shape of the optional payload parameter of the [`ResultCallback`](#resultcallback) and [`ResultHandler`](#resulthandler) function, which is constrained by the [`object`][js-object] type. Its value can be captured from a type of the provided `capturePayload` optional parameter |

**Parameters:**

| Name: type | Description |
| :-------------------------------------- | :---------- |
| `resultHandler: ResultHandler<Payload>` | The [`function`][js-function] of [`ResultHandler`](#resulthandler) type to handle the `result` and optional `payload` of [`ResultCallback`](#resultcallback) function |
| `resultHandler: ResultHandler<Payload>` | The [`function`][js-function] that is guarded by the [`ResultHandler`](#resulthandler) type to handle the result and optional payload of the [`ResultCallback`](#resultcallback) function |
| `capturePayload?: Payload` | An optional [`object`][js-object] of generic type `Payload` that is used only to capture the value by the generic type variable `Payload` |

**Returns:**
Expand Down Expand Up @@ -332,13 +332,13 @@ static guard<Payload extends object>(

| Name: type | Description |
| :---------------------------------------- | :---------- |
| `resultCallback: ResultCallback<Payload>` | The [`function`][js-function] of [`ResultCallback`](#resultcallback) type with the shape of payload from the generic variable `Payload` to guard |
| `resultCallback: ResultCallback<Payload>` | The [`function`][js-function] of [`ResultCallback`](#resultcallback) type with the shape of payload from the generic type variable `Payload` to guard |

**Returns:**

| Returns | Type | Description |
| :------------------------------------------ | :-------: | :----------- |
| `resultCallback is ResultCallback<Payload>` | `boolean` | The **return type** is [`boolean`][js-boolean] as the result of its statement that indicates the provided `resultCallback` is a [`function`][js-function] of a [`ResultCallback`](#resultcallback) type with the shape of payload from the generic variable `Payload` |
| `resultCallback is ResultCallback<Payload>` | `boolean` | The **return type** is [`boolean`][js-boolean] as the result of its statement that indicates the provided `resultCallback` is a [`function`][js-function] of a [`ResultCallback`](#resultcallback) type with the shape of payload from the generic type variable `Payload` |

The **return value** is a `boolean` indicating whether the provided `resultCallback` parameter is a [`function`][js-function].

Expand Down Expand Up @@ -371,7 +371,7 @@ static isCallback<AllowNames extends string>(

| Name | Description |
| :-------------------------- | :---------- |
| `AllowNames extends string` | An optional generic variable of `AllowNames` name that is constrained by the [`string`][js-string] type and is used to indicate allowed names under which callback functions can be stored for the return type `value is Callback<AllowNames>`. Its value can be captured from the provided `allowNames` rest parameter |
| `AllowNames extends string` | An optional generic type variable of `AllowNames` name that is constrained by the [`string`][js-string] type and is used to indicate allowed names under which callback functions can be stored for the return type `value is Callback<AllowNames>`. Its value can be captured from the provided `allowNames` rest parameter |

**Parameters:**

Expand All @@ -384,7 +384,7 @@ static isCallback<AllowNames extends string>(

| Returns | Type | Description |
| :------------------------------ | :-------: | :---------- |
| `value is Callback<AllowNames>` | `boolean` | The **return type** is `boolean` as the result of its statement that indicates the provided `value` is a [`Callback`](#callback) with allowed names from the provided `allowNames` parameter or generic variable `AllowNames` |
| `value is Callback<AllowNames>` | `boolean` | The **return type** is `boolean` as the result of its statement that indicates the provided `value` is a [`Callback`](#callback) with allowed names from the provided `allowNames` parameter or generic type variable `AllowNames` |

The **return value** is a `boolean` indicating whether the `value` is an instance of [`Callback`](#callback) .

Expand Down Expand Up @@ -428,7 +428,7 @@ new Callback<AllowNames extends string>(...allowNames: AllowNames[]) {

| Name | Description |
| :---------------------------- | :---------- |
| `AllowedNames extends string` | A generic variable `AllowNames` that is constrained by the [`string`][js-string] type and is used to **restrict** allowed names under which callback functions can be stored. By default, its value is captured from the provided `allowNames` rest parameter |
| `AllowedNames extends string` | A generic type variable `AllowNames` that is constrained by the [`string`][js-string] type and is used to **restrict** allowed names under which callback functions can be stored. By default, its value is captured from the provided `allowNames` rest parameter |

**Parameters:**

Expand Down Expand Up @@ -511,7 +511,7 @@ callback
```

```typescript
// Generic variable payload example usage.
// Generic type variable payload example usage.
import { Callback } from '@angular-package/callback';
/**
* Initialize `Callback`.
Expand Down Expand Up @@ -804,7 +804,7 @@ firstNameCallback(false, { id: 5, name: 'there is no name' });

#### `CallbackPayload`


![experimental]

Experimental shape for a generic type variable `Payload`.

Expand Down Expand Up @@ -887,8 +887,6 @@ isString('it is a string', (result: boolean, payload) => {

#### `ResultCallback`

![experimental]

Represents a callback function with parameters, a `result` of a [`boolean`][js-boolean] type, and an optional `payload` of a generic type `Payload`.

```typescript
Expand Down
6 changes: 4 additions & 2 deletions src/lib/callback.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export class Callback<AllowNames extends string> {
/**
* Defines the `function` of `ResultCallback` type that contains a `ResultHandler` function to handle the `result` and optional
* `payload`.
* @param resultHandler The function of `ResultHandler` type to handle the `result` and optional `payload` of `ResultCallback` function.
* @var Payload The shape of the optional payload parameter of the `ResultCallback` and `ResultHandler` function, which is constrained by
* the `object` type. Its value can be captured from a type of the provided `capturePayload` optional parameter.
* @param resultHandler The `function` that is guarded by the `ResultHandler` type to handle the result and optional payload of the
* `ResultCallback` function.
* @param capturePayload An optional `object` of generic type `Payload` that is used only to capture the value by the generic type
* variable `Payload`.
* @returns The return value is a function of a `ResultCallback` type that contains a function that handles its result.
Expand Down Expand Up @@ -99,7 +102,6 @@ export class Callback<AllowNames extends string> {
//#endregion

/**
* Manages the callback function of a `ResultCallback` type.
* Initialize an instance of a `Callback` with the allowed names under which callback functions can be stored.
* @param allowNames A rest parameter of allowed names of a `string` type, under which callback functions can be stored.
*/
Expand Down
31 changes: 16 additions & 15 deletions src/test/callback.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,6 @@ testing.describe(Callback.name, () => {
});

//#region Callback public methods
testing.describe(`.prototype.setCallback()`, () => {
testing.it(
'should properly sets the callback function under the `setCallback` name',
() => {
toBe.undefined(callback.getCallback('setCallback'));
callback.setResultCallback('setCallback', (result: boolean, value: any) => {
if (is.false(result)) {
throw new ValidationError('The given value must be a string type');
}
});
toBe.function(callback.getCallback('setCallback'));
}
);
});

testing.describe(`.prototype.getCallback()`, () => {
try {
is.string(5, callback.getCallback('setCallback'));
Expand Down Expand Up @@ -234,5 +219,21 @@ testing.describe(Callback.name, () => {
}
});
});

testing.describe(`.prototype.setResultCallback()`, () => {
testing.it(
'should properly sets the callback function under the `setCallback` name',
() => {
toBe.undefined(callback.getCallback('setCallback'));
callback.setResultCallback('setCallback', (result: boolean, value: any) => {
if (is.false(result)) {
throw new ValidationError('The given value must be a string type');
}
});
toBe.function(callback.getCallback('setCallback'));
}
);
});

//#endregion
});