Skip to content

Commit

Permalink
refactor(is): change the callback parameter payload type to `Callba…
Browse files Browse the repository at this point in the history
…ckPayload` instead of `typeof payload`, change the `payload` type for some functions, change the way of checking for some functions, remove the default payload passed to the callback if not necessary, remove use of `typeOf() function if not necessary, update jsdoc.
  • Loading branch information
sciborrudnicki committed Sep 20, 2021
1 parent b9f8166 commit 6d190f5
Show file tree
Hide file tree
Showing 34 changed files with 431 additions and 504 deletions.
24 changes: 10 additions & 14 deletions src/is/lib/is-array.func.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
// Function.
import { resultCallback } from '../../lib/result-callback.func';
import { typeOf } from '../../lib/type-of.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any value is an object type, an instance of `Array` and the obtained type from its `Object.prototype` is equal to `'array'`.
* The value is also checked by the `isArray()` method of `Array`.
* @param value The `value` of any type to check.
* Checks if any value is the type obtained from its `Object.prototype` equal to `'array'` or an `object` type. The value is also checked
* by the `isArray()` method of `Array`.
* @param value The value of any type to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is an `Array`.
* @angularpackage
*/
export const isArray = <Type = any, Payload extends object = object>(
export const isArray = <Type, Payload extends object = object>(
value: any,
callback: ResultCallback<any, typeof payload> = resultCallback,
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is Array<Type> =>
callback(
typeOf(value) === 'array' &&
Array.isArray(value) === true &&
value instanceof Array === true &&
typeof value === 'object',
(typeOf(value) === 'array' || typeof value === 'object') &&
Array.isArray(value),
value,
{ name: isArray.name, ...payload } as any
payload
);
18 changes: 6 additions & 12 deletions src/is/lib/is-big-int.func.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
// Function.
import { resultCallback } from '../../lib/result-callback.func';
import { typeOf } from '../../lib/type-of.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any `value` is a `bigint` type and the obtained type from its `Object.prototype` is equal to `'bigint'`.
* @param value The `value` of `any` type to check.
* Checks if any value is a `bigint` type.
* @param value The value of `any` type to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is a `bigint`.
* @angularpackage
*/
export const isBigInt = <Payload extends object>(
value: any,
callback: ResultCallback<any, typeof payload> = resultCallback,
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is bigint =>
callback(typeOf(value) === 'bigint' && typeof value === 'bigint', value, {
name: isBigInt.name,
...payload,
} as Payload);
callback(typeof value === 'bigint', value, payload);
22 changes: 11 additions & 11 deletions src/is/lib/is-boolean-object.func.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
// Function.
import { resultCallback } from '../../lib/result-callback.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
import { typeOf } from '../../lib/type-of.func';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any `value` is an `object` type and an instance of both `Boolean` and `Object`.
* @param value The `value` of any type to check.
* Checks if any value is of the type obtained from its `Object.prototype` equal to `'boolean'` or an `object` type, and an instance of
* `Boolean` that is equal to `true` or `false`.
* @param value The value of any type to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is an instance of `Boolean`.
* @angularpackage
*/
export const isBooleanObject = <Payload extends object>(
value: any,
callback: ResultCallback<any, typeof payload> = resultCallback,
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is Boolean =>
callback(
typeof value === 'object' &&
value instanceof Boolean === true &&
value instanceof Object === true,
(typeOf(value) === 'boolean' || typeof value === 'object') &&
value instanceof Boolean &&
(value.valueOf() === true || value.valueOf() === false),
value,
{ name: isBooleanObject.name, ...payload } as Payload
payload
);
19 changes: 7 additions & 12 deletions src/is/lib/is-boolean-type.func.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
// Function.
import { resultCallback } from '../../lib/result-callback.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any `value` is a `boolean` type, and it's not an instance of both `Boolean` and `Object`, and equal to `true` or `false`.
* @param value The `value` of any type to check.
* Checks if any value is a `boolean` type equal to `true` or `false`.
* @param value The value of any type to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is a `boolean` type.
* @angularpackage
*/
export const isBooleanType = <Payload extends object>(
value: any,
callback: ResultCallback<any, typeof payload> = resultCallback,
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is boolean =>
callback(
value instanceof Boolean === false &&
value instanceof Object === false &&
typeof value === 'boolean' &&
(value === true || value === false),
typeof value === 'boolean' && (value === true || value === false),
value,
{ name: isBooleanType.name, ...payload } as Payload
payload
);
32 changes: 17 additions & 15 deletions src/is/lib/is-boolean.func.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
// Function.
import { isBooleanObject } from './is-boolean-object.func';
import { isBooleanType } from './is-boolean-type.func';
import { resultCallback } from '../../lib/result-callback.func';
import { typeOf } from '../../lib/type-of.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { AnyBoolean } from '../../type/any-boolean.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any `value` is a `boolean` type(by using `isBooleanType()`), or an instance of `Boolean`(by using `isBooleanObject()`) and the
* obtained type from its `Object.prototype` is equal to `'boolean'`.
* @param value The `value` of any type to check.
* Checks if any value is a `boolean` type, or the obtained type from its `Object.prototype` equal to `'boolean'`, or an `object` type
* and an instance of `Boolean` that is equal to `true` or `false`.
* @param value The value of any type to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is a `boolean` type or an instance of `Boolean`.
* @angularpackage
*/
export const isBoolean = <Payload extends object>(
export const isBoolean = <
Type extends AnyBoolean = boolean,
Payload extends object = object
>(
value: any,
callback: ResultCallback<any, typeof payload> = resultCallback,
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is boolean =>
): value is Type =>
callback(
typeOf(value) === 'boolean' &&
(isBooleanType(value) || isBooleanObject(value)),
(typeof value === 'boolean' ||
typeOf(value) === 'boolean' ||
(typeof value === 'object' && value instanceof Boolean)) &&
(value.valueOf() === true || value.valueOf() === false),
value,
{ name: isBoolean.name, ...payload } as Payload
payload
);
22 changes: 9 additions & 13 deletions src/is/lib/is-class.func.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
// Function.
import { resultCallback } from '../../lib/result-callback.func';
import { typeOf } from '../../lib/type-of.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any value is a `function` type, an instance of both `Function` and `Object`, and the obtained type from its `Object.prototype`
* is equal to `'function'`. It also confirms it's a `class` by using `regexp` on the obtained string from its `Function.prototype`.
* @param value The `value` of any type to check.
* Checks if any value is a `function` type or of the type obtained from its `Object.prototype` equal to `'function'` and an instance of
* `Function`. It also confirms it's a `class` by using `regexp` on the obtained string from its `Function.prototype`.
* @param value The value of any type to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is a `class`.
* @angularpackage
*/
export const isClass = <Class = Function, Payload extends object = object>(
value: any,
callback: ResultCallback<any, typeof payload> = resultCallback,
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is Class =>
callback(
typeOf(value) === 'function' &&
typeof value === 'function' &&
value instanceof Function === true &&
value instanceof Object === true
typeof value === 'function' ||
(typeOf(value) === 'function' && value instanceof Function)
? /class/.test(Function.prototype.toString.call(value).slice(0, 5))
: false,
value,
{ name: isClass.name, ...payload } as Payload
payload
);
21 changes: 9 additions & 12 deletions src/is/lib/is-date.func.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
// Function.
import { typeOf } from '../../lib/type-of.func';
import { resultCallback } from '../../lib/result-callback.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any `value` is an `object` type, an instance of both `Date` and `Object` and the obtained type from its `Object.prototype` is
* equal to `'date'`.
* @param value The `value` of any type to check.
* Checks if any value is of the type obtained from its `Object.prototype` equal to `'date'` or an `object` type, and an instance of
* `Date`. The value is checked against a valid date by using `isNaN()` method.
* @param value The value of any type to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is a date.
* @angularpackage
*/
export const isDate = <Payload extends object>(
value: any,
callback: ResultCallback<any, typeof payload> = resultCallback,
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is Date =>
callback(
typeOf(value) === 'date' &&
typeof value === 'object' &&
(typeOf(value) === 'date' || typeof value === 'object') &&
value instanceof Date === true &&
value instanceof Object === true,
!isNaN(value),
value,
{ name: isDate.name, ...payload } as Payload
payload
);
22 changes: 6 additions & 16 deletions src/is/lib/is-defined.func.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
// Function.
import { resultCallback } from '../../lib/result-callback.func';
import { typeOf } from '../../lib/type-of.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { Defined } from '../../type/defined.type';
import { ResultCallback } from '../../type/result-callback.type';
/**
* Checks if any `value` is not an `undefined` type, is not equal to `undefined`, and the obtained type from its `Object.prototype` is not
* equal to `'undefined'`.
* @param value The `value` of a generic type variable `Type`, by default of type captured from the provided `value`, to check.
* Checks if any value is not an `undefined` type and is not equal to `undefined`.
* @param value The value of a generic type variable `Type`, by default of type captured from the provided `value`, to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is defined, not `undefined`.
* @angularpackage
*/
export const isDefined = <Type, Payload extends object = object>(
value: Type,
callback: ResultCallback<Type, typeof payload> = resultCallback,
callback: ResultCallback<Type, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is Defined<Type> =>
callback(
typeOf(value) !== 'undefined' &&
typeof value !== 'undefined' &&
value !== undefined,
value,
{ name: isDefined.name, ...payload } as any
);
callback(typeof value !== 'undefined' && value !== undefined, value, payload);
26 changes: 11 additions & 15 deletions src/is/lib/is-false.func.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
// Function.
import { isBooleanObject } from './is-boolean-object.func';
import { isBooleanType } from './is-boolean-type.func';
import { isBoolean } from './is-boolean.func';
import { resultCallback } from '../../lib/result-callback.func';
import { typeOf } from '../../lib/type-of.func';
// Interface.
import { CallbackPayload } from '../../type/callback-payload.type';
// Type.
import { CallbackPayload } from '../../type/callback-payload.type';
import { ResultCallback } from '../../type/result-callback.type';
import { isBoolean } from './is-boolean.func';
/**
* Checks if any `value` is a `boolean` type or an instance of `Boolean`(by using `isBoolean()`) equal to `false`.
* @param value The `value` of any type to check.
* Checks if any value is a `boolean` type or an instance of `Boolean` by using `isBoolean()` function, that is equal to `false`.
* @param value The value of any type to check.
* @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 a generic type variable `Payload` that is assigned to the `payload` of the provided `callback`
* 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 `value` is a `boolean` type or an instance of `Boolean` that
* is equal to `false`.
* @angularpackage
*/
export const isFalse = <Payload extends object>(
value: any,
callback: ResultCallback<any, typeof payload> = resultCallback,
callback: ResultCallback<any, CallbackPayload<Payload>> = resultCallback,
payload?: CallbackPayload<Payload>
): value is false =>
callback(isBoolean(value) ? value.valueOf() === false : false, value, {
name: isFalse.name,
...payload,
} as Payload);
callback(
isBoolean(value) ? value.valueOf() === false : false,
value,
payload
);

0 comments on commit 6d190f5

Please sign in to comment.