Skip to content

Commit

Permalink
feat(are): add functions to check the values of a rest parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
sciborrudnicki committed Sep 17, 2021
1 parent e541187 commit 823dac3
Show file tree
Hide file tree
Showing 11 changed files with 850 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/are/lib/are-bigint.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Function.
import { isArray } from '../../is/lib/is-array.func';
import { isBigInt } from '../../is/lib/is-big-int.func';
import { isFunction } from '../../is/lib/is-function.func';
import { resultCallback } from '../../lib/result-callback.func';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ForEachCallback } from '../../type/foreach-callback.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if the values are a `bigint` type by using `every()`, `forEach()` and `some()` methods of the returned object.
* @param value A rest parameter of `any` type to check its elements against a `bigint` type.
* @returns The return value is an `object` of `every()`, `some()` and `forEach()` as a methods of checking supplied `values`.
* @angularpackage
*/
export const areBigInt = (...values: any[]) => {
return {
/**
* Checks if every of the provided `values` of `areBigInt()` is a `string` type.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of
* this check, and `payload` of the default `CallbackPayload` shape with optional properties from the provided `payload`, to handle
* them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is a `boolean` indicating whether the provided `values` of `areBigInt()` are a `bigint` type.
*/
every: <Payload extends object>(
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): boolean =>
callback(
values.every((value) => isBigInt(value)),
values,
{ name: areBigInt.name, ...payload } as Payload
),

/**
* The `forEach()` method executes a provided callback function once for each element of the supplied `values` of `areBigInt()`.
* @param forEachCallback A callback `function` of `ForEachCallback` type with parameters, the `value` that has been checked, the
* `result` of this check, `index` of each element, the provided `values` and `payload` of the default `CallbackPayload` shape with
* optional properties from the provided `payload`, to handle.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is void.
*/
forEach: <Payload extends object>(
forEachCallback: ForEachCallback<any, CallbackPayload<Payload>>,
payload?: CallbackPayload<Payload>
) => {
if (isArray(values) && isFunction(forEachCallback)) {
values.forEach((value, index) =>
forEachCallback(isBigInt(value), value, index, values, {
name: areBigInt.name,
...payload,
} as CallbackPayload<Payload>)
);
}
},

/**
* Checks if some of the provided `values` of `areBigInt()` are a `bigint` type.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of
* this check, and `payload` of the default `CallbackPayload` shape with optional properties from the provided `payload`, to handle
* them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is a `boolean` indicating whether some of the provided `values` of `areBigInt()` are a `bigint` type.
*/
some: <Payload extends object>(
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): boolean =>
callback(
isArray(values) ? values.some((value) => isBigInt(value)) : false,
values,
{ name: areBigInt.name, ...payload } as Payload
),
};
};
79 changes: 79 additions & 0 deletions src/are/lib/are-boolean.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Function.
import { isArray } from '../../is/lib/is-array.func';
import { isFunction } from '../../is/lib/is-function.func';
import { isBoolean } from '../../is/lib/is-boolean.func';
import { resultCallback } from '../../lib/result-callback.func';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ForEachCallback } from '../../type/foreach-callback.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if the values are a `boolean` type or an instance of `Boolean` by using `every()`, `forEach()` and `some()` methods of the
* returned object.
* @param value A rest parameter of `any` type to check its elements against a `boolean` type or an instance of `Boolean`.
* @returns The return value is an `object` with `every()`, `some()` and `forEach()` as a methods of checking supplied `values`.
* @angularpackage
*/
export const areBoolean = (...values: any[]) => {
return {
/**
* Checks if every of the provided `values` of `areBoolean()` is a `boolean` type or an instance of `Boolean`.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of
* this check, and `payload` of the default `CallbackPayload` shape with optional properties from the provided `payload`, to handle
* them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is a `boolean` indicating whether the provided values of `areBoolean()` are a `boolean` type or an instance
* of `Boolean`.
*/
every: <Payload extends object>(
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): boolean =>
callback(
values.every((value) => isBoolean(value)),
values,
{ name: areBoolean.name, ...payload } as Payload
),

/**
* The `forEach()` method executes a provided callback function once for each element of the supplied `values` of `areBoolean()`.
* @param forEachCallback A callback `function` of `ForEachCallback` type with parameters, the `value` that has been checked, the
* `result` of this check, `index` of each element, the provided `values` and `payload` of the default `CallbackPayload` shape with
* optional properties from the provided `payload`, to handle them before the `result` return.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is void.
*/
forEach: <Payload extends object>(
forEachCallback: ForEachCallback<any, CallbackPayload<Payload>>,
payload?: CallbackPayload<Payload>
) => {
if (isArray(values) && isFunction(forEachCallback)) {
values.forEach((value, index) =>
forEachCallback(isBoolean(value), value, index, values, {
name: areBoolean.name,
...payload,
} as CallbackPayload<Payload>)
);
}
},

/**
* Checks if some of the provided `values` of `areBoolean()` are a `boolean` type or an instance of `Boolean`.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of
* this check, and `payload` of the default `CallbackPayload` shape with optional properties from the provided `payload`, to handle
* them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is a `boolean` indicating whether some of the provided `values` of `areBoolean()` are a `boolean` type or
* an instance of `Boolean`.
*/
some: <Payload extends object>(
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): boolean =>
callback(
isArray(values) ? values.some((value) => isBoolean(value)) : false,
values,
{ name: areBoolean.name, ...payload } as Payload
),
};
};
76 changes: 76 additions & 0 deletions src/are/lib/are-date.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Function.
import { isArray } from '../../is/lib/is-array.func';
import { isFunction } from '../../is/lib/is-function.func';
import { isDate } from '../../is/lib/is-date.func';
import { resultCallback } from '../../lib/result-callback.func';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ForEachCallback } from '../../type/foreach-callback.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if the values are `Date` by using `every()`, `forEach()` and `some()` methods of the returned object.
* @param value A rest parameter of `any` type to check its elements against `Date`.
* @returns The return value is an `object` with `every()`, `some()` and `forEach()` as a methods of checking supplied `values`.
* @angularpackage
*/
export const areDate = (...values: any[]) => {
return {
/**
* Checks if every of the provided `values` of `areDate()` is a `Date`.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of
* this check, and `payload` of the default `CallbackPayload` shape with optional properties from the provided `payload`, to handle
* them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is a `boolean` indicating whether the provided `values` of `areDate()` are `Date`.
*/
every: <Payload extends object>(
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): boolean =>
callback(
values.every((value) => isDate(value)),
values,
{ name: areDate.name, ...payload } as Payload
),

/**
* The `forEach()` method executes a provided callback function once for each element of the supplied `values` of `areDate()`.
* @param forEachCallback A callback `function` of `ForEachCallback` type with parameters, the `value` that has been checked, the
* `result` of this check, `index` of each element, the provided `values` and `payload` of the default `CallbackPayload` shape with
* optional properties from the provided `payload`, to handle them before the `result` return.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is void.
*/
forEach: <Payload extends object>(
forEachCallback: ForEachCallback<any, CallbackPayload<Payload>>,
payload?: CallbackPayload<Payload>
) => {
if (isArray(values) && isFunction(forEachCallback)) {
values.forEach((value, index) =>
forEachCallback(isDate(value), value, index, values, {
name: areDate.name,
...payload,
} as CallbackPayload<Payload>)
);
}
},

/**
* Checks if some of the provided `values` of `areDate()` are `Date`.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of
* this check, and `payload` of the default `CallbackPayload` shape with optional properties from the provided `payload`, to handle
* them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is a `boolean` indicating whether some of the provided `values` of `areDate()` are `Date`.
*/
some: <Payload extends object>(
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): boolean =>
callback(
isArray(values) ? values.some((value) => isDate(value)) : false,
values,
{ name: areDate.name, ...payload } as Payload
),
};
};
76 changes: 76 additions & 0 deletions src/are/lib/are-defined.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Function.
import { isArray } from '../../is/lib/is-array.func';
import { isFunction } from '../../is/lib/is-function.func';
import { isDefined } from '../../is/lib/is-defined.func';
import { resultCallback } from '../../lib/result-callback.func';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ForEachCallback } from '../../type/foreach-callback.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if the values are defined by using `every()`, `forEach()` and `some()` methods of the returned object.
* @param value A rest parameter of `any` type to check its elements against defined.
* @returns The return value is an `object` with `every()`, `some()` and `forEach()` as a methods of checking supplied `values`.
* @angularpackage
*/
export const areDefined = (...values: any[]) => {
return {
/**
* Checks if every of the provided `values` of `areDefined()` is defined.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of
* this check, and `payload` of the default `CallbackPayload` shape with optional properties from the provided `payload`, to handle
* them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is a `boolean` indicating whether the provided `values` of `areDefined()` are defined.
*/
every: <Payload extends object>(
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): boolean =>
callback(
values.every((value) => isDefined(value)),
values,
{ name: areDefined.name, ...payload } as Payload
),

/**
* The `forEach()` method executes a provided callback function once for each element of the supplied `values` of `areDefined()`.
* @param forEachCallback A callback `function` of `ForEachCallback` type with parameters, the `value` that has been checked, the
* `result` of this check, `index` of each element, the provided `values` and `payload` of the default `CallbackPayload` shape with
* optional properties from the provided `payload`, to handle them before the `result` return.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is void.
*/
forEach: <Payload extends object>(
forEachCallback: ForEachCallback<any, CallbackPayload<Payload>>,
payload?: CallbackPayload<Payload>
) => {
if (isArray(values) && isFunction(forEachCallback)) {
values.forEach((value, index) =>
forEachCallback(isDefined(value), value, index, values, {
name: areDefined.name,
...payload,
} as CallbackPayload<Payload>)
);
}
},

/**
* Checks if some of the provided `values` of `areDefined()` are defined.
* @param callback A callback `function` of `ResultCallback` type with parameters, the `value` that has been checked, the `result` of
* this check, and `payload` of the default `CallbackPayload` shape with optional properties from the provided `payload`, to handle
* them before the `result` return. By default, it uses `resultCallback()` function.
* @param payload An optional `object` of `CallbackPayload` that is assigned to the `payload` of the supplied `callback` function.
* @returns The return value is a `boolean` indicating whether some of the provided `values` of `areDefined()` are defined.
*/
some: <Payload extends object>(
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>,
): boolean =>
callback(
isArray(values) ? values.some((value) => isDefined(value)) : false,
values,
{ name: areDefined.name, ...payload } as Payload
),
};
};

0 comments on commit 823dac3

Please sign in to comment.