diff --git a/README.md b/README.md index 78219b2f..bf9b14b6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Useful packages based on the [angular.io](https://angular.io/). | prism | `Prism` highlighter module. | *In Progress* | [Readme][prism-readme-github] | | property | Features to handle properties. | *In Progress* | [Readme][property-readme-github] | | ui | User interface based on **[Spectre.css](https://github.com/picturepan2/spectre)**. | *In Progress* | [Github][ui-readme-github] | -| type | Common types, type guards and checkers. | [![npm version][type-npm-svg]][type-npm-badge] | [Github][type-readme-github] \| [npm][type-readme-npm] | +| type | Common types, type guards and type checkers. | [![npm version][type-npm-svg]][type-npm-badge] | [Github][type-readme-github] \| [npm][type-readme-npm] | ## angular-package/type @@ -21,43 +21,58 @@ Common types, type guards and type checkers. [![GitHub license](https://img.shields.io/github/license/angular-package/type)](https://github.com/angular-package/type/blob/main/LICENSE) ```typescript -// Guard functions. +// `guard` prefix functions. import { guardArray, + guardBigInt, + guardBoolean, guardFunction, + guardInstance, + guardKey, + guardNull, guardNumber, guardObject, guardObjectKey, guardPrimitive, guardString, - guardType + guardSymbol, + guardType, + guardUndefined } from '@angular-package/type'; ``` ```typescript -// Check is functions. +// Check `is` prefix functions. import { isArray, isBigInt, isBoolean, + isBooleanObject, + isBooleanType, isDefined, isFunction, isInstance, + isKey, isNull, isNumber, + isNumberObject, + isNumberType, isObject, + isObjectKey, isPrimitive, isString, + isStringObject, + isStringType, isSymbol, isType, isUndefined } from '@angular-package/type'; -// Check are functions. +// Check `are` prefix functions. import { areString } from '@angular-package/type'; ``` ```typescript -// Check is NOT functions. +// Check `isNot` prefix functions. import { isNotBoolean, isNotDefined, @@ -70,74 +85,95 @@ import { ``` ```typescript -// Guard and is object. -import { are, guard, is } from '@angular-package/type'; +// Object. +import { are, guard, is, isNot } from '@angular-package/type'; ``` ```typescript // Types. -import { Constructor, CycleHook, Func, Primitive, Primitives, Types } from '@angular-package/type'; +import { Constructor, CycleHook, Func, Key, Primitive, Primitives, ResultCallback, Type, Types } from '@angular-package/type'; ``` ## Features * Checks if * **any** value is - * an `Array` of any type with [isArray](#isArray). - * a `'bigint'` type with [isBigInt](#isBigInt). - * a `'boolean'` type with [isBoolean](#isBoolean). - * a `function` type with [isFunction](#isFunction). - * a generic type `instance` with [isInstance](#isInstance). - * a `null` type with [isNull](#isNull). - * a `number` type with [isNumber](#isNumber). - * a generic type `'object'` with [isObject](#isObject). - * a one of the primitive `boolean`, `bigint`, `number`, `string` type with [isPrimitive](#isPrimitive). - * a `string` type with [isString](#isString). - * a `symbol` type with [isSymbol](#isSymbol). - * a generic type instance, `'function'`, `'object'` or primitive type with [isType](#isType). - * a `'undefined'` type with [isUndefined](#isUndefined). + * an `Array` of any type with [`isArray()`](#isarray). + * a `bigint` type with [`isBigInt()`](#isbigint). + * a `boolean` with [`isBoolean()`](#isboolean). + * an `object`type and instance of [`Boolean`][boolean] and [`Object`][object] with [`isBooleanObject()`](#isbooleanobject). + * a `boolean` type not an instance of [`Boolean`][boolean] and [`Object`][object], and equal to `true` or `false` with [`isBooleanType()`](#isbooleantype). + * a `function` with [`isFunction()`](#isfunction). + * a generic type `instance` with [`isInstance()`](#isinstance). + * a [`Key`](#Key) type with [`isKey()`](#iskey). + * a `null` with [`isNull()`](#isnull). + * a `number` with [`isNumber()`](#isnumber). + * an `object` type and instance of [`Number`][Number] and [`Object`][object] with [`isNumberObject()`](#isnumberobject). + * a `number` type and **not** instance of [`Number`][Number] and [`Object`][object] with [`isNumberType()`](#isnumbertype). + * a generic type `object` with [`isObject()`](#isobject). + * an `object` with its own specified [`Key`](#Key) with [`isObjectKey()`](#isobjectkey). + * a one of the primitive `boolean`, `bigint`, `number`, `string` with [`isPrimitive()`](#isPrimitive). + * a `string` with [`isString()`](#isstring). + * an `object` type and instance of [`String`][string] and [`Object`][object] with [`isStringObject()`](#isstringobject). + * a `string` type and **not** instance of [`String`][string] and [`Object`][object] with [`isStringType()`](#isstringtype). + * a `symbol` with [`isSymbol()`](#isSymbol). + * a generic type instance, `function`, `object` or primitive type with [`isType()`](#istype). + * a `undefined` type with [`isUndefined()`](#isundefined). * an **unknown** value is - * defined with [isDefined](#isDefined). - * an **unknown** value is NOT - * a `'boolean'` type with [isNotBoolean](#isNotBoolean) - * a `'function'` type with [isNotFunction](#isNotFunction) - * a `'null'` type with [isNotNull](#isNotNull) - * a `'number'` type with [isNotNumber](#isNotNumber) - * a `'string'` type with [isNotString](#isNotString) - * a `'undefined'` type with [isNotUndefined](#isNotUndefined) + * defined with [`isDefined()`](#isdefined). + * an **unknown** value is **not** a + * `boolean` type with [`isNotBoolean()`](#isnotboolean) + * `function` type with [`isNotFunction()`](#isnotfunction) + * `null` type with [`isNotNull()`](#isnotnull) + * `number` type with [`isNotNumber()`](#isnotnumber) + * `string` type with [`isNotString()`](#isnotstring) + * `undefined` type with [`isNotUndefined()`](#isnotundefined) * Guard the value to be - * an `Array` of generic type with [guardArray](#guardArray). - * a `function` type with [guardFunction](#guardFunction). - * a `number` type with [guardNumber](#guardNumber). - * a generic `'object'` type that contains `key` with [guardObjectKey](#guardObjectKey). - * a generic `'object'` type with [guardObject](#guardObject). - * a one of the `Primitives` with [guardPrimitive](#guardPrimitive). - * a `string` type with [guardString](#guardString). - * a generic type from one of the [`Types`](#types) type with [Type guard](#guardType). + * an [`Array`][array] of a generic type with [`guardArray()`](#guardarray). + * a `bigint` with [`guardBigInt()`](#guardbigint). + * a `boolean` with [`guardBoolean()`](#guardboolean). + * a `function` type with [`guardFunction()`](#guardfunction). + * an instance with [`guardInstance()`](#guardinstance). + * a `null` with [`guardNull()`](#guardnull). + * a [`Key`](#Key) with [`guardKey()`](#guardkey). + * a `number` with [`guardNumber()`](#guardnumber). + * an `object` of a generic type with [`guardObject()`](#guardobject). + * an `object` of a generic type that contains `key` with [`guardObjectKey()`](#guardiobjectkey). + * a one of the [`Primitives`](#primitives) with [`guardPrimitive()`](#guardprimitive). + * a `string` with [`guardString()`](#guardstring). + * a `symbol` with [`guardSymbol()`](#guardsymbol). + * a generic type from one of the [`Types`](#types) type with [`guardType()`](#guardtype). + * `undefined` with [`guardUndefined()`](#guardundefined). ## How angular-package understands Check -> Is to check the return value to be the same as expected. +> Is to check the inputted value to be **the same** as **expected**. Type guard -> Is to guard type from parameter to not let input unexpected value in the code editor. +> Is to guard the parameter type to **not let** input **unexpected** value in the **code editor**. Guard -> Is a combination of both above to type guard input in the code editor and check the return. +> Is a **combination** of both above to **guard type** in the **code editor** and to check inputted value. ---- * [Installation](#installation) -* [Object](#are-object) - * [`are`](#are-object) - * [`guard`](#guard-object) - * [`is`](#is-object) -* [Checks](#checks) - * [are](#areString) - * [is](#isArray) -* [Guards](#guards) -* [Types](#types) +* [resultCallback](#resultcallback) +* [Check](#check) + * [are](#are) + * [is](#is) + * [isNot](#isnot) +* [Guard](#guard) +* [Experimental](#Experimental) + * [BigIntObject](#bigintobject) + * [BooleanObject](#booleanobject) + * [NumberObject](#numberobject) + * [PrimitiveObject](#primitiveobject) + * [StringObject](#stringobject) + * [SymbolObject](#symbolobject) + * [isParam()](#isparam) +* [Common types](#common-types) * [Git](#git) * [Commit](#commit) * [Versioning](#versioning) @@ -153,614 +189,1486 @@ Install `@angular-package/type` package with command: npm i --save @angular-package/type ``` -## are Object +## resultCallback -Object `are` with some of **check are** functions. +Default function to handle `callback`. ```typescript -const are: Are = { - string: areString +const resultCallback: ResultCallback = (result: boolean): boolean => result; +``` + +Custom function to handle `callback`. + +```typescript +const customCallback: ResultCallback = (result: boolean): boolean => { + if (result === false) { + throw new Error('error'); + } + return result; }; + +const stringResult = isString('Lorem ipsum', customCallback); + ``` -## guard Object +## Check + +### are -Object `guard` with all **guard** functions. +Tne object contains prefixed with `are` functions. ```typescript -const guardIs: GuardIs = { - array: guardArray, - function: guardFunction, - number: guardNumber, - objectKey: guardObjectKey, - object: guardObject, - primitive: guardPrimitive, - string: guardString, - type: guardType -}; -const guard: Guard = { - is: guardIs +const are: Are = { + string: areString }; +``` + +---- + +### areString + + Use `areString()` or `are.string()` to check if all of **any** arguments are a `string` type. +```typescript +const areString = (...args: any): boolean => check('string', ...args); ``` -## is Object +| Parameter | Type | Description | +| :-------- | :---: | :------------------------------------------------- | +| ...args | `any` | Any arguments to check they're all a `string` type | + +The **return value** is a `boolean` value. + +[Example usage on playground][are-string] -Object `is` with all **check is** functions and **check is not** in `not` property. +---- + +### is + +The object contains prefixed with `is` functions and prefixed with `isNot` functions in property `not`. ```typescript const is: Is = { array: isArray, - bigInt: isBigInt, + bigInt: isBigInt, // deprecated + bigint: isBigInt, boolean: isBoolean, + booleanObject: isBooleanObject, + booleanType: isBooleanType, defined: isDefined, function: isFunction, + instance: isInstance, + key: isKey, not: isNot, null: isNull, number: isNumber, + numberObject: isNumberObject, + numberType: isNumberType, object: isObject, + objectKey: isObjectKey, primitive: isPrimitive, string: isString, + stringObject: isStringObject, + stringType: isStringType, symbol: isSymbol, type: isType, undefined: isUndefined }; ``` -## isNot Object +---- -Object `isNot` with all **check is not** functions. +### isArray + +Use `isArray()` or `is.array()` to check if **any** `value` is an [`Array`][array], [`Array`][array] instance, and `object` type. ```typescript -const isNot: IsNot = { - boolean: isNotBoolean, - defined: isNotDefined, - function: isNotFunction, - null: isNotNull, - number: isNotNumber, - string: isNotString, - undefined: isNotUndefined -}; +const isArray: IsArray = (value: any, callback: ResultCallback = resultCallback): value is Array => + callback( + typeOf(value) === 'array' && + Array.isArray(value) === true && + value instanceof Array === true && + typeof value === 'object' + ); ``` -## Checks +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -### areString +The **return value** is a `boolean` indicating whether or not the `value` is an [`Array`][array]. - Use `areString()` or `are.string()` to check if all of **any** arguments are a `'string'` type. The return value is a `boolean` value. +```typescript +// Example usage +const ARRAY_NUMBER = [1, 2, 3]; +const ARRAY_STRING = ['a', 'b', 'c']; + +isArray(ARRAY_NUMBER); // true +isArray(ARRAY_STRING); // true +``` + +[Example usage on playground][is-array] + +---- + +### isBigInt + +Use `isBigInt()` or `is.bigint()` to check if **any** `value` is a `bigint` type. ```typescript -const areString = (...args: any): boolean => check('string', ...args); +const isBigInt: IsBigInt = (value: any, callback: ResultCallback = resultCallback): value is bigint => + callback(typeOf(value) === 'bigint' && typeof value === 'bigint'); ``` | Parameter | Type | Description | -|-----------| :---: |-------------| -| ...args | `any` | Any arguments to check they're all a `'string'` type. | +| :-------- | :---: | :---------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][are-string] +The **return value** is a `boolean` indicating whether or not the `value` is a `bigint`. -### isArray +```typescript +// Example usage +const BIGINT = 9007199254740991n; +const NUMBER = 27; + +isBigInt(NUMBER); // false +isBigInt(BIGINT); // true +``` -Use `isArray()` or `is.array()` to check if **any** `value` is an `Array` of `'object'` generic `Type` type and `Array` instance. The return value is a `boolean` value. +[Example usage on playground][is-bigint] | [How to detect `bigint` type][detect-bigint] + +---- + +### isBoolean + +Use `isBoolean()` or `is.boolean()` to check if **any** `value` is a `boolean` type not instance of [`Boolean`][boolean] and [`Object`][object] or `object` type instance of [`Boolean`][boolean] and [`Object`][object]. ```typescript -const isArray: IsArray = (value: any): value is Array => - typeOf(value) === 'array' && - Array.isArray(value) === true && - value instanceof Array === true && - typeof value === 'object'; +const isBoolean: IsBoolean = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback(typeOf(value) === 'boolean' && (isBooleanType(value) || isBooleanObject(value))); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's an `Array` of `'object'` generic `Type` type and `Array` instance. | +| Parameter | Type | Description | +| :---------| :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][is-array] +The **return value** is a `boolean` indicating whether or not the `value` is a `boolean`. -### isBigInt +```typescript +// Example usage +const BOOLEAN = false; +const BOOLEAN_INSTANCE = new Boolean(false); -Use `isBigInt()` or `is.bigInt()` to check if **any** `value` is a `'bigint'` type. The return value is a `boolean` value. +isBoolean(BOOLEAN); // true +isBoolean(BOOLEAN_INSTANCE); // true +``` + +[Example usage on playground][is-boolean] | [How to detect the `boolean` type][detect-boolean] + +---- + +### isBooleanObject + +Use `isBooleanObject()` or `is.booleanObject()` to check if **any** `value` is an `object` type and instance of [`Boolean`][boolean] and [`Object`][object]. ```typescript -const isBigInt: IsBigInt = (value: any): value is bigint => - typeOf(value) === 'bigint' && - typeof value === 'bigint'; +const isBooleanObject: IsBooleanObject = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback(typeof value === 'object' && value instanceof Boolean === true && value instanceof Object === true); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's a `'bigint'` type. | +| Parameter | Type | Description | +| :---------| :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][is-bigint] | [How to detect 'bigint' type][detect-bigint] +The **return value** is a `boolean` indicating whether or not the `value` is a [`Boolean`][boolean] instance. -### isBoolean +```typescript +// Example usage +const BOOLEAN = false; +const BOOLEAN_INSTANCE = new Boolean(false); + +isBooleanObject(BOOLEAN); // false +isBooleanObject(BOOLEAN_INSTANCE); // true +``` + +---- + +### isBooleanType -Use `isBoolean()` or `is.boolean()` to check if **any** `value` is a `'boolean'` type not instance of `Boolean` and `Object` or `'object'` type instance of `Boolean` and `Object`. The return value is a `boolean` value. - -```typescript -const isBoolean: IsBoolean = (value: any): value is boolean => - typeOf(value) === 'boolean' && - ( - ( - typeof value === 'object' && - value instanceof Boolean === true && - value instanceof Object === true - ) - || - ( - value instanceof Boolean === false && - value instanceof Object === false && - typeof value === 'boolean' && - (value === true || value === false) - ) +Use `isBooleanType()` or `is.booleanType()` to check if **any** `value` is a `boolean` type not an instance of [`Boolean`][boolean] and [`Object`][object], and equal to `true` or `false`. + +```typescript +const isBooleanType: IsBooleanType = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback( + value instanceof Boolean === false && + value instanceof Object === false && + typeof value === 'boolean' && + (value === true || value === false) ); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's a `'boolean'` type not instance of `Boolean` and `Object` or `'object'` type instance of `Boolean` and `Object`. | +| Parameter | Type | Description | +| :---------| :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `boolean` type. + +```typescript +// Example usage +const BOOLEAN = false; +const BOOLEAN_INSTANCE = new Boolean(false); -[Example usage][is-boolean] | [How to detect 'boolean' type][detect-boolean] +isBooleanType(BOOLEAN); // true +isBooleanType(BOOLEAN_INSTANCE); // false +``` + +---- ### isDefined -Use `isDefined()` or `is.defined()` to check if an **unknown** `value` is NOT an `'undefined'` type and is not equal to `undefined`. The return value is a `boolean` value. +Use `isDefined()` or `is.defined()` to check if an **unknown** `value` is NOT an `undefined` type and is NOT equal to `undefined`. + +```typescript +const isDefined: IsDefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'undefined' && typeof value !== 'undefined' && value !== undefined); +``` + +| Parameter | Type | Description | +| :-------- | :-------: | :---------------------------- | +| value | `unknown` | An `unknown` `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is defined, not `undefined`. ```typescript -const isDefined: IsDefined = (value: unknown): boolean => - typeOf(value) !== 'undefined' && - typeof value !== 'undefined' && - value !== undefined; +// Example usage +const UNDEFINED = undefined; +let defined; + +isDefined(UNDEFINED); // false +isDefined(defined); // false ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT an `'undefined'` type and is not equal to `undefined`. | +---- ### isFunction -Use `isFunction()` or `is.function()` to check if **any** `value` is a `'function'` type, instance of `Function`, and `Object`. The return value is a `boolean` value. +Use `isFunction()` or `is.function()` to check if **any** `value` is a `function` type, an instance of [`Function`][function] and [`Object`][object]. ```typescript -const isFunction: IsFunction = (value: any): value is Func => - typeOf(value) === 'function' && - typeof value === 'function' && - value instanceof Function === true && - value instanceof Object === true; +const isFunction: IsFunction = (value: any, callback: ResultCallback = resultCallback): value is Func => + callback( + typeOf(value) === 'function' && + typeof value === 'function' && + value instanceof Function === true && + value instanceof Object === true + ); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check it's a `'function'` type, instance of `Function` and `Object`. | +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `function`. + +```typescript +// Example usage +class Class { x = 5; } +const FUNC: Func = (x: number): any => { return x + 5; } + +isFunction(Class); // true +isFunction(FUNC); // true +isFunction(() => 5); // true +``` -[Example usage][is-function] | [How to detect 'function' type][detect-function] +[Example usage on playground][is-function] | [How to detect `function` type][detect-function] + +---- ### isInstance -Use `isInstance()` or `is.instance()` to check if **any** value is a generic `Obj` type `constructor` instance and is an `Object`. +Use `isInstance()` or `is.instance()` to check if **any** value is an `object` of a generic `Obj` type equal to an `instance` of [`Constructor`](#Constructor) type. ```typescript -const isInstance: IsInstance = (value: any, instance: Constructor): value is Obj => - isObject(value) && - value instanceof instance === true && - isString(instance.prototype.constructor.name) === true; +const isInstance: IsInstance = ( + value: any, + instance: Constructor, + callback: ResultCallback = resultCallback + ): value is Obj => + callback( + isObject(value) ? + isFunction(instance) ? + value instanceof instance === true + : false + : false + ); ``` -| Parameter | Type | Description | -|-----------| :----------------: |-------------| -| value | `any` | Any generic `Obj` type `value` instance to compare with `type` instance. | -| type | `Constructor` | Creates generic `Obj` type instance to compare with argument `value`. | +| Parameter | Type | Description | +| :-------- | :--------------------------------: | :---------- | +| value | `any` | Any `value` to compare with the `instance` | +| instance | [`Constructor`](#Constructor) | A generic `Obj` [`Constructor`](#Constructor) type to create an `instance` to compare with the `value` | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is an `instance` of a generic `Obj`. -[Example usage][is-instance] | [How to detect `constructor` instance][detect-instance] +```typescript +// Example usage +class Some { x = 127; } +class Two { y = 'Lorem ipsum'; } + +const SOME = new Some(); +const TWO = new Two(); + +isInstance(TWO, Some); // false +isInstance(SOME, Some); // true +isInstance(TWO, Two); // true and type error +``` + +[Example usage on playground][is-instance] | [How to detect `constructor` instance][detect-instance] + +---- + +### isKey + +Use `isKey()` or `is.key()` to check if **any** `value` is one of the `string`, `number`, or `symbol`. + +```typescript +const isKey: IsKey = (value: any, callback: ResultCallback = resultCallback): value is Key => + callback(isString(value) || isNumber(value) || isSymbol(value)); +``` + +| Parameter | Type | Description | +| :-------- | :---: |:-------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a [`Key`](#Key). + +```typescript +// Example usage +const STRING = 'surname'; +const STRING_INSTANCE = new String(STRING); +isKey(STRING); // true +isKey(STRING_INSTANCE); // true + +const NUMBER = 27; +const NUMBER_INSTANCE = new Number(NUMBER); +isKey(NUMBER); // true +isKey(NUMBER_INSTANCE); // true + +const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER); +const SYMBOL_STRING: unique symbol = Symbol(STRING); +isKey(SYMBOL_NUMBER); // true +isKey(SYMBOL_STRING); // true +``` + +---- ### isNull -Use `isNull()` or `is.null()` to check if **any** `value` is an `'object'` type and equal to `null`. +Use `isNull()` or `is.null()` to check if **any** `value` is an `object` type and equal to `null`. ```typescript -const isNull: IsNull = (value: any): value is null => - typeOf(value) === 'null' && - typeof value === 'object' && - value === null; +const isNull: IsNull = (value: any, callback: ResultCallback = resultCallback): value is null => + callback(typeOf(value) === 'null' && typeof value === 'object' && value === null); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's a `null` value and an `'object'` type. | +| Parameter | Type | Description | +| :-------- | :---: |--------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is `null`. + +```typescript +// Example usage +/** + * typeof === 'object' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NULL: any = null; +const NUMBER = 27; + +isNull(NULL); // true +isNull(NUMBER); // false +``` + +[Example usage on playground][is-null] | [How to detect `null` type][detect-null] -[Example usage][is-null] | [How to detect `null` type][detect-null] +---- ### isNumber -Use `isNumber()` or `is.number()` to check if **any** `value` is a '`number`' type not instance of `Number` and `Object` or `'object'` type instance of `Number` and `Object`. The return value is a `boolean` value. - -```typescript -const isNumber: IsNumber = (value: any): value is number => - typeOf(value) === 'number' && - ( - isFinite(value) === true && - ( - value instanceof Number === false && - value instanceof Object === false && - typeof value === 'number' - ) - || - ( - typeof value === 'object' && - value instanceof Number === true && - value instanceof Object === true - ) - ); +Use `isNumber()` or `is.number()` to check if **any** `value` is a `number` type not an instance of [`Number`][Number] and [`Object`][object] or `object` type instance of [`Number`][Number] and [`Object`][object]. + +```typescript +const isNumber: IsNumber = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(typeOf(value) === 'number' && isFinite(value) === true && (isNumberType(value) || isNumberObject(value))); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any ``value`` to check if it's a `'number'` type not instance of `Number` and `Object` or `'object'` type instance of `Number` and `Object`. | +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][is-number] | [How to detect `'number'` type][detect-number] +The **return value** is a `boolean` indicating whether or not the `value` is a `number`. + +[Example usage on playground][is-number] | [How to detect a `number` type][detect-number] + +---- + +### isNumberObject + +Use `isNumberObject()` or `is.numberObject()` to check if **any** `value` is an `object` type and instance of [`Number`][Number] and [`Object`][object]. + +```typescript +const isNumberObject: IsNumberObject = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(typeof value === 'object' && value instanceof Number === true && value instanceof Object === true); +``` + +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a [`Number`][Number] instance. + +```typescript +// Example usage +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER: any = 10304050; + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER_INSTANCE: any = Number(NUMBER); + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === true + * instanceof Object === true + */ +const NUMBER_NEW_INSTANCE: any = new Number(NUMBER); + +isNumberObject(NUMBER); // false +isNumberObject(NUMBER_INSTANCE); // false +isNumberObject(NUMBER_NEW_INSTANCE); // true +``` + +---- + +### isNumberType + +Use `isNumberType()` or `is.numberType()` to check if **any** `value` is a `number` type not an instance of [`Number`][Number] and [`Object`][object]. + +```typescript +const isNumberType: IsNumberType = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(value instanceof Number === false && value instanceof Object === false && typeof value === 'number'); +``` + +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `number`. + +```typescript +// Example usage +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER: any = 10304050; + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER_INSTANCE: any = Number(NUMBER); + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === true + * instanceof Object === true + */ +const NUMBER_NEW_INSTANCE: any = new Number(NUMBER); + +isNumberType(NUMBER); // true +isNumberType(NUMBER_INSTANCE); // true +isNumberType(NUMBER_NEW_INSTANCE); // false +``` + +---- ### isObject -Use `isObject()` or `is.object()` to check if **any** `value` is a generic `Obj` `'object'` type and `Object` instance with the possibility of containing `key`. The return value is a `boolean` value. +Use `isObject()` or `is.object()` to check if **any** `value` is an `object` of a generic `Obj` type and [`Object`][object] instance with the possibility of containing the `key`. ```typescript -const isObject: IsObject = (value: any, key?: string): value is Obj => - typeOf(value) === 'object' && - typeof value === 'object' && - value instanceof Object === true - ? isString(key) === true - ? value.hasOwnProperty(key) === true +const isObject: IsObject = (value: any, key?: Key): value is Obj => + (typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true) + ? isKey(key) + ? key in value : true : false; ``` -| Parameter | Type | Description | -|-----------| :------: |-------------| -| value | `any` | Any `value` to check if it's a generic `'object'` type and `Object` instance. | -| key? | `string` | Property name to find in argument `value`. | +| Parameter | Type | Description | +| :-------- | :-----------: | :---------- | +| value | `any` | Any `value` to check | +| key? | [`Key`](#Key) | Property name to find in the `value` | + +The **return value** is a `boolean` indicating whether or not the `value` is an `object`. + +[Example usage on playground][is-object] | [How to detect an `object` type][detect-object] + +```typescript +// Example usage +const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER); +const SYMBOL_STRING: unique symbol = Symbol(STRING); + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER: any = 10304050; + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER_INSTANCE: any = Number(NUMBER); + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === true + * instanceof Object === true + */ +const NUMBER_NEW_INSTANCE: any = new Number(NUMBER); + +/** + * typeof === 'string' + * instanceof Function === false + * instanceof Object === false + * instanceof String === false + */ +const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz'; + +/** + * typeof === 'string' + * instanceof Function === false + * instanceof Object === false + * instanceof String === false + */ +const STRING_INSTANCE: any = String(STRING); + +/** + * typeof === 'string' + * instanceof Function === false + * instanceof Object === true + * instanceof String === true + */ +const STRING_NEW_INSTANCE: any = new String(STRING); + +const OBJECT_ONE: ObjectOne = { + 'key as string': true, + 1030405027: 'key is number', + 5: 'key is also number', + [NUMBER]: 'key is number', + [string]: 'key is string', + [SYMBOL_NUMBER]: 'key is symbol number', + [SYMBOL_STRING]: 6, + x: 3000 +}; + +isObject(OBJECT_ONE); // true +isObject(OBJECT_ONE, 'key as string'); // true +isObject(OBJECT_ONE, STRING); // true +isObject(OBJECT_ONE, STRING_NEW_INSTANCE); // true +isObject(OBJECT_ONE, 1030405027); // true +isObject(OBJECT_ONE, NUMBER); // true +isObject(OBJECT_ONE, NUMBER_NEW_INSTANCE); // true +isObject(OBJECT_ONE, SYMBOL_NUMBER); // true +isObject(OBJECT_ONE, SYMBOL_STRING); // true + +``` + +---- + +### isObjectKey + +Use `isObject()` or `is.object()` to check if **any** `value` is an `object` with its own specified keys of the [`Key`](#Key). -[Example usage][is-object] | [How to detect `'object'` type][detect-object] +```typescript +const isObjectKey: IsObjectKey = ( + value: any, + key: Key | Key[], + callback: ResultCallback = resultCallback +): value is Type => + callback( + isObject(value) ? + isArray(key) ? + key.every(k => isKey(k) ? ({}).hasOwnProperty.call(value, k) === true : false) + : isKey(key) ? + ({}).hasOwnProperty.call(value, key) + : false + : false + ); +``` + +| Parameter | Type | Description | +| :-------- | :------------------------------: | :---------------------------------------------------- | +| value | `any` | Any `value` to check if it contains a specified `key` | +| key | [`Key`](#key) \| [`Key`](#Key)[] | A [`Key`](#Key) type or an array of [`Key`](#Key) type to check in the `value` | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is an `object` with its own specified keys. + +---- ### isPrimitive -Use `isPrimitive()` or `is.primitive()` to check if **any** `value` is a generic `Type` type one of the primitive `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` type. The return value is a `boolean` value. +Use `isPrimitive()` or `is.primitive()` to check if **any** `value` is the [`Primitive`](#primitive) type from a `type` of the [`Primitives`](#primitives) type. ```typescript -const isPrimitive: IsPrimitive = (value: any, type: Primitives): value is Type => { +const isPrimitive: IsPrimitive = ( + value: any, + type: Primitives, + callback: ResultCallback = resultCallback +): value is T => { if (isString(type)) { switch (type) { - case 'bigint': return isBigInt(value); - case 'boolean': return isBoolean(value); - case 'number': return isNumber(value); - case 'string': return isString(value); - case 'symbol': return isSymbol(value); - case 'undefined': return isUndefined(value); + case 'bigint': return isBigInt(value, callback); + case 'boolean': return isBoolean(value, callback); + case 'number': return isNumber(value, callback); + case 'null': return isNull(value, callback); + case 'string': return isString(value, callback); + case 'symbol': return isSymbol(value, callback); + case 'undefined': return isUndefined(value, callback); } } + return false; }; ``` -| Parameter | Type | Description | -|-----------| :----------: |--------------| -| value | `any` | Any `value` to check if it's a generic `Type` type from on of the `type`. | -| type | `Primitives` | One of the `Primitives` `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` type to check `value`. | +| Parameter | Type | Description | +| :-------- | :-------------------------: | :------------------------------------------------------------------------ | +| value | `any` | Any `value` to check if it's a `Primitive` from the `type` | +| type | [`Primitives`](#primitives) | A `string` type from the [`Primitives`](#primitives) to check the `value` | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][is-primitive] +The **return value** is a `boolean` indicating whether or not the `value` is a `type` from the [`Primitives`](#primitives). + +[Example usage on playground][is-primitive] + +---- ### isString -Use `isString()` or `is.string()` to check if **any** `value` is a `'string'` type, not instance of `Object` and `String` or `'object'` type and instance of `String` and `Object`. The return value is a `boolean` value. +Use `isString()` or `is.string()` to check if **any** `value` is a `string` type, not instance of [`Object`][object] and [`String`][string] or `object` type and instance of [`String`][string] and [`Object`][object]. ```typescript -const isString: IsString = (value: any): value is string => - typeOf(value) === 'string' && - ( - typeof value === 'object' && - value instanceof Object === true && - value instanceof String === true - ) - || - ( - value instanceof Object === false && - value instanceof String === false && - typeof value === 'string' - ); +const isString: IsString = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(typeOf(value) === 'string' && (isStringType(value) || isStringObject(value))); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any value to check if it's a `'string'` type, not an instance of `Object` and `String` or `'object'` type and instance of `String` and `Object`. | +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `string`. + +---- + +### isStringObject -[Example usage][is-string] | [How to detect `'string'` type][detect-string] +Use `isStringObject()` or `is.stringObject()` to check if **any** `value` is an `object` type and instance of [`String`][string] and [`Object`][object]. + +```typescript +const isStringObject: IsStringObject = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(value instanceof Object === true && value instanceof String === true && typeof value === 'object'); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a [`String`][string] instance. + +---- + +### isStringType + +Use `isStringType()` or `is.stringType()` to check if **any** `value` is a `string` type and **not** instance of [`String`][string] and [`Object`][object]. + +```typescript +const isStringType: IsStringType = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(value instanceof Object === false && value instanceof String === false && typeof value === 'string'); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------------------------------------------: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `string`. + +---- ### isSymbol -Use `isSymbol()` or `is.symbol()` to check if **any** `value` is a `'symbol'` type. The return value is a `boolean` value. +Use `isSymbol()` or `is.symbol()` to check if **any** `value` is a `symbol` type. ```typescript -const isSymbol: IsSymbol = (value: any): value is symbol => - typeOf(value) === 'symbol' && - typeof value === 'symbol'; +const isSymbol: IsSymbol = (value: any, callback: ResultCallback = resultCallback): value is symbol => + callback(typeOf(value) === 'symbol' && typeof value === 'symbol'); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's a `'symbol'` type. | +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `symbol`. -[Example usage][is-symbol] | [How to detect `'symbol'` type][detect-symbol] +[Example usage on playground][is-symbol] | [How to detect `symbol` type][detect-symbol] + +---- ### isType -Use `isType()` or `is.type()` Check if **any** `value` is a generic `Type` type constructor, `'function'`, `'object'` or primitive type. The return value is a `boolean` value. +Use `isType()` or `is.type()` to check if **any** `value` is the [`Type`](#type) from a `type` of the [`Types`](#types) type. ```typescript -const isType: IsType = (value: any, type: Types): value is Type => { +const isType: IsType = (value: any, type: Types, callback: ResultCallback = resultCallback): value is T => { if (isString(type)) { switch (type) { - case 'bigint': return isBigInt(value); - case 'boolean': return isBoolean(value); - case 'function': return isFunction(value); - case 'number': return isNumber(value); - case 'object': return isObject(value); - case 'string': return isString(value); - case 'symbol': return isSymbol(value); - case 'undefined': return isUndefined(value); + // Primitives. + case 'bigint': + case 'boolean': + case 'number': + case 'null' : + case 'string': + case 'symbol': + case 'undefined': return isPrimitive(value, type, callback); + // Function. + case 'function': return isFunction(value, callback); + // Object. + case 'object': return isObject(value); } + } else if (isNotNull(type)) { + return isInstance(value, type, callback); } - return type ? isInstance(value, type) : false; + return false; }; + ``` -| Parameter | Type | Description | -|-----------| :-----------: |-------------| -| value | `any` | Any value to check it is a generic `Type` type from one of the `type`. | -| type | `Types` | Generic constructor `Type`, `'function'`, `'object'` or one of the `Primitives` `'bigint'`, `'boolean'`, `'number'`, `'symbol'`, `'string'`, `'undefined'` to check `value` type. | +| Parameter | Type | Description | +| :-------- | :------------------: | :-------------------------------------------------- | +| value | `any` | Any `value` to check if its type is from the `type` | +| type | [`Types`](#types) | A `string` or generic `Constructor` type from the [`Types`](#types) to check the `value` | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is the [`Type`](#type) from a `type` of the [`Types`](#types). -[Example usage][is-type] +[Example usage on playground][is-type] + +---- ### isUndefined -Use `isUndefined()` or `is.undefined()` to check if **any** `value` is an `'undefined'` type and equal to `undefined`. The return value is a `boolean` value. +Use `isUndefined()` or `is.undefined()` to check if **any** `value` is an `undefined` type and equal to `undefined`. ```typescript -const isUndefined: IsUndefined = (value: any): value is undefined => - typeOf(value) === 'undefined' && - typeof value === 'undefined' && - value === undefined; +const isUndefined: IsUndefined = (value: any, callback: ResultCallback = resultCallback): value is undefined => + callback(typeOf(value) === 'undefined' && typeof value === 'undefined' && value === undefined); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's an `'undefined'` type, and equal to `undefined`. | +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is `undefined`. -[Example usage][is-undefined] | [How to detect `'undefined'` type][detect-undefined] +[Example usage on playground][is-undefined] | [How to detect `undefined` type][detect-undefined] + +---- + +### isNot + +Object `isNot` with all **check is not** functions. + +```typescript +const isNot: IsNot = { + boolean: isNotBoolean, + defined: isNotDefined, + function: isNotFunction, + null: isNotNull, + number: isNotNumber, + string: isNotString, + undefined: isNotUndefined +}; +``` ### isNotBoolean -Use `isNotBoolean()` or `is.not.boolean()` to check if an **unknown** `value` is NOT a `'boolean'` type, NOT equal to `true` or `false` and NOT an instance of a `Boolean`. The return value is a `boolean` value. +Use `isNotBoolean()` or `is.not.boolean()` to check if an **unknown** `value` is **not** a `boolean` type, **not** equal to `true` or `false` and **not** an instance of a [`Boolean`][boolean]. ```typescript -const isNotBoolean: IsNotBoolean = (value: unknown): boolean => - typeOf(value) !== 'boolean' && - typeof value !== 'boolean' && - value instanceof Boolean === false && - value !== true && - value !== false; +const isNotBoolean: IsNotBoolean = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback( + typeOf(value) !== 'boolean' && + typeof value !== 'boolean' && + value instanceof Boolean === false && + value !== true && + value !== false + ); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT a `'boolean'` type, NOT equal to `true` or `false` and NOT an instance of `Boolean`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is not a `boolean`. + +---- ### isNotDefined -Use `isNotDefined()` or `is.not.defined()` to check if an **unknown** `value` is an `'undefined'` type and is equal to `undefined`. The return value is a `boolean` value. +Use `isNotDefined()` or `is.not.defined()` to check if an **unknown** `value` is an `undefined` type and is equal to `undefined`. ```typescript -const isNotDefined: IsNotDefined = (value: unknown): boolean => - typeOf(value) === 'undefined' && - typeof value === 'undefined' && - value === undefined; +const isNotDefined: IsNotDefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) === 'undefined' && typeof value === 'undefined' && value === undefined); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's an `'undefined'` type and is equal to `undefined`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not defined, is `undefined`. + +---- ### isNotFunction -Use `isNotFunction()` or `is.not.function()` to check if an **unknown** `value` is NOT a `'function'` type and NOT an instance of `Function`. The return value is a `boolean` value. +Use `isNotFunction()` or `is.not.function()` to check if an **unknown** `value` is **not** a `function` type and **not** an instance of `Function`. ```typescript -const isNotFunction: IsNotFunction = (value: unknown): boolean => - typeOf(value) !== 'function' && - typeof value !== 'function' && - value instanceof Function === false; +const isNotFunction: IsNotFunction = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'function' && typeof value !== 'function' && value instanceof Function === false); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT a `'function'` type and NOT an instance of `Function`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not a `function`. + +---- ### isNotNull -Use `isNotNull()` or `is.not.null()` to check if an **unknown** `value` is NOT a `'null'` type and NOT equal to `null`. The return value is a `boolean` value. +Use `isNotNull()` or `is.not.null()` to check if an **unknown** `value` is **not** a `null` type and **not** equal to `null`. ```typescript -const isNotNull: IsNotNull = (value: unknown): boolean => - typeOf(value) !== 'null' && - value !== null; +const isNotNull: IsNotNull = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'null' && value !== null); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT a `'null'` type and NOT equal to `null`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not `null`. + +---- ### isNotNumber -Use `isNotNumber()` or `is.not.number()` to check if an **unknown** `value` is NOT a `'number'` type and NOT an instance of `Number`. The return value is a `boolean` value. +Use `isNotNumber()` or `is.not.number()` to check if an **unknown** `value` is **not** a `number` type and **not** an instance of `Number`. ```typescript -const isNotNumber: IsNotNumber = (value: any): boolean => - typeOf(value) !== 'number' && - typeof value !== 'number' && - isFinite(value) === false && - value instanceof Number === false; +const isNotNumber: IsNotNumber = (value: any, callback: ResultCallback = resultCallback): boolean => + callback( + typeOf(value) !== 'number' && + typeof value !== 'number' && + value instanceof Number === false + ); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown value to check if it's NOT a `'number'` type and NOT an instance of `Number`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not a `number`. + +---- ### isNotString -Use `isNotString()` or `is.not.string()` to check if an **unknown** `value` is NOT a `'string'` type and NOT an instance of `String`. The return value is a `boolean` value. +Use `isNotString()` or `is.not.string()` to check if an **unknown** `value` is **not** a `string` type and **not** an instance of `String`. ```typescript -const isNotString: IsNotString = (value: unknown): boolean => - typeOf(value) !== 'string' && - typeof value !== 'string' && - value instanceof String === false; +const isNotString: IsNotString = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'string' && typeof value !== 'string' && value instanceof String === false); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT a `'string'` type or NOT an `'object'` type and NOT an instance of `String`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not a `string`. + +---- ### isNotUndefined -Use `isNotUndefined()` or `is.not.undefined()` to check if an **unknown** `value` is NOT an `'undefined'` type and NOT equal to `undefined`. The return value is a `boolean` value. +Use `isNotUndefined()` or `is.not.undefined()` to check if an **unknown** `value` is **not** an `undefined` type and **not** equal to `undefined`. ```typescript -const isNotUndefined: IsNotUndefined = (value: unknown): boolean => - typeOf(value) !== 'undefined' && - typeof value !== 'undefined' && - value !== undefined; +const isNotUndefined: IsNotUndefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'undefined' && typeof value !== 'undefined' && value !== undefined); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An Unknown `value` to check if it's NOT an `'undefined'` type and NOT equal to `undefined`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not `undefined`. + +---- + +## Guard + +### guard + +The object contains prefixed with `guard` functions in `is` property. + +```typescript +const guardIs: GuardIs = { + array: guardArray, + bigint: guardBigInt, + boolean: guardBoolean, + function: guardFunction, + instance: guardInstance, + key: guardKey, + number: guardNumber, + null: guardNull, + object: guardObject, + objectKey: guardObjectKey, + primitive: guardPrimitive, + string: guardString, + symbol: guardSymbol, + type: guardType, + undefined: guardUndefined +}; +const guard: Guard = { + is: guardIs +}; -## Guards +``` ### guardArray -Use `guardArray()` or `guard.is.array()` to guard the `value` to be a generic `Array` `Type`. The return value is a `boolean` value. +Use `guardArray()` or `guard.is.array()` to guard the `value` to be an [`Array`][array] of a generic `Type`. ```typescript -const guardArray: GuardArray = (value: Array): value is Array => isArray(value); +const guardArray: GuardArray = (value: Array, callback?: ResultCallback): value is Array => + isArray(value, callback); ``` -| Parameter | Type | Description | -|-----------| :-----------: |-------------| -| value | `Array` | Array generic `Type` type `value` to guard. | +| Parameter | Type | Description | +|-----------| :---------------------------------: |-------------| +| value | `Array` | A generic `Type` `Array` `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is an [`Array`][array] of a generic `Type`. + +[Example usage on playground][guard-array] + +---- + +### guardBigInt + +Use `guardBigInt()` or `guard.is.bigint()` to guard the `value` to be a `bigint`. + +```typescript +const guardBigInt: GuardBigInt = (value: bigint, callback?: ResultCallback): value is bigint => + isBigInt(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------------------- | +| value | `bigint` | A `bigint` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `bigint`. + +---- + +### guardBoolean -[Example usage][guard-array] +Use `guardBoolean()` or `guard.is.boolean()` to guard the `value` to be a `boolean`. + +```typescript +const guardBoolean: GuardBoolean = (value: boolean, callback?: ResultCallback): value is boolean => + isBoolean(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :-------------------------------- | +| value | `boolean` | A `boolean` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `boolean`. + +---- ### guardFunction -Use `guardFunction()` or `guard.is.function()` to guard the `func` value to be a `Func` type. The return value is a `boolean` value. +Use `guardFunction()` or `guard.is.function()` to guard the `value` to be a [`Func`](#Func) type. ```typescript -const guardFunction: GuardFunction = (func: Func): func is Func => isFunction(func); +const guardFunction: GuardFunction = (value: Func, callback?: ResultCallback): value is Func => + isFunction(value, callback); ``` -| Parameter | Type | Description | -|-----------| :----: |--------------| -| func | `Func` | Type `Func` value to guard. | +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :-------------------------------------- | +| value | [`Func`](#Func) | A [`Func`](#Func) type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][guard-function] +The return value is a `boolean` indicating whether or not the `value` is a [`Func`](#Func). + +[Example usage on playground][guard-function] + +---- + +### guardInstance + +Use `guardInstance()` or `guard.is.instance()` to guard the `value` to be an `object` of a generic `Obj` type equal to an `instance` of [`Constructor`](#Constructor) type. + +```typescript +const guardInstance: GuardInstance = (value: Obj, instance: Constructor, callback?: ResultCallback): value is Obj => + isInstance(value, instance, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :--------------------------------------------------- | +| value | `Obj` | An `Obj` type `value` to compare with the `instance` | +| instance | [`Constructor`](#Constructor) | A generic `Obj` [`Constructor`](#Constructor) type to create an `instance` to compare with the `value` | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is an `instance` of a generic `Obj`. + +---- + +### guardKey + +Use `guardKey()` or `guard.is.key()` to guard the `value` to be one of the `string`, `number`, or `symbol`. + +```typescript +const guardKey: GuardKey = (value: Key, callback?: ResultCallback): value is Key => + isKey(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------------------------ | +| value | [`Key`](#Key) | A [`Key`](#Key) type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a [`Key`](#Key). + +---- + +### guardNull + +Use `guardNull()` or `guard.is.null()` to guard the `value` to be a `null`. + +```typescript +const guardNull: GuardNull = (value: null, callback?: ResultCallback): value is null => + isNull(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :----------------------------- | +| value | `null` | A `null` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `null`. + +---- ### guardNumber -Use `guardNumber()` or `guard.is.number()` to guard the `value` to be a `number` type. The return value is a `boolean` value. +Use `guardNumber()` or `guard.is.number()` to guard the `value` to be a `number`. ```typescript -const guardNumber: GuardNumber = (value: number): value is number => isNumber(value); +const guardNumber: GuardNumber = (value: number, callback?: ResultCallback): value is number => + isNumber(value, callback); ``` -| Parameter | Type | Description | -|-----------| :------: |--------------| -| value | `number` | Type `number` value to guard. | +| Parameter | Type | Description | +|---------- | :---------------------------------: | :-------------------------------- | +| value | `number` | A `number` type `value` to guard. | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][guard-number] +The **return value** is a `boolean` indicating whether or not the `value` is a `number`. + +[Example usage on playground][guard-number] + +---- ### guardObject -Use `guardObject()` or `guard.is.object()` to guard the `object` value to be a generic `Obj` type. The return value is a `boolean` value. +Use `guardObject()` or `guard.is.object()` to guard the `value` to be an `object` of a generic `Obj` type. ```typescript -const guardObject: GuardObject = (object: Obj): object is Obj => isObject(object); +const guardObject: GuardObject = (value: Obj): value is Obj => isObject(value); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| object | `Obj` | Generic `Obj` type value to guard. | +| Parameter | Type | Description | +| :-------- | :--------------------: | :------------------------------------ | +| value | `Obj` extends `object` | A generic `Obj` type `value` to guard | + +The **return value** is a `boolean` indicating whether or not the `value` is an `object` of a generic `Obj`. -[Example usage][guard-object] +[Example usage on playground][guard-object] + +---- ### guardObjectKey -Use `guardObjectKey()` or `guard.is.objectKey()` to guard the `object` to be a generic `Obj` type and to contains the `key` property. The return value is a `boolean` value. +Use `guardObjectKey()` or `guard.is.objectKey()` to guard the `value` to be an `object` of a generic `Obj` type that contains the `key` property of the [`Key`](#Key) type. ```typescript -const guardObjectKey: GuardObjectKey = (object: Obj, key: Key): object is Obj => - guardObject(object) ? isString(key) ? key in object : true : false; +const guardObjectKey: GuardObjectKey = (value: Obj, key: Key): value is Obj => + guardObject(value) ? isKey(key) ? key in value : true : false; ``` -| Parameter | Type | Description | -|-------------| :---: |---------------| -| object | `Obj` | Generic `Obj` type `value` that contains the `key` property to guard. | -| key | `Key` | Name of the property that the `object` contains. | +| Parameter | Type | Description | +| :---------- | :-----------: | :------------------------------------------------------------ | +| value | `Obj` | A generic `Obj` type `value` that contains the `key` to guard | +| key | `Key` | A `Key` type name of the property that the `value` contains | + +The **return value** is a `boolean` indicating whether or not the `value` is an `object` of a generic `Obj` containing the `Key`. + +[Example usage on playground][guard-object-key] -[Example usage][guard-object-key] +---- ### guardPrimitive -Use `guardPrimitive()` or `guard.is.primitive()` to guard the `value` to be a generic `Type` from one of the `Primitives`. The return value is a `boolean` value. +Use `guardPrimitive()` or `guard.is.primitive()` to guard the `value` to be the [`Primitive`](#primitive) from a `type` of the [`Primitives`](#primitives). ```typescript -const guardPrimitive: GuardPrimitive = (value: Type, type: Primitives): value is Type => isPrimitive(value, type); +const guardPrimitive: GuardPrimitive = + (value: Type, type: Primitives, callback?: ResultCallback): value is Type => + isPrimitive(value, type, callback); ``` -| Parameter | Type | Description | -|-------------| :----------: |--------------| -| value | `Type` | A generic `Type` type `value` to guard. | -| type | `Primitives` | One of the `Primitives` `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` to check `value`. | +| Parameter | Type | Description | +| :---------- | :--------------------------------------: | :---------------------------------- | +| value | `Type` extends [`Primitive`](#primitive) | A `Primitive` type `value` to guard | +| type | [`Primitives`](#primitives) | A `string` type from the [`Primitives`](#primitives) to check the `value` | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is the [`Primitive`](#primitive) from the `type`. -[Example usage][guard-primitive] +[Example usage on playground][guard-primitive] + +---- ### guardString -Use `guardString()` or `guard.is.string()` to guard the `value` to be a `string` type. The return value is a `boolean` value. +Use `guardString()` or `guard.is.string()` to guard the `value` to be a `string`. ```typescript -const guardString: GuardString = (value: string): value is string => isString(value); +const guardString: GuardString = (value: string, callback?: ResultCallback): value is string => + isString(value, callback); ``` -| Parameter | Type | Description | -|-------------| :---: |---------------| -| value | `string` | String type value to guard. | +| Parameter | Type | Description | +|-------------| :---------------------------------: | :------------------------------- | +| value | `string` | A `string` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][guard-string] +The return value is a `boolean` indicating whether or not the `value` is a `string`. + +[Example usage on playground][guard-string] + +---- + +### guardSymbol + +Use `guardSymbol()` or `guard.is.symbol()` to guard the `value` to be a `symbol`. + +```typescript +const guardSymbol: GuardSymbol = (value: symbol, callback?: ResultCallback): value is symbol => + isSymbol(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :----------------------------- | +| value | `symbol` | A `symbol` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `symbol`. + +---- ### guardType -Use `guardType()` or `guard.is.type()` to guard the `value` to be a generic `Type` type from one of the `Types` type. The return value is a `boolean` value. +Use `guardType()` or `guard.is.type()` to guard the `value` to be the [`Type`](#type) from a `type` of the [`Types`](#types). + +```typescript +const guardType: GuardType = (value: T, type: Types, callback?: ResultCallback): value is T => + isType(value, type, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------------------------------------- | +| value | `T` extends [`Type`](#type) | A [`Type`](#type) `value` to guard with the `type` | +| type | [`Types`](#types) | A `string` or generic [`Constructor`](#constructor) type from the [`Types`](#types) to check the `value` | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is a `type` from the [`Types`](#types). + +[Example usage on playground][guard-type] + +---- + +### guardUndefined + +Use `guardUndefined()` or `guard.is.undefined()` to guard the `value` to be `undefined`. + +```typescript +const guardUndefined: GuardUndefined = (value: undefined, callback?: ResultCallback): value is undefined => + isUndefined(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :---------------------------------- | +| value | `undefined` | A `undefined` type `value` to guard | +| callback | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is `undefined`. + +---- + +## Experimental + +### BigIntObject + +```typescript +class BigIntObject { + static set set(value: any) { + PrimitiveObject.bigint = BigInt(value); + } + static get get(): BigInt { + return PrimitiveObject.bigint; + } +} +``` + +---- + +### BooleanObject + +```typescript +class BooleanObject { + /** + * `false` when empty, 0, null, '', false + * `true` when 'true', 'false', 'Su Lin whatever', [], {}, true + */ + static set set(value: any) { + PrimitiveObject.boolean = new Boolean(value); + } + static get get(): Boolean { + return PrimitiveObject.boolean; + } +} +``` + +---- + +### NumberObject + +```typescript +class NumberObject { + static set set(value: any) { + PrimitiveObject.number = new Number(value); + } + static get get(): Number { + return PrimitiveObject.number; + } +} +``` + +---- + +### PrimitiveObject ```typescript -const guardType: GuardType = (value: Type, type: Types): value is Type => isType(value, type); +class PrimitiveObject { + static bigint: BigInt; + static boolean: Boolean; + static number: Number; + static string: String; + static symbol: Symbol; +} ``` -| Parameter | Type | Description | -|-------------| :---: |---------------| -| value | `Type` | A generic `Type ` `value` to guard. | -| type | `Types` | Constructor generic `Type`, `'function'`, `'object'` or one of the `Primitives` `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` to check `value`. | +---- -[Example usage][guard-type] +### StringObject -## Common Types +```typescript +class StringObject { + static set set(value: any) { + PrimitiveObject.string = new String(value); + } + static get get(): String { + return PrimitiveObject.string; + } +} +``` + +---- + +### SymbolObject + +```typescript +class SymbolObject { + static set set(value: string | number | undefined) { + PrimitiveObject.symbol = Symbol(value); + } + static get get(): Symbol { + return PrimitiveObject.symbol; + } +} +``` + +---- + +### isParam + +Method decorator to check the type and return `undefined` if it's not the same as expected. + +```typescript +function isParam(...param: Array): MethodDecorator { + return (target: Func | object, key: string | symbol, descriptor: any): any => { + const originalMethod = descriptor.value; + + descriptor.value = function(): void { + if (is.array(param) && is.defined(arguments)) { + param.forEach((name: string, index: number) => { + if (is.number(index) && index < arguments.length) { + if (is.defined(arguments[index])) { + switch (name) { + case 'number': + if (is.number(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + case 'object': + if (is.object(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + case 'string': + if (is.string(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + } + } + } + }); + } + const result = originalMethod.apply(this, arguments); + return result; + }; + + return descriptor; + }; +} +``` + +Example usage. + +```typescript +// Example usage +const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz'; +const NUMBER: any = 10304050; +// TestClass +class TestClass { + @isParam('object', 'string', 'number') + public testMethod(object?: any, firstName?: any, age?: any): { object: any, firstName: any, age: any } { + return {object, firstName, age}; + } +} +const resultTRUE = new TestClass().testMethod({firstName: 'NoName'}, STRING, NUMBER); +const resultFALSE = new TestClass().testMethod(NUMBER, {firstName: 'NoName'}, STRING); + +resultTRUE === { + object: {firstName: 'NoName'}, + string: '!@#$%^&*()abcdefghijklmnoprstuwyz', + number: 10304050 +}; + +resultTRUE === { + object: undefined, + string: undefined, + number: undefined +}; + +``` + +---- + +## Common types ### Constructor @@ -777,24 +1685,54 @@ type CycleHook = 'ngAfterContentInit' | 'ngAfterContentChecked' | 'ngAfterViewIn ### Func +Function type. + ```typescript type Func = (...param: any) => any; ``` +### Key + +Name of the `object` property. + +```typescript +type Key = number | string | symbol; +``` + ### Primitive +All [`Primitive`][primitive] types. + ```typescript -type Primitive = boolean | bigint | null | number | string | symbol | undefined; +type Primitive = bigint | boolean | null | number | string | symbol | undefined; ``` ### Primitives +All [`Primitive`](#primitive) types as `string`. + +```typescript +type Primitives = 'bigint' | 'boolean' | 'null' | 'number' | 'symbol' | 'string' | 'undefined'; +``` + +### ResultCallback + ```typescript -type Primitives = 'bigint' | 'boolean' | 'number' | 'symbol' | 'string' | 'undefined'; +type ResultCallback = (result: boolean) => boolean; +``` + +### Type + +Main types. + +```typescript +type Type = Func | object | Primitive; ``` ### Types +Main types as `string`. + ```typescript type Types = Constructor | 'function' | 'object' | Primitives; ``` @@ -831,6 +1769,14 @@ How do I know when to release 1.0.0? MIT © angular-package ([license](https://github.com/angular-package/type/blob/main/LICENSE)) +[array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array +[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean +[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions +[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number +[object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object +[primitive]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive +[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String + [new]: https://img.shields.io/badge/-new-red [type-npm-svg]: https://badge.fury.io/js/%40angular-package%2Ftype.svg @@ -859,9 +1805,9 @@ MIT © angular-package ([license](https://github.com/angular-package/type/blob/m [is-array]: https://www.typescriptlang.org/play?jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgABVYAWxYoFnxkbBg+EBBiZBoYAB94lnY2WAywUmJidJh8sL5XYuY2TgyIeDKk4tIwABNkdDZkFv9A2IBJCABBJiZCeDitMkpqWjpuGCIpmBYIGGHR+AAeGnguf1BIWBX1saoBk-GcTYAVWS5J8iodhgXtWJW1kbGbu+4-GDgsgwD2Qcyw4JgAHJCF94JCYAAyBH-T4bUTHWEgsEQ5TkRHIgGLPFsaA0IwgTAXbA4ph4pEo3oU15LcE4SEgQTCKCQ-bgaAwBTXABKfQAcgBxOKQxxMZBhZaICCkMIAfh5fgO-NFAFUALIAIQAokK4gAmADsvMOMH1qFQ9kNg1FcXQJC8Vv52tFABFDQAxMWG71xZptDpgLoe2CDIVCwYATUFIolVAumyqHB4OAA2tDIRZIXx81DgJCALpRtaxhM6g3G1OwzalcpMLMwbPWCymiwAZgrGr50er8eCIt1fWufQAaoaGxtNqEWBEojE29moRn2MWLRZcbF+5rYAApBSoUUAfSng3s2sNcQA3uhElRrKaewBfD1JOTEEDsXAAAZJmK4oARYGIbLgwESnQvgwAA9PBMCusQXgDpA36iL+-4AbWRpCmByxDJieHGrBPgIUhKFeDA6EQJh2GAba9qOqKhEQWMuDMQ6TrkZRyFusgdEMX+gFer6AaikG7HEZB4n+oG3p8YhAmoUJmoiThMZxomwogTJFy4NpCbQeKylIXuwnJFhokAcZ8akQR4GyZx9mOeZcC0upfKaYB9mjn046TjOBmYv5Y4TtOhoeZZGnWYxAEnmel7XreoWQUlF5Xje0VwSp1FCUAA [is-bigint]: https://www.typescriptlang.org/play?target=7&jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgASQgAIRZ2YLBYHC0ySmpaOm4YIgSYFggYPki2KH9QSFgsiKiYqlCy6Ni8dPIqGnoqeuRM7Nz2fO4-GDhZDHidbCwcAHJO-LGYADIZ3v6UEExWkfHJmLHC8GgYBQAVACVggDkAcWwYMccmZABbTMQIUjuAfi2-It2TgFUAWTCAFFDpcAEwAdm2xRgYVQqHsgIAgidLugSF4obswsEzqd9pcAJwABiJ4OsBIJoIArAAWcE0okU6xgTEgYhyYggdi4AAGB2O5x5FlKkRquH5pzOdF8MAA9LKYGjiF5Pjs2RyubzfgDgUL2tUYrhtUDDtKfHKFUqVV91aJOdyebD4UiTnqReUoLgnQjkWaLYr0chVZBbfbedjcSd9m7wqLDRG8X75XAmOQ-EA [is-boolean]: https://www.typescriptlang.org/play?target=7&jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgASQgAIRAQYmQabDwiMkpqWjpuGATyGBYIGD5I6Jp-UEhYbIiomLAqUPKCsDitRKoaeioM5CycvIrYrC4-GDhZDEadbCwcAHJuusmYADJ5gbxlwdxVwaGUEEx28amQQWEoOcWNwb22aBojHZhayv24Jkyzzc3LkpvkO-4hESeynIGzoGwAPmCNut3hdtB0rlBvncHr0JjB0CQvAsljD0nCsl8wLdMH9jk8McQsW8YYEfrt8RMpjNKqccTDRh1Gc9MhC8YlyZjkKD3sKYL4-MVoDAFAAVABKwQAcgBxOKTRxMZAAWyyiAgpC1AH5JkVwFLFQBVACyYQAonK4gAmADsppK91QqHstoAgoq4hSvG6pWFPd6-QB9eUW21xIHIYOwUNe32KiNK2V+tyxnBgZBOe75Sq4QNC4MVUTEEDsXAAA1lCpVtYsZSLNFwDaVyrovhgAHo++jBRKzRWqzXa5abfbm50UWBcFO7XKez5+4PSyPIGPq3Xk+HFbPWz0F-vU6v19zkFuIDuJ2fI9HbUfwm3T2HU1G5TGLwOrze7z3D9IwzGUs2fFtXxPXAHzTUDwN-Qd4z8IA -[is-function]: https://www.typescriptlang.org/play?target=7&jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgAMVIwYGw8UWjEQiZCAFsqGnpualp-QOQYAEkIUPCoFnBIrTJKdNSsHiJymBYIELDgf2BiT0a3dohGgG8YAA9IgFYfGABfP1BIWAaCkWKwKjz5opKcMvJk2gYYWvJ6xvnuPxg4WQxN5DpsLBwAcnRmtbB7mAAyd9PzlBBMfeydweT0Kizen2+APqMxoRj+TVB6zucCYBwhZyhbGgsOQ8P4QhEtxwynIrXA0BgACFUKh7ABRACCADlIiTkGSZlScgBxHJMgAqkQAnAAGEUAdmsQqFACZhgAWcXykXS6xgDkUpkAVQAspS6QAlSLWGXijWwBT8g187mRe46+AwMCkBJ8VyHe7mkJapluKjHDYDKjO12uXYpNL9JjIKCkJhgQYwADUMFGEym5NgfMtzLcdMiYGQThgXQ6uF8GcgIGIcmIIHYuAABqWeo2LHNnotcC2IHRfDAAPQDlHkSsQau1+u4e6Unl8-n3dv5TvgXCz3kCvtjIcwdAkLxjieiOsNmc0+nMxeHVZd6m0xlMreD4d74gH6bjmvHqf3bV6w1Xh2iJgLgf76gaT47q+77kkeJ7Tpa1pMtygHLsBuCITakEvvuyCHl+8GNsEPpuG214riBxG+thI54R+cE-tm-K5nSqE3quTEsTR0HIEAA -[is-instance]: https://www.typescriptlang.org/play?target=7&jsx=0#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAq4EAfFALxQIQDuUAFAHTMCGqA5kgFxQsIgBtALoBKCmSKQA3AChQkKAEkki5MD6ZolPAHkARgCsS9AG4sANugg8+IADRQAlmo3XYiFBmy5dhkmPIyM0toRyQofQNZeWhlAGU0Z3YKBmCrG34AoIsrJ3DPJOjiJSRIiGwU3yNTHLdbBwBrCBAAfh4ChHYsqDTQ8MjZTA9gKBidADMUmpCMkBF2xM7xCMNy4EYwVDgcGMYcBNQkxkwLc2mrEUYkc0dNegAOBwBaAEZLnAAZOBoIVBgWJAQegiQbDPIHJI8eKLZKUc51TI8Xp5KAdWFkegxOCTZHkPFQADkcFW2AJUAAZOSerUnC4EJpsSsDGsKVTkc4UK5GRDOmIAD580bERm4-EEtEE0FqPJlbBQ0okkbaSLGXqzRrNNqomHzakhFGRcQyKBCyATeEBMXE5mk1nG00QEU0vGUImKsmU+3sukMyaypX4tBWe0mlrgmH0JpzCiBjAQEMmqBhqO0vW5F2jOOs1I0gFMy4ACwBOhoCAACltIKhQJHmpbKEH44moDxG-aeOMLICpSg8qpOfS3Mp++pB5UVfD1bSB5oePA1F4cPgVbr2f1DEaTWF-VVVbUxJ6Td6Z47JhzR5oYw2s4fw4dOvRz64NlsdsRjsNF7hGAgWABbCB60zKxZBkTBzABcI4jgACYAgpBwgAbygAAPFIXgAJgAdikKAAF8wPg8ICBoOA4MgqBkJAFICS+VAID-JwwCQdA-0lfCZBkAB6LioAANTYRwWD0cwICQMCwTiAgACVFAAOQAcQAfT4gBBD4AFUAFEaLohimJYv8WklCTpTkjSAFkACEtOklT1O0lJsJ7EYrJ0HQPi01S5PszSdMoTtzG7Uzew0uSABEtIAMXkrTwt87SlIIAB1HQUnQBAABMIHGZwIEy2RRJGMLIpiuS4oSrSXKgOIdAsrSYA+VS4jiFJqDoaDYKI4FqpSnRGua1rKHaqASLI7qQU4oZkDgUTGHMOB2EfFQfQgPBOogciEOMPqBpahwNq2pARBBKAeKgQLARC2aIHmxblpHVx1pgzaiOMWr6r2uIDpeo6Ttw8622mpAbrupawkewdnq6yCdtSr6HDGv7TsBuMZCAA -[is-null]: https://www.typescriptlang.org/play?target=7&jsx=0#code/C4TwDgpgBAkgzgOQK4BsVQLxQBQDcCGKSEAXFPgHYgCUmAfFAUdAJZxQWooDcAUAMYB7CnGBRQkAPIAzTDibEylGmVEAnFhQDm9KJIBGAKwj9gAOjBrBwa+AhmbAZWAbtZ-oRR5CxambgoLPwQ2AAcADRQALQAjH42ADKCAO4QagDC+HAh1HxCImJsyGhk8MXoWN7MSlTUZAqs7Jxo9LxQ4nYyVb6YGFgA5M0o-VAAZKNtHZCCsg29A4JGJsAj45NzfVhDecKiUI4AKgBKMAgA4nL9SWoQALZQLGBwSLcA-P07BVAIAKoJCXJtgJdmJfgBZABCAFEjnIAEwAdk+ewhkkkCShAEEEHJpIRssixKj0ViEAB9Y4-KFyFzEQlQYkY7Fk06HbHpalbCDJBmCQQoCCUbB4lDZXLAkT8+woQRabAAA0OJ3O8siRS42CVpzO1FyUAA9PqoCLshK4FKzDK5fLfv9VQ9EBrbQlddwDUbaRAzRarQrwdCjvb1WhsP6Ya73cb8V78uaBZbZQrGaSg46Q8nsRHDVHRTHdj7E-KM+TKVDU+VsMWKUcqVmjSa85L476i2imeTWQd2WW1WmvFXO926znTUA +[is-function]: https://www.typescriptlang.org/play?target=7&jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgAMVIwYGw8UWjEQiZCAFsqGnpualp-QOQYAEkIUPCoFnBIrTJKdNSsHiJymBYIELDgf2BiT0a3dohGgG8YAA9IgFYfGABfP1BIWAaCkWKwKjz5opKcMvJk2gYYWvJ6xvm0rIxN5DpsLBwAcnRmtbAbmAAyF7hZEEx97Ovb+8Ki2ebz22mybGgNCMXyagPW1zgTAOIJ+9RmUOQMP4QhEVxwynIrXA0BgACFUKh7ABRACCADlIgTkESZmScgBxHJ0gAqkQAnAAGAUAdmsfL5ACZhgAWYXSgXi6xgFkkukAVQAsqSqQAlSLWCXClWwBTcnVc9mRG4a+AwMCkBJ8VyHG7GkJquluKjHDYDKj2x2uXYpNL9JjIKCkJhgQYwADUMFGEymxNgXNN9LcVMiYGQThgXQ6uF8KcgIGIcmIIHYuAABoWerWLHMHotcA2IHRfDAAPQ9xHkUsQcuV6u4G6kjlc7k3Zv5VvgXCTzk8rtjPswdAkLxDkeiKs1icU6n02eHVZt8mU2l0te9-tb4g76bDiv7sc3dVa3VnltwsC4F+2o6neG6Ps+xJ7ge46muadLsr+87-rgsEWqBD7bsgu5vtBtbBB6bhNueC4AfhnroQOWEvlBH7ptymZUohF6LnRDEUeBVGQThY61sWaTDERf4LIufHVImFFMkAA +[is-instance]: https://www.typescriptlang.org/play?target=7&jsx=0&module=6#code/PTAEBUE8AcFMDoBQAXGtQGED2A7AzsgE4CuAxsloQDxRwB8oAvKDrAO6gAU8PAhoQHM8ALlC8ckANoBdAJRMGtWAG4UaUAEk8AMWI5yAS1xMuAN14AbYrFHjI8xg3NX0BvFx7xo-XgFtbEg4MdqqocJp4GvjI4qTozFQA8gBGAFZ0nM7WAZAANKAG0bE2mLgEJOSUSWl0QaBZru4pqaHqWgDKRIUCJpmW2WKBCvX9jaDl3a3hWs2w5CbV6X0uOfkA1rCQAPyiEzgCdQ0FTWmqIKC6+shGOEikZcigYbCJAGa9DTmyu137w7PkLyELAUZ7wCidQjdeCkSwWZbWWTwPAWAxxTgADnyAFoAIxIigAGSwbFghAwvDwsE4slU92ix0uhlwoi0TOuxmYCJKdm+Ixcxw8PG8hD8X2GdmGzze3IcjGYAHJXnpmTgFaAAGQap5oLDvI7yxXKq43dVa-nWApFfSwPUXFUcnBMeVPEjoc1HQoEYp2gGPQ2u6x0h7HSHdVl4MN-LmfQb2USe9x7HqOLjPO0Gl0KrBpObIM3az3WuK+3PzD2jK3em12qMHUAAHwbOrgGcrAYVyYVwYZbj9Eb9C2aJhzqTzGVjdnWmx241+BwTlbcoGHjkQoDTaBlDTlitHeYLLdt+vbWf35EPRerJfeg4DRER643oC2ofnnA29ifz5foE-VotWBv1AUQHyAjdRFeSwqR7Ahjiia8Si0BCYhtIcam5VYq1QuJRGwaIKgoahmlqRcBWXVc6CfPsy2QRYJ1GeQtmAtx2RuTgvRw2AmOAjcry4u1OOKZ1mDAzVtTcOsOOLBBoGBUE0BhB5CMoeAcD8bjgMg6DwJA0AoIsGDEEQUgLEpdx2iwXxYAwMy8HcABvUAAA8TFxAAmAB2ZRQAAX1AUzzIgNgsFsoKnMgEwFWJQhYF8ApoDwYhfG7PzjPOAA1fgDF4ZILFgPATJDdpwAAJQ0AA5ABxAB9DKAEFCQAVQAUSimK4oSpLfC2VL6TgiqmoAWQAIRa0q6sa1qTC8nz+seEbEkSQkWvqirJuatrmAMmCApDJqKoAERa7RKpaw6NtamrwAAdUSEw9AAE1gV5ClgR7VHyx4DuO06KvOy6Wtgx52kSIaWowQl6vadoTFYDhLOssL7JpOaQ1uxJIeh2HmHh4LQrsvBUeM-qsHy+ALCwAQOMiGSqERmzCYyDGsZh-IGeRvBZFpUBzh2oDSfJynqbcFDinpqzGfMjJQfB1n2nZyXOe5nzzjAor8DJhBhZpsWbQlpGmc4FmobZ-HlZ5tW3UQIA +[is-null]: https://www.typescriptlang.org/play?target=7&jsx=0&ssl=11&ssc=21&pln=11&pc=33#code/C4TwDgpgBAkgzgOQK4BsVQLxQBQDcCGKSEAXFPgHYgCUmAfFAUdAJZxQWooDcAUAMYB7CnGBRQkAPIAzTDibEylGmVEAnFhQDm9KJIBGAKwj9gAOjBrBwa+AhmbAZWAbtZ-oRR5CxambgoLPwQ2AAcADRQALQAjH42ADKCAO4QagDC+HAh1HxCImJsyGhk8MXoWN7MSlTUZAqs7JxouhIQMlW+mBhYAOTNKL1QAGTD4naCsg3dfYJGJsBDo4w+0D1YA3nColCOACoASjAIAOJyvUlqEAC2UCxgcEjXAPy9WwVQCACqCQlymwJtmJvgBZABCAFEDnIAEwAdneOzBkkkCQhAEEEHJpIRsoixMjURiEAB9Q5fCFyFzEfFQQlozEk477THpSkbCDJOmCQQoCCUbA4lDZXKAkS8+woQRabAAA32R1OssiRS42AVxxO1FyUAA9LqoELsmK4BKzFKZbLvr9lXdEGrrQltdw9QbqRATWaLXLQZCDrbVWhsL6oc7XYbcR78qa+ebpXL6cSA-ag4nMWH9RHhVHtl747K06TyRDk+VsIWyQcKRmDUac+LY96CyiGaTmXtWSWVSmvBX252a1njUA [is-number]: https://www.typescriptlang.org/play?target=7&jsx=0#code/C4TwDgpgBAkgzgOQK4FsBGEBOUC8UAUAbgIYA2SEAXFMQHYgCUuAfFCedAJZxS2oaYA3ACgAxgHtacYFFCQA8gDNcBdhWp1G1aZk60A5iyjy0AKwijgAOjCZxwe+AhWHAZWC6DV0WVJEyFAxWcKScohD4ABwANFAAtACMQQ4AMuIA7lgAwsRwEQwiElIy3MjoWNTwZQIq-hwa9AzUalw8fOXYOMzCULJOSnWBuDh4AOTtAqNQAGTTPQTzvdwAYnqcwBEtTCN4HhQzc729+ItHLVB60nTh4srVWMN4imR5B6e955fA1xC3xmYWGQ7KDPUivWbvPqQP7nYHjfhYUanBinAA+qNOJyORzkv2UsJGUFG4gBlimEOxHwCXGKPz+906hL20AplM+tNoN2UJnMlkeskwFGR8wKYkk0igACEYABxGAIAAqKgAnAAGVUAdgSyuVACYAKwAFg1htVOoStEK4pkkvk8hSAFEAIIIFSgvJW4pQBAAVQAspKHQAlFS6jWeiW+gPBgD68tcCpdWQdKloEHS3oRmHw+tFRQlCaD8plKlGaUwEBQFzAcFQAH5RhHxKRnKRxPp8AADaVyxWd2KlLP4HvyhUMApQAD0k5BLwgYqkzdb7a7tvtzoQ-YuiCHa8dLvHginM-d8-zS6sbY7najgaDW8HHXwt+Dh+PAqF55bl5XN-9d7jBAEyTB0Hx3J8XyDQDgIQZM32nD8z3FC8ry7QtizAhl8HQhAZXgk852EYQgA [is-object]: https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&target=7&jsx=0#code/JYOwLgpgTgZghgYwgAgPICMBWEFgJLjTxLIDeyAHgFzIgCuAtutANzIC+AUKJLIiqjAALaBmy4yyAOY16TVh05gAngAcUAMTogEyALzIAFADpTquFDgMacEMoCU+gHzJbylkrUo8AZzE4wfWQAHjEnQwA3OAAbOggbOwAaZABrCGUAfhofMChQKUc9FyjYlGAfNCwPFXVkXwA5AHswerpo6KDImLiabRSQRoB3EEKXdEbG6IhbDwRGkBzkcqaWtuiaBubW9s6SnuQ+geH7GnHJ6ZBnTmRkGohUGC7SxwBCPQMAcnp2j+QAMj+12QexQbwM32is3mizuD123XirjsJ2QOTyICkzkq4jAxlUUGazS8xjAjQAyrl8sYEDFok84vZjD5osAkIYABzJAC0AEZGaSADJDaAAYTgPgghnsUIWgXKFPRmIM9MRbhRIKWFTR+Sxd0aMGBCP072QH21GI+MsW5X8uA2fiwASCoSw4RBCWUyTSmWylIx6qN5WxVxusMeIMKJo+jUduF+AKBeoNGvenxjOPjgJuGtAOVsSH12KdqduUDiQJuGU1Cvyhm9kYMuXLNxbyCr3qWlxTJqbKABRg14uxjKE4tQwwACgT1FAVHX0g3S82WzRe0CaPBohKPEDOAhouKKiLoU3cI0oCKDz4KuQKEEAKxsLhzWXIEWoepkgAqACUAKoil+qA-gA+iKAoAIJkmSQQgBAgxvieZZnheV4+FKVqBAAUmSH4gagABCWEAKKATQtr4IQfAkAYt40AAzIoL6LBof71CKNBaDonTULQjDMFAKJuFiFCYcgBGoKgArERB9RBL2O7MecxjRI0UiGAABsesrIaSqGHhpyQ2rGYDBNpaJ0Chl6HuE76fr+AFAaB4FQWS9jSsgAD0nlLhAe7QspqnqRpOF4YRJGAYZmoUaEwiiCZ4ShfU+FEaRX7uWw3m+f5CyBWpmmsexUXGTiwRcQg4SFSKGVeT5m4SjlPh5cFdnfv+gHAWBkHQQA2hQAC6xUOqV5mnnp1nXrZH5tY5nUudByQfBQHw1Vla5KVMKn5SFuHJeFaU9cog1GcNASxSIUAUYlu0pRFX6LcoK0eVl9V+RtEBbcFVV9cd0UmWV2gVYYVWLctq11TEDXvZ9mkSVJMn1ENMVuOEcPSbJ4PIK9nBAA [is-primitive]: https://www.typescriptlang.org/play?target=7&jsx=0#code/C4TwDgpgBACgTgSwLYOAgbhAzlAvFAcgCMEBzBAO2AKgB9CiB7RgGwgEMKb6CKBXJEQhxuhLCEGtRBLMEQVS0vhQAmEAGaUIKggG4AUPtCQoASSwAhZm054oACnTsWfCAC4onEAEo8APignF2gEHCZWDgoDY2hzADkBITg7R2dXDy9fXACg1yhQqH5BYWjwWKx4ZFQMaHwAHgAVMr9U4IyKEAAaKBiPSpQ0TCwsnLSQnCbIUpNzAGU5SlIU3PdPDpHAsfycWXlSafLZiSYWZbH2n39N4O2ocUkWA7MsAFVVDS0VM7a1y+zrvIFZRqTQUbQGfQAY0YFFk2wsZFMVG+6V+3g8K1uJHIyP+MUY6gBtVw+GIZEo1AM0NhwHh1kiHnMVgitnwrVRmQxWwK4RsFCu7JCNM4kIgBKgzL5UAAZNKiflhRRReKAPJEABWEEhwF8tHo9kFeBJPTgeT18pJ+HUziwEF8sp6ZXFmMtDHpnAI3ipMLhoQSxTgjKw-qSKNWnPlBSKobxTsJLuNvESwhoAH5tgAxSioCCC3xcm6UWQisWEkPCGVyzFF4Al1UarXAb007bzPZBtuLMMXdGRnYLBQC-HxraugiMBvamgO6uK5WEtWa7WVyNz0tQTsKXX0YcWxO7RZ6KE+2mhI4PDvHVjdtEFwE7K+nWOQZ2j-ePo-U32vd6g7RBt4QU+G8I2rHBgQ+MEvn+BN8Agv8vgdXdYMIeDPiPY8W1CfpqkwIMcMGWooEaZpBQubpelgRABhqYY73GKBJlqAIAG99CgfJCXsM8B1IewYm8Xw2I4jisAAd1QSEAAsHAEqBhJEjjIXYW0GHJKgCA8OAIGAPg4H5UIEVIJFgDzAxFKUlToGId0uC0nS9IMyxbLM9iLOU1SkwDTSoG03T9O2cs4FciyoA86yDwUHy-Mc1teJC9yrLED97P8pzzxOBLFPC1Df3Q1LYtCQDIO0LKoAAXzcyqOJigLrRYW0DHKiEv1pCwVRVAAZABRABBOI7HqxrMLhCxTAAcVMOIGjsABOAAGeaAHYAEZZtmgAmABWAAWJadvm9aVqiEbaTiF4AFkLG6gAlOwVo2pbmzhWYGhuqbxrsAgLpAQpk2SUJPxPKAXjiAARbqMym7qwbsNCoOe2lZgATSurq7Ay1h7AIeHtE9Z6IgAOhYRg+IAAzGybprJ7psOo3CIDqbEKRaSmpoaboyRxahBN0KAAHp+ZNVwRqJknyfarq+riGntgImo6ki0hWY6nr+s53lIk9L0BaFuQRa-MXSfsMnzqu27ZbpqpCLqaNhBaM3rpuzm7ZEXndeFiBRbYYnjbJ173ricbLYqembaVloA4+zmle1vnBc972IF98mUbRzqQ-lzBFcfSPUfazqY4-d2E-1r3DZ98WTdBiGobiGHM7DhXcZUFoa8h6Gwc5lu449svDAr5Oq9Ny6ncb62FeZqgHdH26NfUnmdYToby59I3ycdi3adDifs4j+xN+dsReN75ebVX2F15NqOg-Hmjs81zhI7e6O3RZLgS6Flek5T6+X9v7eWdGau2foHcaLt-qny-uffQQA diff --git a/packages/type/README.md b/packages/type/README.md index 78219b2f..bf9b14b6 100644 --- a/packages/type/README.md +++ b/packages/type/README.md @@ -8,7 +8,7 @@ Useful packages based on the [angular.io](https://angular.io/). | prism | `Prism` highlighter module. | *In Progress* | [Readme][prism-readme-github] | | property | Features to handle properties. | *In Progress* | [Readme][property-readme-github] | | ui | User interface based on **[Spectre.css](https://github.com/picturepan2/spectre)**. | *In Progress* | [Github][ui-readme-github] | -| type | Common types, type guards and checkers. | [![npm version][type-npm-svg]][type-npm-badge] | [Github][type-readme-github] \| [npm][type-readme-npm] | +| type | Common types, type guards and type checkers. | [![npm version][type-npm-svg]][type-npm-badge] | [Github][type-readme-github] \| [npm][type-readme-npm] | ## angular-package/type @@ -21,43 +21,58 @@ Common types, type guards and type checkers. [![GitHub license](https://img.shields.io/github/license/angular-package/type)](https://github.com/angular-package/type/blob/main/LICENSE) ```typescript -// Guard functions. +// `guard` prefix functions. import { guardArray, + guardBigInt, + guardBoolean, guardFunction, + guardInstance, + guardKey, + guardNull, guardNumber, guardObject, guardObjectKey, guardPrimitive, guardString, - guardType + guardSymbol, + guardType, + guardUndefined } from '@angular-package/type'; ``` ```typescript -// Check is functions. +// Check `is` prefix functions. import { isArray, isBigInt, isBoolean, + isBooleanObject, + isBooleanType, isDefined, isFunction, isInstance, + isKey, isNull, isNumber, + isNumberObject, + isNumberType, isObject, + isObjectKey, isPrimitive, isString, + isStringObject, + isStringType, isSymbol, isType, isUndefined } from '@angular-package/type'; -// Check are functions. +// Check `are` prefix functions. import { areString } from '@angular-package/type'; ``` ```typescript -// Check is NOT functions. +// Check `isNot` prefix functions. import { isNotBoolean, isNotDefined, @@ -70,74 +85,95 @@ import { ``` ```typescript -// Guard and is object. -import { are, guard, is } from '@angular-package/type'; +// Object. +import { are, guard, is, isNot } from '@angular-package/type'; ``` ```typescript // Types. -import { Constructor, CycleHook, Func, Primitive, Primitives, Types } from '@angular-package/type'; +import { Constructor, CycleHook, Func, Key, Primitive, Primitives, ResultCallback, Type, Types } from '@angular-package/type'; ``` ## Features * Checks if * **any** value is - * an `Array` of any type with [isArray](#isArray). - * a `'bigint'` type with [isBigInt](#isBigInt). - * a `'boolean'` type with [isBoolean](#isBoolean). - * a `function` type with [isFunction](#isFunction). - * a generic type `instance` with [isInstance](#isInstance). - * a `null` type with [isNull](#isNull). - * a `number` type with [isNumber](#isNumber). - * a generic type `'object'` with [isObject](#isObject). - * a one of the primitive `boolean`, `bigint`, `number`, `string` type with [isPrimitive](#isPrimitive). - * a `string` type with [isString](#isString). - * a `symbol` type with [isSymbol](#isSymbol). - * a generic type instance, `'function'`, `'object'` or primitive type with [isType](#isType). - * a `'undefined'` type with [isUndefined](#isUndefined). + * an `Array` of any type with [`isArray()`](#isarray). + * a `bigint` type with [`isBigInt()`](#isbigint). + * a `boolean` with [`isBoolean()`](#isboolean). + * an `object`type and instance of [`Boolean`][boolean] and [`Object`][object] with [`isBooleanObject()`](#isbooleanobject). + * a `boolean` type not an instance of [`Boolean`][boolean] and [`Object`][object], and equal to `true` or `false` with [`isBooleanType()`](#isbooleantype). + * a `function` with [`isFunction()`](#isfunction). + * a generic type `instance` with [`isInstance()`](#isinstance). + * a [`Key`](#Key) type with [`isKey()`](#iskey). + * a `null` with [`isNull()`](#isnull). + * a `number` with [`isNumber()`](#isnumber). + * an `object` type and instance of [`Number`][Number] and [`Object`][object] with [`isNumberObject()`](#isnumberobject). + * a `number` type and **not** instance of [`Number`][Number] and [`Object`][object] with [`isNumberType()`](#isnumbertype). + * a generic type `object` with [`isObject()`](#isobject). + * an `object` with its own specified [`Key`](#Key) with [`isObjectKey()`](#isobjectkey). + * a one of the primitive `boolean`, `bigint`, `number`, `string` with [`isPrimitive()`](#isPrimitive). + * a `string` with [`isString()`](#isstring). + * an `object` type and instance of [`String`][string] and [`Object`][object] with [`isStringObject()`](#isstringobject). + * a `string` type and **not** instance of [`String`][string] and [`Object`][object] with [`isStringType()`](#isstringtype). + * a `symbol` with [`isSymbol()`](#isSymbol). + * a generic type instance, `function`, `object` or primitive type with [`isType()`](#istype). + * a `undefined` type with [`isUndefined()`](#isundefined). * an **unknown** value is - * defined with [isDefined](#isDefined). - * an **unknown** value is NOT - * a `'boolean'` type with [isNotBoolean](#isNotBoolean) - * a `'function'` type with [isNotFunction](#isNotFunction) - * a `'null'` type with [isNotNull](#isNotNull) - * a `'number'` type with [isNotNumber](#isNotNumber) - * a `'string'` type with [isNotString](#isNotString) - * a `'undefined'` type with [isNotUndefined](#isNotUndefined) + * defined with [`isDefined()`](#isdefined). + * an **unknown** value is **not** a + * `boolean` type with [`isNotBoolean()`](#isnotboolean) + * `function` type with [`isNotFunction()`](#isnotfunction) + * `null` type with [`isNotNull()`](#isnotnull) + * `number` type with [`isNotNumber()`](#isnotnumber) + * `string` type with [`isNotString()`](#isnotstring) + * `undefined` type with [`isNotUndefined()`](#isnotundefined) * Guard the value to be - * an `Array` of generic type with [guardArray](#guardArray). - * a `function` type with [guardFunction](#guardFunction). - * a `number` type with [guardNumber](#guardNumber). - * a generic `'object'` type that contains `key` with [guardObjectKey](#guardObjectKey). - * a generic `'object'` type with [guardObject](#guardObject). - * a one of the `Primitives` with [guardPrimitive](#guardPrimitive). - * a `string` type with [guardString](#guardString). - * a generic type from one of the [`Types`](#types) type with [Type guard](#guardType). + * an [`Array`][array] of a generic type with [`guardArray()`](#guardarray). + * a `bigint` with [`guardBigInt()`](#guardbigint). + * a `boolean` with [`guardBoolean()`](#guardboolean). + * a `function` type with [`guardFunction()`](#guardfunction). + * an instance with [`guardInstance()`](#guardinstance). + * a `null` with [`guardNull()`](#guardnull). + * a [`Key`](#Key) with [`guardKey()`](#guardkey). + * a `number` with [`guardNumber()`](#guardnumber). + * an `object` of a generic type with [`guardObject()`](#guardobject). + * an `object` of a generic type that contains `key` with [`guardObjectKey()`](#guardiobjectkey). + * a one of the [`Primitives`](#primitives) with [`guardPrimitive()`](#guardprimitive). + * a `string` with [`guardString()`](#guardstring). + * a `symbol` with [`guardSymbol()`](#guardsymbol). + * a generic type from one of the [`Types`](#types) type with [`guardType()`](#guardtype). + * `undefined` with [`guardUndefined()`](#guardundefined). ## How angular-package understands Check -> Is to check the return value to be the same as expected. +> Is to check the inputted value to be **the same** as **expected**. Type guard -> Is to guard type from parameter to not let input unexpected value in the code editor. +> Is to guard the parameter type to **not let** input **unexpected** value in the **code editor**. Guard -> Is a combination of both above to type guard input in the code editor and check the return. +> Is a **combination** of both above to **guard type** in the **code editor** and to check inputted value. ---- * [Installation](#installation) -* [Object](#are-object) - * [`are`](#are-object) - * [`guard`](#guard-object) - * [`is`](#is-object) -* [Checks](#checks) - * [are](#areString) - * [is](#isArray) -* [Guards](#guards) -* [Types](#types) +* [resultCallback](#resultcallback) +* [Check](#check) + * [are](#are) + * [is](#is) + * [isNot](#isnot) +* [Guard](#guard) +* [Experimental](#Experimental) + * [BigIntObject](#bigintobject) + * [BooleanObject](#booleanobject) + * [NumberObject](#numberobject) + * [PrimitiveObject](#primitiveobject) + * [StringObject](#stringobject) + * [SymbolObject](#symbolobject) + * [isParam()](#isparam) +* [Common types](#common-types) * [Git](#git) * [Commit](#commit) * [Versioning](#versioning) @@ -153,614 +189,1486 @@ Install `@angular-package/type` package with command: npm i --save @angular-package/type ``` -## are Object +## resultCallback -Object `are` with some of **check are** functions. +Default function to handle `callback`. ```typescript -const are: Are = { - string: areString +const resultCallback: ResultCallback = (result: boolean): boolean => result; +``` + +Custom function to handle `callback`. + +```typescript +const customCallback: ResultCallback = (result: boolean): boolean => { + if (result === false) { + throw new Error('error'); + } + return result; }; + +const stringResult = isString('Lorem ipsum', customCallback); + ``` -## guard Object +## Check + +### are -Object `guard` with all **guard** functions. +Tne object contains prefixed with `are` functions. ```typescript -const guardIs: GuardIs = { - array: guardArray, - function: guardFunction, - number: guardNumber, - objectKey: guardObjectKey, - object: guardObject, - primitive: guardPrimitive, - string: guardString, - type: guardType -}; -const guard: Guard = { - is: guardIs +const are: Are = { + string: areString }; +``` + +---- + +### areString + + Use `areString()` or `are.string()` to check if all of **any** arguments are a `string` type. +```typescript +const areString = (...args: any): boolean => check('string', ...args); ``` -## is Object +| Parameter | Type | Description | +| :-------- | :---: | :------------------------------------------------- | +| ...args | `any` | Any arguments to check they're all a `string` type | + +The **return value** is a `boolean` value. + +[Example usage on playground][are-string] -Object `is` with all **check is** functions and **check is not** in `not` property. +---- + +### is + +The object contains prefixed with `is` functions and prefixed with `isNot` functions in property `not`. ```typescript const is: Is = { array: isArray, - bigInt: isBigInt, + bigInt: isBigInt, // deprecated + bigint: isBigInt, boolean: isBoolean, + booleanObject: isBooleanObject, + booleanType: isBooleanType, defined: isDefined, function: isFunction, + instance: isInstance, + key: isKey, not: isNot, null: isNull, number: isNumber, + numberObject: isNumberObject, + numberType: isNumberType, object: isObject, + objectKey: isObjectKey, primitive: isPrimitive, string: isString, + stringObject: isStringObject, + stringType: isStringType, symbol: isSymbol, type: isType, undefined: isUndefined }; ``` -## isNot Object +---- -Object `isNot` with all **check is not** functions. +### isArray + +Use `isArray()` or `is.array()` to check if **any** `value` is an [`Array`][array], [`Array`][array] instance, and `object` type. ```typescript -const isNot: IsNot = { - boolean: isNotBoolean, - defined: isNotDefined, - function: isNotFunction, - null: isNotNull, - number: isNotNumber, - string: isNotString, - undefined: isNotUndefined -}; +const isArray: IsArray = (value: any, callback: ResultCallback = resultCallback): value is Array => + callback( + typeOf(value) === 'array' && + Array.isArray(value) === true && + value instanceof Array === true && + typeof value === 'object' + ); ``` -## Checks +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -### areString +The **return value** is a `boolean` indicating whether or not the `value` is an [`Array`][array]. - Use `areString()` or `are.string()` to check if all of **any** arguments are a `'string'` type. The return value is a `boolean` value. +```typescript +// Example usage +const ARRAY_NUMBER = [1, 2, 3]; +const ARRAY_STRING = ['a', 'b', 'c']; + +isArray(ARRAY_NUMBER); // true +isArray(ARRAY_STRING); // true +``` + +[Example usage on playground][is-array] + +---- + +### isBigInt + +Use `isBigInt()` or `is.bigint()` to check if **any** `value` is a `bigint` type. ```typescript -const areString = (...args: any): boolean => check('string', ...args); +const isBigInt: IsBigInt = (value: any, callback: ResultCallback = resultCallback): value is bigint => + callback(typeOf(value) === 'bigint' && typeof value === 'bigint'); ``` | Parameter | Type | Description | -|-----------| :---: |-------------| -| ...args | `any` | Any arguments to check they're all a `'string'` type. | +| :-------- | :---: | :---------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][are-string] +The **return value** is a `boolean` indicating whether or not the `value` is a `bigint`. -### isArray +```typescript +// Example usage +const BIGINT = 9007199254740991n; +const NUMBER = 27; + +isBigInt(NUMBER); // false +isBigInt(BIGINT); // true +``` -Use `isArray()` or `is.array()` to check if **any** `value` is an `Array` of `'object'` generic `Type` type and `Array` instance. The return value is a `boolean` value. +[Example usage on playground][is-bigint] | [How to detect `bigint` type][detect-bigint] + +---- + +### isBoolean + +Use `isBoolean()` or `is.boolean()` to check if **any** `value` is a `boolean` type not instance of [`Boolean`][boolean] and [`Object`][object] or `object` type instance of [`Boolean`][boolean] and [`Object`][object]. ```typescript -const isArray: IsArray = (value: any): value is Array => - typeOf(value) === 'array' && - Array.isArray(value) === true && - value instanceof Array === true && - typeof value === 'object'; +const isBoolean: IsBoolean = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback(typeOf(value) === 'boolean' && (isBooleanType(value) || isBooleanObject(value))); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's an `Array` of `'object'` generic `Type` type and `Array` instance. | +| Parameter | Type | Description | +| :---------| :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][is-array] +The **return value** is a `boolean` indicating whether or not the `value` is a `boolean`. -### isBigInt +```typescript +// Example usage +const BOOLEAN = false; +const BOOLEAN_INSTANCE = new Boolean(false); -Use `isBigInt()` or `is.bigInt()` to check if **any** `value` is a `'bigint'` type. The return value is a `boolean` value. +isBoolean(BOOLEAN); // true +isBoolean(BOOLEAN_INSTANCE); // true +``` + +[Example usage on playground][is-boolean] | [How to detect the `boolean` type][detect-boolean] + +---- + +### isBooleanObject + +Use `isBooleanObject()` or `is.booleanObject()` to check if **any** `value` is an `object` type and instance of [`Boolean`][boolean] and [`Object`][object]. ```typescript -const isBigInt: IsBigInt = (value: any): value is bigint => - typeOf(value) === 'bigint' && - typeof value === 'bigint'; +const isBooleanObject: IsBooleanObject = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback(typeof value === 'object' && value instanceof Boolean === true && value instanceof Object === true); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's a `'bigint'` type. | +| Parameter | Type | Description | +| :---------| :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][is-bigint] | [How to detect 'bigint' type][detect-bigint] +The **return value** is a `boolean` indicating whether or not the `value` is a [`Boolean`][boolean] instance. -### isBoolean +```typescript +// Example usage +const BOOLEAN = false; +const BOOLEAN_INSTANCE = new Boolean(false); + +isBooleanObject(BOOLEAN); // false +isBooleanObject(BOOLEAN_INSTANCE); // true +``` + +---- + +### isBooleanType -Use `isBoolean()` or `is.boolean()` to check if **any** `value` is a `'boolean'` type not instance of `Boolean` and `Object` or `'object'` type instance of `Boolean` and `Object`. The return value is a `boolean` value. - -```typescript -const isBoolean: IsBoolean = (value: any): value is boolean => - typeOf(value) === 'boolean' && - ( - ( - typeof value === 'object' && - value instanceof Boolean === true && - value instanceof Object === true - ) - || - ( - value instanceof Boolean === false && - value instanceof Object === false && - typeof value === 'boolean' && - (value === true || value === false) - ) +Use `isBooleanType()` or `is.booleanType()` to check if **any** `value` is a `boolean` type not an instance of [`Boolean`][boolean] and [`Object`][object], and equal to `true` or `false`. + +```typescript +const isBooleanType: IsBooleanType = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback( + value instanceof Boolean === false && + value instanceof Object === false && + typeof value === 'boolean' && + (value === true || value === false) ); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's a `'boolean'` type not instance of `Boolean` and `Object` or `'object'` type instance of `Boolean` and `Object`. | +| Parameter | Type | Description | +| :---------| :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `boolean` type. + +```typescript +// Example usage +const BOOLEAN = false; +const BOOLEAN_INSTANCE = new Boolean(false); -[Example usage][is-boolean] | [How to detect 'boolean' type][detect-boolean] +isBooleanType(BOOLEAN); // true +isBooleanType(BOOLEAN_INSTANCE); // false +``` + +---- ### isDefined -Use `isDefined()` or `is.defined()` to check if an **unknown** `value` is NOT an `'undefined'` type and is not equal to `undefined`. The return value is a `boolean` value. +Use `isDefined()` or `is.defined()` to check if an **unknown** `value` is NOT an `undefined` type and is NOT equal to `undefined`. + +```typescript +const isDefined: IsDefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'undefined' && typeof value !== 'undefined' && value !== undefined); +``` + +| Parameter | Type | Description | +| :-------- | :-------: | :---------------------------- | +| value | `unknown` | An `unknown` `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is defined, not `undefined`. ```typescript -const isDefined: IsDefined = (value: unknown): boolean => - typeOf(value) !== 'undefined' && - typeof value !== 'undefined' && - value !== undefined; +// Example usage +const UNDEFINED = undefined; +let defined; + +isDefined(UNDEFINED); // false +isDefined(defined); // false ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT an `'undefined'` type and is not equal to `undefined`. | +---- ### isFunction -Use `isFunction()` or `is.function()` to check if **any** `value` is a `'function'` type, instance of `Function`, and `Object`. The return value is a `boolean` value. +Use `isFunction()` or `is.function()` to check if **any** `value` is a `function` type, an instance of [`Function`][function] and [`Object`][object]. ```typescript -const isFunction: IsFunction = (value: any): value is Func => - typeOf(value) === 'function' && - typeof value === 'function' && - value instanceof Function === true && - value instanceof Object === true; +const isFunction: IsFunction = (value: any, callback: ResultCallback = resultCallback): value is Func => + callback( + typeOf(value) === 'function' && + typeof value === 'function' && + value instanceof Function === true && + value instanceof Object === true + ); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check it's a `'function'` type, instance of `Function` and `Object`. | +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `function`. + +```typescript +// Example usage +class Class { x = 5; } +const FUNC: Func = (x: number): any => { return x + 5; } + +isFunction(Class); // true +isFunction(FUNC); // true +isFunction(() => 5); // true +``` -[Example usage][is-function] | [How to detect 'function' type][detect-function] +[Example usage on playground][is-function] | [How to detect `function` type][detect-function] + +---- ### isInstance -Use `isInstance()` or `is.instance()` to check if **any** value is a generic `Obj` type `constructor` instance and is an `Object`. +Use `isInstance()` or `is.instance()` to check if **any** value is an `object` of a generic `Obj` type equal to an `instance` of [`Constructor`](#Constructor) type. ```typescript -const isInstance: IsInstance = (value: any, instance: Constructor): value is Obj => - isObject(value) && - value instanceof instance === true && - isString(instance.prototype.constructor.name) === true; +const isInstance: IsInstance = ( + value: any, + instance: Constructor, + callback: ResultCallback = resultCallback + ): value is Obj => + callback( + isObject(value) ? + isFunction(instance) ? + value instanceof instance === true + : false + : false + ); ``` -| Parameter | Type | Description | -|-----------| :----------------: |-------------| -| value | `any` | Any generic `Obj` type `value` instance to compare with `type` instance. | -| type | `Constructor` | Creates generic `Obj` type instance to compare with argument `value`. | +| Parameter | Type | Description | +| :-------- | :--------------------------------: | :---------- | +| value | `any` | Any `value` to compare with the `instance` | +| instance | [`Constructor`](#Constructor) | A generic `Obj` [`Constructor`](#Constructor) type to create an `instance` to compare with the `value` | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is an `instance` of a generic `Obj`. -[Example usage][is-instance] | [How to detect `constructor` instance][detect-instance] +```typescript +// Example usage +class Some { x = 127; } +class Two { y = 'Lorem ipsum'; } + +const SOME = new Some(); +const TWO = new Two(); + +isInstance(TWO, Some); // false +isInstance(SOME, Some); // true +isInstance(TWO, Two); // true and type error +``` + +[Example usage on playground][is-instance] | [How to detect `constructor` instance][detect-instance] + +---- + +### isKey + +Use `isKey()` or `is.key()` to check if **any** `value` is one of the `string`, `number`, or `symbol`. + +```typescript +const isKey: IsKey = (value: any, callback: ResultCallback = resultCallback): value is Key => + callback(isString(value) || isNumber(value) || isSymbol(value)); +``` + +| Parameter | Type | Description | +| :-------- | :---: |:-------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a [`Key`](#Key). + +```typescript +// Example usage +const STRING = 'surname'; +const STRING_INSTANCE = new String(STRING); +isKey(STRING); // true +isKey(STRING_INSTANCE); // true + +const NUMBER = 27; +const NUMBER_INSTANCE = new Number(NUMBER); +isKey(NUMBER); // true +isKey(NUMBER_INSTANCE); // true + +const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER); +const SYMBOL_STRING: unique symbol = Symbol(STRING); +isKey(SYMBOL_NUMBER); // true +isKey(SYMBOL_STRING); // true +``` + +---- ### isNull -Use `isNull()` or `is.null()` to check if **any** `value` is an `'object'` type and equal to `null`. +Use `isNull()` or `is.null()` to check if **any** `value` is an `object` type and equal to `null`. ```typescript -const isNull: IsNull = (value: any): value is null => - typeOf(value) === 'null' && - typeof value === 'object' && - value === null; +const isNull: IsNull = (value: any, callback: ResultCallback = resultCallback): value is null => + callback(typeOf(value) === 'null' && typeof value === 'object' && value === null); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's a `null` value and an `'object'` type. | +| Parameter | Type | Description | +| :-------- | :---: |--------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is `null`. + +```typescript +// Example usage +/** + * typeof === 'object' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NULL: any = null; +const NUMBER = 27; + +isNull(NULL); // true +isNull(NUMBER); // false +``` + +[Example usage on playground][is-null] | [How to detect `null` type][detect-null] -[Example usage][is-null] | [How to detect `null` type][detect-null] +---- ### isNumber -Use `isNumber()` or `is.number()` to check if **any** `value` is a '`number`' type not instance of `Number` and `Object` or `'object'` type instance of `Number` and `Object`. The return value is a `boolean` value. - -```typescript -const isNumber: IsNumber = (value: any): value is number => - typeOf(value) === 'number' && - ( - isFinite(value) === true && - ( - value instanceof Number === false && - value instanceof Object === false && - typeof value === 'number' - ) - || - ( - typeof value === 'object' && - value instanceof Number === true && - value instanceof Object === true - ) - ); +Use `isNumber()` or `is.number()` to check if **any** `value` is a `number` type not an instance of [`Number`][Number] and [`Object`][object] or `object` type instance of [`Number`][Number] and [`Object`][object]. + +```typescript +const isNumber: IsNumber = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(typeOf(value) === 'number' && isFinite(value) === true && (isNumberType(value) || isNumberObject(value))); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any ``value`` to check if it's a `'number'` type not instance of `Number` and `Object` or `'object'` type instance of `Number` and `Object`. | +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][is-number] | [How to detect `'number'` type][detect-number] +The **return value** is a `boolean` indicating whether or not the `value` is a `number`. + +[Example usage on playground][is-number] | [How to detect a `number` type][detect-number] + +---- + +### isNumberObject + +Use `isNumberObject()` or `is.numberObject()` to check if **any** `value` is an `object` type and instance of [`Number`][Number] and [`Object`][object]. + +```typescript +const isNumberObject: IsNumberObject = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(typeof value === 'object' && value instanceof Number === true && value instanceof Object === true); +``` + +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a [`Number`][Number] instance. + +```typescript +// Example usage +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER: any = 10304050; + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER_INSTANCE: any = Number(NUMBER); + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === true + * instanceof Object === true + */ +const NUMBER_NEW_INSTANCE: any = new Number(NUMBER); + +isNumberObject(NUMBER); // false +isNumberObject(NUMBER_INSTANCE); // false +isNumberObject(NUMBER_NEW_INSTANCE); // true +``` + +---- + +### isNumberType + +Use `isNumberType()` or `is.numberType()` to check if **any** `value` is a `number` type not an instance of [`Number`][Number] and [`Object`][object]. + +```typescript +const isNumberType: IsNumberType = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(value instanceof Number === false && value instanceof Object === false && typeof value === 'number'); +``` + +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `number`. + +```typescript +// Example usage +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER: any = 10304050; + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER_INSTANCE: any = Number(NUMBER); + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === true + * instanceof Object === true + */ +const NUMBER_NEW_INSTANCE: any = new Number(NUMBER); + +isNumberType(NUMBER); // true +isNumberType(NUMBER_INSTANCE); // true +isNumberType(NUMBER_NEW_INSTANCE); // false +``` + +---- ### isObject -Use `isObject()` or `is.object()` to check if **any** `value` is a generic `Obj` `'object'` type and `Object` instance with the possibility of containing `key`. The return value is a `boolean` value. +Use `isObject()` or `is.object()` to check if **any** `value` is an `object` of a generic `Obj` type and [`Object`][object] instance with the possibility of containing the `key`. ```typescript -const isObject: IsObject = (value: any, key?: string): value is Obj => - typeOf(value) === 'object' && - typeof value === 'object' && - value instanceof Object === true - ? isString(key) === true - ? value.hasOwnProperty(key) === true +const isObject: IsObject = (value: any, key?: Key): value is Obj => + (typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true) + ? isKey(key) + ? key in value : true : false; ``` -| Parameter | Type | Description | -|-----------| :------: |-------------| -| value | `any` | Any `value` to check if it's a generic `'object'` type and `Object` instance. | -| key? | `string` | Property name to find in argument `value`. | +| Parameter | Type | Description | +| :-------- | :-----------: | :---------- | +| value | `any` | Any `value` to check | +| key? | [`Key`](#Key) | Property name to find in the `value` | + +The **return value** is a `boolean` indicating whether or not the `value` is an `object`. + +[Example usage on playground][is-object] | [How to detect an `object` type][detect-object] + +```typescript +// Example usage +const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER); +const SYMBOL_STRING: unique symbol = Symbol(STRING); + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER: any = 10304050; + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === false + * instanceof Object === false + */ +const NUMBER_INSTANCE: any = Number(NUMBER); + +/** + * typeof === 'number' + * instanceof Function === false + * instanceof Number === true + * instanceof Object === true + */ +const NUMBER_NEW_INSTANCE: any = new Number(NUMBER); + +/** + * typeof === 'string' + * instanceof Function === false + * instanceof Object === false + * instanceof String === false + */ +const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz'; + +/** + * typeof === 'string' + * instanceof Function === false + * instanceof Object === false + * instanceof String === false + */ +const STRING_INSTANCE: any = String(STRING); + +/** + * typeof === 'string' + * instanceof Function === false + * instanceof Object === true + * instanceof String === true + */ +const STRING_NEW_INSTANCE: any = new String(STRING); + +const OBJECT_ONE: ObjectOne = { + 'key as string': true, + 1030405027: 'key is number', + 5: 'key is also number', + [NUMBER]: 'key is number', + [string]: 'key is string', + [SYMBOL_NUMBER]: 'key is symbol number', + [SYMBOL_STRING]: 6, + x: 3000 +}; + +isObject(OBJECT_ONE); // true +isObject(OBJECT_ONE, 'key as string'); // true +isObject(OBJECT_ONE, STRING); // true +isObject(OBJECT_ONE, STRING_NEW_INSTANCE); // true +isObject(OBJECT_ONE, 1030405027); // true +isObject(OBJECT_ONE, NUMBER); // true +isObject(OBJECT_ONE, NUMBER_NEW_INSTANCE); // true +isObject(OBJECT_ONE, SYMBOL_NUMBER); // true +isObject(OBJECT_ONE, SYMBOL_STRING); // true + +``` + +---- + +### isObjectKey + +Use `isObject()` or `is.object()` to check if **any** `value` is an `object` with its own specified keys of the [`Key`](#Key). -[Example usage][is-object] | [How to detect `'object'` type][detect-object] +```typescript +const isObjectKey: IsObjectKey = ( + value: any, + key: Key | Key[], + callback: ResultCallback = resultCallback +): value is Type => + callback( + isObject(value) ? + isArray(key) ? + key.every(k => isKey(k) ? ({}).hasOwnProperty.call(value, k) === true : false) + : isKey(key) ? + ({}).hasOwnProperty.call(value, key) + : false + : false + ); +``` + +| Parameter | Type | Description | +| :-------- | :------------------------------: | :---------------------------------------------------- | +| value | `any` | Any `value` to check if it contains a specified `key` | +| key | [`Key`](#key) \| [`Key`](#Key)[] | A [`Key`](#Key) type or an array of [`Key`](#Key) type to check in the `value` | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is an `object` with its own specified keys. + +---- ### isPrimitive -Use `isPrimitive()` or `is.primitive()` to check if **any** `value` is a generic `Type` type one of the primitive `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` type. The return value is a `boolean` value. +Use `isPrimitive()` or `is.primitive()` to check if **any** `value` is the [`Primitive`](#primitive) type from a `type` of the [`Primitives`](#primitives) type. ```typescript -const isPrimitive: IsPrimitive = (value: any, type: Primitives): value is Type => { +const isPrimitive: IsPrimitive = ( + value: any, + type: Primitives, + callback: ResultCallback = resultCallback +): value is T => { if (isString(type)) { switch (type) { - case 'bigint': return isBigInt(value); - case 'boolean': return isBoolean(value); - case 'number': return isNumber(value); - case 'string': return isString(value); - case 'symbol': return isSymbol(value); - case 'undefined': return isUndefined(value); + case 'bigint': return isBigInt(value, callback); + case 'boolean': return isBoolean(value, callback); + case 'number': return isNumber(value, callback); + case 'null': return isNull(value, callback); + case 'string': return isString(value, callback); + case 'symbol': return isSymbol(value, callback); + case 'undefined': return isUndefined(value, callback); } } + return false; }; ``` -| Parameter | Type | Description | -|-----------| :----------: |--------------| -| value | `any` | Any `value` to check if it's a generic `Type` type from on of the `type`. | -| type | `Primitives` | One of the `Primitives` `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` type to check `value`. | +| Parameter | Type | Description | +| :-------- | :-------------------------: | :------------------------------------------------------------------------ | +| value | `any` | Any `value` to check if it's a `Primitive` from the `type` | +| type | [`Primitives`](#primitives) | A `string` type from the [`Primitives`](#primitives) to check the `value` | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][is-primitive] +The **return value** is a `boolean` indicating whether or not the `value` is a `type` from the [`Primitives`](#primitives). + +[Example usage on playground][is-primitive] + +---- ### isString -Use `isString()` or `is.string()` to check if **any** `value` is a `'string'` type, not instance of `Object` and `String` or `'object'` type and instance of `String` and `Object`. The return value is a `boolean` value. +Use `isString()` or `is.string()` to check if **any** `value` is a `string` type, not instance of [`Object`][object] and [`String`][string] or `object` type and instance of [`String`][string] and [`Object`][object]. ```typescript -const isString: IsString = (value: any): value is string => - typeOf(value) === 'string' && - ( - typeof value === 'object' && - value instanceof Object === true && - value instanceof String === true - ) - || - ( - value instanceof Object === false && - value instanceof String === false && - typeof value === 'string' - ); +const isString: IsString = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(typeOf(value) === 'string' && (isStringType(value) || isStringObject(value))); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any value to check if it's a `'string'` type, not an instance of `Object` and `String` or `'object'` type and instance of `String` and `Object`. | +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `string`. + +---- + +### isStringObject -[Example usage][is-string] | [How to detect `'string'` type][detect-string] +Use `isStringObject()` or `is.stringObject()` to check if **any** `value` is an `object` type and instance of [`String`][string] and [`Object`][object]. + +```typescript +const isStringObject: IsStringObject = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(value instanceof Object === true && value instanceof String === true && typeof value === 'object'); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a [`String`][string] instance. + +---- + +### isStringType + +Use `isStringType()` or `is.stringType()` to check if **any** `value` is a `string` type and **not** instance of [`String`][string] and [`Object`][object]. + +```typescript +const isStringType: IsStringType = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(value instanceof Object === false && value instanceof String === false && typeof value === 'string'); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------------------------------------------: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `string`. + +---- ### isSymbol -Use `isSymbol()` or `is.symbol()` to check if **any** `value` is a `'symbol'` type. The return value is a `boolean` value. +Use `isSymbol()` or `is.symbol()` to check if **any** `value` is a `symbol` type. ```typescript -const isSymbol: IsSymbol = (value: any): value is symbol => - typeOf(value) === 'symbol' && - typeof value === 'symbol'; +const isSymbol: IsSymbol = (value: any, callback: ResultCallback = resultCallback): value is symbol => + callback(typeOf(value) === 'symbol' && typeof value === 'symbol'); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's a `'symbol'` type. | +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `symbol`. -[Example usage][is-symbol] | [How to detect `'symbol'` type][detect-symbol] +[Example usage on playground][is-symbol] | [How to detect `symbol` type][detect-symbol] + +---- ### isType -Use `isType()` or `is.type()` Check if **any** `value` is a generic `Type` type constructor, `'function'`, `'object'` or primitive type. The return value is a `boolean` value. +Use `isType()` or `is.type()` to check if **any** `value` is the [`Type`](#type) from a `type` of the [`Types`](#types) type. ```typescript -const isType: IsType = (value: any, type: Types): value is Type => { +const isType: IsType = (value: any, type: Types, callback: ResultCallback = resultCallback): value is T => { if (isString(type)) { switch (type) { - case 'bigint': return isBigInt(value); - case 'boolean': return isBoolean(value); - case 'function': return isFunction(value); - case 'number': return isNumber(value); - case 'object': return isObject(value); - case 'string': return isString(value); - case 'symbol': return isSymbol(value); - case 'undefined': return isUndefined(value); + // Primitives. + case 'bigint': + case 'boolean': + case 'number': + case 'null' : + case 'string': + case 'symbol': + case 'undefined': return isPrimitive(value, type, callback); + // Function. + case 'function': return isFunction(value, callback); + // Object. + case 'object': return isObject(value); } + } else if (isNotNull(type)) { + return isInstance(value, type, callback); } - return type ? isInstance(value, type) : false; + return false; }; + ``` -| Parameter | Type | Description | -|-----------| :-----------: |-------------| -| value | `any` | Any value to check it is a generic `Type` type from one of the `type`. | -| type | `Types` | Generic constructor `Type`, `'function'`, `'object'` or one of the `Primitives` `'bigint'`, `'boolean'`, `'number'`, `'symbol'`, `'string'`, `'undefined'` to check `value` type. | +| Parameter | Type | Description | +| :-------- | :------------------: | :-------------------------------------------------- | +| value | `any` | Any `value` to check if its type is from the `type` | +| type | [`Types`](#types) | A `string` or generic `Constructor` type from the [`Types`](#types) to check the `value` | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is the [`Type`](#type) from a `type` of the [`Types`](#types). -[Example usage][is-type] +[Example usage on playground][is-type] + +---- ### isUndefined -Use `isUndefined()` or `is.undefined()` to check if **any** `value` is an `'undefined'` type and equal to `undefined`. The return value is a `boolean` value. +Use `isUndefined()` or `is.undefined()` to check if **any** `value` is an `undefined` type and equal to `undefined`. ```typescript -const isUndefined: IsUndefined = (value: any): value is undefined => - typeOf(value) === 'undefined' && - typeof value === 'undefined' && - value === undefined; +const isUndefined: IsUndefined = (value: any, callback: ResultCallback = resultCallback): value is undefined => + callback(typeOf(value) === 'undefined' && typeof value === 'undefined' && value === undefined); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| value | `any` | Any `value` to check if it's an `'undefined'` type, and equal to `undefined`. | +| Parameter | Type | Description | +| :-------- | :---: | :------------------- | +| value | `any` | Any `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is `undefined`. -[Example usage][is-undefined] | [How to detect `'undefined'` type][detect-undefined] +[Example usage on playground][is-undefined] | [How to detect `undefined` type][detect-undefined] + +---- + +### isNot + +Object `isNot` with all **check is not** functions. + +```typescript +const isNot: IsNot = { + boolean: isNotBoolean, + defined: isNotDefined, + function: isNotFunction, + null: isNotNull, + number: isNotNumber, + string: isNotString, + undefined: isNotUndefined +}; +``` ### isNotBoolean -Use `isNotBoolean()` or `is.not.boolean()` to check if an **unknown** `value` is NOT a `'boolean'` type, NOT equal to `true` or `false` and NOT an instance of a `Boolean`. The return value is a `boolean` value. +Use `isNotBoolean()` or `is.not.boolean()` to check if an **unknown** `value` is **not** a `boolean` type, **not** equal to `true` or `false` and **not** an instance of a [`Boolean`][boolean]. ```typescript -const isNotBoolean: IsNotBoolean = (value: unknown): boolean => - typeOf(value) !== 'boolean' && - typeof value !== 'boolean' && - value instanceof Boolean === false && - value !== true && - value !== false; +const isNotBoolean: IsNotBoolean = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback( + typeOf(value) !== 'boolean' && + typeof value !== 'boolean' && + value instanceof Boolean === false && + value !== true && + value !== false + ); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT a `'boolean'` type, NOT equal to `true` or `false` and NOT an instance of `Boolean`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is not a `boolean`. + +---- ### isNotDefined -Use `isNotDefined()` or `is.not.defined()` to check if an **unknown** `value` is an `'undefined'` type and is equal to `undefined`. The return value is a `boolean` value. +Use `isNotDefined()` or `is.not.defined()` to check if an **unknown** `value` is an `undefined` type and is equal to `undefined`. ```typescript -const isNotDefined: IsNotDefined = (value: unknown): boolean => - typeOf(value) === 'undefined' && - typeof value === 'undefined' && - value === undefined; +const isNotDefined: IsNotDefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) === 'undefined' && typeof value === 'undefined' && value === undefined); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's an `'undefined'` type and is equal to `undefined`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not defined, is `undefined`. + +---- ### isNotFunction -Use `isNotFunction()` or `is.not.function()` to check if an **unknown** `value` is NOT a `'function'` type and NOT an instance of `Function`. The return value is a `boolean` value. +Use `isNotFunction()` or `is.not.function()` to check if an **unknown** `value` is **not** a `function` type and **not** an instance of `Function`. ```typescript -const isNotFunction: IsNotFunction = (value: unknown): boolean => - typeOf(value) !== 'function' && - typeof value !== 'function' && - value instanceof Function === false; +const isNotFunction: IsNotFunction = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'function' && typeof value !== 'function' && value instanceof Function === false); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT a `'function'` type and NOT an instance of `Function`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not a `function`. + +---- ### isNotNull -Use `isNotNull()` or `is.not.null()` to check if an **unknown** `value` is NOT a `'null'` type and NOT equal to `null`. The return value is a `boolean` value. +Use `isNotNull()` or `is.not.null()` to check if an **unknown** `value` is **not** a `null` type and **not** equal to `null`. ```typescript -const isNotNull: IsNotNull = (value: unknown): boolean => - typeOf(value) !== 'null' && - value !== null; +const isNotNull: IsNotNull = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'null' && value !== null); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT a `'null'` type and NOT equal to `null`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not `null`. + +---- ### isNotNumber -Use `isNotNumber()` or `is.not.number()` to check if an **unknown** `value` is NOT a `'number'` type and NOT an instance of `Number`. The return value is a `boolean` value. +Use `isNotNumber()` or `is.not.number()` to check if an **unknown** `value` is **not** a `number` type and **not** an instance of `Number`. ```typescript -const isNotNumber: IsNotNumber = (value: any): boolean => - typeOf(value) !== 'number' && - typeof value !== 'number' && - isFinite(value) === false && - value instanceof Number === false; +const isNotNumber: IsNotNumber = (value: any, callback: ResultCallback = resultCallback): boolean => + callback( + typeOf(value) !== 'number' && + typeof value !== 'number' && + value instanceof Number === false + ); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown value to check if it's NOT a `'number'` type and NOT an instance of `Number`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not a `number`. + +---- ### isNotString -Use `isNotString()` or `is.not.string()` to check if an **unknown** `value` is NOT a `'string'` type and NOT an instance of `String`. The return value is a `boolean` value. +Use `isNotString()` or `is.not.string()` to check if an **unknown** `value` is **not** a `string` type and **not** an instance of `String`. ```typescript -const isNotString: IsNotString = (value: unknown): boolean => - typeOf(value) !== 'string' && - typeof value !== 'string' && - value instanceof String === false; +const isNotString: IsNotString = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'string' && typeof value !== 'string' && value instanceof String === false); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An unknown `value` to check if it's NOT a `'string'` type or NOT an `'object'` type and NOT an instance of `String`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not a `string`. + +---- ### isNotUndefined -Use `isNotUndefined()` or `is.not.undefined()` to check if an **unknown** `value` is NOT an `'undefined'` type and NOT equal to `undefined`. The return value is a `boolean` value. +Use `isNotUndefined()` or `is.not.undefined()` to check if an **unknown** `value` is **not** an `undefined` type and **not** equal to `undefined`. ```typescript -const isNotUndefined: IsNotUndefined = (value: unknown): boolean => - typeOf(value) !== 'undefined' && - typeof value !== 'undefined' && - value !== undefined; +const isNotUndefined: IsNotUndefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'undefined' && typeof value !== 'undefined' && value !== undefined); ``` -| Parameter | Type | Description | -|-----------| :-------: |-------------| -| value | `unknown` | An Unknown `value` to check if it's NOT an `'undefined'` type and NOT equal to `undefined`. | +| Parameter | Type | Description | +| :-------- | :-------: | :-------------------------- | +| value | `unknown` | An unknown `value` to check | +| callback | [`ResultCallback`](#resultcallback)=[`resultCallback`](#resultcallback) | [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is not `undefined`. + +---- + +## Guard + +### guard + +The object contains prefixed with `guard` functions in `is` property. + +```typescript +const guardIs: GuardIs = { + array: guardArray, + bigint: guardBigInt, + boolean: guardBoolean, + function: guardFunction, + instance: guardInstance, + key: guardKey, + number: guardNumber, + null: guardNull, + object: guardObject, + objectKey: guardObjectKey, + primitive: guardPrimitive, + string: guardString, + symbol: guardSymbol, + type: guardType, + undefined: guardUndefined +}; +const guard: Guard = { + is: guardIs +}; -## Guards +``` ### guardArray -Use `guardArray()` or `guard.is.array()` to guard the `value` to be a generic `Array` `Type`. The return value is a `boolean` value. +Use `guardArray()` or `guard.is.array()` to guard the `value` to be an [`Array`][array] of a generic `Type`. ```typescript -const guardArray: GuardArray = (value: Array): value is Array => isArray(value); +const guardArray: GuardArray = (value: Array, callback?: ResultCallback): value is Array => + isArray(value, callback); ``` -| Parameter | Type | Description | -|-----------| :-----------: |-------------| -| value | `Array` | Array generic `Type` type `value` to guard. | +| Parameter | Type | Description | +|-----------| :---------------------------------: |-------------| +| value | `Array` | A generic `Type` `Array` `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is an [`Array`][array] of a generic `Type`. + +[Example usage on playground][guard-array] + +---- + +### guardBigInt + +Use `guardBigInt()` or `guard.is.bigint()` to guard the `value` to be a `bigint`. + +```typescript +const guardBigInt: GuardBigInt = (value: bigint, callback?: ResultCallback): value is bigint => + isBigInt(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------------------- | +| value | `bigint` | A `bigint` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `bigint`. + +---- + +### guardBoolean -[Example usage][guard-array] +Use `guardBoolean()` or `guard.is.boolean()` to guard the `value` to be a `boolean`. + +```typescript +const guardBoolean: GuardBoolean = (value: boolean, callback?: ResultCallback): value is boolean => + isBoolean(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :-------------------------------- | +| value | `boolean` | A `boolean` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `boolean`. + +---- ### guardFunction -Use `guardFunction()` or `guard.is.function()` to guard the `func` value to be a `Func` type. The return value is a `boolean` value. +Use `guardFunction()` or `guard.is.function()` to guard the `value` to be a [`Func`](#Func) type. ```typescript -const guardFunction: GuardFunction = (func: Func): func is Func => isFunction(func); +const guardFunction: GuardFunction = (value: Func, callback?: ResultCallback): value is Func => + isFunction(value, callback); ``` -| Parameter | Type | Description | -|-----------| :----: |--------------| -| func | `Func` | Type `Func` value to guard. | +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :-------------------------------------- | +| value | [`Func`](#Func) | A [`Func`](#Func) type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][guard-function] +The return value is a `boolean` indicating whether or not the `value` is a [`Func`](#Func). + +[Example usage on playground][guard-function] + +---- + +### guardInstance + +Use `guardInstance()` or `guard.is.instance()` to guard the `value` to be an `object` of a generic `Obj` type equal to an `instance` of [`Constructor`](#Constructor) type. + +```typescript +const guardInstance: GuardInstance = (value: Obj, instance: Constructor, callback?: ResultCallback): value is Obj => + isInstance(value, instance, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :--------------------------------------------------- | +| value | `Obj` | An `Obj` type `value` to compare with the `instance` | +| instance | [`Constructor`](#Constructor) | A generic `Obj` [`Constructor`](#Constructor) type to create an `instance` to compare with the `value` | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is an `instance` of a generic `Obj`. + +---- + +### guardKey + +Use `guardKey()` or `guard.is.key()` to guard the `value` to be one of the `string`, `number`, or `symbol`. + +```typescript +const guardKey: GuardKey = (value: Key, callback?: ResultCallback): value is Key => + isKey(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------------------------ | +| value | [`Key`](#Key) | A [`Key`](#Key) type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a [`Key`](#Key). + +---- + +### guardNull + +Use `guardNull()` or `guard.is.null()` to guard the `value` to be a `null`. + +```typescript +const guardNull: GuardNull = (value: null, callback?: ResultCallback): value is null => + isNull(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :----------------------------- | +| value | `null` | A `null` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `null`. + +---- ### guardNumber -Use `guardNumber()` or `guard.is.number()` to guard the `value` to be a `number` type. The return value is a `boolean` value. +Use `guardNumber()` or `guard.is.number()` to guard the `value` to be a `number`. ```typescript -const guardNumber: GuardNumber = (value: number): value is number => isNumber(value); +const guardNumber: GuardNumber = (value: number, callback?: ResultCallback): value is number => + isNumber(value, callback); ``` -| Parameter | Type | Description | -|-----------| :------: |--------------| -| value | `number` | Type `number` value to guard. | +| Parameter | Type | Description | +|---------- | :---------------------------------: | :-------------------------------- | +| value | `number` | A `number` type `value` to guard. | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][guard-number] +The **return value** is a `boolean` indicating whether or not the `value` is a `number`. + +[Example usage on playground][guard-number] + +---- ### guardObject -Use `guardObject()` or `guard.is.object()` to guard the `object` value to be a generic `Obj` type. The return value is a `boolean` value. +Use `guardObject()` or `guard.is.object()` to guard the `value` to be an `object` of a generic `Obj` type. ```typescript -const guardObject: GuardObject = (object: Obj): object is Obj => isObject(object); +const guardObject: GuardObject = (value: Obj): value is Obj => isObject(value); ``` -| Parameter | Type | Description | -|-----------| :---: |-------------| -| object | `Obj` | Generic `Obj` type value to guard. | +| Parameter | Type | Description | +| :-------- | :--------------------: | :------------------------------------ | +| value | `Obj` extends `object` | A generic `Obj` type `value` to guard | + +The **return value** is a `boolean` indicating whether or not the `value` is an `object` of a generic `Obj`. -[Example usage][guard-object] +[Example usage on playground][guard-object] + +---- ### guardObjectKey -Use `guardObjectKey()` or `guard.is.objectKey()` to guard the `object` to be a generic `Obj` type and to contains the `key` property. The return value is a `boolean` value. +Use `guardObjectKey()` or `guard.is.objectKey()` to guard the `value` to be an `object` of a generic `Obj` type that contains the `key` property of the [`Key`](#Key) type. ```typescript -const guardObjectKey: GuardObjectKey = (object: Obj, key: Key): object is Obj => - guardObject(object) ? isString(key) ? key in object : true : false; +const guardObjectKey: GuardObjectKey = (value: Obj, key: Key): value is Obj => + guardObject(value) ? isKey(key) ? key in value : true : false; ``` -| Parameter | Type | Description | -|-------------| :---: |---------------| -| object | `Obj` | Generic `Obj` type `value` that contains the `key` property to guard. | -| key | `Key` | Name of the property that the `object` contains. | +| Parameter | Type | Description | +| :---------- | :-----------: | :------------------------------------------------------------ | +| value | `Obj` | A generic `Obj` type `value` that contains the `key` to guard | +| key | `Key` | A `Key` type name of the property that the `value` contains | + +The **return value** is a `boolean` indicating whether or not the `value` is an `object` of a generic `Obj` containing the `Key`. + +[Example usage on playground][guard-object-key] -[Example usage][guard-object-key] +---- ### guardPrimitive -Use `guardPrimitive()` or `guard.is.primitive()` to guard the `value` to be a generic `Type` from one of the `Primitives`. The return value is a `boolean` value. +Use `guardPrimitive()` or `guard.is.primitive()` to guard the `value` to be the [`Primitive`](#primitive) from a `type` of the [`Primitives`](#primitives). ```typescript -const guardPrimitive: GuardPrimitive = (value: Type, type: Primitives): value is Type => isPrimitive(value, type); +const guardPrimitive: GuardPrimitive = + (value: Type, type: Primitives, callback?: ResultCallback): value is Type => + isPrimitive(value, type, callback); ``` -| Parameter | Type | Description | -|-------------| :----------: |--------------| -| value | `Type` | A generic `Type` type `value` to guard. | -| type | `Primitives` | One of the `Primitives` `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` to check `value`. | +| Parameter | Type | Description | +| :---------- | :--------------------------------------: | :---------------------------------- | +| value | `Type` extends [`Primitive`](#primitive) | A `Primitive` type `value` to guard | +| type | [`Primitives`](#primitives) | A `string` type from the [`Primitives`](#primitives) to check the `value` | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is the [`Primitive`](#primitive) from the `type`. -[Example usage][guard-primitive] +[Example usage on playground][guard-primitive] + +---- ### guardString -Use `guardString()` or `guard.is.string()` to guard the `value` to be a `string` type. The return value is a `boolean` value. +Use `guardString()` or `guard.is.string()` to guard the `value` to be a `string`. ```typescript -const guardString: GuardString = (value: string): value is string => isString(value); +const guardString: GuardString = (value: string, callback?: ResultCallback): value is string => + isString(value, callback); ``` -| Parameter | Type | Description | -|-------------| :---: |---------------| -| value | `string` | String type value to guard. | +| Parameter | Type | Description | +|-------------| :---------------------------------: | :------------------------------- | +| value | `string` | A `string` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | -[Example usage][guard-string] +The return value is a `boolean` indicating whether or not the `value` is a `string`. + +[Example usage on playground][guard-string] + +---- + +### guardSymbol + +Use `guardSymbol()` or `guard.is.symbol()` to guard the `value` to be a `symbol`. + +```typescript +const guardSymbol: GuardSymbol = (value: symbol, callback?: ResultCallback): value is symbol => + isSymbol(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :----------------------------- | +| value | `symbol` | A `symbol` type `value` to guard | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is a `symbol`. + +---- ### guardType -Use `guardType()` or `guard.is.type()` to guard the `value` to be a generic `Type` type from one of the `Types` type. The return value is a `boolean` value. +Use `guardType()` or `guard.is.type()` to guard the `value` to be the [`Type`](#type) from a `type` of the [`Types`](#types). + +```typescript +const guardType: GuardType = (value: T, type: Types, callback?: ResultCallback): value is T => + isType(value, type, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :------------------------------------------------- | +| value | `T` extends [`Type`](#type) | A [`Type`](#type) `value` to guard with the `type` | +| type | [`Types`](#types) | A `string` or generic [`Constructor`](#constructor) type from the [`Types`](#types) to check the `value` | +| callback? | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The return value is a `boolean` indicating whether or not the `value` is a `type` from the [`Types`](#types). + +[Example usage on playground][guard-type] + +---- + +### guardUndefined + +Use `guardUndefined()` or `guard.is.undefined()` to guard the `value` to be `undefined`. + +```typescript +const guardUndefined: GuardUndefined = (value: undefined, callback?: ResultCallback): value is undefined => + isUndefined(value, callback); +``` + +| Parameter | Type | Description | +| :-------- | :---------------------------------: | :---------------------------------- | +| value | `undefined` | A `undefined` type `value` to guard | +| callback | [`ResultCallback`](#resultcallback) | Optional [`ResultCallback`](#resultcallback) function to handle result before returns eg. to throw an `Error` | + +The **return value** is a `boolean` indicating whether or not the `value` is `undefined`. + +---- + +## Experimental + +### BigIntObject + +```typescript +class BigIntObject { + static set set(value: any) { + PrimitiveObject.bigint = BigInt(value); + } + static get get(): BigInt { + return PrimitiveObject.bigint; + } +} +``` + +---- + +### BooleanObject + +```typescript +class BooleanObject { + /** + * `false` when empty, 0, null, '', false + * `true` when 'true', 'false', 'Su Lin whatever', [], {}, true + */ + static set set(value: any) { + PrimitiveObject.boolean = new Boolean(value); + } + static get get(): Boolean { + return PrimitiveObject.boolean; + } +} +``` + +---- + +### NumberObject + +```typescript +class NumberObject { + static set set(value: any) { + PrimitiveObject.number = new Number(value); + } + static get get(): Number { + return PrimitiveObject.number; + } +} +``` + +---- + +### PrimitiveObject ```typescript -const guardType: GuardType = (value: Type, type: Types): value is Type => isType(value, type); +class PrimitiveObject { + static bigint: BigInt; + static boolean: Boolean; + static number: Number; + static string: String; + static symbol: Symbol; +} ``` -| Parameter | Type | Description | -|-------------| :---: |---------------| -| value | `Type` | A generic `Type ` `value` to guard. | -| type | `Types` | Constructor generic `Type`, `'function'`, `'object'` or one of the `Primitives` `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` to check `value`. | +---- -[Example usage][guard-type] +### StringObject -## Common Types +```typescript +class StringObject { + static set set(value: any) { + PrimitiveObject.string = new String(value); + } + static get get(): String { + return PrimitiveObject.string; + } +} +``` + +---- + +### SymbolObject + +```typescript +class SymbolObject { + static set set(value: string | number | undefined) { + PrimitiveObject.symbol = Symbol(value); + } + static get get(): Symbol { + return PrimitiveObject.symbol; + } +} +``` + +---- + +### isParam + +Method decorator to check the type and return `undefined` if it's not the same as expected. + +```typescript +function isParam(...param: Array): MethodDecorator { + return (target: Func | object, key: string | symbol, descriptor: any): any => { + const originalMethod = descriptor.value; + + descriptor.value = function(): void { + if (is.array(param) && is.defined(arguments)) { + param.forEach((name: string, index: number) => { + if (is.number(index) && index < arguments.length) { + if (is.defined(arguments[index])) { + switch (name) { + case 'number': + if (is.number(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + case 'object': + if (is.object(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + case 'string': + if (is.string(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + } + } + } + }); + } + const result = originalMethod.apply(this, arguments); + return result; + }; + + return descriptor; + }; +} +``` + +Example usage. + +```typescript +// Example usage +const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz'; +const NUMBER: any = 10304050; +// TestClass +class TestClass { + @isParam('object', 'string', 'number') + public testMethod(object?: any, firstName?: any, age?: any): { object: any, firstName: any, age: any } { + return {object, firstName, age}; + } +} +const resultTRUE = new TestClass().testMethod({firstName: 'NoName'}, STRING, NUMBER); +const resultFALSE = new TestClass().testMethod(NUMBER, {firstName: 'NoName'}, STRING); + +resultTRUE === { + object: {firstName: 'NoName'}, + string: '!@#$%^&*()abcdefghijklmnoprstuwyz', + number: 10304050 +}; + +resultTRUE === { + object: undefined, + string: undefined, + number: undefined +}; + +``` + +---- + +## Common types ### Constructor @@ -777,24 +1685,54 @@ type CycleHook = 'ngAfterContentInit' | 'ngAfterContentChecked' | 'ngAfterViewIn ### Func +Function type. + ```typescript type Func = (...param: any) => any; ``` +### Key + +Name of the `object` property. + +```typescript +type Key = number | string | symbol; +``` + ### Primitive +All [`Primitive`][primitive] types. + ```typescript -type Primitive = boolean | bigint | null | number | string | symbol | undefined; +type Primitive = bigint | boolean | null | number | string | symbol | undefined; ``` ### Primitives +All [`Primitive`](#primitive) types as `string`. + +```typescript +type Primitives = 'bigint' | 'boolean' | 'null' | 'number' | 'symbol' | 'string' | 'undefined'; +``` + +### ResultCallback + ```typescript -type Primitives = 'bigint' | 'boolean' | 'number' | 'symbol' | 'string' | 'undefined'; +type ResultCallback = (result: boolean) => boolean; +``` + +### Type + +Main types. + +```typescript +type Type = Func | object | Primitive; ``` ### Types +Main types as `string`. + ```typescript type Types = Constructor | 'function' | 'object' | Primitives; ``` @@ -831,6 +1769,14 @@ How do I know when to release 1.0.0? MIT © angular-package ([license](https://github.com/angular-package/type/blob/main/LICENSE)) +[array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array +[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean +[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions +[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number +[object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object +[primitive]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive +[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String + [new]: https://img.shields.io/badge/-new-red [type-npm-svg]: https://badge.fury.io/js/%40angular-package%2Ftype.svg @@ -859,9 +1805,9 @@ MIT © angular-package ([license](https://github.com/angular-package/type/blob/m [is-array]: https://www.typescriptlang.org/play?jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgABVYAWxYoFnxkbBg+EBBiZBoYAB94lnY2WAywUmJidJh8sL5XYuY2TgyIeDKk4tIwABNkdDZkFv9A2IBJCABBJiZCeDitMkpqWjpuGCIpmBYIGGHR+AAeGnguf1BIWBX1saoBk-GcTYAVWS5J8iodhgXtWJW1kbGbu+4-GDgsgwD2Qcyw4JgAHJCF94JCYAAyBH-T4bUTHWEgsEQ5TkRHIgGLPFsaA0IwgTAXbA4ph4pEo3oU15LcE4SEgQTCKCQ-bgaAwBTXABKfQAcgBxOKQxxMZBhZaICCkMIAfh5fgO-NFAFUALIAIQAokK4gAmADsvMOMH1qFQ9kNg1FcXQJC8Vv52tFABFDQAxMWG71xZptDpgLoe2CDIVCwYATUFIolVAumyqHB4OAA2tDIRZIXx81DgJCALpRtaxhM6g3G1OwzalcpMLMwbPWCymiwAZgrGr50er8eCIt1fWufQAaoaGxtNqEWBEojE29moRn2MWLRZcbF+5rYAApBSoUUAfSng3s2sNcQA3uhElRrKaewBfD1JOTEEDsXAAAZJmK4oARYGIbLgwESnQvgwAA9PBMCusQXgDpA36iL+-4AbWRpCmByxDJieHGrBPgIUhKFeDA6EQJh2GAba9qOqKhEQWMuDMQ6TrkZRyFusgdEMX+gFer6AaikG7HEZB4n+oG3p8YhAmoUJmoiThMZxomwogTJFy4NpCbQeKylIXuwnJFhokAcZ8akQR4GyZx9mOeZcC0upfKaYB9mjn046TjOBmYv5Y4TtOhoeZZGnWYxAEnmel7XreoWQUlF5Xje0VwSp1FCUAA [is-bigint]: https://www.typescriptlang.org/play?target=7&jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgASQgAIRZ2YLBYHC0ySmpaOm4YIgSYFggYPki2KH9QSFgsiKiYqlCy6Ni8dPIqGnoqeuRM7Nz2fO4-GDhZDHidbCwcAHJO-LGYADIZ3v6UEExWkfHJmLHC8GgYBQAVACVggDkAcWwYMccmZABbTMQIUjuAfi2-It2TgFUAWTCAFFDpcAEwAdm2xRgYVQqHsgIAgidLugSF4obswsEzqd9pcAJwABiJ4OsBIJoIArAAWcE0okU6xgTEgYhyYggdi4AAGB2O5x5FlKkRquH5pzOdF8MAA9LKYGjiF5Pjs2RyubzfgDgUL2tUYrhtUDDtKfHKFUqVV91aJOdyebD4UiTnqReUoLgnQjkWaLYr0chVZBbfbedjcSd9m7wqLDRG8X75XAmOQ-EA [is-boolean]: https://www.typescriptlang.org/play?target=7&jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgASQgAIRAQYmQabDwiMkpqWjpuGATyGBYIGD5I6Jp-UEhYbIiomLAqUPKCsDitRKoaeioM5CycvIrYrC4-GDhZDEadbCwcAHJuusmYADJ5gbxlwdxVwaGUEEx28amQQWEoOcWNwb22aBojHZhayv24Jkyzzc3LkpvkO-4hESeynIGzoGwAPmCNut3hdtB0rlBvncHr0JjB0CQvAsljD0nCsl8wLdMH9jk8McQsW8YYEfrt8RMpjNKqccTDRh1Gc9MhC8YlyZjkKD3sKYL4-MVoDAFAAVABKwQAcgBxOKTRxMZAAWyyiAgpC1AH5JkVwFLFQBVACyYQAonK4gAmADsppK91QqHstoAgoq4hSvG6pWFPd6-QB9eUW21xIHIYOwUNe32KiNK2V+tyxnBgZBOe75Sq4QNC4MVUTEEDsXAAA1lCpVtYsZSLNFwDaVyrovhgAHo++jBRKzRWqzXa5abfbm50UWBcFO7XKez5+4PSyPIGPq3Xk+HFbPWz0F-vU6v19zkFuIDuJ2fI9HbUfwm3T2HU1G5TGLwOrze7z3D9IwzGUs2fFtXxPXAHzTUDwN-Qd4z8IA -[is-function]: https://www.typescriptlang.org/play?target=7&jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgAMVIwYGw8UWjEQiZCAFsqGnpualp-QOQYAEkIUPCoFnBIrTJKdNSsHiJymBYIELDgf2BiT0a3dohGgG8YAA9IgFYfGABfP1BIWAaCkWKwKjz5opKcMvJk2gYYWvJ6xvnuPxg4WQxN5DpsLBwAcnRmtbB7mAAyd9PzlBBMfeydweT0Kizen2+APqMxoRj+TVB6zucCYBwhZyhbGgsOQ8P4QhEtxwynIrXA0BgACFUKh7ABRACCADlIiTkGSZlScgBxHJMgAqkQAnAAGEUAdmsQqFACZhgAWcXykXS6xgDkUpkAVQAspS6QAlSLWGXijWwBT8g187mRe46+AwMCkBJ8VyHe7mkJapluKjHDYDKjO12uXYpNL9JjIKCkJhgQYwADUMFGEym5NgfMtzLcdMiYGQThgXQ6uF8GcgIGIcmIIHYuAABqWeo2LHNnotcC2IHRfDAAPQDlHkSsQau1+u4e6Unl8-n3dv5TvgXCz3kCvtjIcwdAkLxjieiOsNmc0+nMxeHVZd6m0xlMreD4d74gH6bjmvHqf3bV6w1Xh2iJgLgf76gaT47q+77kkeJ7Tpa1pMtygHLsBuCITakEvvuyCHl+8GNsEPpuG214riBxG+thI54R+cE-tm-K5nSqE3quTEsTR0HIEAA -[is-instance]: https://www.typescriptlang.org/play?target=7&jsx=0#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAq4EAfFALxQIQDuUAFAHTMCGqA5kgFxQsIgBtALoBKCmSKQA3AChQkKAEkki5MD6ZolPAHkARgCsS9AG4sANugg8+IADRQAlmo3XYiFBmy5dhkmPIyM0toRyQofQNZeWhlAGU0Z3YKBmCrG34AoIsrJ3DPJOjiJSRIiGwU3yNTHLdbBwBrCBAAfh4ChHYsqDTQ8MjZTA9gKBidADMUmpCMkBF2xM7xCMNy4EYwVDgcGMYcBNQkxkwLc2mrEUYkc0dNegAOBwBaAEZLnAAZOBoIVBgWJAQegiQbDPIHJI8eKLZKUc51TI8Xp5KAdWFkegxOCTZHkPFQADkcFW2AJUAAZOSerUnC4EJpsSsDGsKVTkc4UK5GRDOmIAD580bERm4-EEtEE0FqPJlbBQ0okkbaSLGXqzRrNNqomHzakhFGRcQyKBCyATeEBMXE5mk1nG00QEU0vGUImKsmU+3sukMyaypX4tBWe0mlrgmH0JpzCiBjAQEMmqBhqO0vW5F2jOOs1I0gFMy4ACwBOhoCAACltIKhQJHmpbKEH44moDxG-aeOMLICpSg8qpOfS3Mp++pB5UVfD1bSB5oePA1F4cPgVbr2f1DEaTWF-VVVbUxJ6Td6Z47JhzR5oYw2s4fw4dOvRz64NlsdsRjsNF7hGAgWABbCB60zKxZBkTBzABcI4jgACYAgpBwgAbygAAPFIXgAJgAdikKAAF8wPg8ICBoOA4MgqBkJAFICS+VAID-JwwCQdA-0lfCZBkAB6LioAANTYRwWD0cwICQMCwTiAgACVFAAOQAcQAfT4gBBD4AFUAFEaLohimJYv8WklCTpTkjSAFkACEtOklT1O0lJsJ7EYrJ0HQPi01S5PszSdMoTtzG7Uzew0uSABEtIAMXkrTwt87SlIIAB1HQUnQBAABMIHGZwIEy2RRJGMLIpiuS4oSrSXKgOIdAsrSYA+VS4jiFJqDoaDYKI4FqpSnRGua1rKHaqASLI7qQU4oZkDgUTGHMOB2EfFQfQgPBOogciEOMPqBpahwNq2pARBBKAeKgQLARC2aIHmxblpHVx1pgzaiOMWr6r2uIDpeo6Ttw8622mpAbrupawkewdnq6yCdtSr6HDGv7TsBuMZCAA -[is-null]: https://www.typescriptlang.org/play?target=7&jsx=0#code/C4TwDgpgBAkgzgOQK4BsVQLxQBQDcCGKSEAXFPgHYgCUmAfFAUdAJZxQWooDcAUAMYB7CnGBRQkAPIAzTDibEylGmVEAnFhQDm9KJIBGAKwj9gAOjBrBwa+AhmbAZWAbtZ-oRR5CxambgoLPwQ2AAcADRQALQAjH42ADKCAO4QagDC+HAh1HxCImJsyGhk8MXoWN7MSlTUZAqs7Jxo9LxQ4nYyVb6YGFgA5M0o-VAAZKNtHZCCsg29A4JGJsAj45NzfVhDecKiUI4AKgBKMAgA4nL9SWoQALZQLGBwSLcA-P07BVAIAKoJCXJtgJdmJfgBZABCAFEjnIAEwAdk+ewhkkkCShAEEEHJpIRssixKj0ViEAB9Y4-KFyFzEQlQYkY7Fk06HbHpalbCDJBmCQQoCCUbB4lDZXLAkT8+woQRabAAA0OJ3O8siRS42CVpzO1FyUAA9PqoCLshK4FKzDK5fLfv9VQ9EBrbQlddwDUbaRAzRarQrwdCjvb1WhsP6Ya73cb8V78uaBZbZQrGaSg46Q8nsRHDVHRTHdj7E-KM+TKVDU+VsMWKUcqVmjSa85L476i2imeTWQd2WW1WmvFXO926znTUA +[is-function]: https://www.typescriptlang.org/play?target=7&jsx=0#code/MYewdgzgLgBFCeAHApgeQGYwLwwBQDcBDAGwFdkAuGQseASiugCcBLMAc2wD4ZUAjAFbJgUAHSImIKFKTJR0gMpRWHUcBLECJcnVERiLYMlwAOADQwAtAEZd0gDIgA7siYBhQhGN0A3ACgEFBgAMVIwYGw8UWjEQiZCAFsqGnpualp-QOQYAEkIUPCoFnBIrTJKdNSsHiJymBYIELDgf2BiT0a3dohGgG8YAA9IgFYfGABfP1BIWAaCkWKwKjz5opKcMvJk2gYYWvJ6xvm0rIxN5DpsLBwAcnRmtbAbmAAyF7hZEEx97Ovb+8Ki2ebz22mybGgNCMXyagPW1zgTAOIJ+9RmUOQMP4QhEVxwynIrXA0BgACFUKh7ABRACCADlIgTkESZmScgBxHJ0gAqkQAnAAGAUAdmsfL5ACZhgAWYXSgXi6xgFkkukAVQAsqSqQAlSLWCXClWwBTcnVc9mRG4a+AwMCkBJ8VyHG7GkJquluKjHDYDKj2x2uXYpNL9JjIKCkJhgQYwADUMFGEymxNgXNN9LcVMiYGQThgXQ6uF8KcgIGIcmIIHYuAABoWerWLHMHotcA2IHRfDAAPQ9xHkUsQcuV6u4G6kjlc7k3Zv5VvgXCTzk8rtjPswdAkLxDkeiKs1icU6n02eHVZt8mU2l0te9-tb4g76bDiv7sc3dVa3VnltwsC4F+2o6neG6Ps+xJ7ge46muadLsr+87-rgsEWqBD7bsgu5vtBtbBB6bhNueC4AfhnroQOWEvlBH7ptymZUohF6LnRDEUeBVGQThY61sWaTDERf4LIufHVImFFMkAA +[is-instance]: https://www.typescriptlang.org/play?target=7&jsx=0&module=6#code/PTAEBUE8AcFMDoBQAXGtQGED2A7AzsgE4CuAxsloQDxRwB8oAvKDrAO6gAU8PAhoQHM8ALlC8ckANoBdAJRMGtWAG4UaUAEk8AMWI5yAS1xMuAN14AbYrFHjI8xg3NX0BvFx7xo-XgFtbEg4MdqqocJp4GvjI4qTozFQA8gBGAFZ0nM7WAZAANKAG0bE2mLgEJOSUSWl0QaBZru4pqaHqWgDKRIUCJpmW2WKBCvX9jaDl3a3hWs2w5CbV6X0uOfkA1rCQAPyiEzgCdQ0FTWmqIKC6+shGOEikZcigYbCJAGa9DTmyu137w7PkLyELAUZ7wCidQjdeCkSwWZbWWTwPAWAxxTgADnyAFoAIxIigAGSwbFghAwvDwsE4slU92ix0uhlwoi0TOuxmYCJKdm+Ixcxw8PG8hD8X2GdmGzze3IcjGYAHJXnpmTgFaAAGQap5oLDvI7yxXKq43dVa-nWApFfSwPUXFUcnBMeVPEjoc1HQoEYp2gGPQ2u6x0h7HSHdVl4MN-LmfQb2USe9x7HqOLjPO0Gl0KrBpObIM3az3WuK+3PzD2jK3em12qMHUAAHwbOrgGcrAYVyYVwYZbj9Eb9C2aJhzqTzGVjdnWmx241+BwTlbcoGHjkQoDTaBlDTlitHeYLLdt+vbWf35EPRerJfeg4DRER643oC2ofnnA29ifz5foE-VotWBv1AUQHyAjdRFeSwqR7Ahjiia8Si0BCYhtIcam5VYq1QuJRGwaIKgoahmlqRcBWXVc6CfPsy2QRYJ1GeQtmAtx2RuTgvRw2AmOAjcry4u1OOKZ1mDAzVtTcOsOOLBBoGBUE0BhB5CMoeAcD8bjgMg6DwJA0AoIsGDEEQUgLEpdx2iwXxYAwMy8HcABvUAAA8TFxAAmAB2ZRQAAX1AUzzIgNgsFsoKnMgEwFWJQhYF8ApoDwYhfG7PzjPOAA1fgDF4ZILFgPATJDdpwAAJQ0AA5ABxAB9DKAEFCQAVQAUSimK4oSpLfC2VL6TgiqmoAWQAIRa0q6sa1qTC8nz+seEbEkSQkWvqirJuatrmAMmCApDJqKoAERa7RKpaw6NtamrwAAdUSEw9AAE1gV5ClgR7VHyx4DuO06KvOy6Wtgx52kSIaWowQl6vadoTFYDhLOssL7JpOaQ1uxJIeh2HmHh4LQrsvBUeM-qsHy+ALCwAQOMiGSqERmzCYyDGsZh-IGeRvBZFpUBzh2oDSfJynqbcFDinpqzGfMjJQfB1n2nZyXOe5nzzjAor8DJhBhZpsWbQlpGmc4FmobZ-HlZ5tW3UQIA +[is-null]: https://www.typescriptlang.org/play?target=7&jsx=0&ssl=11&ssc=21&pln=11&pc=33#code/C4TwDgpgBAkgzgOQK4BsVQLxQBQDcCGKSEAXFPgHYgCUmAfFAUdAJZxQWooDcAUAMYB7CnGBRQkAPIAzTDibEylGmVEAnFhQDm9KJIBGAKwj9gAOjBrBwa+AhmbAZWAbtZ-oRR5CxambgoLPwQ2AAcADRQALQAjH42ADKCAO4QagDC+HAh1HxCImJsyGhk8MXoWN7MSlTUZAqs7JxouhIQMlW+mBhYAOTNKL1QAGTD4naCsg3dfYJGJsBDo4w+0D1YA3nColCOACoASjAIAOJyvUlqEAC2UCxgcEjXAPy9WwVQCACqCQlymwJtmJvgBZABCAFEDnIAEwAdneOzBkkkCQhAEEEHJpIRsoixMjURiEAB9Q5fCFyFzEfFQQlozEk477THpSkbCDJOmCQQoCCUbA4lDZXKAkS8+woQRabAAA32R1OssiRS42AVxxO1FyUAA9LqoELsmK4BKzFKZbLvr9lXdEGrrQltdw9QbqRATWaLXLQZCDrbVWhsL6oc7XYbcR78qa+ebpXL6cSA-ag4nMWH9RHhVHtl747K06TyRDk+VsIWyQcKRmDUac+LY96CyiGaTmXtWSWVSmvBX252a1njUA [is-number]: https://www.typescriptlang.org/play?target=7&jsx=0#code/C4TwDgpgBAkgzgOQK4FsBGEBOUC8UAUAbgIYA2SEAXFMQHYgCUuAfFCedAJZxS2oaYA3ACgAxgHtacYFFCQA8gDNcBdhWp1G1aZk60A5iyjy0AKwijgAOjCZxwe+AhWHAZWC6DV0WVJEyFAxWcKScohD4ABwANFAAtACMQQ4AMuIA7lgAwsRwEQwiElIy3MjoWNTwZQIq-hwa9AzUalw8fOXYOMzCULJOSnWBuDh4AOTtAqNQAGTTPQTzvdwAYnqcwBEtTCN4HhQzc729+ItHLVB60nTh4srVWMN4imR5B6e955fA1xC3xmYWGQ7KDPUivWbvPqQP7nYHjfhYUanBinAA+qNOJyORzkv2UsJGUFG4gBlimEOxHwCXGKPz+906hL20AplM+tNoN2UJnMlkeskwFGR8wKYkk0igACEYABxGAIAAqKgAnAAGVUAdgSyuVACYAKwAFg1htVOoStEK4pkkvk8hSAFEAIIIFSgvJW4pQBAAVQAspKHQAlFS6jWeiW+gPBgD68tcCpdWQdKloEHS3oRmHw+tFRQlCaD8plKlGaUwEBQFzAcFQAH5RhHxKRnKRxPp8AADaVyxWd2KlLP4HvyhUMApQAD0k5BLwgYqkzdb7a7tvtzoQ-YuiCHa8dLvHginM-d8-zS6sbY7najgaDW8HHXwt+Dh+PAqF55bl5XN-9d7jBAEyTB0Hx3J8XyDQDgIQZM32nD8z3FC8ry7QtizAhl8HQhAZXgk852EYQgA [is-object]: https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&target=7&jsx=0#code/JYOwLgpgTgZghgYwgAgPICMBWEFgJLjTxLIDeyAHgFzIgCuAtutANzIC+AUKJLIiqjAALaBmy4yyAOY16TVh05gAngAcUAMTogEyALzIAFADpTquFDgMacEMoCU+gHzJbylkrUo8AZzE4wfWQAHjEnQwA3OAAbOggbOwAaZABrCGUAfhofMChQKUc9FyjYlGAfNCwPFXVkXwA5AHswerpo6KDImLiabRSQRoB3EEKXdEbG6IhbDwRGkBzkcqaWtuiaBubW9s6SnuQ+geH7GnHJ6ZBnTmRkGohUGC7SxwBCPQMAcnp2j+QAMj+12QexQbwM32is3mizuD123XirjsJ2QOTyICkzkq4jAxlUUGazS8xjAjQAyrl8sYEDFok84vZjD5osAkIYABzJAC0AEZGaSADJDaAAYTgPgghnsUIWgXKFPRmIM9MRbhRIKWFTR+Sxd0aMGBCP072QH21GI+MsW5X8uA2fiwASCoSw4RBCWUyTSmWylIx6qN5WxVxusMeIMKJo+jUduF+AKBeoNGvenxjOPjgJuGtAOVsSH12KdqduUDiQJuGU1Cvyhm9kYMuXLNxbyCr3qWlxTJqbKABRg14uxjKE4tQwwACgT1FAVHX0g3S82WzRe0CaPBohKPEDOAhouKKiLoU3cI0oCKDz4KuQKEEAKxsLhzWXIEWoepkgAqACUAKoil+qA-gA+iKAoAIJkmSQQgBAgxvieZZnheV4+FKVqBAAUmSH4gagABCWEAKKATQtr4IQfAkAYt40AAzIoL6LBof71CKNBaDonTULQjDMFAKJuFiFCYcgBGoKgArERB9RBL2O7MecxjRI0UiGAABsesrIaSqGHhpyQ2rGYDBNpaJ0Chl6HuE76fr+AFAaB4FQWS9jSsgAD0nlLhAe7QspqnqRpOF4YRJGAYZmoUaEwiiCZ4ShfU+FEaRX7uWw3m+f5CyBWpmmsexUXGTiwRcQg4SFSKGVeT5m4SjlPh5cFdnfv+gHAWBkHQQA2hQAC6xUOqV5mnnp1nXrZH5tY5nUudByQfBQHw1Vla5KVMKn5SFuHJeFaU9cog1GcNASxSIUAUYlu0pRFX6LcoK0eVl9V+RtEBbcFVV9cd0UmWV2gVYYVWLctq11TEDXvZ9mkSVJMn1ENMVuOEcPSbJ4PIK9nBAA [is-primitive]: https://www.typescriptlang.org/play?target=7&jsx=0#code/C4TwDgpgBACgTgSwLYOAgbhAzlAvFAcgCMEBzBAO2AKgB9CiB7RgGwgEMKb6CKBXJEQhxuhLCEGtRBLMEQVS0vhQAmEAGaUIKggG4AUPtCQoASSwAhZm054oACnTsWfCAC4onEAEo8APignF2gEHCZWDgoDY2hzADkBITg7R2dXDy9fXACg1yhQqH5BYWjwWKx4ZFQMaHwAHgAVMr9U4IyKEAAaKBiPSpQ0TCwsnLSQnCbIUpNzAGU5SlIU3PdPDpHAsfycWXlSafLZiSYWZbH2n39N4O2ocUkWA7MsAFVVDS0VM7a1y+zrvIFZRqTQUbQGfQAY0YFFk2wsZFMVG+6V+3g8K1uJHIyP+MUY6gBtVw+GIZEo1AM0NhwHh1kiHnMVgitnwrVRmQxWwK4RsFCu7JCNM4kIgBKgzL5UAAZNKiflhRRReKAPJEABWEEhwF8tHo9kFeBJPTgeT18pJ+HUziwEF8sp6ZXFmMtDHpnAI3ipMLhoQSxTgjKw-qSKNWnPlBSKobxTsJLuNvESwhoAH5tgAxSioCCC3xcm6UWQisWEkPCGVyzFF4Al1UarXAb007bzPZBtuLMMXdGRnYLBQC-HxraugiMBvamgO6uK5WEtWa7WVyNz0tQTsKXX0YcWxO7RZ6KE+2mhI4PDvHVjdtEFwE7K+nWOQZ2j-ePo-U32vd6g7RBt4QU+G8I2rHBgQ+MEvn+BN8Agv8vgdXdYMIeDPiPY8W1CfpqkwIMcMGWooEaZpBQubpelgRABhqYY73GKBJlqAIAG99CgfJCXsM8B1IewYm8Xw2I4jisAAd1QSEAAsHAEqBhJEjjIXYW0GHJKgCA8OAIGAPg4H5UIEVIJFgDzAxFKUlToGId0uC0nS9IMyxbLM9iLOU1SkwDTSoG03T9O2cs4FciyoA86yDwUHy-Mc1teJC9yrLED97P8pzzxOBLFPC1Df3Q1LYtCQDIO0LKoAAXzcyqOJigLrRYW0DHKiEv1pCwVRVAAZABRABBOI7HqxrMLhCxTAAcVMOIGjsABOAAGeaAHYAEZZtmgAmABWAAWJadvm9aVqiEbaTiF4AFkLG6gAlOwVo2pbmzhWYGhuqbxrsAgLpAQpk2SUJPxPKAXjiAARbqMym7qwbsNCoOe2lZgATSurq7Ay1h7AIeHtE9Z6IgAOhYRg+IAAzGybprJ7psOo3CIDqbEKRaSmpoaboyRxahBN0KAAHp+ZNVwRqJknyfarq+riGntgImo6ki0hWY6nr+s53lIk9L0BaFuQRa-MXSfsMnzqu27ZbpqpCLqaNhBaM3rpuzm7ZEXndeFiBRbYYnjbJ173ricbLYqembaVloA4+zmle1vnBc972IF98mUbRzqQ-lzBFcfSPUfazqY4-d2E-1r3DZ98WTdBiGobiGHM7DhXcZUFoa8h6Gwc5lu449svDAr5Oq9Ny6ncb62FeZqgHdH26NfUnmdYToby59I3ycdi3adDifs4j+xN+dsReN75ebVX2F15NqOg-Hmjs81zhI7e6O3RZLgS6Flek5T6+X9v7eWdGau2foHcaLt-qny-uffQQA diff --git a/packages/type/package-lock.json b/packages/type/package-lock.json index 3cd4f911..23eb9418 100644 --- a/packages/type/package-lock.json +++ b/packages/type/package-lock.json @@ -1,12 +1,12 @@ { "name": "@angular-package/type", - "version": "3.1.2", + "version": "3.2.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@angular-package/type", - "version": "3.1.2", + "version": "3.2.1", "license": "MIT", "dependencies": { "tslib": "^2.1.0" diff --git a/packages/type/package.json b/packages/type/package.json index 81280e25..97c2b3cc 100644 --- a/packages/type/package.json +++ b/packages/type/package.json @@ -1,6 +1,6 @@ { "name": "@angular-package/type", - "version": "3.1.2", + "version": "3.2.1", "description": "Common types, type guards and type checkers.", "author": "Angular Package (https://wvvw.dev)", "homepage": "https://github.com/angular-package/type#readme", diff --git a/packages/type/src/guard/index.ts b/packages/type/src/guard/index.ts index c58eb95b..e3bd41a8 100644 --- a/packages/type/src/guard/index.ts +++ b/packages/type/src/guard/index.ts @@ -1,10 +1,18 @@ +// `guard` object. export { guard } from './lib/guard.object'; - -export { guardArray } from './lib/guard-array.func.type'; +// `guard` prefix functions, +export { guardArray } from './lib/guard-array.func'; +export { guardBigInt } from './lib/guard-big-int.func'; +export { guardBoolean } from './lib/guard-boolean.func'; export { guardFunction } from './lib/guard-function.func'; +export { guardInstance } from './lib/guard-instance.func'; +export { guardKey } from './lib/guard-key.func'; export { guardNumber } from './lib/guard-number.func'; -export { guardObjectKey } from './lib/guard-object-key.func'; +export { guardNull } from './lib/guard-null.func'; export { guardObject } from './lib/guard-object.func'; +export { guardObjectKey } from './lib/guard-object-key.func'; export { guardPrimitive } from './lib/guard-primitive.func'; export { guardString } from './lib/guard-string.func'; +export { guardSymbol } from './lib/guard-symbol.func'; export { guardType } from './lib/guard-type.func'; +export { guardUndefined } from './lib/guard-undefined.func'; diff --git a/packages/type/src/guard/interface/guard-is.interface.ts b/packages/type/src/guard/interface/guard-is.interface.ts index f7a70b54..e897c4e2 100644 --- a/packages/type/src/guard/interface/guard-is.interface.ts +++ b/packages/type/src/guard/interface/guard-is.interface.ts @@ -1,22 +1,33 @@ - import { GuardArray } from '../type/guard-array.type'; +import { GuardBigInt } from '../type/guard-big-int.type'; +import { GuardBoolean } from '../type/guard-boolean.type'; import { GuardFunction } from '../type/guard-function.type'; +import { GuardKey } from '../type/guard-key.type'; import { GuardNumber } from '../type/guard-number.type'; +import { GuardObject } from '../type/guard-object.type'; import { GuardObjectKey } from '../type/guard-object-key.type'; import { GuardPrimitive } from '../type/guard-primitive.type'; import { GuardString } from '../type/guard-string.type'; import { GuardType } from '../type/guard-type.type'; -import { GuardObject } from '../type/guard-object.type'; +import { GuardInstance } from '../type/guard-instance.type'; +import { GuardNull } from '../type/guard-null.type'; +import { GuardSymbol } from '../type/guard-symbol.type'; +import { GuardUndefined } from '../type/guard-undefined.type'; export interface GuardIs { array: GuardArray; + bigint: GuardBigInt; + boolean: GuardBoolean; function: GuardFunction; - // TODO: Guard not - // not: GuardNotIs; + instance: GuardInstance; + key: GuardKey; + null: GuardNull; number: GuardNumber; object: GuardObject; objectKey: GuardObjectKey; primitive: GuardPrimitive; string: GuardString; + symbol: GuardSymbol; type: GuardType; + undefined: GuardUndefined; } diff --git a/packages/type/src/guard/lib/guard-array.func.ts b/packages/type/src/guard/lib/guard-array.func.ts new file mode 100644 index 00000000..56f392ef --- /dev/null +++ b/packages/type/src/guard/lib/guard-array.func.ts @@ -0,0 +1,13 @@ +// Function. +import { isArray } from '../../is/lib/is-array.func'; +// Type. +import { GuardArray } from '../type/guard-array.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Guard the `value` to be an `Array` of a generic `Type`. + * @param value A generic `Type` `Array` `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is an `Array` of a generic `Type`. + */ +export const guardArray: GuardArray = (value: Array, callback?: ResultCallback): value is Array => + isArray(value, callback); diff --git a/packages/type/src/guard/lib/guard-array.func.type.ts b/packages/type/src/guard/lib/guard-array.func.type.ts deleted file mode 100644 index fbcc11c1..00000000 --- a/packages/type/src/guard/lib/guard-array.func.type.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { isArray } from '../../is/lib/is-array.func'; -import { GuardArray } from '../type/guard-array.type'; -/** - * Guard the `value` to be a generic `Array` `Type`. - * Use `isArray()` function to check ONLY. - * @param value Array generic type value to guard. - * @returns boolean. - */ -export const guardArray: GuardArray = (value: Array): value is Array => isArray(value); diff --git a/packages/type/src/guard/lib/guard-big-int.func.ts b/packages/type/src/guard/lib/guard-big-int.func.ts new file mode 100644 index 00000000..a65dad13 --- /dev/null +++ b/packages/type/src/guard/lib/guard-big-int.func.ts @@ -0,0 +1,13 @@ +// Function. +import { isBigInt } from '../../is/lib/is-big-int.func'; +// Type. +import { GuardBigInt } from '../type/guard-big-int.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Guard the `value` to be a `bigint`. + * @param value A `bigint` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `bigint`. + */ +export const guardBigInt: GuardBigInt = (value: bigint, callback?: ResultCallback): value is bigint => + isBigInt(value, callback); diff --git a/packages/type/src/guard/lib/guard-boolean.func.ts b/packages/type/src/guard/lib/guard-boolean.func.ts new file mode 100644 index 00000000..45cd759a --- /dev/null +++ b/packages/type/src/guard/lib/guard-boolean.func.ts @@ -0,0 +1,13 @@ +// Function. +import { isBoolean } from '../../is/lib/is-boolean.func'; +// Type. +import { GuardBoolean } from '../type/guard-boolean.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Guard the `value` to be a `boolean`. + * @param value A `boolean` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `boolean`. + */ +export const guardBoolean: GuardBoolean = (value: boolean, callback?: ResultCallback): value is boolean => + isBoolean(value, callback); diff --git a/packages/type/src/guard/lib/guard-function.func.ts b/packages/type/src/guard/lib/guard-function.func.ts index 6e25ae2f..e99c26e1 100644 --- a/packages/type/src/guard/lib/guard-function.func.ts +++ b/packages/type/src/guard/lib/guard-function.func.ts @@ -1,9 +1,14 @@ -import { GuardFunction } from '../type/guard-function.type'; +// Function. import { isFunction } from '../../is/lib/is-function.func'; +// Type. +import { GuardFunction } from '../type/guard-function.type'; import { Func } from '../../type/func.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Guard the `func` value to be a `Func` type. - * @param func `Func` type value to guard. - * @returns boolean. + * Guard the `value` to be a `Func` type. + * @param value A `Func` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `Func`. */ -export const guardFunction: GuardFunction = (func: Func): func is Func => isFunction(func); +export const guardFunction: GuardFunction = (value: Func, callback?: ResultCallback): value is Func => + isFunction(value, callback); diff --git a/packages/type/src/guard/lib/guard-instance.func.ts b/packages/type/src/guard/lib/guard-instance.func.ts new file mode 100644 index 00000000..14169c5b --- /dev/null +++ b/packages/type/src/guard/lib/guard-instance.func.ts @@ -0,0 +1,15 @@ +// Function. +import { isInstance } from '../../is/lib/is-instance.func'; +// Type. +import { Constructor } from '../../type/constructor.type'; +import { GuardInstance } from '../type/guard-instance.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Guard the `value` to guard the `value` to be an `object` of a generic `Obj` type equal to an `instance`. + * @param value An `Obj` type `value` to compare with the `instance`. + * @param instance A generic `Obj` `Constructor` type to create an `instance` to compare with the `value`. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `symbol`. + */ +export const guardInstance: GuardInstance = (value: Obj, instance: Constructor, callback?: ResultCallback): value is Obj => + isInstance(value, instance, callback); diff --git a/packages/type/src/guard/lib/guard-is.object.ts b/packages/type/src/guard/lib/guard-is.object.ts index 858eaf7c..ecfec87a 100644 --- a/packages/type/src/guard/lib/guard-is.object.ts +++ b/packages/type/src/guard/lib/guard-is.object.ts @@ -1,21 +1,36 @@ -import { GuardIs } from '../interface/guard-is.interface'; -import { guardArray } from './guard-array.func.type'; +// Function. +import { guardArray } from './guard-array.func'; +import { guardBigInt } from './guard-big-int.func'; +import { guardBoolean } from './guard-boolean.func'; import { guardFunction } from './guard-function.func'; +import { guardInstance } from './guard-instance.func'; +import { guardKey } from './guard-key.func'; +import { guardNull } from './guard-null.func'; import { guardNumber } from './guard-number.func'; import { guardObject } from './guard-object.func'; import { guardObjectKey } from './guard-object-key.func'; import { guardPrimitive } from './guard-primitive.func'; import { guardString } from './guard-string.func'; +import { guardSymbol } from './guard-symbol.func'; import { guardType } from './guard-type.func'; - +import { guardUndefined } from './guard-undefined.func'; +// Interface. +import { GuardIs } from '../interface/guard-is.interface'; +// `guardIs`. export const guardIs: GuardIs = { - // TODO: add other guards etc. boolean, null, undefined array: guardArray, + bigint: guardBigInt, + boolean: guardBoolean, function: guardFunction, + instance: guardInstance, + key: guardKey, number: guardNumber, - objectKey: guardObjectKey, + null: guardNull, object: guardObject, + objectKey: guardObjectKey, primitive: guardPrimitive, string: guardString, - type: guardType + symbol: guardSymbol, + type: guardType, + undefined: guardUndefined }; diff --git a/packages/type/src/guard/lib/guard-key.func.ts b/packages/type/src/guard/lib/guard-key.func.ts new file mode 100644 index 00000000..c68e5930 --- /dev/null +++ b/packages/type/src/guard/lib/guard-key.func.ts @@ -0,0 +1,14 @@ +// Function. +import { isKey } from '../../is/lib/is-key.func'; +// Type. +import { GuardKey } from '../type/guard-key.type'; +import { Key } from '../../type/key.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Guard the `value` to be one of the `string`, `number`, or `symbol`. + * @param value A `Key` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `Key`. + */ +export const guardKey: GuardKey = (value: Key, callback?: ResultCallback): value is Key => + isKey(value, callback); diff --git a/packages/type/src/guard/lib/guard-null.func.ts b/packages/type/src/guard/lib/guard-null.func.ts new file mode 100644 index 00000000..3d8cf171 --- /dev/null +++ b/packages/type/src/guard/lib/guard-null.func.ts @@ -0,0 +1,13 @@ +// Function. +import { isNull } from '../../is/lib/is-null.func'; +// Type. +import { GuardNull } from '../type/guard-null.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Guard the `value` to be a `null`. + * @param value A `null` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `null`. + */ +export const guardNull: GuardNull = (value: null, callback?: ResultCallback): value is null => + isNull(value, callback); diff --git a/packages/type/src/guard/lib/guard-number.func.ts b/packages/type/src/guard/lib/guard-number.func.ts index be32399b..9747e88b 100644 --- a/packages/type/src/guard/lib/guard-number.func.ts +++ b/packages/type/src/guard/lib/guard-number.func.ts @@ -1,9 +1,13 @@ +// Function. import { isNumber } from '../../is/lib/is-number.func'; +// Type. import { GuardNumber } from '../type/guard-number.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Guard the `value` to be a `number` type. - * Use `isNumber()` function for check ONLY. - * @param value Type `number` value to guard. - * @returns boolean + * Guard the `value` to be a `number`. + * @param value A `number` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `number`. */ -export const guardNumber: GuardNumber = (value: number): value is number => isNumber(value); +export const guardNumber: GuardNumber = (value: number, callback?: ResultCallback): value is number => + isNumber(value, callback); diff --git a/packages/type/src/guard/lib/guard-object-key.func.ts b/packages/type/src/guard/lib/guard-object-key.func.ts index 854c8a8c..ba57034a 100644 --- a/packages/type/src/guard/lib/guard-object-key.func.ts +++ b/packages/type/src/guard/lib/guard-object-key.func.ts @@ -1,12 +1,13 @@ +// Function. import { guardObject } from './guard-object.func'; +import { isKey } from '../../is/lib/is-key.func'; +// Type. import { GuardObjectKey } from '../type/guard-object-key.type'; -import { isString } from '../../is/lib/is-string.func'; /** - * Guard the `object` to be a generic `Obj` type and to contains the `key` property. - * Use `isObject()` function for check ONLY. - * @param object Generic `Obj` type `value` that contains the `key` property to guard. - * @param key Name of the property that the `object` contains. - * @returns boolean + * Guard the `value` to be an `object` of a generic `Obj` type that contains the `key` property of the `Key` type. + * @param value A generic `Obj` type `value` that contains the `key` to guard. + * @param key A `Key` type name of the property that the `value` contains. + * @returns A `boolean` indicating whether or not the `value` is an `object` of a generic `Obj` containing the `Key`. */ -export const guardObjectKey: GuardObjectKey = (object: Obj, key: Key): object is Obj => - guardObject(object) ? isString(key) ? key in object : true : false; +export const guardObjectKey: GuardObjectKey = (value: Obj, key: Key): value is Obj => + guardObject(value) ? isKey(key) ? key in value : true : false; diff --git a/packages/type/src/guard/lib/guard-object.func.ts b/packages/type/src/guard/lib/guard-object.func.ts index 246fa895..7614cb20 100644 --- a/packages/type/src/guard/lib/guard-object.func.ts +++ b/packages/type/src/guard/lib/guard-object.func.ts @@ -1,9 +1,13 @@ -import { GuardObject } from '../type/guard-object.type'; +// Function. import { isObject } from '../../is/lib/is-object.func'; +// Type. +import { GuardObject } from '../type/guard-object.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Guard the `obj` value to be a generic object `Obj`. - * Use `isObject()` function for check ONLY. - * @param object Generic `Obj` type value to guard. - * @returns boolean + * Guard the `value` to be an `object` of a generic `Obj` type. + * @param value A generic `Obj` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is an `object` of a generic `Obj`. */ -export const guardObject: GuardObject = (object: Obj): object is Obj => isObject(object); +export const guardObject: GuardObject = (value: Obj, callback?: ResultCallback): value is Obj => + isObject(value); diff --git a/packages/type/src/guard/lib/guard-primitive.func.ts b/packages/type/src/guard/lib/guard-primitive.func.ts index 27c73727..9d3fbc7f 100644 --- a/packages/type/src/guard/lib/guard-primitive.func.ts +++ b/packages/type/src/guard/lib/guard-primitive.func.ts @@ -1,10 +1,17 @@ +// Function. +import { isPrimitive } from '../../is/lib/is-primitive.func'; +// Type. import { GuardPrimitive } from '../type/guard-primitive.type'; +import { Primitive } from '../../type/primitive.type'; import { Primitives } from '../../type/primitives.type'; -import { isPrimitive } from '../../is/lib/is-primitive.func'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Guard the `value` to be a generic `Type` from one of the `Primitives`. - * Use `isPrimitive()` function for check ONLY. - * @param value A generic `Type` type value to guard. - * @param type One of the `Primitives` `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` to check `value`. + * Guard the `value` to be the `Primitive` from a `type` of the `Primitives`. + * @param value A `Primitive` type `value` to guard. + * @param type A `string` type from the `Primitives` to check the `value`. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is the `Primitive` from the `type`. */ -export const guardPrimitive: GuardPrimitive = (value: Type, type: Primitives): value is Type => isPrimitive(value, type); +export const guardPrimitive: GuardPrimitive = + (value: Type, type: Primitives, callback?: ResultCallback): value is Type => + isPrimitive(value, type, callback); diff --git a/packages/type/src/guard/lib/guard-string.func.ts b/packages/type/src/guard/lib/guard-string.func.ts index 6b810b3d..03e930ff 100644 --- a/packages/type/src/guard/lib/guard-string.func.ts +++ b/packages/type/src/guard/lib/guard-string.func.ts @@ -1,8 +1,13 @@ -import { GuardString } from '../type/guard-string.type'; +// Function. import { isString } from '../../is/lib/is-string.func'; +// Type. +import { GuardString } from '../type/guard-string.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Guard the `value` to be a `string` type. - * Use `isString()` function for check ONLY. - * @param value String type value to guard. + * Guard the `value` to be a `string`. + * @param value A `string` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `string`. */ -export const guardString: GuardString = (value: string): value is string => isString(value); +export const guardString: GuardString = (value: string, callback?: ResultCallback): value is string => + isString(value, callback); diff --git a/packages/type/src/guard/lib/guard-symbol.func.ts b/packages/type/src/guard/lib/guard-symbol.func.ts new file mode 100644 index 00000000..5247daff --- /dev/null +++ b/packages/type/src/guard/lib/guard-symbol.func.ts @@ -0,0 +1,13 @@ +// Function. +import { isSymbol } from '../../is/lib/is-symbol.func'; +// Type. +import { GuardSymbol } from '../type/guard-symbol.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Guard the `value` to be a `symbol`. + * @param value A `symbol` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a `symbol`. + */ +export const guardSymbol: GuardSymbol = (value: symbol, callback?: ResultCallback): value is symbol => + isSymbol(value, callback); diff --git a/packages/type/src/guard/lib/guard-type.func.ts b/packages/type/src/guard/lib/guard-type.func.ts index d9e77718..d6180297 100644 --- a/packages/type/src/guard/lib/guard-type.func.ts +++ b/packages/type/src/guard/lib/guard-type.func.ts @@ -1,10 +1,16 @@ +// Function. import { isType } from '../../is/lib/is-type.func'; +// Type. import { GuardType } from '../type/guard-type.type'; +import { ResultCallback } from '../../type/result-callback.type'; +import { Type } from '../../type/type.type'; import { Types } from '../../type/types.type'; /** - * Guard the `value` to be a generic `Type` type from one of the `Types` type. - * Use `isType()` function for check ONLY. - * @param value A generic `Type` value to guard. - * @param type Constructor generic `Type`, `'function'`, `'object'` or one of the `Primitives` `'boolean'`, `'bigint'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` to check `value`. + * Guard the `value` to be the `Type` from a `type` of the `Types`. + * @param value A `Type` `value` to guard with the `type`. + * @param type A `string` or generic `Constructor` type from the `Types` to check the `value`. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is a type from the `Types`. */ -export const guardType: GuardType = (value: Type, type: Types): value is Type => isType(value, type); +export const guardType: GuardType = (value: T, type: Types, callback?: ResultCallback): value is T => + isType(value, type, callback); diff --git a/packages/type/src/guard/lib/guard-undefined.func.ts b/packages/type/src/guard/lib/guard-undefined.func.ts new file mode 100644 index 00000000..6c452401 --- /dev/null +++ b/packages/type/src/guard/lib/guard-undefined.func.ts @@ -0,0 +1,13 @@ +// Function. +import { isUndefined } from '../../is/lib/is-undefined.func'; +// Type. +import { GuardUndefined } from '../type/guard-undefined.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Guard the `value` to be `undefined`. + * @param value A `undefined` type `value` to guard. + * @param callback Optional `ResultCallback` function to handle result before returns. + * @returns A `boolean` indicating whether or not the `value` is `undefined`. + */ +export const guardUndefined: GuardUndefined = (value: undefined, callback?: ResultCallback): value is undefined => + isUndefined(value, callback); diff --git a/packages/type/src/guard/lib/guard.object.ts b/packages/type/src/guard/lib/guard.object.ts index da7697c9..2bf7dc5a 100644 --- a/packages/type/src/guard/lib/guard.object.ts +++ b/packages/type/src/guard/lib/guard.object.ts @@ -1,6 +1,8 @@ -import { Guard } from '../interface/guard-interface'; +// Object. import { guardIs } from './guard-is.object'; - +// Interface. +import { Guard } from '../interface/guard-interface'; +// `guard` export const guard: Guard = { is: guardIs }; diff --git a/packages/type/src/guard/test/guard-array.spec.ts b/packages/type/src/guard/test/guard-array.spec.ts index 2d8d1ddb..721fc838 100644 --- a/packages/type/src/guard/test/guard-array.spec.ts +++ b/packages/type/src/guard/test/guard-array.spec.ts @@ -1,11 +1,10 @@ // Function. -import { guardArray } from '../lib/guard-array.func.type'; - +import { guardArray } from '../lib/guard-array.func'; // Variables. +import { BIGINT, BIGINT_INSTANCE } from '../../is/test/variables/big-int.const'; import { Class } from '../../is/test/variables/class.const'; import { Func } from '../../type/func.type'; import { ObjectOne, ObjectTwo, OBJECT_ONE, OBJECT_TWO } from '../../is/test/variables/object.const'; -import { BIGINT, BIGINT_INSTANCE } from '../../is/test/variables/big-int.const'; import { ARRAY_BIGINT, ARRAY_BOOLEAN, @@ -16,8 +15,8 @@ import { ARRAY_OBJECT_ONE, ARRAY_OBJECT_TWO, ARRAY_STRING, - ARRAY_SYMBOL_STRING, ARRAY_SYMBOL_NUMBER, + ARRAY_SYMBOL_STRING, ARRAY_UNDEFINED } from '../../is/test/variables/array.const'; import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from '../../is/test/variables/boolean.const'; @@ -25,8 +24,8 @@ import { FUNCTION } from '../../is/test/variables/function.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../is/test/variables/number.const'; import { STRING, STRING_INSTANCE } from '../../is/test/variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../is/test/variables/symbol.const'; -import { notDefined } from '../../is/test/variables.const'; import { UNDEFINED } from '../../is/test/variables/undefined.const'; +import { notDefined } from '../../is/test/variables/not-defined.const'; describe('guardArray', () => { // TRUE @@ -62,26 +61,15 @@ describe('guardArray', () => { expect(guardArray(FALSE_INSTANCE)).toBeFalsy(); expect(guardArray(TRUE_INSTANCE)).toBeFalsy(); }); - // it(`'function' | Function`, () => { - // expect(guardArray([FUNCTION])).toBeTrue(); - // }); it(`'number' | Number`, () => { expect(guardArray(NUMBER)).toBeFalsy(); expect(guardArray(NUMBER_INSTANCE)).toBeFalsy(); expect(guardArray(NUMBER_NEW_INSTANCE)).toBeFalsy(); }); - // it(`'object' | Object`, () => { - // expect(guardArray([OBJECT_ONE])).toBeTruthy(); - // expect(guardArray([OBJECT_TWO])).toBeTruthy(); - // }); it(`'string' | String`, () => { expect(guardArray(STRING)).toBeFalsy(); expect(guardArray(STRING_INSTANCE)).toBeFalsy(); }); - it(`'symbol'`, () => { - expect(guardArray(SYMBOL_NUMBER)).toBeFalsy(); - expect(guardArray(SYMBOL_STRING)).toBeFalsy(); - }); it(`'undefined'`, () => { expect(guardArray(notDefined)).toBeFalse(); expect(guardArray(UNDEFINED)).toBeFalse(); diff --git a/packages/type/src/guard/test/guard-bigint.spec.ts b/packages/type/src/guard/test/guard-bigint.spec.ts new file mode 100644 index 00000000..6c71783f --- /dev/null +++ b/packages/type/src/guard/test/guard-bigint.spec.ts @@ -0,0 +1,86 @@ +// Function. +import { guardBigInt } from '../lib/guard-big-int.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from '../../is/test/variables/big-int.const'; +// import { Class, CLASS } from '../../is/test/variables/class.const'; +// import { FUNCTION } from '../../is/test/variables/function.const'; +import { NULL } from '../../is/test/variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../is/test/variables/number.const'; +// import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from '../../is/test/variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../../is/test/variables/string.const'; +// import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../is/test/variables/symbol.const'; +import { TRUE, FALSE, FALSE_EXPECTATION, TRUE_EXPECTATION, TRUE_INSTANCE, FALSE_INSTANCE } from '../../is/test/variables/boolean.const'; +import { UNDEFINED } from '../../is/test/variables/undefined.const'; + +describe(`guardBigInt`, () => { + // Defined. + it('is DEFINED', () => expect(guardBigInt).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + guardBigInt(STRING, (result: boolean) => { + expect(result).toBe(FALSE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(guardBigInt(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(guardBigInt(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + // it(`FUNCTION`, () => expect(guardBigInt(FUNCTION)).toBe(FALSE)); + // it(`Class`, () => expect(guardBigInt(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + // it(`CLASS`, () => expect(guardBigInt(CLASS)).toBe(FALSE)); + // it(`OBJECT_ONE`, () => expect(guardBigInt(OBJECT_ONE)).toBe(FALSE)); + // it(`OBJECT_TWO`, () => expect(guardBigInt(OBJECT_TWO)).toBe(FALSE)); + // it(`new Object(OBJECT_ONE_NEW})`, () => expect(guardBigInt(OBJECT_ONE_NEW)).toBe(FALSE)); + // it(`new Object(OBJECT_TWO_NEW})`, () => expect(guardBigInt(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(guardBigInt(BIGINT)).toBe(TRUE)); + it(`${BIGINT_EXPECTATION}`, () => expect(guardBigInt(BIGINT_INSTANCE)).toBe(TRUE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(guardBigInt(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(guardBigInt(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(guardBigInt(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(guardBigInt(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(guardBigInt(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(guardBigInt(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(guardBigInt(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(guardBigInt(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(guardBigInt(STRING)).toBe(FALSE)); + it(`String(${STRING})`, () => expect(guardBigInt(STRING_INSTANCE)).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(guardBigInt(STRING_NEW_INSTANCE)).toBe(FALSE)); + }); + // symbol + describe(`symbol`, () => { + // it(`Symbol(${NUMBER})`, () => expect(guardBigInt(SYMBOL_NUMBER)).toBe(FALSE)); + // it(`Symbol(${STRING})`, () => expect(guardBigInt(SYMBOL_STRING)).toBe(FALSE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(guardBigInt(UNDEFINED)).toBe(FALSE)); + }); + }); +}); diff --git a/packages/type/src/guard/test/guard.spec.ts b/packages/type/src/guard/test/guard.spec.ts new file mode 100644 index 00000000..d2d5f165 --- /dev/null +++ b/packages/type/src/guard/test/guard.spec.ts @@ -0,0 +1,21 @@ +import { guard } from '../lib/guard.object'; + +describe('`guard`', () => { + describe('DEFINED', () => { + it('guard', () => expect(guard).toBeDefined()); + it('guard.is.array()', () => expect(guard.is.array).toBeDefined()); + it('guard,is.boolean()', () => expect(guard.is.boolean).toBeDefined()); + it('guard,is.function()', () => expect(guard.is.function).toBeDefined()); + it('guard,is.instance()', () => expect(guard.is.instance).toBeDefined()); + it('guard,is.key()', () => expect(guard.is.key).toBeDefined()); + it('guard,is.null()', () => expect(guard.is.null).toBeDefined()); + it('guard,is.number()', () => expect(guard.is.number).toBeDefined()); + it('guard,is.object()', () => expect(guard.is.object).toBeDefined()); + it('guard,is.objectKey()', () => expect(guard.is.objectKey).toBeDefined()); + it('guard,is.primitive()', () => expect(guard.is.primitive).toBeDefined()); + it('guard,is.string()', () => expect(guard.is.string).toBeDefined()); + it('guard,is.symbol()', () => expect(guard.is.symbol).toBeDefined()); + it('guard,is.type()', () => expect(guard.is.type).toBeDefined()); + it('guard,is.undefined()', () => expect(guard.is.undefined).toBeDefined()); + }); +}); diff --git a/packages/type/src/guard/type/guard-array.type.ts b/packages/type/src/guard/type/guard-array.type.ts index 6e4613f9..9599fb30 100644 --- a/packages/type/src/guard/type/guard-array.type.ts +++ b/packages/type/src/guard/type/guard-array.type.ts @@ -1 +1,2 @@ -export type GuardArray = (value: Array) => value is Array; +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardArray = (value: Array, callback?: ResultCallback) => value is Array; diff --git a/packages/type/src/guard/type/guard-big-int.type.ts b/packages/type/src/guard/type/guard-big-int.type.ts new file mode 100644 index 00000000..25f520e3 --- /dev/null +++ b/packages/type/src/guard/type/guard-big-int.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardBigInt = (value: bigint, callback?: ResultCallback) => value is bigint; diff --git a/packages/type/src/guard/type/guard-boolean.type.ts b/packages/type/src/guard/type/guard-boolean.type.ts new file mode 100644 index 00000000..d9fb88c4 --- /dev/null +++ b/packages/type/src/guard/type/guard-boolean.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardBoolean = (value: boolean, callback?: ResultCallback) => value is boolean; diff --git a/packages/type/src/guard/type/guard-function.type.ts b/packages/type/src/guard/type/guard-function.type.ts index 30071929..82f6b8c0 100644 --- a/packages/type/src/guard/type/guard-function.type.ts +++ b/packages/type/src/guard/type/guard-function.type.ts @@ -1,2 +1,3 @@ import { Func } from '../../type/func.type'; -export type GuardFunction = (func: Func) => func is Func; +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardFunction = (value: Func, callback?: ResultCallback) => value is Func; diff --git a/packages/type/src/guard/type/guard-instance.type.ts b/packages/type/src/guard/type/guard-instance.type.ts new file mode 100644 index 00000000..22911fcf --- /dev/null +++ b/packages/type/src/guard/type/guard-instance.type.ts @@ -0,0 +1,3 @@ +import { Constructor } from '../../type/constructor.type'; +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardInstance = (value: Obj, instance: Constructor, callback?: ResultCallback) => value is Obj; diff --git a/packages/type/src/guard/type/guard-key.type.ts b/packages/type/src/guard/type/guard-key.type.ts new file mode 100644 index 00000000..274cb19f --- /dev/null +++ b/packages/type/src/guard/type/guard-key.type.ts @@ -0,0 +1,3 @@ +import { Key } from '../../type/key.type'; +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardKey = (value: Key, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/guard/type/guard-null.type.ts b/packages/type/src/guard/type/guard-null.type.ts new file mode 100644 index 00000000..75643565 --- /dev/null +++ b/packages/type/src/guard/type/guard-null.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardNull = (value: null, callback?: ResultCallback) => value is null; diff --git a/packages/type/src/guard/type/guard-number.type.ts b/packages/type/src/guard/type/guard-number.type.ts index 2ebe8a2f..6ab2e6ba 100644 --- a/packages/type/src/guard/type/guard-number.type.ts +++ b/packages/type/src/guard/type/guard-number.type.ts @@ -1 +1,2 @@ -export type GuardNumber = (value: number) => value is number; +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardNumber = (value: number, callback?: ResultCallback) => value is number; diff --git a/packages/type/src/guard/type/guard-object-key.type.ts b/packages/type/src/guard/type/guard-object-key.type.ts index dbcfc6c6..48b4fb46 100644 --- a/packages/type/src/guard/type/guard-object-key.type.ts +++ b/packages/type/src/guard/type/guard-object-key.type.ts @@ -1 +1 @@ -export type GuardObjectKey = (object: Obj, key: Key) => object is Obj; +export type GuardObjectKey = (value: Obj, key: Key) => value is Obj; diff --git a/packages/type/src/guard/type/guard-object.type.ts b/packages/type/src/guard/type/guard-object.type.ts index 02ea3694..761a5e83 100644 --- a/packages/type/src/guard/type/guard-object.type.ts +++ b/packages/type/src/guard/type/guard-object.type.ts @@ -1 +1,2 @@ -export type GuardObject = (object: Obj) => object is Obj; +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardObject = (value: Obj, callback?: ResultCallback) => value is Obj; diff --git a/packages/type/src/guard/type/guard-primitive.type.ts b/packages/type/src/guard/type/guard-primitive.type.ts index a2680526..fc0a2467 100644 --- a/packages/type/src/guard/type/guard-primitive.type.ts +++ b/packages/type/src/guard/type/guard-primitive.type.ts @@ -1,2 +1,4 @@ +import { Primitive } from '../../type/primitive.type'; import { Primitives } from '../../type/primitives.type'; -export type GuardPrimitive = (value: Type, type: Primitives) => value is Type; +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardPrimitive = (value: T, type: Primitives, callback?: ResultCallback) => value is T; diff --git a/packages/type/src/guard/type/guard-string.type.ts b/packages/type/src/guard/type/guard-string.type.ts index 6f752b53..250702f4 100644 --- a/packages/type/src/guard/type/guard-string.type.ts +++ b/packages/type/src/guard/type/guard-string.type.ts @@ -1 +1,2 @@ -export type GuardString = (value: string) => value is string; +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardString = (value: string, callback?: ResultCallback) => value is string; diff --git a/packages/type/src/guard/type/guard-symbol.type.ts b/packages/type/src/guard/type/guard-symbol.type.ts new file mode 100644 index 00000000..8cd97295 --- /dev/null +++ b/packages/type/src/guard/type/guard-symbol.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardSymbol = (value: symbol, callback?: ResultCallback) => value is symbol; diff --git a/packages/type/src/guard/type/guard-type.type.ts b/packages/type/src/guard/type/guard-type.type.ts index 9e04612a..6563c07a 100644 --- a/packages/type/src/guard/type/guard-type.type.ts +++ b/packages/type/src/guard/type/guard-type.type.ts @@ -1,2 +1,4 @@ +import { ResultCallback } from '../../type/result-callback.type'; import { Types } from '../../type/types.type'; -export type GuardType = (value: Type, type: Types) => value is Type; +import { Type } from '../../type/type.type'; +export type GuardType = (value: T, type: Types, callback?: ResultCallback) => value is T; diff --git a/packages/type/src/guard/type/guard-undefined.type.ts b/packages/type/src/guard/type/guard-undefined.type.ts new file mode 100644 index 00000000..875793e2 --- /dev/null +++ b/packages/type/src/guard/type/guard-undefined.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type GuardUndefined = (value: undefined, callback?: ResultCallback) => value is undefined; diff --git a/packages/type/src/is/index.ts b/packages/type/src/is/index.ts index 08dcb09f..99047c4c 100644 --- a/packages/type/src/is/index.ts +++ b/packages/type/src/is/index.ts @@ -2,16 +2,36 @@ export { isArray } from './lib/is-array.func'; export { isBigInt } from './lib/is-big-int.func'; export { isBoolean } from './lib/is-boolean.func'; +export { isBooleanObject } from './lib/is-boolean-object.func'; +export { isBooleanType } from './lib/is-boolean-type.func'; export { isDefined } from './lib/is-defined.func'; export { isFunction } from './lib/is-function.func'; export { isInstance } from './lib/is-instance.func'; -export { isNumber } from './lib/is-number.func'; +export { isKey } from './lib/is-key.func'; export { isNull } from './lib/is-null.func'; +export { isNumber } from './lib/is-number.func'; +export { isNumberObject } from './lib/is-number-object.func'; +export { isNumberType } from './lib/is-number-type.func'; export { isObject } from './lib/is-object.func'; +export { isObjectKey } from './lib/is-object-key.func'; export { isPrimitive } from './lib/is-primitive.func'; export { isString } from './lib/is-string.func'; +export { isStringObject } from './lib/is-string-object.func'; +export { isStringType } from './lib/is-string-type.func'; export { isSymbol } from './lib/is-symbol.func'; export { isType } from './lib/is-type.func'; export { isUndefined } from './lib/is-undefined.func'; -// `is` object with all above functions. +// `is` object. export { is } from './lib/is.object'; +// All `isNot` functions. +export { + isNotBoolean, + isNotDefined, + isNotFunction, + isNotNull, + isNotNumber, + isNotString, + isNotUndefined +} from './not'; +// `isNot`. +export { isNot } from './not'; diff --git a/packages/type/src/is/interface/is.interface.ts b/packages/type/src/is/interface/is.interface.ts index 10aa8c45..69b16e1d 100644 --- a/packages/type/src/is/interface/is.interface.ts +++ b/packages/type/src/is/interface/is.interface.ts @@ -1,32 +1,49 @@ import { IsArray } from '../type/is-array.type'; -import { IsBoolean } from '../type/is-boolean.type'; import { IsBigInt } from '../type/is-big-int.type'; +import { IsBoolean } from '../type/is-boolean.type'; +import { IsBooleanObject } from '../type/is-boolean-object.type'; import { IsDefined } from '../type/is-defined.type'; import { IsFunction } from '../type/is-function.type'; +import { IsInstance } from '../type/is-instance.type'; +import { IsKey } from '../type/is-key.type'; import { IsNot } from '../not/interface/is-not.interface'; import { IsNull } from '../type/is-null.type'; import { IsNumber } from '../type/is-number.type'; import { IsObject } from '../type/is-object.type'; +import { IsObjectKey } from '../type/is-object-key.type'; import { IsPrimitive } from '../type/is-primitive.type'; import { IsString } from '../type/is-string.type'; +import { IsStringObject } from '../type/is-string-object.type'; +import { IsStringType } from '../type/is-string-type.type'; import { IsSymbol } from '../type/is-symbol.type'; import { IsType } from '../type/is-type.type'; import { IsUndefined } from '../type/is-undefined.type'; -import { IsInstance } from '../type/is-instance.type'; +import { IsBooleanType } from '../type/is-boolean-type.type'; +import { IsNumberObject } from '../type/is-number-object.type'; +import { IsNumberType } from '../type/is-number-type.type'; export interface Is { array: IsArray; bigInt: IsBigInt; + bigint: IsBigInt; boolean: IsBoolean; + booleanObject: IsBooleanObject; + booleanType: IsBooleanType; defined: IsDefined; function: IsFunction; instance: IsInstance; + key: IsKey; not: IsNot; null: IsNull; number: IsNumber; + numberObject: IsNumberObject; + numberType: IsNumberType; object: IsObject; + objectKey: IsObjectKey; primitive: IsPrimitive; string: IsString; + stringObject: IsStringObject; + stringType: IsStringType; symbol: IsSymbol; type: IsType; undefined: IsUndefined; diff --git a/packages/type/src/is/lib/is-array.func.ts b/packages/type/src/is/lib/is-array.func.ts index a474a6de..b777bda0 100644 --- a/packages/type/src/is/lib/is-array.func.ts +++ b/packages/type/src/is/lib/is-array.func.ts @@ -1,13 +1,20 @@ -import { IsArray } from '../type/is-array.type'; +// Function. +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsArray } from '../type/is-array.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is an `Array` of `'object'` generic `Type` type and `Array` instance. - * Use the `guardArray()` function to type-guard `array` also. - * @param value Any `value` to check if it's an `Array` of `'object'` generic `Type` type and `Array` instance. - * @returns boolean. + * Checks if any `value` is an `Array`, `Array` instance, and `object` type. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is an `Array`. */ -export const isArray: IsArray = (value: any): value is Array => - typeOf(value) === 'array' && - Array.isArray(value) === true && - value instanceof Array === true && - typeof value === 'object'; +export const isArray: IsArray = (value: any, callback: ResultCallback = resultCallback): value is Array => + callback( + typeOf(value) === 'array' && + Array.isArray(value) === true && + value instanceof Array === true && + typeof value === 'object' + ); diff --git a/packages/type/src/is/lib/is-big-int.func.ts b/packages/type/src/is/lib/is-big-int.func.ts index 2d47e8f2..39e2649c 100644 --- a/packages/type/src/is/lib/is-big-int.func.ts +++ b/packages/type/src/is/lib/is-big-int.func.ts @@ -1,10 +1,15 @@ -import { IsBigInt } from '../type/is-big-int.type'; +// Function. +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsBigInt } from '../type/is-big-int.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is a `'bigint'` type. - * @param value Any `value` to check if it's a `'bigint'` type. - * @returns boolean + * Checks if any `value` is a `bigint` type. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `bigint`. */ -export const isBigInt: IsBigInt = (value: any): value is bigint => - typeOf(value) === 'bigint' && - typeof value === 'bigint'; +export const isBigInt: IsBigInt = (value: any, callback: ResultCallback = resultCallback): value is bigint => + callback(typeOf(value) === 'bigint' && typeof value === 'bigint'); diff --git a/packages/type/src/is/lib/is-boolean-object.func.ts b/packages/type/src/is/lib/is-boolean-object.func.ts new file mode 100644 index 00000000..9d7766d8 --- /dev/null +++ b/packages/type/src/is/lib/is-boolean-object.func.ts @@ -0,0 +1,15 @@ +// Function. +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsBooleanObject } from '../type/is-boolean-object.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Checks if any `value` is an `object` type and instance of `Boolean` and `Object`. + * @function `isBooleanObject` + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `Boolean` instance. + */ +export const isBooleanObject: IsBooleanObject = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback(typeof value === 'object' && value instanceof Boolean === true && value instanceof Object === true); diff --git a/packages/type/src/is/lib/is-boolean-type.func.ts b/packages/type/src/is/lib/is-boolean-type.func.ts new file mode 100644 index 00000000..9d5e8135 --- /dev/null +++ b/packages/type/src/is/lib/is-boolean-type.func.ts @@ -0,0 +1,20 @@ +// Function. +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsBooleanType } from '../type/is-boolean-type.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Checks if any `value` is a `boolean` type not an instance of `Boolean` and `Object`, and equal to `true` or `false`. + * @function `isBooleanType` + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `boolean` type. + */ +export const isBooleanType: IsBooleanType = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback( + value instanceof Boolean === false && + value instanceof Object === false && + typeof value === 'boolean' && + (value === true || value === false) + ); diff --git a/packages/type/src/is/lib/is-boolean.func.ts b/packages/type/src/is/lib/is-boolean.func.ts index f6ad846d..2205cfd3 100644 --- a/packages/type/src/is/lib/is-boolean.func.ts +++ b/packages/type/src/is/lib/is-boolean.func.ts @@ -1,24 +1,17 @@ -import { IsBoolean } from '../type/is-boolean.type'; +// 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'; +// Type. +import { IsBoolean } from '../type/is-boolean.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is a `'boolean'` type not instance of `Boolean` and `Object` or `'object'` type instance of `Boolean` and `Object`. - * @param value Any `value` to check if it's a `'boolean'` type not instance of `Boolean` and `Object` - * or `'object'` type instance of `Boolean` and `Object`. - * @returns boolean. + * Checks if any `value` is a `boolean` type not instance of `Boolean` and `Object` or `object` type instance of `Boolean` and `Object`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `boolean`. */ -export const isBoolean: IsBoolean = (value: any): value is boolean => - typeOf(value) === 'boolean' && - ( - ( - typeof value === 'object' && - value instanceof Boolean === true && - value instanceof Object === true - ) - || - ( - value instanceof Boolean === false && - value instanceof Object === false && - typeof value === 'boolean' && - (value === true || value === false) - ) - ); +export const isBoolean: IsBoolean = (value: any, callback: ResultCallback = resultCallback): value is boolean => + callback(typeOf(value) === 'boolean' && (isBooleanType(value) || isBooleanObject(value))); diff --git a/packages/type/src/is/lib/is-defined.func.ts b/packages/type/src/is/lib/is-defined.func.ts index fb89c321..75048ce8 100644 --- a/packages/type/src/is/lib/is-defined.func.ts +++ b/packages/type/src/is/lib/is-defined.func.ts @@ -1,11 +1,15 @@ -import { IsDefined } from '../type/is-defined.type'; +// Function. +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsDefined } from '../type/is-defined.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if an unknown `value` is NOT an `'undefined'` type and is not equal to `undefined`. - * @param value An unknown `value` to check if it's NOT an `'undefined'` type and is not equal to `undefined`. - * @returns boolean. + * Checks if an unknown `value` is NOT an `undefined` type and is NOT equal to `undefined`. + * @param value An `unknown` `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is defined, not `undefined`. */ -export const isDefined: IsDefined = (value: unknown): boolean => - typeOf(value) !== 'undefined' && - typeof value !== 'undefined' && - value !== undefined; +export const isDefined: IsDefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'undefined' && typeof value !== 'undefined' && value !== undefined); diff --git a/packages/type/src/is/lib/is-function.func.ts b/packages/type/src/is/lib/is-function.func.ts index 0d055f0d..3cf26994 100644 --- a/packages/type/src/is/lib/is-function.func.ts +++ b/packages/type/src/is/lib/is-function.func.ts @@ -1,13 +1,21 @@ -import { IsFunction } from '../type/is-function.type'; -import { Func } from '../../type/func.type'; +// Function. +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { Func } from '../../type/func.type'; +import { IsFunction } from '../type/is-function.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is a `'function'` type, instance of `Function` and `Object`. - * @param value Any `value` to check if it's a `'function'` type, instance of `Function` and `Object`. - * @returns boolean. + * Checks if any `value` is a `function` type, an instance of `Function` and `Object`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `function`. */ -export const isFunction: IsFunction = (value: any): value is Func => - typeOf(value) === 'function' && - typeof value === 'function' && - value instanceof Function === true && - value instanceof Object === true; +export const isFunction: IsFunction = (value: any, callback: ResultCallback = resultCallback): value is Func => + callback( + typeOf(value) === 'function' && + typeof value === 'function' && + value instanceof Function === true && + value instanceof Object === true + ); diff --git a/packages/type/src/is/lib/is-instance.func.ts b/packages/type/src/is/lib/is-instance.func.ts index deef0e07..66dd8122 100644 --- a/packages/type/src/is/lib/is-instance.func.ts +++ b/packages/type/src/is/lib/is-instance.func.ts @@ -1,13 +1,28 @@ +// Function. +import { isFunction } from './is-function.func'; +import { isObject } from './is-object.func'; +import { resultCallback } from '../../lib/result-callback.func'; +// Type. import { Constructor } from '../../type/constructor.type'; import { IsInstance } from '../type/is-instance.type'; -import { isString } from './is-string.func'; -import { isObject } from './is-object.func'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is a generic `Obj` type `constructor` instance and is an `Object`. - * @param value Any generic `Obj` type `value` instance to compare with `type` instance. - * @param instance Creates generic `Obj` type instance to compare with argument `value`. + * Checks if any `value` is an `object` of a generic `Obj` type equal to an `instance` of `Constructor` type. + * @param value Any `value` to compare with the `instance`. + * @param instance A generic `Obj` `Constructor` type to create an `instance` to compare with the `value`. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is an `instance` of a generic `Obj`. */ -export const isInstance: IsInstance = (value: any, instance: Constructor): value is Obj => - isObject(value) && - value instanceof instance === true && - isString(instance.prototype.constructor.name); +export const isInstance: IsInstance = ( + value: any, + instance: Constructor, + callback: ResultCallback = resultCallback + ): value is Obj => + callback( + isObject(value) ? + isFunction(instance) ? + value instanceof instance === true + : false + : false + ); diff --git a/packages/type/src/is/lib/is-key.func.ts b/packages/type/src/is/lib/is-key.func.ts new file mode 100644 index 00000000..f906eeed --- /dev/null +++ b/packages/type/src/is/lib/is-key.func.ts @@ -0,0 +1,18 @@ +// Function. +import { isNumber } from './is-number.func'; +import { isString } from './is-string.func'; +import { isSymbol } from './is-symbol.func'; +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsKey } from '../type/is-key.type'; +import { Key } from '../../type/key.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Checks if any `value` is one of the `string`, `number`, or `symbol`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `Key`. + */ +export const isKey: IsKey = (value: any, callback: ResultCallback = resultCallback): value is Key => + callback(isString(value) || isNumber(value) || isSymbol(value)); diff --git a/packages/type/src/is/lib/is-null.func.ts b/packages/type/src/is/lib/is-null.func.ts index bd32b7af..1b69045b 100644 --- a/packages/type/src/is/lib/is-null.func.ts +++ b/packages/type/src/is/lib/is-null.func.ts @@ -1,11 +1,15 @@ -import { IsNull } from '../type/is-null.type'; +// Function. +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsNull } from '../type/is-null.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is an `'object'` type and equal to `null`. - * @param value Any `value` to check if it's a `null` value and an `'object'` type. - * @returns boolean + * Checks if any `value` is an `object` type and equal to `null`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is `null`. */ -export const isNull: IsNull = (value: any): value is null => - typeOf(value) === 'null' && - typeof value === 'object' && - value === null; +export const isNull: IsNull = (value: any, callback: ResultCallback = resultCallback): value is null => + callback(typeOf(value) === 'null' && typeof value === 'object' && value === null); diff --git a/packages/type/src/is/lib/is-number-object.func.ts b/packages/type/src/is/lib/is-number-object.func.ts new file mode 100644 index 00000000..f6563648 --- /dev/null +++ b/packages/type/src/is/lib/is-number-object.func.ts @@ -0,0 +1,14 @@ +// Function. +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsNumberObject } from '../type/is-number-object.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Checks if any `value` is an `object` type and instance of `Number` and `Object`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `Number` instance. + */ +export const isNumberObject: IsNumberObject = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(typeof value === 'object' && value instanceof Number === true && value instanceof Object === true); diff --git a/packages/type/src/is/lib/is-number-type.func.ts b/packages/type/src/is/lib/is-number-type.func.ts new file mode 100644 index 00000000..073df8c6 --- /dev/null +++ b/packages/type/src/is/lib/is-number-type.func.ts @@ -0,0 +1,14 @@ +// Function. +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsNumberType } from '../type/is-number-type.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Checks if any `value` is a `boolean` type not an instance of `Boolean` and `Object`, and equal to `true` or `false`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `number` type. + */ +export const isNumberType: IsNumberType = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(value instanceof Number === false && value instanceof Object === false && typeof value === 'number'); diff --git a/packages/type/src/is/lib/is-number.func.ts b/packages/type/src/is/lib/is-number.func.ts index 89cacea3..636078c5 100644 --- a/packages/type/src/is/lib/is-number.func.ts +++ b/packages/type/src/is/lib/is-number.func.ts @@ -1,25 +1,17 @@ -import { IsNumber } from '../type/is-number.type'; +// Function. +import { isNumberObject } from './is-number-object.func'; +import { isNumberType } from './is-number-type.func'; +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsNumber } from '../type/is-number.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is a `'number'` type not instance of `Number` and `Object` or `'object'` type instance of `Number` and `Object`. - * Use the `guardNumber()` function to type-guard `number` also. - * @param value Any value to check if it's a `'number'` type not instance of `Number` and `Object` - * or `'object'` type instance of `Number` and `Object`. - * @returns boolean + * Checks if any `value` is a `number` type not an instance of `Number` and `Object` or `object` type instance of `Number` and `Object`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `number`. */ -export const isNumber: IsNumber = (value: any): value is number => - typeOf(value) === 'number' && - ( - isFinite(value) === true && - ( - value instanceof Number === false && - value instanceof Object === false && - typeof value === 'number' - ) - || - ( - typeof value === 'object' && - value instanceof Number === true && - value instanceof Object === true - ) - ); +export const isNumber: IsNumber = (value: any, callback: ResultCallback = resultCallback): value is number => + callback(typeOf(value) === 'number' && isFinite(value) === true && (isNumberType(value) || isNumberObject(value))); diff --git a/packages/type/src/is/lib/is-object-key.func.ts b/packages/type/src/is/lib/is-object-key.func.ts new file mode 100644 index 00000000..208baf3e --- /dev/null +++ b/packages/type/src/is/lib/is-object-key.func.ts @@ -0,0 +1,31 @@ +// Function. +import { isArray } from './is-array.func'; +import { isKey } from './is-key.func'; +import { isObject } from './is-object.func'; +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsObjectKey } from '../type/is-object-key.type'; +import { Key } from '../../type/key.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Checks if any `value` is an `object` with its own specified `key` of the `Key` type. + * @param value Any `value` to check if it contains a specified `key`. + * @param key A `Key` type or an array of `Key` type to check the `value`. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is an `object` with its own specified keys. + */ +export const isObjectKey: IsObjectKey = ( + value: any, + key: Key | Key[], + callback: ResultCallback = resultCallback +): value is Type => + callback( + isObject(value) ? + isArray(key) ? + key.every(k => isKey(k) ? ({}).hasOwnProperty.call(value, k) === true : false) + : isKey(key) ? + ({}).hasOwnProperty.call(value, key) + : false + : false + ); diff --git a/packages/type/src/is/lib/is-object.func.ts b/packages/type/src/is/lib/is-object.func.ts index e01b60e8..bffe391c 100644 --- a/packages/type/src/is/lib/is-object.func.ts +++ b/packages/type/src/is/lib/is-object.func.ts @@ -1,18 +1,18 @@ -import { isString } from './is-string.func'; -import { IsObject } from '../type/is-object.type'; +// Function. +import { isKey } from './is-key.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsObject } from '../type/is-object.type'; +import { Key } from '../../type/key.type'; /** - * Checks if any `value` is a generic `Obj` `'object'` type and `Object` instance with the possibility of containing `key`. - * Use the `guardObject()` function to type-guard generic object type also. - * @param value Any `value` to check if it's a generic `'object'` type and `Object` instance. - * @param [key] Property name to find in argument `value`. - * @returns boolean + * Checks if any `value` is an `object` of a generic `Obj` type and `Object` instance with the possibility of containing the `key`. + * @param value Any `value` to check. + * @param key Property name to find in the `value`. + * @returns A `boolean` indicating whether or not the `value` is an `object`. */ -export const isObject: IsObject = (value: any, key?: string): value is Obj => - typeOf(value) === 'object' && - typeof value === 'object' && - value instanceof Object === true - ? isString(key) +export const isObject: IsObject = (value: any, key?: Key): value is Obj => + (typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true) + ? isKey(key) ? key in value : true : false; diff --git a/packages/type/src/is/lib/is-param.decorator.ts b/packages/type/src/is/lib/is-param.decorator.ts new file mode 100644 index 00000000..620b0fa2 --- /dev/null +++ b/packages/type/src/is/lib/is-param.decorator.ts @@ -0,0 +1,40 @@ +import { Func } from '../../type/func.type'; +import { is } from './is.object'; + +export function isParam(...param: Array): MethodDecorator { + return (target: Func | object, key: string | symbol, descriptor: any): any => { + const originalMethod = descriptor.value; + + descriptor.value = function(): void { + if (is.array(param) && is.defined(arguments)) { + param.forEach((name: string, index: number) => { + if (is.number(index) && index < arguments.length) { + if (is.defined(arguments[index])) { + switch (name) { + case 'number': + if (is.number(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + case 'object': + if (is.object(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + case 'string': + if (is.string(arguments[index]) === false) { + arguments[index] = undefined; + } + break; + } + } + } + }); + } + const result = originalMethod.apply(this, arguments); + return result; + }; + + return descriptor; + }; +} diff --git a/packages/type/src/is/lib/is-primitive.func.ts b/packages/type/src/is/lib/is-primitive.func.ts index 58b81b46..13a81f2c 100644 --- a/packages/type/src/is/lib/is-primitive.func.ts +++ b/packages/type/src/is/lib/is-primitive.func.ts @@ -1,26 +1,39 @@ -import { IsPrimitive } from '../type/is-primitive.type'; -import { Primitives } from '../../type/primitives.type'; -import { isString } from './is-string.func'; +// Function. import { isBigInt } from './is-big-int.func'; import { isBoolean } from './is-boolean.func'; +import { isNull } from './is-null.func'; import { isNumber } from './is-number.func'; +import { isString } from './is-string.func'; import { isSymbol } from './is-symbol.func'; import { isUndefined } from './is-undefined.func'; +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsPrimitive } from '../type/is-primitive.type'; +import { Primitive } from '../../type/primitive.type'; +import { Primitives } from '../../type/primitives.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is a generic type from one of the `Primitives`. - * Use the `guardPrimitive()` function to type-guard generic type also. - * @param value Any `value` to check if it's a generic type from the argument `type`. - * @param type One of the `Primitives` `'bigint'`, `'boolean'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` type to check `value`. + * Checks if any `value` is the `Primitive` type from a `type` of the `Primitives` type. + * @param value Any `value` to check if it's a `Primitive` from the `type`. + * @param type A `string` type from the `Primitives` to check the `value`. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `type` from the `Primitives`. */ -export const isPrimitive: IsPrimitive = (value: any, type: Primitives): value is Type => { +export const isPrimitive: IsPrimitive = ( + value: any, + type: Primitives, + callback: ResultCallback = resultCallback +): value is T => { if (isString(type)) { switch (type) { - case 'bigint': return isBigInt(value); - case 'boolean': return isBoolean(value); - case 'number': return isNumber(value); - case 'string': return isString(value); - case 'symbol': return isSymbol(value); - case 'undefined': return isUndefined(value); + case 'bigint': return isBigInt(value, callback); + case 'boolean': return isBoolean(value, callback); + case 'number': return isNumber(value, callback); + case 'null': return isNull(value, callback); + case 'string': return isString(value, callback); + case 'symbol': return isSymbol(value, callback); + case 'undefined': return isUndefined(value, callback); } } return false; diff --git a/packages/type/src/is/lib/is-string-object.func.ts b/packages/type/src/is/lib/is-string-object.func.ts new file mode 100644 index 00000000..21519df3 --- /dev/null +++ b/packages/type/src/is/lib/is-string-object.func.ts @@ -0,0 +1,14 @@ +// Function. +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsStringObject } from '../type/is-string-object.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Checks if any `value` is an `object` type instance of `String` and `Object`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `String` instance. + */ +export const isStringObject: IsStringObject = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(value instanceof Object === true && value instanceof String === true && typeof value === 'object'); diff --git a/packages/type/src/is/lib/is-string-type.func.ts b/packages/type/src/is/lib/is-string-type.func.ts new file mode 100644 index 00000000..d6237968 --- /dev/null +++ b/packages/type/src/is/lib/is-string-type.func.ts @@ -0,0 +1,14 @@ +// Function. +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsStringType } from '../type/is-string-type.type'; +import { ResultCallback } from '../../type/result-callback.type'; +/** + * Checks if any `value` is a `string` type not instance of `String` and `Object`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the value is a `string`. + */ +export const isStringType: IsStringType = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(value instanceof Object === false && value instanceof String === false && typeof value === 'string'); diff --git a/packages/type/src/is/lib/is-string.func.ts b/packages/type/src/is/lib/is-string.func.ts index 8e30cb00..0b01886e 100644 --- a/packages/type/src/is/lib/is-string.func.ts +++ b/packages/type/src/is/lib/is-string.func.ts @@ -1,25 +1,17 @@ -import { IsString } from '../type/is-string.type'; +// Function. +import { isStringObject } from './is-string-object.func'; +import { isStringType } from './is-string-type.func'; +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsString } from '../type/is-string.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is a `'string'` type, not instance of `Object` and `String` - * or `'object'` type and instance of `String` and `Object`. - * Use the`guardString()` function to type-guard `string` also. - * @param value Any value to check if it's a `'string'` type, not an instance of `Object` and `String` - * or `'object'` type and instance of `String` and `Object`. - * @returns boolean. + * Checks if any `value` is a `string` type, not instance of `Object` and `String` or `object` type and instance of `String` and `Object`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `string`. */ -export const isString: IsString = (value: any): value is string => - typeOf(value) === 'string' && - ( - ( - typeof value === 'object' && - value instanceof Object === true && - value instanceof String === true - ) - || - ( - value instanceof Object === false && - value instanceof String === false && - typeof value === 'string' - ) - ); +export const isString: IsString = (value: any, callback: ResultCallback = resultCallback): value is string => + callback(typeOf(value) === 'string' && (isStringType(value) || isStringObject(value))); diff --git a/packages/type/src/is/lib/is-symbol.func.ts b/packages/type/src/is/lib/is-symbol.func.ts index 2c62e472..17431da8 100644 --- a/packages/type/src/is/lib/is-symbol.func.ts +++ b/packages/type/src/is/lib/is-symbol.func.ts @@ -1,11 +1,15 @@ -import { IsSymbol } from '../type/is-symbol.type'; +// Function. +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsSymbol } from '../type/is-symbol.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is a `'symbol'` type. - * Use the`guardSymbol()` function to type-guard `symbol` also. - * @param value Any `value` to check if it's a `'symbol'` type. - * @returns boolean. + * Checks if any `value` is a `symbol` type. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is a `symbol`. */ -export const isSymbol: IsSymbol = (value: any): value is symbol => - typeOf(value) === 'symbol' && - typeof value === 'symbol'; +export const isSymbol: IsSymbol = (value: any, callback: ResultCallback = resultCallback): value is symbol => + callback(typeOf(value) === 'symbol' && typeof value === 'symbol'); diff --git a/packages/type/src/is/lib/is-type.func.ts b/packages/type/src/is/lib/is-type.func.ts index b11bd469..13488945 100644 --- a/packages/type/src/is/lib/is-type.func.ts +++ b/packages/type/src/is/lib/is-type.func.ts @@ -1,32 +1,42 @@ -import { IsType } from '../type/is-type.type'; -import { Types } from '../../type/types.type'; -import { isString } from './is-string.func'; -import { isBigInt } from './is-big-int.func'; -import { isBoolean } from './is-boolean.func'; -import { isNumber } from './is-number.func'; -import { isSymbol } from './is-symbol.func'; -import { isUndefined } from './is-undefined.func'; -import { isObject } from './is-object.func'; +// Function. import { isFunction } from './is-function.func'; import { isInstance } from './is-instance.func'; +import { isNotNull } from '../not/lib/is-not-null.func'; +import { isObject } from './is-object.func'; +import { isPrimitive } from './is-primitive.func'; +import { isString } from './is-string.func'; +import { resultCallback } from '../../lib/result-callback.func'; +// Type. +import { IsType } from '../type/is-type.type'; +import { ResultCallback } from '../../type/result-callback.type'; +import { Type } from '../../type/type.type'; +import { Types } from '../../type/types.type'; /** - * Checks if any `value` is a generic `Type` type constructor, `'function'`, `'object'` or primitive type. - * Use the `guardType()` to type-guard generic `Type` type also. - * @param value Any value to check if it's a generic `Type` type from one of the `type`. - * @param type Generic constructor `Type`, `'function'`, `'object'` or one of the `Primitives` `'bigint'`, `'boolean'`, `'number'`, `'string'`, `'symbol'`, `'undefined'` to check `value` type. + * Checks if any `value` is the `Type` from a `type` of the `Types` type. + * @param value Any `value` to check if its type is from the `type`. + * @param type A `string` or generic `Constructor` type from the `Types` to check the `value`. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is the `Type` from a `type` of the `Types`. */ -export const isType: IsType = (value: any, type: Types): value is Type => { +export const isType: IsType = (value: any, type: Types, callback: ResultCallback = resultCallback): value is T => { if (isString(type)) { switch (type) { - case 'bigint': return isBigInt(value); - case 'boolean': return isBoolean(value); - case 'function': return isFunction(value); - case 'number': return isNumber(value); - case 'object': return isObject(value); - case 'string': return isString(value); - case 'symbol': return isSymbol(value); - case 'undefined': return isUndefined(value); + // Primitives. + case 'bigint': + case 'boolean': + case 'number': + case 'null' : + case 'string': + case 'symbol': + case 'undefined': return isPrimitive(value, type, callback); + // Function. + case 'function': return isFunction(value, callback); + // Object. + case 'object': return isObject(value); } + } else if (isNotNull(type)) { + return isInstance(value, type, callback); } - return type ? isInstance(value, type) : false; + return false; }; diff --git a/packages/type/src/is/lib/is-undefined.func.ts b/packages/type/src/is/lib/is-undefined.func.ts index 3034a90d..81921742 100644 --- a/packages/type/src/is/lib/is-undefined.func.ts +++ b/packages/type/src/is/lib/is-undefined.func.ts @@ -1,11 +1,15 @@ -import { IsUndefined } from '../type/is-undefined.type'; +// Function. +import { resultCallback } from '../../lib/result-callback.func'; import { typeOf } from '../../lib/type-of.func'; +// Type. +import { IsUndefined } from '../type/is-undefined.type'; +import { ResultCallback } from '../../type/result-callback.type'; /** - * Checks if any `value` is an `'undefined'` type and equal to `undefined`. - * @param value Any `value` to check if it's an `'undefined'` type and equal to `undefined`. - * @returns boolean. + * Checks if any `value` is an `undefined` type and equal to `undefined`. + * @param value Any `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is `undefined`. */ -export const isUndefined: IsUndefined = (value: any): value is undefined => - typeOf(value) === 'undefined' && - typeof value === 'undefined' && - value === undefined; +export const isUndefined: IsUndefined = (value: any, callback: ResultCallback = resultCallback): value is undefined => + callback(typeOf(value) === 'undefined' && typeof value === 'undefined' && value === undefined); diff --git a/packages/type/src/is/lib/is.object.ts b/packages/type/src/is/lib/is.object.ts index d3cc099a..3d20a7c9 100644 --- a/packages/type/src/is/lib/is.object.ts +++ b/packages/type/src/is/lib/is.object.ts @@ -1,36 +1,53 @@ -// Interface. -import { Is } from '../interface/is.interface'; -// Functions. +// Function. import { isArray } from './is-array.func'; import { isBigInt } from './is-big-int.func'; import { isBoolean } from './is-boolean.func'; +import { isBooleanObject } from './is-boolean-object.func'; +import { isBooleanType } from './is-boolean-type.func'; import { isDefined } from './is-defined.func'; import { isFunction } from './is-function.func'; import { isInstance } from './is-instance.func'; +import { isKey } from './is-key.func'; import { isNull } from './is-null.func'; import { isNumber } from './is-number.func'; +import { isNumberObject } from './is-number-object.func'; +import { isNumberType } from './is-number-type.func'; import { isObject } from './is-object.func'; +import { isObjectKey } from './is-object-key.func'; import { isPrimitive } from './is-primitive.func'; import { isString } from './is-string.func'; +import { isStringObject } from './is-string-object.func'; +import { isStringType } from './is-string-type.func'; import { isSymbol } from './is-symbol.func'; import { isType } from './is-type.func'; import { isUndefined } from './is-undefined.func'; -// Objects. +// Object. import { isNot } from '../not/lib/is-not.object'; - +// Interface. +import { Is } from '../interface/is.interface'; +// `is`. export const is: Is = { array: isArray, - bigInt: isBigInt, + bigInt: isBigInt, // deprecated + bigint: isBigInt, boolean: isBoolean, + booleanObject: isBooleanObject, + booleanType: isBooleanType, defined: isDefined, function: isFunction, instance: isInstance, + key: isKey, not: isNot, null: isNull, number: isNumber, + numberObject: isNumberObject, + numberType: isNumberType, object: isObject, + objectKey: isObjectKey, primitive: isPrimitive, string: isString, + stringObject: isStringObject, + stringType: isStringType, symbol: isSymbol, type: isType, undefined: isUndefined diff --git a/packages/type/src/is/not/lib/is-not-boolean.func.ts b/packages/type/src/is/not/lib/is-not-boolean.func.ts index d4b86a2b..f90d0a07 100644 --- a/packages/type/src/is/not/lib/is-not-boolean.func.ts +++ b/packages/type/src/is/not/lib/is-not-boolean.func.ts @@ -1,15 +1,21 @@ +// Function. +import { resultCallback } from '../../../lib/result-callback.func'; +import { typeOf } from '../../../lib/type-of.func'; // Type. import { IsNotBoolean } from '../type/is-not-boolean.type'; -import { typeOf } from '../../../lib/type-of.func'; +import { ResultCallback } from '../../../type/result-callback.type'; /** - * Checks if an unknown `value` is NOT a `'boolean'` type, NOT equal to `true` or `false` and NOT instance of `Boolean`. - * @param value An unknown `value` to check if it's NOT a `'boolean'` type, - * NOT equal to `true` or `false` and NOT instance of `Boolean`. - * @returns boolean. + * Checks if an unknown `value` is NOT a `boolean` type, NOT equal to `true` or `false` and NOT instance of `Boolean`. + * @param value An unknown `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is not `boolean`. */ -export const isNotBoolean: IsNotBoolean = (value: unknown): boolean => - typeOf(value) !== 'boolean' && - typeof value !== 'boolean' && - value instanceof Boolean === false && - value !== true && - value !== false; +export const isNotBoolean: IsNotBoolean = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback( + typeOf(value) !== 'boolean' && + typeof value !== 'boolean' && + value instanceof Boolean === false && + value !== true && + value !== false + ); diff --git a/packages/type/src/is/not/lib/is-not-defined.func.ts b/packages/type/src/is/not/lib/is-not-defined.func.ts index 2a4c047d..4015349d 100644 --- a/packages/type/src/is/not/lib/is-not-defined.func.ts +++ b/packages/type/src/is/not/lib/is-not-defined.func.ts @@ -1,13 +1,15 @@ +// Function. +import { resultCallback } from '../../../lib/result-callback.func'; +import { typeOf } from '../../../lib/type-of.func'; // Type. import { IsNotDefined } from '../type/is-not-defined.type'; -import { typeOf } from '../../../lib/type-of.func'; - +import { ResultCallback } from '../../../type/result-callback.type'; /** - * Checks if an unknown `value` is a `'undefined'` type and is equal to `undefined`. - * @param value An unknown `value` to check if it's an `'undefined'` type and is equal to `undefined`. - * @returns boolean. + * Checks if an unknown `value` is an `undefined` type and is equal to `undefined`. + * @param value An unknown `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is not defined. */ -export const isNotDefined: IsNotDefined = (value: unknown): boolean => - typeOf(value) === 'undefined' && - typeof value === 'undefined' && - value === undefined; +export const isNotDefined: IsNotDefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) === 'undefined' && typeof value === 'undefined' && value === undefined); diff --git a/packages/type/src/is/not/lib/is-not-function.func.ts b/packages/type/src/is/not/lib/is-not-function.func.ts index 6cd92485..1e7878f5 100644 --- a/packages/type/src/is/not/lib/is-not-function.func.ts +++ b/packages/type/src/is/not/lib/is-not-function.func.ts @@ -1,11 +1,15 @@ -import { IsNotFunction } from '../type/is-not-function.type'; +// Function. +import { resultCallback } from '../../../lib/result-callback.func'; import { typeOf } from '../../../lib/type-of.func'; +// Type. +import { IsNotFunction } from '../type/is-not-function.type'; +import { ResultCallback } from '../../../type/result-callback.type'; /** - * Checks if an unknown `value` is NOT a `'function'` type and NOT an instance of `Function`. - * @param value An unknown `value` to check if it's NOT a `'function'` type and NOT an instance of `Function`. - * @returns boolean. + * Checks if an unknown `value` is not a `function` type and not an instance of `Function`. + * @param value An unknown `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is not a `function`. */ -export const isNotFunction: IsNotFunction = (value: unknown): boolean => - typeOf(value) !== 'function' && - typeof value !== 'function' && - value instanceof Function === false; +export const isNotFunction: IsNotFunction = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'function' && typeof value !== 'function' && value instanceof Function === false); diff --git a/packages/type/src/is/not/lib/is-not-null.func.ts b/packages/type/src/is/not/lib/is-not-null.func.ts index 49713731..e3c7bff5 100644 --- a/packages/type/src/is/not/lib/is-not-null.func.ts +++ b/packages/type/src/is/not/lib/is-not-null.func.ts @@ -1,10 +1,15 @@ -import { IsNotNull } from '../type/is-not-null.type'; +// Function. +import { resultCallback } from '../../../lib/result-callback.func'; import { typeOf } from '../../../lib/type-of.func'; +// Type. +import { IsNotNull } from '../type/is-not-null.type'; +import { ResultCallback } from '../../../type/result-callback.type'; /** - * Checks if an unknown `value` is NOT a `'null'` type and NOT equal to `null`. - * @param value An unknown `value` to check if it's NOT a `'null'` type and NOT equal to `null`. - * @returns boolean + * Checks if an unknown `value` is not a `null` type and not equal to `null`. + * @param value An unknown `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is not `null`. */ -export const isNotNull: IsNotNull = (value: unknown): boolean => - typeOf(value) !== 'null' && - value !== null; +export const isNotNull: IsNotNull = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'null' && value !== null); diff --git a/packages/type/src/is/not/lib/is-not-number.func.ts b/packages/type/src/is/not/lib/is-not-number.func.ts index b668f67d..3dea9829 100644 --- a/packages/type/src/is/not/lib/is-not-number.func.ts +++ b/packages/type/src/is/not/lib/is-not-number.func.ts @@ -1,12 +1,20 @@ -import { IsNotNumber } from '../type/is-not-number.type'; +// Function. +import { resultCallback } from '../../../lib/result-callback.func'; import { typeOf } from '../../../lib/type-of.func'; +// Type. +import { IsNotNumber } from '../type/is-not-number.type'; +import { ResultCallback } from '../../../type/result-callback.type'; + /** - * Checks if an unknown `value` is NOT a `'number'` type and NOT an instance of `Number`. - * @param value An unknown value to check if it's NOT a `'number'` type, and NOT an instance of `Number`. - * @returns boolean + * Checks if any `value` is not a `number` type and not an instance of `Number`. + * @param value An unknown `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is not a `number`. */ -export const isNotNumber: IsNotNumber = (value: any): boolean => - typeOf(value) !== 'number' && - typeof value !== 'number' && - isFinite(value) === false && - value instanceof Number === false; +export const isNotNumber: IsNotNumber = (value: any, callback: ResultCallback = resultCallback): boolean => + callback( + typeOf(value) !== 'number' && + typeof value !== 'number' && + value instanceof Number === false + ); diff --git a/packages/type/src/is/not/lib/is-not-string.func.ts b/packages/type/src/is/not/lib/is-not-string.func.ts index ec32371e..1408912a 100644 --- a/packages/type/src/is/not/lib/is-not-string.func.ts +++ b/packages/type/src/is/not/lib/is-not-string.func.ts @@ -1,11 +1,15 @@ -import { IsNotString } from '../type/is-not-string.type'; +// Function. +import { resultCallback } from '../../../lib/result-callback.func'; import { typeOf } from '../../../lib/type-of.func'; +// Type. +import { IsNotString } from '../type/is-not-string.type'; +import { ResultCallback } from '../../../type/result-callback.type'; /** - * Checks if an unknown `value` is NOT a `'string'` type and NOT an instance of `String`. - * @param value An unknown `value` to check if it's NOT a `'string'` type and NOT an instance of `String`. - * @returns boolean. + * Checks if an unknown `value` is not a `string` type and not an instance of `String`. + * @param value An unknown `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is not a `string`. */ -export const isNotString: IsNotString = (value: unknown): boolean => - typeOf(value) !== 'string' && - typeof value !== 'string' && - value instanceof String === false; +export const isNotString: IsNotString = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'string' && typeof value !== 'string' && value instanceof String === false); diff --git a/packages/type/src/is/not/lib/is-not-undefined.func.ts b/packages/type/src/is/not/lib/is-not-undefined.func.ts index 7333c595..afe5744c 100644 --- a/packages/type/src/is/not/lib/is-not-undefined.func.ts +++ b/packages/type/src/is/not/lib/is-not-undefined.func.ts @@ -1,11 +1,15 @@ -import { IsNotUndefined } from '../type/is-not-undefined.type'; +// Function. +import { resultCallback } from '../../../lib/result-callback.func'; import { typeOf } from '../../../lib/type-of.func'; +// Type. +import { IsNotUndefined } from '../type/is-not-undefined.type'; +import { ResultCallback } from '../../../type/result-callback.type'; /** - * Checks if an unknown `value` is NOT an `'undefined'` type and NOT equal to `undefined`. - * @param value An Unknown `value` to check if it's NOT an `'undefined'` type and NOT equal to `undefined`. - * @returns boolean. + * Checks if an unknown `value` is NOT an `undefined` type and NOT equal to `undefined`. + * @param value An unknown `value` to check. + * @param callback `ResultCallback` function to handle result before returns. + * @callback `resultCallback`. + * @returns A `boolean` indicating whether or not the `value` is not `undefined`. */ -export const isNotUndefined: IsNotUndefined = (value: unknown): boolean => - typeOf(value) !== 'undefined' && - typeof value !== 'undefined' && - value !== undefined; +export const isNotUndefined: IsNotUndefined = (value: unknown, callback: ResultCallback = resultCallback): boolean => + callback(typeOf(value) !== 'undefined' && typeof value !== 'undefined' && value !== undefined); diff --git a/packages/type/src/is/not/lib/is-not.object.ts b/packages/type/src/is/not/lib/is-not.object.ts index 5e23a269..71856213 100644 --- a/packages/type/src/is/not/lib/is-not.object.ts +++ b/packages/type/src/is/not/lib/is-not.object.ts @@ -1,11 +1,11 @@ import { IsNot } from '../interface/is-not.interface'; import { isNotBoolean } from './is-not-boolean.func'; -import { isNotNumber } from './is-not-number.func'; -import { isNotUndefined } from './is-not-undefined.func'; +import { isNotDefined } from './is-not-defined.func'; +import { isNotFunction } from './is-not-function.func'; import { isNotNull } from './is-not-null.func'; +import { isNotNumber } from './is-not-number.func'; import { isNotString } from './is-not-string.func'; -import { isNotFunction } from './is-not-function.func'; -import { isNotDefined } from './is-not-defined.func'; +import { isNotUndefined } from './is-not-undefined.func'; export const isNot: IsNot = { // TODO array: isArray, diff --git a/packages/type/src/is/not/test/is-not-boolean.spec.ts b/packages/type/src/is/not/test/is-not-boolean.spec.ts index 9fe71a7d..6e2b4678 100644 --- a/packages/type/src/is/not/test/is-not-boolean.spec.ts +++ b/packages/type/src/is/not/test/is-not-boolean.spec.ts @@ -1,17 +1,17 @@ // Function. import { isNotBoolean } from '../lib/is-not-boolean.func'; // Variables. -import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from '../../test/variables/boolean.const'; import { BIGINT, BIGINT_INSTANCE } from '../../test/variables/big-int.const'; import { Class, CLASS } from '../../test/variables/class.const'; -import { notDefined } from '../../test/variables.const'; -import { UNDEFINED } from '../../test/variables/undefined.const'; -import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../test/variables/symbol.const'; -import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../../test/variables/string.const'; -import { OBJECT_ONE, OBJECT_TWO } from '../../test/variables/object.const'; -import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../test/variables/number.const'; -import { NULL } from '../../test/variables/null.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from '../../test/variables/boolean.const'; import { FUNCTION } from '../../test/variables/function.const'; +import { NULL } from '../../test/variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../test/variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO } from '../../test/variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../../test/variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../test/variables/symbol.const'; +import { UNDEFINED } from '../../test/variables/undefined.const'; +import { notDefined } from '../../test/variables/not-defined.const'; describe('isNotBoolean', () => { // TRUE diff --git a/packages/type/src/is/not/test/is-not-function.spec.ts b/packages/type/src/is/not/test/is-not-function.spec.ts new file mode 100644 index 00000000..da454d8f --- /dev/null +++ b/packages/type/src/is/not/test/is-not-function.spec.ts @@ -0,0 +1,91 @@ +// Function. +import { isNotFunction } from '../lib/is-not-function.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from '../../test/variables/big-int.const'; +import { Class, CLASS } from '../../test/variables/class.const'; +import { FUNCTION } from '../../test/variables/function.const'; +import { NULL } from '../../test/variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../test/variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from '../../test/variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../../test/variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../test/variables/symbol.const'; +import { TRUE, TRUE_EXPECTATION, FALSE, TRUE_INSTANCE, FALSE_EXPECTATION, FALSE_INSTANCE } from '../../test/variables/boolean.const'; +import { UNDEFINED } from '../../test/variables/undefined.const'; +/** + * Checks + * ✓ typeOf() = 'function + * ✓ typeof !== 'function' + * ✓ instanceof Function === false + */ +describe(`isNotFunction`, () => { + // Defined. + it('is DEFINED', () => expect(isNotFunction).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isNotFunction(STRING, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isNotFunction(FUNCTION, 'function')).toBe(TRUE)); + // it(`${Class}`, () => expect(isNotFunction(Class, 'function')).toBe(TRUE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isNotFunction(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isNotFunction(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isNotFunction(CLASS)).toBe(TRUE)); + it(`OBJECT_ONE`, () => expect(isNotFunction(OBJECT_ONE)).toBe(TRUE)); + it(`OBJECT_TWO`, () => expect(isNotFunction(OBJECT_TWO)).toBe(TRUE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isNotFunction(OBJECT_ONE_NEW)).toBe(TRUE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isNotFunction(OBJECT_TWO_NEW)).toBe(TRUE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isNotFunction(BIGINT)).toBe(TRUE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isNotFunction(BIGINT_INSTANCE)).toBe(TRUE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isNotFunction(TRUE)).toBe(TRUE)); + it(`${FALSE}`, () => expect(isNotFunction(FALSE)).toBe(TRUE)); + it(`${TRUE_EXPECTATION}`, () => expect(isNotFunction(TRUE_INSTANCE)).toBe(TRUE)); + it(`${FALSE_EXPECTATION}`, () => expect(isNotFunction(FALSE_INSTANCE)).toBe(TRUE)); + }); + + // null + it(`${NULL}`, () => expect(isNotFunction(NULL)).toBe(TRUE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isNotFunction(NUMBER)).toBe(TRUE)); + it(`Number(${NUMBER})`, () => expect(isNotFunction(NUMBER_INSTANCE)).toBe(TRUE)); + it(`new Number(${NUMBER})`, () => expect(isNotFunction(NUMBER_NEW_INSTANCE)).toBe(TRUE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isNotFunction(STRING)).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isNotFunction(STRING_INSTANCE)).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isNotFunction(STRING_NEW_INSTANCE)).toBe(TRUE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isNotFunction(SYMBOL_NUMBER)).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isNotFunction(SYMBOL_STRING)).toBe(TRUE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isNotFunction(UNDEFINED)).toBe(TRUE)); + }); + }); +}); diff --git a/packages/type/src/is/not/test/is-not-null.spec.ts b/packages/type/src/is/not/test/is-not-null.spec.ts new file mode 100644 index 00000000..0770a75c --- /dev/null +++ b/packages/type/src/is/not/test/is-not-null.spec.ts @@ -0,0 +1,90 @@ +// Function. +import { isNotNull } from '../lib/is-not-null.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from '../../test/variables/big-int.const'; +import { Class, CLASS } from '../../test/variables/class.const'; +import { FUNCTION } from '../../test/variables/function.const'; +import { NULL } from '../../test/variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../test/variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from '../../test/variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../../test/variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../test/variables/symbol.const'; +import { TRUE, TRUE_EXPECTATION, FALSE, TRUE_INSTANCE, FALSE_EXPECTATION, FALSE_INSTANCE } from '../../test/variables/boolean.const'; +import { UNDEFINED } from '../../test/variables/undefined.const'; +/** + * Checks + * ✓ typeOf() = 'null + * ✓ value !== null + */ +describe(`isNotNull`, () => { + // Defined. + it('is DEFINED', () => expect(isNotNull).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isNotNull(STRING, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isNotNull(FUNCTION, 'function')).toBe(TRUE)); + // it(`${Class}`, () => expect(isNotNull(Class, 'function')).toBe(TRUE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isNotNull(FUNCTION)).toBe(TRUE)); + it(`Class`, () => expect(isNotNull(Class)).toBe(TRUE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isNotNull(CLASS)).toBe(TRUE)); + it(`OBJECT_ONE`, () => expect(isNotNull(OBJECT_ONE)).toBe(TRUE)); + it(`OBJECT_TWO`, () => expect(isNotNull(OBJECT_TWO)).toBe(TRUE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isNotNull(OBJECT_ONE_NEW)).toBe(TRUE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isNotNull(OBJECT_TWO_NEW)).toBe(TRUE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isNotNull(BIGINT)).toBe(TRUE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isNotNull(BIGINT_INSTANCE)).toBe(TRUE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isNotNull(TRUE)).toBe(TRUE)); + it(`${FALSE}`, () => expect(isNotNull(FALSE)).toBe(TRUE)); + it(`${TRUE_EXPECTATION}`, () => expect(isNotNull(TRUE_INSTANCE)).toBe(TRUE)); + it(`${FALSE_EXPECTATION}`, () => expect(isNotNull(FALSE_INSTANCE)).toBe(TRUE)); + }); + + // null + it(`${NULL}`, () => expect(isNotNull(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isNotNull(NUMBER)).toBe(TRUE)); + it(`Number(${NUMBER})`, () => expect(isNotNull(NUMBER_INSTANCE)).toBe(TRUE)); + it(`new Number(${NUMBER})`, () => expect(isNotNull(NUMBER_NEW_INSTANCE)).toBe(TRUE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isNotNull(STRING)).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isNotNull(STRING_INSTANCE)).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isNotNull(STRING_NEW_INSTANCE)).toBe(TRUE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isNotNull(SYMBOL_NUMBER)).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isNotNull(SYMBOL_STRING)).toBe(TRUE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isNotNull(UNDEFINED)).toBe(TRUE)); + }); + }); +}); diff --git a/packages/type/src/is/not/test/is-not-number.spec.ts b/packages/type/src/is/not/test/is-not-number.spec.ts new file mode 100644 index 00000000..754e9b89 --- /dev/null +++ b/packages/type/src/is/not/test/is-not-number.spec.ts @@ -0,0 +1,91 @@ +// Function. +import { isNotNumber } from '../lib/is-not-number.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from '../../test/variables/big-int.const'; +import { Class, CLASS } from '../../test/variables/class.const'; +import { FUNCTION } from '../../test/variables/function.const'; +import { NULL } from '../../test/variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../test/variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from '../../test/variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../../test/variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../test/variables/symbol.const'; +import { TRUE, TRUE_EXPECTATION, FALSE, TRUE_INSTANCE, FALSE_INSTANCE, FALSE_EXPECTATION } from '../../test/variables/boolean.const'; +import { UNDEFINED } from '../../test/variables/undefined.const'; +/** + * Checks + * ✓ typeOf() = 'number + * ✓ typeof !== 'number' + * ✓ instanceof Number === false + */ +describe(`isNotNumber`, () => { + // Defined. + it('is DEFINED', () => expect(isNotNumber).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isNotNumber(STRING, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isNotNumber(FUNCTION, 'function')).toBe(TRUE)); + // it(`${Class}`, () => expect(isNotNumber(Class, 'function')).toBe(TRUE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isNotNumber(FUNCTION)).toBe(TRUE)); + it(`Class`, () => expect(isNotNumber(Class)).toBe(TRUE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isNotNumber(CLASS)).toBe(TRUE)); + it(`OBJECT_ONE`, () => expect(isNotNumber(OBJECT_ONE)).toBe(TRUE)); + it(`OBJECT_TWO`, () => expect(isNotNumber(OBJECT_TWO)).toBe(TRUE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isNotNumber(OBJECT_ONE_NEW)).toBe(TRUE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isNotNumber(OBJECT_TWO_NEW)).toBe(TRUE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isNotNumber(BIGINT)).toBe(TRUE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isNotNumber(BIGINT_INSTANCE)).toBe(TRUE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isNotNumber(TRUE)).toBe(TRUE)); + it(`${FALSE}`, () => expect(isNotNumber(FALSE)).toBe(TRUE)); + it(`${TRUE_EXPECTATION}`, () => expect(isNotNumber(TRUE_INSTANCE)).toBe(TRUE)); + it(`${FALSE_EXPECTATION}`, () => expect(isNotNumber(FALSE_INSTANCE)).toBe(TRUE)); + }); + + // null + it(`${NULL}`, () => expect(isNotNumber(NULL)).toBe(TRUE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isNotNumber(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isNotNumber(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isNotNumber(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isNotNumber(STRING)).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isNotNumber(STRING_INSTANCE)).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isNotNumber(STRING_NEW_INSTANCE)).toBe(TRUE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isNotNumber(SYMBOL_NUMBER)).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isNotNumber(SYMBOL_STRING)).toBe(TRUE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isNotNumber(UNDEFINED)).toBe(TRUE)); + }); + }); +}); diff --git a/packages/type/src/is/not/test/is-not-string.spec.ts b/packages/type/src/is/not/test/is-not-string.spec.ts new file mode 100644 index 00000000..fadfb2d6 --- /dev/null +++ b/packages/type/src/is/not/test/is-not-string.spec.ts @@ -0,0 +1,91 @@ +// Function. +import { isNotString } from '../lib/is-not-string.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from '../../test/variables/big-int.const'; +import { Class, CLASS } from '../../test/variables/class.const'; +import { FUNCTION } from '../../test/variables/function.const'; +import { NULL } from '../../test/variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../test/variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from '../../test/variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../../test/variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../test/variables/symbol.const'; +import { TRUE, TRUE_EXPECTATION, FALSE, TRUE_INSTANCE, FALSE_EXPECTATION, FALSE_INSTANCE } from '../../test/variables/boolean.const'; +import { UNDEFINED } from '../../test/variables/undefined.const'; +/** + * Checks + * ✓ typeOf() = 'string + * ✓ typeof !== 'string' + * ✓ instanceof String === false + */ +describe(`isNotString`, () => { + // Defined. + it('is DEFINED', () => expect(isNotString).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isNotString(STRING, (result: boolean) => { + expect(result).toBe(FALSE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isNotString(FUNCTION, 'function')).toBe(TRUE)); + // it(`${Class}`, () => expect(isNotString(Class, 'function')).toBe(TRUE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isNotString(FUNCTION)).toBe(TRUE)); + it(`Class`, () => expect(isNotString(Class)).toBe(TRUE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isNotString(CLASS)).toBe(TRUE)); + it(`OBJECT_ONE`, () => expect(isNotString(OBJECT_ONE)).toBe(TRUE)); + it(`OBJECT_TWO`, () => expect(isNotString(OBJECT_TWO)).toBe(TRUE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isNotString(OBJECT_ONE_NEW)).toBe(TRUE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isNotString(OBJECT_TWO_NEW)).toBe(TRUE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isNotString(BIGINT)).toBe(TRUE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isNotString(BIGINT_INSTANCE)).toBe(TRUE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isNotString(TRUE)).toBe(TRUE)); + it(`${FALSE}`, () => expect(isNotString(FALSE)).toBe(TRUE)); + it(`${TRUE_EXPECTATION}`, () => expect(isNotString(TRUE_INSTANCE)).toBe(TRUE)); + it(`${FALSE_EXPECTATION}`, () => expect(isNotString(FALSE_INSTANCE)).toBe(TRUE)); + }); + + // null + it(`${NULL}`, () => expect(isNotString(NULL)).toBe(TRUE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isNotString(NUMBER)).toBe(TRUE)); + it(`Number(${NUMBER})`, () => expect(isNotString(NUMBER_INSTANCE)).toBe(TRUE)); + it(`new Number(${NUMBER})`, () => expect(isNotString(NUMBER_NEW_INSTANCE)).toBe(TRUE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isNotString(STRING)).toBe(FALSE)); + it(`String(${STRING})`, () => expect(isNotString(STRING_INSTANCE)).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(isNotString(STRING_NEW_INSTANCE)).toBe(FALSE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isNotString(SYMBOL_NUMBER)).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isNotString(SYMBOL_STRING)).toBe(TRUE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isNotString(UNDEFINED)).toBe(TRUE)); + }); + }); +}); diff --git a/packages/type/src/is/not/test/is-not-undefined.spec.ts b/packages/type/src/is/not/test/is-not-undefined.spec.ts new file mode 100644 index 00000000..d9753290 --- /dev/null +++ b/packages/type/src/is/not/test/is-not-undefined.spec.ts @@ -0,0 +1,91 @@ +// Function. +import { isNotUndefined } from '../lib/is-not-undefined.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from '../../test/variables/big-int.const'; +import { Class, CLASS } from '../../test/variables/class.const'; +import { FUNCTION } from '../../test/variables/function.const'; +import { NULL } from '../../test/variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../../test/variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from '../../test/variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../../test/variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from '../../test/variables/symbol.const'; +import { TRUE, TRUE_EXPECTATION, FALSE, TRUE_INSTANCE } from '../../test/variables/boolean.const'; +import { UNDEFINED } from '../../test/variables/undefined.const'; +/** + * Checks + * ✓ typeOf() = 'undefined + * ✓ typeof === 'undefined' + * ✓ value === undefined + */ +describe(`isNotUndefined`, () => { + // Defined. + it('is DEFINED', () => expect(isNotUndefined).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isNotUndefined(STRING, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isNotUndefined(FUNCTION, 'function')).toBe(TRUE)); + // it(`${Class}`, () => expect(isNotUndefined(Class, 'function')).toBe(TRUE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isNotUndefined(FUNCTION)).toBe(TRUE)); + it(`Class`, () => expect(isNotUndefined(Class)).toBe(TRUE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isNotUndefined(CLASS)).toBe(TRUE)); + it(`OBJECT_ONE`, () => expect(isNotUndefined(OBJECT_ONE)).toBe(TRUE)); + it(`OBJECT_TWO`, () => expect(isNotUndefined(OBJECT_TWO)).toBe(TRUE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isNotUndefined(OBJECT_ONE_NEW)).toBe(TRUE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isNotUndefined(OBJECT_TWO_NEW)).toBe(TRUE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isNotUndefined(BIGINT)).toBe(TRUE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isNotUndefined(BIGINT_INSTANCE)).toBe(TRUE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isNotUndefined(TRUE)).toBe(TRUE)); + it(`${FALSE}`, () => expect(isNotUndefined(FALSE)).toBe(TRUE)); + it(`${TRUE_EXPECTATION}`, () => expect(isNotUndefined(TRUE_INSTANCE)).toBe(TRUE)); + it(`${TRUE_EXPECTATION}`, () => expect(isNotUndefined(TRUE_INSTANCE)).toBe(TRUE)); + }); + + // null + it(`${NULL}`, () => expect(isNotUndefined(NULL)).toBe(TRUE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isNotUndefined(NUMBER)).toBe(TRUE)); + it(`Number(${NUMBER})`, () => expect(isNotUndefined(NUMBER_INSTANCE)).toBe(TRUE)); + it(`new Number(${NUMBER})`, () => expect(isNotUndefined(NUMBER_NEW_INSTANCE)).toBe(TRUE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isNotUndefined(STRING)).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isNotUndefined(STRING_INSTANCE)).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isNotUndefined(STRING_NEW_INSTANCE)).toBe(TRUE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isNotUndefined(SYMBOL_NUMBER)).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isNotUndefined(SYMBOL_STRING)).toBe(TRUE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isNotUndefined(UNDEFINED)).toBe(FALSE)); + }); + }); +}); diff --git a/packages/type/src/is/not/test/is-not.spec.ts b/packages/type/src/is/not/test/is-not.spec.ts new file mode 100644 index 00000000..8c57dab8 --- /dev/null +++ b/packages/type/src/is/not/test/is-not.spec.ts @@ -0,0 +1,28 @@ +import { isNot } from '../lib/is-not.object'; + +describe('`isNot`', () => { + describe('DEFINED', () => { + it('isNot', () => expect(isNot).toBeDefined()); + // it('isNot.array()', () => expect(isNot.boolean).toBeDefined()); + it('isNot.boolean()', () => expect(isNot.boolean).toBeDefined()); + // it('isNot.booleanObject()', () => expect(isNot.booleanObject).toBeDefined()); + // it('isNot.booleanType()', () => expect(isNot.booleanType).toBeDefined()); + it('isNot.defined()', () => expect(isNot.defined).toBeDefined()); + it('isNot.function()', () => expect(isNot.function).toBeDefined()); + // it('isNot.instance()', () => expect(isNot.instance).toBeDefined()); + // it('isNot.key()', () => expect(isNot.key).toBeDefined()); + it('isNot.null()', () => expect(isNot.null).toBeDefined()); + it('isNot.number()', () => expect(isNot.number).toBeDefined()); + // it('isNot.numberObject()', () => expect(isNot.numberObject).toBeDefined()); + // it('isNot.numberType()', () => expect(isNot.numberType).toBeDefined()); + // it('isNot.object()', () => expect(isNot.object).toBeDefined()); + // it('isNot.objectKey()', () => expect(isNot.objectKey).toBeDefined()); + // it('isNot.primitive()', () => expect(isNot.primitive).toBeDefined()); + it('isNot.string()', () => expect(isNot.string).toBeDefined()); + // it('isNot.stringObject()', () => expect(isNot.stringObject).toBeDefined()); + // it('isNot.stringType()', () => expect(isNot.stringType).toBeDefined()); + // it('isNot.symbol()', () => expect(isNot.symbol).toBeDefined()); + // it('isNot.type()', () => expect(isNot.type).toBeDefined()); + it('isNot.undefined()', () => expect(isNot.undefined).toBeDefined()); + }); +}); diff --git a/packages/type/src/is/not/type/is-not-boolean.type.ts b/packages/type/src/is/not/type/is-not-boolean.type.ts index 6689561a..47e3b15d 100644 --- a/packages/type/src/is/not/type/is-not-boolean.type.ts +++ b/packages/type/src/is/not/type/is-not-boolean.type.ts @@ -1 +1,2 @@ -export type IsNotBoolean = (value: unknown) => boolean; +import { ResultCallback } from '../../../type/result-callback.type'; +export type IsNotBoolean = (value: unknown, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/is/not/type/is-not-defined.type.ts b/packages/type/src/is/not/type/is-not-defined.type.ts index ea742cb2..f2dbea98 100644 --- a/packages/type/src/is/not/type/is-not-defined.type.ts +++ b/packages/type/src/is/not/type/is-not-defined.type.ts @@ -1 +1,2 @@ -export type IsNotDefined = (value: unknown) => boolean; +import { ResultCallback } from '../../../type/result-callback.type'; +export type IsNotDefined = (value: unknown, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/is/not/type/is-not-function.type.ts b/packages/type/src/is/not/type/is-not-function.type.ts index 886ab0ee..1e351720 100644 --- a/packages/type/src/is/not/type/is-not-function.type.ts +++ b/packages/type/src/is/not/type/is-not-function.type.ts @@ -1 +1,2 @@ -export type IsNotFunction = (value: unknown) => boolean; +import { ResultCallback } from '../../../type/result-callback.type'; +export type IsNotFunction = (value: unknown, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/is/not/type/is-not-null.type.ts b/packages/type/src/is/not/type/is-not-null.type.ts index 0a3a5d63..9aad4260 100644 --- a/packages/type/src/is/not/type/is-not-null.type.ts +++ b/packages/type/src/is/not/type/is-not-null.type.ts @@ -1 +1,2 @@ -export type IsNotNull = (value: unknown) => boolean; +import { ResultCallback } from '../../../type/result-callback.type'; +export type IsNotNull = (value: unknown, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/is/not/type/is-not-number.type.ts b/packages/type/src/is/not/type/is-not-number.type.ts index 36cec6b3..f8756ec4 100644 --- a/packages/type/src/is/not/type/is-not-number.type.ts +++ b/packages/type/src/is/not/type/is-not-number.type.ts @@ -1 +1,2 @@ -export type IsNotNumber = (value: any) => boolean; +import { ResultCallback } from '../../../type/result-callback.type'; +export type IsNotNumber = (value: any, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/is/not/type/is-not-string.type.ts b/packages/type/src/is/not/type/is-not-string.type.ts index f7f9bea1..a90f76a3 100644 --- a/packages/type/src/is/not/type/is-not-string.type.ts +++ b/packages/type/src/is/not/type/is-not-string.type.ts @@ -1 +1,2 @@ -export type IsNotString = (value: unknown) => boolean; +import { ResultCallback } from '../../../type/result-callback.type'; +export type IsNotString = (value: unknown, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/is/not/type/is-not-undefined.type.ts b/packages/type/src/is/not/type/is-not-undefined.type.ts index 1351db2e..105bc872 100644 --- a/packages/type/src/is/not/type/is-not-undefined.type.ts +++ b/packages/type/src/is/not/type/is-not-undefined.type.ts @@ -1 +1,2 @@ -export type IsNotUndefined = (value: unknown) => boolean; +import { ResultCallback } from '../../../type/result-callback.type'; +export type IsNotUndefined = (value: unknown, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/is/test/is-array.spec.ts b/packages/type/src/is/test/is-array.spec.ts index 9bd2322e..d8372eae 100644 --- a/packages/type/src/is/test/is-array.spec.ts +++ b/packages/type/src/is/test/is-array.spec.ts @@ -1,14 +1,15 @@ - -import { isArray } from '../lib/is-array.func'; -import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; -import { FUNCTION } from './variables/function.const'; +import { Class } from './variables/class.const'; import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { Func } from '../../type/func.type'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo } from './variables/object.const'; import { STRING, STRING_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; -import { notDefined } from './variables.const'; import { UNDEFINED } from './variables/undefined.const'; +import { isArray } from '../lib/is-array.func'; +import { notDefined } from './variables/not-defined.const'; import { ARRAY_BIGINT, ARRAY_BOOLEAN, @@ -19,69 +20,63 @@ import { ARRAY_OBJECT_ONE, ARRAY_OBJECT_TWO, ARRAY_STRING, - ARRAY_SYMBOL_STRING, ARRAY_SYMBOL_NUMBER, + ARRAY_SYMBOL_STRING, ARRAY_UNDEFINED } from './variables/array.const'; -import { Class } from './variables/class.const'; -import { Func } from '../../type/func.type'; describe('isArray', () => { // TRUE - it('is DEFINED', () => { - expect(isArray).toBeDefined(); - }); - it('Array', () => expect(isArray(ARRAY_BIGINT)).toBeTruthy()); - it('Array', () => expect(isArray(ARRAY_BOOLEAN)).toBeTruthy()); - it('Array', () => expect(isArray(ARRAY_CLASS)).toBeTruthy()); - it('Array', () => expect(isArray(ARRAY_FUNCTION)).toBeTruthy()); - it('Array', () => expect(isArray(ARRAY_NULL)).toBeTruthy()); - it('Array', () => expect(isArray(ARRAY_NUMBER)).toBeTruthy()); + it('is DEFINED', () => expect(isArray).toBeDefined()); + it('Array', () => expect(isArray(ARRAY_BIGINT)).toBe(TRUE)); + it('Array', () => expect(isArray(ARRAY_BOOLEAN)).toBe(TRUE)); + it('Array', () => expect(isArray(ARRAY_CLASS)).toBe(TRUE)); + it('Array', () => expect(isArray(ARRAY_FUNCTION)).toBe(TRUE)); + it('Array', () => expect(isArray(ARRAY_NULL)).toBe(TRUE)); + it('Array', () => expect(isArray(ARRAY_NUMBER)).toBe(TRUE)); it('Array Array', () => { - expect(isArray(ARRAY_OBJECT_ONE)).toBeTruthy(); - expect(isArray(ARRAY_OBJECT_TWO)).toBeTruthy(); + expect(isArray(ARRAY_OBJECT_ONE)).toBe(TRUE); + expect(isArray(ARRAY_OBJECT_TWO)).toBe(TRUE); }); - it('Array', () => expect(isArray(ARRAY_STRING)).toBeTruthy()); + it('Array', () => expect(isArray(ARRAY_STRING)).toBe(TRUE)); it('Array', () => { - expect(isArray(ARRAY_SYMBOL_STRING)).toBeTruthy(); - expect(isArray(ARRAY_SYMBOL_NUMBER)).toBeTruthy(); + expect(isArray(ARRAY_SYMBOL_STRING)).toBe(TRUE); + expect(isArray(ARRAY_SYMBOL_NUMBER)).toBe(TRUE); }); - it('Array', () => expect(isArray(ARRAY_UNDEFINED)).toBeTruthy()); + it('Array', () => expect(isArray(ARRAY_UNDEFINED)).toBe(TRUE)); // FALSE it(`'bigint'`, () => { - expect(isArray(BIGINT)).toBeFalsy(); - expect(isArray(BIGINT_INSTANCE)).toBeFalsy(); + expect(isArray(BIGINT)).toBe(FALSE); + expect(isArray(BIGINT_INSTANCE)).toBe(FALSE); }); it(`'boolean' | Boolean`, () => { - expect(isArray(FALSE)).toBeFalsy(); - expect(isArray(TRUE)).toBeFalsy(); - expect(isArray(FALSE_INSTANCE)).toBeFalsy(); - expect(isArray(TRUE_INSTANCE)).toBeFalsy(); - }); - it(`'function' | Function`, () => { - expect(isArray(FUNCTION)).toBeFalsy(); + expect(isArray(FALSE)).toBe(FALSE); + expect(isArray(TRUE)).toBe(FALSE); + expect(isArray(FALSE_INSTANCE)).toBe(FALSE); + expect(isArray(TRUE_INSTANCE)).toBe(FALSE); }); + it(`'function' | Function`, () => expect(isArray(FUNCTION)).toBe(FALSE)); it(`'number' | Number`, () => { - expect(isArray(NUMBER)).toBeFalsy(); - expect(isArray(NUMBER_INSTANCE)).toBeFalsy(); - expect(isArray(NUMBER_NEW_INSTANCE)).toBeFalsy(); + expect(isArray(NUMBER)).toBe(FALSE); + expect(isArray(NUMBER_INSTANCE)).toBe(FALSE); + expect(isArray(NUMBER_NEW_INSTANCE)).toBe(FALSE); }); it(`'object' | Object`, () => { - expect(isArray(OBJECT_ONE)).toBeFalsy(); - expect(isArray(OBJECT_TWO)).toBeFalsy(); + expect(isArray(OBJECT_ONE)).toBe(FALSE); + expect(isArray(OBJECT_TWO)).toBe(FALSE); }); it(`'string' | String`, () => { - expect(isArray(STRING)).toBeFalsy(); - expect(isArray(STRING_INSTANCE)).toBeFalsy(); + expect(isArray(STRING)).toBe(FALSE); + expect(isArray(STRING_INSTANCE)).toBe(FALSE); }); it(`'symbol'`, () => { - expect(isArray(SYMBOL_NUMBER)).toBeFalsy(); - expect(isArray(SYMBOL_STRING)).toBeFalsy(); + expect(isArray(SYMBOL_NUMBER)).toBe(FALSE); + expect(isArray(SYMBOL_STRING)).toBe(FALSE); }); it(`'undefined'`, () => { - expect(isArray(notDefined)).toBeFalse(); - expect(isArray(UNDEFINED)).toBeFalse(); + expect(isArray(notDefined)).toBe(FALSE); + expect(isArray(UNDEFINED)).toBe(FALSE); }); }); diff --git a/packages/type/src/is/test/is-big-int.spec.ts b/packages/type/src/is/test/is-big-int.spec.ts index 1fca731a..a5319488 100644 --- a/packages/type/src/is/test/is-big-int.spec.ts +++ b/packages/type/src/is/test/is-big-int.spec.ts @@ -1,68 +1,73 @@ -/** - * Checks - * ✓ typeof === 'bigint' - */ -import { isBigInt } from '../lib/is-big-int.func'; -import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; +import { CLASS, Class } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; +import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; -import { CLASS, Class } from './variables/class.const'; -import { NULL } from './variables/null.const'; - +import { isBigInt } from '../lib/is-big-int.func'; +import { notDefined } from './variables/not-defined.const'; +/** + * Checks + * ✓ typeof === 'bigint' + */ describe('isBigInt', () => { // TRUE it('is DEFINED', () => { expect(isBigInt).toBeDefined(); }); it(`'bigint'`, () => { - expect(isBigInt(BIGINT)).toBeTruthy(); - expect(isBigInt(BIGINT_INSTANCE)).toBeTruthy(); + expect(isBigInt(BIGINT)).toBe(TRUE); + expect(isBigInt(BIGINT_INSTANCE)).toBe(TRUE); + isBigInt(BIGINT_INSTANCE, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); }); // FALSE it(`'boolean' | Boolean`, () => { - expect(isBigInt(FALSE)).toBeFalsy(); - expect(isBigInt(TRUE)).toBeFalsy(); - expect(isBigInt(FALSE_INSTANCE)).toBeFalsy(); - expect(isBigInt(TRUE_INSTANCE)).toBeFalsy(); + expect(isBigInt(FALSE)).toBe(FALSE); + expect(isBigInt(TRUE)).toBe(FALSE); + expect(isBigInt(FALSE_INSTANCE)).toBe(FALSE); + expect(isBigInt(TRUE_INSTANCE)).toBe(FALSE); + isBigInt(FALSE, (result: boolean) => { + expect(result).toBe(FALSE); + return result; + }); }); it(`Class | CLASS`, () => { - expect(isBigInt(Class)).toBeFalsy(); - expect(isBigInt(CLASS)).toBeFalsy(); - }); - it(`'function' | Function`, () => { - expect(isBigInt(FUNCTION)).toBeFalsy(); + expect(isBigInt(Class)).toBe(FALSE); + expect(isBigInt(CLASS)).toBe(FALSE); }); + it(`'function' | Function`, () => expect(isBigInt(FUNCTION)).toBe(FALSE)); it(`null | NULL`, () => { - expect(isBigInt(null)).toBeFalsy(); - expect(isBigInt(NULL)).toBeFalsy(); + expect(isBigInt(null)).toBe(FALSE); + expect(isBigInt(NULL)).toBe(FALSE); }); it(`'number' | Number`, () => { - expect(isBigInt(NUMBER)).toBeFalsy(); - expect(isBigInt(NUMBER_INSTANCE)).toBeFalsy(); - expect(isBigInt(NUMBER_NEW_INSTANCE)).toBeFalsy(); + expect(isBigInt(NUMBER)).toBe(FALSE); + expect(isBigInt(NUMBER_INSTANCE)).toBe(FALSE); + expect(isBigInt(NUMBER_NEW_INSTANCE)).toBe(FALSE); }); it(`'object' | Object`, () => { - expect(isBigInt(OBJECT_ONE)).toBeFalsy(); - expect(isBigInt(OBJECT_TWO)).toBeFalsy(); + expect(isBigInt(OBJECT_ONE)).toBe(FALSE); + expect(isBigInt(OBJECT_TWO)).toBe(FALSE); }); it(`'string' | String`, () => { - expect(isBigInt(STRING)).toBeFalsy(); - expect(isBigInt(STRING_INSTANCE)).toBeFalsy(); - expect(isBigInt(STRING_NEW_INSTANCE)).toBeFalsy(); + expect(isBigInt(STRING)).toBe(FALSE); + expect(isBigInt(STRING_INSTANCE)).toBe(FALSE); + expect(isBigInt(STRING_NEW_INSTANCE)).toBe(FALSE); }); it(`'symbol'`, () => { - expect(isBigInt(SYMBOL_NUMBER)).toBeFalsy(); - expect(isBigInt(SYMBOL_STRING)).toBeFalsy(); + expect(isBigInt(SYMBOL_NUMBER)).toBe(FALSE); + expect(isBigInt(SYMBOL_STRING)).toBe(FALSE); }); it(`'undefined'`, () => { - expect(isBigInt(notDefined)).toBeFalse(); - expect(isBigInt(UNDEFINED)).toBeFalse(); + expect(isBigInt(notDefined)).toBe(FALSE); + expect(isBigInt(UNDEFINED)).toBe(FALSE); }); }); diff --git a/packages/type/src/is/test/is-boolean-object.spec.ts b/packages/type/src/is/test/is-boolean-object.spec.ts new file mode 100644 index 00000000..9b120e1b --- /dev/null +++ b/packages/type/src/is/test/is-boolean-object.spec.ts @@ -0,0 +1,71 @@ +// Function. +import { isBooleanObject } from '../lib/is-boolean-object.func'; +// Variables. +import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; +import { notDefined } from './variables/not-defined.const'; +/** + * Checks + * ✓ typeof === 'object' + * ✓ instanceof Object + * ✓ instanceof Boolean + */ +describe('isBooleanObject', () => { + // TRUE + it('is DEFINED', () => expect(isBooleanObject).toBeDefined()); + it(`Boolean`, () => { + expect(isBooleanObject(FALSE_INSTANCE)).toBe(TRUE); + expect(isBooleanObject(TRUE_INSTANCE)).toBe(TRUE); + }); + + // FALSE + it(`boolean`, () => { + expect(isBooleanObject(FALSE)).toBe(FALSE); + expect(isBooleanObject(TRUE)).toBe(FALSE); + expect(isBooleanObject(Boolean(false))).toBe(FALSE); + expect(isBooleanObject(Boolean(true))).toBe(FALSE); + }); + it(`'bigint'`, () => { + expect(isBooleanObject(BIGINT)).toBe(FALSE); + expect(isBooleanObject(BIGINT_INSTANCE)).toBe(FALSE); + }); + it(`Class | CLASS`, () => { + expect(isBooleanObject(Class)).toBe(FALSE); + expect(isBooleanObject(CLASS)).toBe(FALSE); + }); + it(`'function' | Function`, () => expect(isBooleanObject(FUNCTION)).toBe(FALSE)); + it(`null | NULL`, () => { + expect(isBooleanObject(null)).toBe(FALSE); + expect(isBooleanObject(NULL)).toBe(FALSE); + }); + it(`'number' | Number`, () => { + expect(isBooleanObject(NUMBER)).toBe(FALSE); + expect(isBooleanObject(NUMBER_INSTANCE)).toBe(FALSE); + expect(isBooleanObject(NUMBER_NEW_INSTANCE)).toBe(FALSE); + }); + it(`'object' | Object`, () => { + expect(isBooleanObject(OBJECT_ONE)).toBe(FALSE); + expect(isBooleanObject(OBJECT_TWO)).toBe(FALSE); + }); + it(`'string' | String`, () => { + expect(isBooleanObject(STRING)).toBe(FALSE); + expect(isBooleanObject(STRING_INSTANCE)).toBe(FALSE); + expect(isBooleanObject(STRING_NEW_INSTANCE)).toBe(FALSE); + }); + it(`'symbol'`, () => { + expect(isBooleanObject(SYMBOL_NUMBER)).toBe(FALSE); + expect(isBooleanObject(SYMBOL_STRING)).toBe(FALSE); + }); + it(`'undefined'`, () => { + expect(isBooleanObject(notDefined)).toBe(FALSE); + expect(isBooleanObject(UNDEFINED)).toBe(FALSE); + }); +}); diff --git a/packages/type/src/is/test/is-boolean-type.spec.ts b/packages/type/src/is/test/is-boolean-type.spec.ts new file mode 100644 index 00000000..44e459da --- /dev/null +++ b/packages/type/src/is/test/is-boolean-type.spec.ts @@ -0,0 +1,71 @@ +// Function. +import { isBooleanType } from '../lib/is-boolean-type.func'; +// Variables. +import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; +import { notDefined } from './variables/not-defined.const'; +/** + * Checks + * ✓ typeof === 'boolean' + * ✓ value === true + * ✓ value === false + */ +describe('isBooleanType', () => { + // TRUE + it('is DEFINED', () => expect(isBooleanType).toBeDefined()); + it(`boolean`, () => { + expect(isBooleanType(FALSE)).toBe(TRUE); + expect(isBooleanType(TRUE)).toBe(TRUE); + expect(isBooleanType(Boolean(false))).toBe(TRUE); + expect(isBooleanType(Boolean(true))).toBe(TRUE); + }); + + // FALSE + it(`Boolean`, () => { + expect(isBooleanType(FALSE_INSTANCE)).toBe(FALSE); + expect(isBooleanType(TRUE_INSTANCE)).toBe(FALSE); + }); + it(`'bigint'`, () => { + expect(isBooleanType(BIGINT)).toBe(FALSE); + expect(isBooleanType(BIGINT_INSTANCE)).toBe(FALSE); + }); + it(`Class | CLASS`, () => { + expect(isBooleanType(Class)).toBe(FALSE); + expect(isBooleanType(CLASS)).toBe(FALSE); + }); + it(`'function' | Function`, () => expect(isBooleanType(FUNCTION)).toBe(FALSE)); + it(`null | NULL`, () => { + expect(isBooleanType(null)).toBe(FALSE); + expect(isBooleanType(NULL)).toBe(FALSE); + }); + it(`'number' | Number`, () => { + expect(isBooleanType(NUMBER)).toBe(FALSE); + expect(isBooleanType(NUMBER_INSTANCE)).toBe(FALSE); + expect(isBooleanType(NUMBER_NEW_INSTANCE)).toBe(FALSE); + }); + it(`'object' | Object`, () => { + expect(isBooleanType(OBJECT_ONE)).toBe(FALSE); + expect(isBooleanType(OBJECT_TWO)).toBe(FALSE); + }); + it(`'string' | String`, () => { + expect(isBooleanType(STRING)).toBe(FALSE); + expect(isBooleanType(STRING_INSTANCE)).toBe(FALSE); + expect(isBooleanType(STRING_NEW_INSTANCE)).toBe(FALSE); + }); + it(`'symbol'`, () => { + expect(isBooleanType(SYMBOL_NUMBER)).toBe(FALSE); + expect(isBooleanType(SYMBOL_STRING)).toBe(FALSE); + }); + it(`'undefined'`, () => { + expect(isBooleanType(notDefined)).toBe(FALSE); + expect(isBooleanType(UNDEFINED)).toBe(FALSE); + }); +}); diff --git a/packages/type/src/is/test/is-boolean.spec.ts b/packages/type/src/is/test/is-boolean.spec.ts index b7d55008..2d556260 100644 --- a/packages/type/src/is/test/is-boolean.spec.ts +++ b/packages/type/src/is/test/is-boolean.spec.ts @@ -1,72 +1,76 @@ -/** - * Checks - * ✓ typeof === 'boolean' - * ✓ instanceof Boolean - */ - +// Function. import { isBoolean } from '../lib/is-boolean.func'; -import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; +// Variables. import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; - +import { notDefined } from './variables/not-defined.const'; +/** + * Checks + * ✓ typeof === 'boolean' + * ✓ instanceof Boolean + */ describe('isBoolean', () => { // TRUE - it('is DEFINED', () => { - expect(isBoolean).toBeDefined(); - }); + it('is DEFINED', () => expect(isBoolean).toBeDefined()); it(`'boolean' | Boolean`, () => { - expect(isBoolean(FALSE)).toBeTruthy(); - expect(isBoolean(TRUE)).toBeTruthy(); - expect(isBoolean(FALSE_INSTANCE)).toBeTruthy(); - expect(isBoolean(TRUE_INSTANCE)).toBeTruthy(); - expect(isBoolean(Boolean(false))).toBeTruthy(); - expect(isBoolean(Boolean(true))).toBeTruthy(); + expect(isBoolean(FALSE)).toBe(TRUE); + expect(isBoolean(TRUE)).toBe(TRUE); + expect(isBoolean(FALSE_INSTANCE)).toBe(TRUE); + expect(isBoolean(TRUE_INSTANCE)).toBe(TRUE); + expect(isBoolean(Boolean(false))).toBe(TRUE); + expect(isBoolean(Boolean(true))).toBe(TRUE); + isBoolean(FALSE, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); }); // FALSE it(`'bigint'`, () => { - expect(isBoolean(BIGINT)).toBeFalsy(); - expect(isBoolean(BIGINT_INSTANCE)).toBeFalsy(); + expect(isBoolean(BIGINT)).toBe(FALSE); + expect(isBoolean(BIGINT_INSTANCE)).toBe(FALSE); + isBoolean(BIGINT, (result: boolean) => { + expect(result).toBe(FALSE); + return result; + }); }); it(`Class | CLASS`, () => { - expect(isBoolean(Class)).toBeFalsy(); - expect(isBoolean(CLASS)).toBeFalsy(); - }); - it(`'function' | Function`, () => { - expect(isBoolean(FUNCTION)).toBeFalsy(); + expect(isBoolean(Class)).toBe(FALSE); + expect(isBoolean(CLASS)).toBe(FALSE); }); + it(`'function' | Function`, () => expect(isBoolean(FUNCTION)).toBe(FALSE)); it(`null | NULL`, () => { - expect(isBoolean(null)).toBeFalsy(); - expect(isBoolean(NULL)).toBeFalsy(); + expect(isBoolean(null)).toBe(FALSE); + expect(isBoolean(NULL)).toBe(FALSE); }); it(`'number' | Number`, () => { - expect(isBoolean(NUMBER)).toBeFalsy(); - expect(isBoolean(NUMBER_INSTANCE)).toBeFalsy(); - expect(isBoolean(NUMBER_NEW_INSTANCE)).toBeFalsy(); + expect(isBoolean(NUMBER)).toBe(FALSE); + expect(isBoolean(NUMBER_INSTANCE)).toBe(FALSE); + expect(isBoolean(NUMBER_NEW_INSTANCE)).toBe(FALSE); }); it(`'object' | Object`, () => { - expect(isBoolean(OBJECT_ONE)).toBeFalsy(); - expect(isBoolean(OBJECT_TWO)).toBeFalsy(); + expect(isBoolean(OBJECT_ONE)).toBe(FALSE); + expect(isBoolean(OBJECT_TWO)).toBe(FALSE); }); it(`'string' | String`, () => { - expect(isBoolean(STRING)).toBeFalsy(); - expect(isBoolean(STRING_INSTANCE)).toBeFalsy(); - expect(isBoolean(STRING_NEW_INSTANCE)).toBeFalsy(); + expect(isBoolean(STRING)).toBe(FALSE); + expect(isBoolean(STRING_INSTANCE)).toBe(FALSE); + expect(isBoolean(STRING_NEW_INSTANCE)).toBe(FALSE); }); it(`'symbol'`, () => { - expect(isBoolean(SYMBOL_NUMBER)).toBeFalsy(); - expect(isBoolean(SYMBOL_STRING)).toBeFalsy(); + expect(isBoolean(SYMBOL_NUMBER)).toBe(FALSE); + expect(isBoolean(SYMBOL_STRING)).toBe(FALSE); }); it(`'undefined'`, () => { - expect(isBoolean(notDefined)).toBeFalse(); - expect(isBoolean(UNDEFINED)).toBeFalse(); + expect(isBoolean(notDefined)).toBe(FALSE); + expect(isBoolean(UNDEFINED)).toBe(FALSE); }); }); diff --git a/packages/type/src/is/test/is-defined.spec.ts b/packages/type/src/is/test/is-defined.spec.ts index 7b39bbd9..6ff0a2e4 100644 --- a/packages/type/src/is/test/is-defined.spec.ts +++ b/packages/type/src/is/test/is-defined.spec.ts @@ -1,74 +1,77 @@ -/** - * Checks - * ✓ typeof !== 'undefined' - * ✓ vale !== undefined - */ - // Function. import { isDefined } from '../lib/is-defined.func'; // Variables. -import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; +import { notDefined } from './variables/not-defined.const'; +/** + * Checks + * ✓ typeof !== 'undefined' + * ✓ vale !== undefined + */ describe('isDefined', () => { // TRUE - it('is DEFINED', () => { - expect(isDefined).toBeDefined(); - }); + it('is DEFINED', () => expect(isDefined).toBeDefined()); it(`'boolean' | Boolean`, () => { - expect(isDefined(FALSE)).toBeTruthy(); - expect(isDefined(TRUE)).toBeTruthy(); - expect(isDefined(FALSE_INSTANCE)).toBeTruthy(); - expect(isDefined(TRUE_INSTANCE)).toBeTruthy(); - expect(isDefined(Boolean(false))).toBeTruthy(); - expect(isDefined(Boolean(true))).toBeTruthy(); + expect(isDefined(FALSE)).toBe(TRUE); + expect(isDefined(TRUE)).toBe(TRUE); + expect(isDefined(FALSE_INSTANCE)).toBe(TRUE); + expect(isDefined(TRUE_INSTANCE)).toBe(TRUE); + expect(isDefined(Boolean(false))).toBe(TRUE); + expect(isDefined(Boolean(true))).toBe(TRUE); + isDefined(FALSE, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); }); it(`'bigint'`, () => { - expect(isDefined(BIGINT)).toBeTruthy(); - expect(isDefined(BIGINT_INSTANCE)).toBeTruthy(); + expect(isDefined(BIGINT)).toBe(TRUE); + expect(isDefined(BIGINT_INSTANCE)).toBe(TRUE); }); it(`Class | CLASS`, () => { - expect(isDefined(Class)).toBeTruthy(); - expect(isDefined(CLASS)).toBeTruthy(); - }); - it(`'function' | Function`, () => { - expect(isDefined(FUNCTION)).toBeTruthy(); + expect(isDefined(Class)).toBe(TRUE); + expect(isDefined(CLASS)).toBe(TRUE); }); + it(`'function' | Function`, () => expect(isDefined(FUNCTION)).toBe(TRUE)); it(`null | NULL`, () => { - expect(isDefined(null)).toBeTruthy(); - expect(isDefined(NULL)).toBeTruthy(); + expect(isDefined(null)).toBe(TRUE); + expect(isDefined(NULL)).toBe(TRUE); }); it(`'number' | Number`, () => { - expect(isDefined(NUMBER)).toBeTruthy(); - expect(isDefined(NUMBER_INSTANCE)).toBeTruthy(); - expect(isDefined(NUMBER_NEW_INSTANCE)).toBeTruthy(); + expect(isDefined(NUMBER)).toBe(TRUE); + expect(isDefined(NUMBER_INSTANCE)).toBe(TRUE); + expect(isDefined(NUMBER_NEW_INSTANCE)).toBe(TRUE); }); it(`'object' | Object`, () => { - expect(isDefined(OBJECT_ONE)).toBeTruthy(); - expect(isDefined(OBJECT_TWO)).toBeTruthy(); + expect(isDefined(OBJECT_ONE)).toBe(TRUE); + expect(isDefined(OBJECT_TWO)).toBe(TRUE); }); it(`'string' | String`, () => { - expect(isDefined(STRING)).toBeTruthy(); - expect(isDefined(STRING_INSTANCE)).toBeTruthy(); - expect(isDefined(STRING_NEW_INSTANCE)).toBeTruthy(); + expect(isDefined(STRING)).toBe(TRUE); + expect(isDefined(STRING_INSTANCE)).toBe(TRUE); + expect(isDefined(STRING_NEW_INSTANCE)).toBe(TRUE); }); it(`'symbol'`, () => { - expect(isDefined(SYMBOL_NUMBER)).toBeTruthy(); - expect(isDefined(SYMBOL_STRING)).toBeTruthy(); + expect(isDefined(SYMBOL_NUMBER)).toBe(TRUE); + expect(isDefined(SYMBOL_STRING)).toBe(TRUE); }); // FALSE it(`'undefined'`, () => { - expect(isDefined(notDefined)).toBeFalsy(); - expect(isDefined(UNDEFINED)).toBeFalsy(); + expect(isDefined(notDefined)).toBe(FALSE); + expect(isDefined(UNDEFINED)).toBe(FALSE); + isDefined(UNDEFINED, (result: boolean) => { + expect(result).toBe(FALSE); + return result; + }); }); }); diff --git a/packages/type/src/is/test/is-function.spec.ts b/packages/type/src/is/test/is-function.spec.ts index 7b7d7817..03866e05 100644 --- a/packages/type/src/is/test/is-function.spec.ts +++ b/packages/type/src/is/test/is-function.spec.ts @@ -1,70 +1,66 @@ // Function. import { isFunction } from '../lib/is-function.func'; // Variables. -import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; +import { notDefined } from './variables/not-defined.const'; describe('isFunction', () => { // TRUE - it('is DEFINED', () => { - expect(isFunction).toBeDefined(); - }); - it(`'function' | Function`, () => { - expect(isFunction(FUNCTION)).toBeTruthy(); - }); - it(`Class`, () => { - expect(isFunction(Class)).toBeTruthy(); - }); + it('is DEFINED', () => expect(isFunction).toBeDefined()); + it(`'function' | Function`, () => expect(isFunction(FUNCTION)).toBe(TRUE)); + it(`Class`, () => expect(isFunction(Class)).toBe(TRUE)); // FALSE. it(`'boolean' | Boolean`, () => { - expect(isFunction(FALSE)).toBeFalsy(); - expect(isFunction(TRUE)).toBeFalsy(); - expect(isFunction(FALSE_INSTANCE)).toBeFalsy(); - expect(isFunction(TRUE_INSTANCE)).toBeFalsy(); - expect(isFunction(Boolean(false))).toBeFalsy(); - expect(isFunction(Boolean(true))).toBeFalsy(); + expect(isFunction(FALSE)).toBe(FALSE); + expect(isFunction(TRUE)).toBe(FALSE); + expect(isFunction(FALSE_INSTANCE)).toBe(FALSE); + expect(isFunction(TRUE_INSTANCE)).toBe(FALSE); + expect(isFunction(Boolean(false))).toBe(FALSE); + expect(isFunction(Boolean(true))).toBe(FALSE); + isFunction(TRUE, (result: boolean) => { + expect(result).toBe(FALSE); + return result; + }); }); it(`'bigint'`, () => { - expect(isFunction(BIGINT)).toBeFalsy(); - expect(isFunction(BIGINT_INSTANCE)).toBeFalsy(); - }); - it(`CLASS`, () => { - expect(isFunction(CLASS)).toBeFalsy(); + expect(isFunction(BIGINT)).toBe(FALSE); + expect(isFunction(BIGINT_INSTANCE)).toBe(FALSE); }); + it(`CLASS`, () => expect(isFunction(CLASS)).toBe(FALSE)); it(`null | NULL`, () => { - expect(isFunction(null)).toBeFalsy(); - expect(isFunction(NULL)).toBeFalsy(); + expect(isFunction(null)).toBe(FALSE); + expect(isFunction(NULL)).toBe(FALSE); }); it(`'number' | Number`, () => { - expect(isFunction(NUMBER)).toBeFalsy(); - expect(isFunction(NUMBER_INSTANCE)).toBeFalsy(); - expect(isFunction(NUMBER_NEW_INSTANCE)).toBeFalsy(); + expect(isFunction(NUMBER)).toBe(FALSE); + expect(isFunction(NUMBER_INSTANCE)).toBe(FALSE); + expect(isFunction(NUMBER_NEW_INSTANCE)).toBe(FALSE); }); it(`'object' | Object`, () => { - expect(isFunction(OBJECT_ONE)).toBeFalsy(); - expect(isFunction(OBJECT_TWO)).toBeFalsy(); + expect(isFunction(OBJECT_ONE)).toBe(FALSE); + expect(isFunction(OBJECT_TWO)).toBe(FALSE); }); it(`'string' | String`, () => { - expect(isFunction(STRING)).toBeFalsy(); - expect(isFunction(STRING_INSTANCE)).toBeFalsy(); - expect(isFunction(STRING_NEW_INSTANCE)).toBeFalsy(); + expect(isFunction(STRING)).toBe(FALSE); + expect(isFunction(STRING_INSTANCE)).toBe(FALSE); + expect(isFunction(STRING_NEW_INSTANCE)).toBe(FALSE); }); it(`'symbol'`, () => { - expect(isFunction(SYMBOL_NUMBER)).toBeFalsy(); - expect(isFunction(SYMBOL_STRING)).toBeFalsy(); + expect(isFunction(SYMBOL_NUMBER)).toBe(FALSE); + expect(isFunction(SYMBOL_STRING)).toBe(FALSE); }); it(`'undefined'`, () => { - expect(isFunction(notDefined)).toBeFalsy(); - expect(isFunction(UNDEFINED)).toBeFalsy(); + expect(isFunction(notDefined)).toBe(FALSE); + expect(isFunction(UNDEFINED)).toBe(FALSE); }); }); diff --git a/packages/type/src/is/test/is-instance.spec.ts b/packages/type/src/is/test/is-instance.spec.ts index 3de85144..a8b5dbad 100644 --- a/packages/type/src/is/test/is-instance.spec.ts +++ b/packages/type/src/is/test/is-instance.spec.ts @@ -3,67 +3,62 @@ import { isInstance } from '../lib/is-instance.func'; // Variables. import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; -import { Class, CLASS } from './variables/class.const'; +import { Class, CLASS, PERSON, Person, PersonCopy, PERSON_COPY } from './variables/class.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; // import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; +import { notDefined } from './variables/not-defined.const'; describe('isInstance', () => { // TRUE. - it('is DEFINED', () => { - expect(isInstance).toBeDefined(); - }); - it(`CLASS`, () => { - expect(isInstance(CLASS, Class)).toBeTruthy(); + it('is DEFINED', () => expect(isInstance).toBeDefined()); + it(`CLASS instance of Class`, () => expect(isInstance(CLASS, Class)).toBe(TRUE)); + it(`PERSON instance of Person`, () => expect(isInstance(PERSON, Person)).toBe(TRUE)); + it(`PERSON_COPY instance of PersonCopy`, () => expect(isInstance(PERSON_COPY, PersonCopy)).toBe(TRUE)); + it(`CLASS instance of Class expect result to be true`, () => { + isInstance(CLASS, Class, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); }); // FALSE. + it(`PERSON_COPY not instance of Person`, () => expect(isInstance(PERSON_COPY, Person)).toBe(FALSE)); it(`'boolean' | Boolean`, () => { - expect(isInstance(FALSE, FALSE)).toBeFalse(); - expect(isInstance(TRUE, TRUE)).toBeFalse(); - expect(isInstance(FALSE_INSTANCE, FALSE_INSTANCE)).toBeFalse(); - expect(isInstance(TRUE_INSTANCE, TRUE_INSTANCE)).toBeFalse(); + expect(isInstance(FALSE, FALSE)).toBe(FALSE); + expect(isInstance(TRUE, TRUE)).toBe(FALSE); + expect(isInstance(FALSE_INSTANCE, FALSE_INSTANCE)).toBe(FALSE); + expect(isInstance(TRUE_INSTANCE, TRUE_INSTANCE)).toBe(FALSE); }); it(`'bigint'`, () => { - expect(isInstance(BIGINT, BIGINT)).toBeFalse(); - expect(isInstance(BIGINT_INSTANCE, BIGINT_INSTANCE)).toBeFalse(); - }); - it(`CLASS`, () => { - expect(isInstance(Class, Class)).toBeFalse(); - }); - it(`'function' | Function`, () => { - expect(isInstance(FUNCTION, Function)).toBeFalse(); - }); - it(`null | NULL`, () => { - expect(isInstance(NULL, NULL)).toBeFalse(); + expect(isInstance(BIGINT, BIGINT)).toBe(FALSE); + expect(isInstance(BIGINT_INSTANCE, BIGINT_INSTANCE)).toBe(FALSE); }); + it(`Class`, () => expect(isInstance(Class, Class)).toBe(FALSE)); + it(`FUNCTION instance of Function`, () => expect(isInstance(FUNCTION, Function)).toBe(FALSE)); + it(`NULL`, () => expect(isInstance(NULL, NULL)).toBe(FALSE)); it(`'number' | Number`, () => { - expect(isInstance(NUMBER, NUMBER)).toBeFalse(); - expect(isInstance(NUMBER_INSTANCE, NUMBER_INSTANCE)).toBeFalse(); - expect(isInstance(NUMBER_NEW_INSTANCE, NUMBER_NEW_INSTANCE)).toBeFalse(); + expect(isInstance(NUMBER, NUMBER)).toBe(FALSE); + expect(isInstance(NUMBER_INSTANCE, NUMBER_INSTANCE)).toBe(FALSE); + expect(isInstance(NUMBER_NEW_INSTANCE, NUMBER_NEW_INSTANCE)).toBe(FALSE); }); // it(`'object' | Object`, () => { - // expect(isInstance(OBJECT_ONE)).toBeFalse(); - // expect(isInstance(OBJECT_TWO, OBJECT_TWO)).toBeFalse(); + // expect(isInstance(OBJECT_ONE)).toBe(FALSE); + // expect(isInstance(OBJECT_TWO, OBJECT_TWO)).toBe(FALSE); // }); it(`'string' | String`, () => { - expect(isInstance(STRING, STRING)).toBeFalse(); - expect(isInstance(STRING_INSTANCE, STRING_INSTANCE)).toBeFalse(); - expect(isInstance(STRING_NEW_INSTANCE, STRING_NEW_INSTANCE)).toBeFalse(); - }); - it(`'symbol'`, () => { - expect(isInstance(SYMBOL_NUMBER, SYMBOL_NUMBER)).toBeFalse(); - expect(isInstance(SYMBOL_STRING, SYMBOL_STRING)).toBeFalse(); + expect(isInstance(STRING, STRING)).toBe(FALSE); + expect(isInstance(STRING_INSTANCE, STRING_INSTANCE)).toBe(FALSE); + expect(isInstance(STRING_NEW_INSTANCE, STRING_NEW_INSTANCE)).toBe(FALSE); }); // FALSE - it(`'undefined'`, () => { - expect(isInstance(notDefined, notDefined)).toBeFalse(); - expect(isInstance(UNDEFINED, UNDEFINED)).toBeFalse(); + it(`undefined`, () => { + expect(isInstance(notDefined, notDefined)).toBe(FALSE); + expect(isInstance(UNDEFINED, UNDEFINED)).toBe(FALSE); }); }); diff --git a/packages/type/src/is/test/is-key.spec.ts b/packages/type/src/is/test/is-key.spec.ts new file mode 100644 index 00000000..9e376ff1 --- /dev/null +++ b/packages/type/src/is/test/is-key.spec.ts @@ -0,0 +1,85 @@ +// Function. +import { isKey } from '../lib/is-key.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +describe(`isKey`, () => { + // Defined. + it('is DEFINED', () => expect(isKey).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isKey('test', (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isKey(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isKey(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isKey(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isKey(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isKey(CLASS)).toBe(FALSE)); + it(`OBJECT_ONE`, () => expect(isKey(OBJECT_ONE)).toBe(FALSE)); + it(`OBJECT_TWO`, () => expect(isKey(OBJECT_TWO)).toBe(FALSE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isKey(OBJECT_ONE_NEW)).toBe(FALSE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isKey(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isKey(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isKey(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isKey(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isKey(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isKey(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isKey(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isKey(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isKey(NUMBER)).toBe(TRUE)); + it(`Number(${NUMBER})`, () => expect(isKey(NUMBER_INSTANCE)).toBe(TRUE)); + it(`new Number(${NUMBER})`, () => expect(isKey(NUMBER_NEW_INSTANCE)).toBe(TRUE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isKey(STRING)).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isKey(STRING_INSTANCE)).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isKey(STRING_NEW_INSTANCE)).toBe(TRUE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isKey(SYMBOL_NUMBER)).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isKey(SYMBOL_STRING)).toBe(TRUE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isKey(UNDEFINED)).toBe(FALSE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-null.spec.ts b/packages/type/src/is/test/is-null.spec.ts index 4017e633..36c7ccfd 100644 --- a/packages/type/src/is/test/is-null.spec.ts +++ b/packages/type/src/is/test/is-null.spec.ts @@ -1,70 +1,69 @@ // Function. import { isNull } from '../lib/is-null.func'; - // Variables. import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; import { Class, CLASS } from './variables/class.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; +import { notDefined } from './variables/not-defined.const'; describe('isNull', () => { // TRUE - it('is DEFINED', () => { - expect(isNull).toBeDefined(); - }); + it('is DEFINED', () => expect(isNull).toBeDefined()); it(`null | NULL`, () => { - expect(isNull(null)).toBeTrue(); - expect(isNull(NULL)).toBeTrue(); + expect(isNull(null)).toBe(TRUE); + expect(isNull(NULL)).toBe(TRUE); + isNull(NULL, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); }); // FALSE it(`'bigint'`, () => { - expect(isNull(BIGINT)).toBeFalsy(); - expect(isNull(BIGINT_INSTANCE)).toBeFalsy(); + expect(isNull(BIGINT)).toBe(FALSE); + expect(isNull(BIGINT_INSTANCE)).toBe(FALSE); }); it(`'boolean' | Boolean`, () => { - expect(isNull(FALSE)).toBeFalse(); - expect(isNull(TRUE)).toBeFalse(); - expect(isNull(FALSE_INSTANCE)).toBeFalse(); - expect(isNull(TRUE_INSTANCE)).toBeFalse(); - expect(isNull(Boolean(false))).toBeFalse(); - expect(isNull(Boolean(true))).toBeFalse(); + expect(isNull(FALSE)).toBe(FALSE); + expect(isNull(TRUE)).toBe(FALSE); + expect(isNull(FALSE_INSTANCE)).toBe(FALSE); + expect(isNull(TRUE_INSTANCE)).toBe(FALSE); + expect(isNull(Boolean(false))).toBe(FALSE); + expect(isNull(Boolean(true))).toBe(FALSE); }); it(`Class | CLASS`, () => { - expect(isNull(Class)).toBeFalsy(); - expect(isNull(CLASS)).toBeFalsy(); - }); - it(`'function' | Function`, () => { - expect(isNull(FUNCTION)).toBeFalsy(); + expect(isNull(Class)).toBe(FALSE); + expect(isNull(CLASS)).toBe(FALSE); }); + it(`'function' | Function`, () => expect(isNull(FUNCTION)).toBe(FALSE)); it(`'number' | Number`, () => { - expect(isNull(NUMBER)).toBeFalsy(); - expect(isNull(NUMBER_INSTANCE)).toBeFalsy(); - expect(isNull(NUMBER_NEW_INSTANCE)).toBeFalsy(); + expect(isNull(NUMBER)).toBe(FALSE); + expect(isNull(NUMBER_INSTANCE)).toBe(FALSE); + expect(isNull(NUMBER_NEW_INSTANCE)).toBe(FALSE); }); it(`'object' | Object`, () => { - expect(isNull(OBJECT_ONE)).toBeFalsy(); - expect(isNull(OBJECT_TWO)).toBeFalsy(); + expect(isNull(OBJECT_ONE)).toBe(FALSE); + expect(isNull(OBJECT_TWO)).toBe(FALSE); }); it(`'string' | String`, () => { - expect(isNull(STRING)).toBeFalsy(); - expect(isNull(STRING_INSTANCE)).toBeFalsy(); - expect(isNull(STRING_NEW_INSTANCE)).toBeFalsy(); + expect(isNull(STRING)).toBe(FALSE); + expect(isNull(STRING_INSTANCE)).toBe(FALSE); + expect(isNull(STRING_NEW_INSTANCE)).toBe(FALSE); }); it(`'symbol'`, () => { - expect(isNull(SYMBOL_NUMBER)).toBeFalsy(); - expect(isNull(SYMBOL_STRING)).toBeFalsy(); + expect(isNull(SYMBOL_NUMBER)).toBe(FALSE); + expect(isNull(SYMBOL_STRING)).toBe(FALSE); }); it(`'undefined'`, () => { - expect(isNull(notDefined)).toBeFalse(); - expect(isNull(UNDEFINED)).toBeFalse(); + expect(isNull(notDefined)).toBe(FALSE); + expect(isNull(UNDEFINED)).toBe(FALSE); }); }); diff --git a/packages/type/src/is/test/is-number-object.spec.ts b/packages/type/src/is/test/is-number-object.spec.ts new file mode 100644 index 00000000..91317881 --- /dev/null +++ b/packages/type/src/is/test/is-number-object.spec.ts @@ -0,0 +1,89 @@ +// Function. +import { isNumberObject } from '../lib/is-number-object.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +/** + * Checks + * ✓ typeof === 'object' && instanceof Number === true && instanceof Object === true + */ +describe(`isNumberObject`, () => { + // Defined. + it('is DEFINED', () => expect(isNumberObject).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isNumberObject(NUMBER_NEW_INSTANCE, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isNumberObject(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isNumberObject(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isNumberObject(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isNumberObject(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isNumberObject(CLASS)).toBe(FALSE)); + it(`OBJECT_ONE`, () => expect(isNumberObject(OBJECT_ONE)).toBe(FALSE)); + it(`OBJECT_TWO`, () => expect(isNumberObject(OBJECT_TWO)).toBe(FALSE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isNumberObject(OBJECT_ONE_NEW)).toBe(FALSE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isNumberObject(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isNumberObject(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isNumberObject(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isNumberObject(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isNumberObject(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isNumberObject(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isNumberObject(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isNumberObject(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isNumberObject(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isNumberObject(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isNumberObject(NUMBER_NEW_INSTANCE)).toBe(TRUE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isNumberObject(STRING)).toBe(FALSE)); + it(`String(${STRING})`, () => expect(isNumberObject(STRING_INSTANCE)).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(isNumberObject(STRING_NEW_INSTANCE)).toBe(FALSE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isNumberObject(SYMBOL_NUMBER)).toBe(FALSE)); + it(`Symbol(${STRING})`, () => expect(isNumberObject(SYMBOL_STRING)).toBe(FALSE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isNumberObject(UNDEFINED)).toBe(FALSE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-number-type.spec.ts b/packages/type/src/is/test/is-number-type.spec.ts new file mode 100644 index 00000000..1bfb57c4 --- /dev/null +++ b/packages/type/src/is/test/is-number-type.spec.ts @@ -0,0 +1,91 @@ +// Function. +import { isNumberType } from '../lib/is-number-type.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +/** + * Checks + * ✓ typeof === 'number' && instanceof Number === false && instanceof Object === false + * ✓ value === true + * ✓ value === false + */ +describe(`isNumberType`, () => { + // Defined. + it('is DEFINED', () => expect(isNumberType).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isNumberType(NUMBER, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isNumberType(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isNumberType(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isNumberType(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isNumberType(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isNumberType(CLASS)).toBe(FALSE)); + it(`OBJECT_ONE`, () => expect(isNumberType(OBJECT_ONE)).toBe(FALSE)); + it(`OBJECT_TWO`, () => expect(isNumberType(OBJECT_TWO)).toBe(FALSE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isNumberType(OBJECT_ONE_NEW)).toBe(FALSE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isNumberType(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isNumberType(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isNumberType(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isNumberType(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isNumberType(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isNumberType(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isNumberType(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isNumberType(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isNumberType(NUMBER)).toBe(TRUE)); + it(`Number(${NUMBER})`, () => expect(isNumberType(NUMBER_INSTANCE)).toBe(TRUE)); + it(`new Number(${NUMBER})`, () => expect(isNumberType(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isNumberType(STRING)).toBe(FALSE)); + it(`String(${STRING})`, () => expect(isNumberType(STRING_INSTANCE)).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(isNumberType(STRING_NEW_INSTANCE)).toBe(FALSE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isNumberType(SYMBOL_NUMBER)).toBe(FALSE)); + it(`Symbol(${STRING})`, () => expect(isNumberType(SYMBOL_STRING)).toBe(FALSE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isNumberType(UNDEFINED)).toBe(FALSE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-number.spec.ts b/packages/type/src/is/test/is-number.spec.ts index 68b382ce..7eb650a6 100644 --- a/packages/type/src/is/test/is-number.spec.ts +++ b/packages/type/src/is/test/is-number.spec.ts @@ -1,70 +1,73 @@ -/** - * Checks - * ✓ typeof === 'number' - * ✓ instanceof Number - */ - +// Function. import { isNumber } from '../lib/is-number.func'; -import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; +// Type. import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; +import { CLASS, Class } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; +import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; -import { CLASS, Class } from './variables/class.const'; -import { NULL } from './variables/null.const'; - +import { notDefined } from './variables/not-defined.const'; +/** + * Checks + * ✓ typeof === 'number' + * ✓ instanceof Number + * + * ✓ typeof === 'object' + * ✓ instanceof Object + */ describe('isNumber', () => { // TRUE - it('is DEFINED', () => { - expect(isNumber).toBeDefined(); - }); + it('is DEFINED', () => expect(isNumber).toBeDefined()); it(`'number' | Number`, () => { - expect(isNumber(NUMBER)).toBeTruthy(); - expect(isNumber(NUMBER_INSTANCE)).toBeTruthy(); - expect(isNumber(NUMBER_NEW_INSTANCE)).toBeTruthy(); + expect(isNumber(NUMBER)).toBe(TRUE); + expect(isNumber(NUMBER_INSTANCE)).toBe(TRUE); + expect(isNumber(NUMBER_NEW_INSTANCE)).toBe(TRUE); + isNumber(NUMBER_NEW_INSTANCE, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); }); // FALSE it(`'bigint'`, () => { - expect(isNumber(BIGINT)).toBeFalsy(); - expect(isNumber(BIGINT_INSTANCE)).toBeFalsy(); + expect(isNumber(BIGINT)).toBe(FALSE); + expect(isNumber(BIGINT_INSTANCE)).toBe(FALSE); }); it(`'boolean' | Boolean`, () => { - expect(isNumber(FALSE)).toBeFalsy(); - expect(isNumber(TRUE)).toBeFalsy(); - expect(isNumber(FALSE_INSTANCE)).toBeFalsy(); - expect(isNumber(TRUE_INSTANCE)).toBeFalsy(); + expect(isNumber(FALSE)).toBe(FALSE); + expect(isNumber(TRUE)).toBe(FALSE); + expect(isNumber(FALSE_INSTANCE)).toBe(FALSE); + expect(isNumber(TRUE_INSTANCE)).toBe(FALSE); }); it(`Class | CLASS`, () => { - expect(isNumber(Class)).toBeFalsy(); - expect(isNumber(CLASS)).toBeFalsy(); - }); - it(`'function' | Function`, () => { - expect(isNumber(FUNCTION)).toBeFalsy(); + expect(isNumber(Class)).toBe(FALSE); + expect(isNumber(CLASS)).toBe(FALSE); }); + it(`'function' | Function`, () => expect(isNumber(FUNCTION)).toBe(FALSE)); it(`null | NULL`, () => { - expect(isNumber(null)).toBeFalsy(); - expect(isNumber(NULL)).toBeFalsy(); + expect(isNumber(null)).toBe(FALSE); + expect(isNumber(NULL)).toBe(FALSE); }); it(`'object' | Object`, () => { - expect(isNumber(OBJECT_ONE)).toBeFalsy(); - expect(isNumber(OBJECT_TWO)).toBeFalsy(); + expect(isNumber(OBJECT_ONE)).toBe(FALSE); + expect(isNumber(OBJECT_TWO)).toBe(FALSE); }); it(`'string' | String`, () => { - expect(isNumber(STRING)).toBeFalsy(); - expect(isNumber(STRING_INSTANCE)).toBeFalsy(); - expect(isNumber(STRING_NEW_INSTANCE)).toBeFalsy(); + expect(isNumber(STRING)).toBe(FALSE); + expect(isNumber(STRING_INSTANCE)).toBe(FALSE); + expect(isNumber(STRING_NEW_INSTANCE)).toBe(FALSE); }); it(`'symbol'`, () => { - expect(isNumber(SYMBOL_NUMBER)).toBeFalsy(); - expect(isNumber(SYMBOL_STRING)).toBeFalsy(); + expect(isNumber(SYMBOL_NUMBER)).toBe(FALSE); + expect(isNumber(SYMBOL_STRING)).toBe(FALSE); }); it(`'undefined'`, () => { - expect(isNumber(notDefined)).toBeFalse(); - expect(isNumber(UNDEFINED)).toBeFalse(); + expect(isNumber(notDefined)).toBe(FALSE); + expect(isNumber(UNDEFINED)).toBe(FALSE); }); }); diff --git a/packages/type/src/is/test/is-object-key.spec.ts b/packages/type/src/is/test/is-object-key.spec.ts new file mode 100644 index 00000000..6c38b0b0 --- /dev/null +++ b/packages/type/src/is/test/is-object-key.spec.ts @@ -0,0 +1,147 @@ +// Function. +import { isObjectKey } from '../lib/is-object-key.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, TRUE_INSTANCE, FALSE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW, ObjectTwo, ObjectOne } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +describe(`isObjectKey`, () => { + // Defined. + it('is DEFINED', () => expect(isObjectKey).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isObjectKey(CLASS, ['firstName', 'surname'], (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + // ... instance. + describe(`instance`, () => { + describe(`CLASS`, () => { + // number. + it('has number key', () => { + expect(isObjectKey(CLASS, 1030405027)).toBe(TRUE); + expect(isObjectKey(CLASS, 5)).toBe(TRUE); + expect(isObjectKey(CLASS, NUMBER)).toBe(FALSE); // It doesn't find getter + expect(isObjectKey(CLASS, [5, 1030405027])).toBe(TRUE); + }); + + // string. + it('has string key', () => { + expect(isObjectKey(CLASS, 'surname')).toBe(TRUE); + expect(isObjectKey(CLASS, ['firstName', 'surname'])).toBe(TRUE); + }); + + // symbol. + it('has symbol key', () => { + expect(isObjectKey(CLASS, SYMBOL_NUMBER)).toBe(FALSE); + expect(isObjectKey(CLASS, SYMBOL_STRING)).toBe(FALSE); + expect(isObjectKey(CLASS, [SYMBOL_NUMBER, SYMBOL_STRING])).toBe(FALSE); + }); + + // mixed. + it('has string and number key', () => expect(isObjectKey(CLASS, [1030405027, 'firstName', 'surname'])).toBe(TRUE)); + }); + }); + + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isObjectKey(FUNCTION, 'function')).toBe(FALSE)); + it(`CLASS`, () => expect(isObjectKey(Class, 'function')).toBe(FALSE)); + }); + + // ... objects. + describe('object', () => { + describe(`OBJECT_ONE`, () => { + // number. + it('has number key', () => { + expect(isObjectKey(OBJECT_ONE, 1030405027)).toBe(TRUE); + expect(isObjectKey(OBJECT_ONE, 5)).toBe(TRUE); + expect(isObjectKey(OBJECT_ONE, NUMBER)).toBe(TRUE); // It doesn't find getter + expect(isObjectKey(OBJECT_ONE, [5, 1030405027])).toBe(TRUE); + }); + + // string. + it('has string key', () => { + expect(isObjectKey(OBJECT_ONE, 'key as string')).toBe(TRUE); + expect(isObjectKey(OBJECT_ONE, 'x')).toBe(TRUE); + expect(isObjectKey(OBJECT_ONE, STRING)).toBe(TRUE); + expect(isObjectKey(OBJECT_ONE, ['key as string', 'x', STRING])).toBe(TRUE); + }); + + // symbol. + it('has symbol key', () => { + expect(isObjectKey(OBJECT_ONE, SYMBOL_NUMBER)).toBe(TRUE); + expect(isObjectKey(OBJECT_ONE, SYMBOL_STRING)).toBe(TRUE); + expect(isObjectKey(OBJECT_ONE, [SYMBOL_NUMBER, SYMBOL_STRING])).toBe(TRUE); + }); + + // mixed. + it('has mixed key', () => { + expect(isObjectKey(OBJECT_ONE, [ + 'key as string', + 'x', + 1030405027, + 5, + NUMBER, + STRING, + SYMBOL_NUMBER, + SYMBOL_STRING, + ])).toBe(TRUE); + }); + }); + }); + + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isObjectKey(BIGINT, 'bigint')).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isObjectKey(BIGINT_INSTANCE, 'bigint')).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isObjectKey(TRUE, 'boolean')).toBe(FALSE)); + it(`${FALSE}`, () => expect(isObjectKey(FALSE, 'boolean')).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isObjectKey(TRUE_INSTANCE, 'boolean')).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isObjectKey(FALSE_INSTANCE, 'boolean')).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isObjectKey(NULL, 'null')).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isObjectKey(NUMBER, 'number')).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isObjectKey(NUMBER_INSTANCE, 'number')).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isObjectKey(NUMBER_NEW_INSTANCE, 'number')).toBe(FALSE)); + }); + + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isObjectKey(STRING, 'string')).toBe(FALSE)); + it(`String(${STRING})`, () => expect(isObjectKey(STRING_INSTANCE, 'string')).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(isObjectKey(STRING_NEW_INSTANCE, 'string')).toBe(FALSE)); + }); + + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isObjectKey(SYMBOL_NUMBER, 'symbol')).toBe(FALSE)); + it(`Symbol(${STRING})`, () => expect(isObjectKey(SYMBOL_STRING, 'symbol')).toBe(FALSE)); + }); + + // undefined + it(`${UNDEFINED}`, () => expect(isObjectKey(UNDEFINED, 'undefined')).toBe(FALSE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-object.spec.ts b/packages/type/src/is/test/is-object.spec.ts index c66641c2..99ddb5f9 100644 --- a/packages/type/src/is/test/is-object.spec.ts +++ b/packages/type/src/is/test/is-object.spec.ts @@ -1,71 +1,101 @@ -/** - * Checks - * ✓ typeof === 'object' - * ✓ instanceof Object - */ +// Function. import { isObject } from '../lib/is-object.func'; -import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; -import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; +import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; -import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo } from './variables/object.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; -import { CLASS, Class } from './variables/class.const'; -import { NULL } from './variables/null.const'; +/** + * Checks + * ✓ typeof === 'object' + * ✓ instanceof Object + */ +describe(`isObject`, () => { + // Defined. + it('is DEFINED', () => expect(isObject).toBeDefined()); -describe('isObject', () => { - // TRUE - it('is DEFINED', () => { - expect(isObject).toBeDefined(); - }); - it(`Class | CLASS`, () => { - expect(isObject(CLASS)).toBeTruthy(); - }); - it(`'object' | Object`, () => { - expect(isObject(OBJECT_ONE)).toBeTruthy(); - expect(isObject(OBJECT_TWO)).toBeTruthy(); - }); + // Checks ... + describe(`checks`, () => { + // it('callback', () => { + // isObject(OBJECT_ONE, (result: boolean) => { + // expect(result).toBe(TRUE); + // return result; + // }); + // }); - // FALSE - it(`'bigint'`, () => { - expect(isObject(BIGINT)).toBeFalsy(); - expect(isObject(BIGINT_INSTANCE)).toBeFalsy(); - }); - it(`'boolean' | Boolean`, () => { - expect(isObject(FALSE)).toBeFalsy(); - expect(isObject(TRUE)).toBeFalsy(); - expect(isObject(FALSE_INSTANCE)).toBeFalsy(); - expect(isObject(TRUE_INSTANCE)).toBeFalsy(); - }); - it(`Class | CLASS`, () => { - expect(isObject(Class)).toBeFalsy(); - }); - it(`'function' | Function`, () => { - expect(isObject(FUNCTION)).toBeFalsy(); - }); - it(`null | NULL`, () => { - expect(isObject(null)).toBeFalsy(); - expect(isObject(NULL)).toBeFalsy(); - }); - it(`'number' | Number`, () => { - expect(isObject(NUMBER)).toBeFalsy(); - expect(isObject(NUMBER_INSTANCE)).toBeFalsy(); - expect(isObject(NUMBER_NEW_INSTANCE)).toBeFalsy(); - }); - it(`'string' | String`, () => { - expect(isObject(STRING)).toBeFalsy(); - expect(isObject(STRING_INSTANCE)).toBeFalsy(); - expect(isObject(STRING_NEW_INSTANCE)).toBeFalsy(); - }); - it(`'symbol'`, () => { - expect(isObject(SYMBOL_NUMBER)).toBeFalsy(); - expect(isObject(SYMBOL_STRING)).toBeFalsy(); - }); - it(`'undefined'`, () => { - expect(isObject(notDefined)).toBeFalse(); - expect(isObject(UNDEFINED)).toBeFalse(); + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isObject(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isObject(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isObject(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isObject(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isObject(CLASS)).toBe(TRUE)); + it(`OBJECT_ONE`, () => expect(isObject(OBJECT_ONE)).toBe(TRUE)); + it(`OBJECT_TWO`, () => expect(isObject(OBJECT_TWO)).toBe(TRUE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isObject(OBJECT_ONE_NEW)).toBe(TRUE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isObject(OBJECT_TWO_NEW)).toBe(TRUE)); + + describe('OBJECT_ONE has the', () => { + it(`'key as string'`, () => expect(isObject(OBJECT_ONE, 'key as string')).toBe(TRUE)); + it(`key as string ${STRING}`, () => expect(isObject(OBJECT_ONE, STRING)).toBe(TRUE)); + it(`key as string instance ${STRING_NEW_INSTANCE}`, () => expect(isObject(OBJECT_ONE, STRING_NEW_INSTANCE)).toBe(TRUE)); + it(`key as number ${1030405027}`, () => expect(isObject(OBJECT_ONE, 1030405027)).toBe(TRUE)); + it(`key as number ${NUMBER}`, () => expect(isObject(OBJECT_ONE, NUMBER)).toBe(TRUE)); + it(`key as number instance ${NUMBER_NEW_INSTANCE}`, () => expect(isObject(OBJECT_ONE, NUMBER_NEW_INSTANCE)).toBe(TRUE)); + it(`key as SYMBOL_NUMBER`, () => expect(isObject(OBJECT_ONE, SYMBOL_NUMBER)).toBe(TRUE)); + it(`key as SYMBOL_STRING`, () => expect(isObject(OBJECT_ONE, SYMBOL_STRING)).toBe(TRUE)); + }); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isObject(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isObject(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isObject(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isObject(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isObject(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isObject(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isObject(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isObject(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isObject(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isObject(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isObject(STRING)).toBe(FALSE)); + it(`String(${STRING})`, () => expect(isObject(STRING_INSTANCE)).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(isObject(STRING_NEW_INSTANCE)).toBe(FALSE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isObject(SYMBOL_NUMBER)).toBe(FALSE)); + it(`Symbol(${STRING})`, () => expect(isObject(SYMBOL_STRING)).toBe(FALSE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isObject(UNDEFINED)).toBe(FALSE)); + }); }); }); diff --git a/packages/type/src/is/test/is-param.spec.ts b/packages/type/src/is/test/is-param.spec.ts new file mode 100644 index 00000000..9a2ff9a5 --- /dev/null +++ b/packages/type/src/is/test/is-param.spec.ts @@ -0,0 +1,78 @@ +import { NUMBER } from './variables/number.const'; +import { STRING } from './variables/string.const'; +import { isParam } from '../lib/is-param.decorator'; + +class TestClass { + @isParam('object', 'string', 'number') + public testMethod(object?: any, firstName?: any, age?: any): { object: any, firstName: any, age: any } { + return {object, firstName, age}; + } +} + +describe(`isParam`, () => { + const resultTRUE = new TestClass().testMethod({firstName: 'NoName'}, STRING, NUMBER); + const resultFALSE = new TestClass().testMethod(NUMBER, {firstName: 'NoName'}, STRING); + + // Defined. + it('is DEFINED', () => expect(isParam).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + // ... objects. + describe('object', () => { + it(`CLASS`, () => { + expect(resultTRUE.object).toEqual({firstName: 'NoName'}); + expect(resultFALSE.object).toBeUndefined(); + }); + }); + + // ... primitives. + describe(`primitive`, () => { + // bigint + // describe(`bigint`, () => { + // it(`${BIGINT}`, () => expect(isPrimitive(BIGINT, 'bigint')).toBe(TRUE)); + // it(`${BIGINT_EXPECTATION}`, () => expect(isPrimitive(BIGINT_INSTANCE, 'bigint')).toBe(TRUE)); + // }); + + // boolean + // describe(`boolean`, () => { + // it(`${TRUE}`, () => expect(isPrimitive(TRUE, 'boolean')).toBe(TRUE)); + // it(`${FALSE}`, () => expect(isPrimitive(FALSE, 'boolean')).toBe(TRUE)); + // it(`${FALSE_EXPECTATION}`, () => expect(isPrimitive(TRUE_INSTANCE, 'boolean')).toBe(TRUE)); + // it(`${TRUE_EXPECTATION}`, () => expect(isPrimitive(FALSE_INSTANCE, 'boolean')).toBe(TRUE)); + // }); + + // null + // it(`${NULL}`, () => expect(isPrimitive(NULL, 'null')).toBe(TRUE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => { + expect(resultTRUE.age).toBe(NUMBER); + expect(resultFALSE.age).toBeUndefined(); + }); + // it(`Number(${NUMBER})`, () => expect(isPrimitive(NUMBER_INSTANCE, 'number')).toBe(TRUE)); + // it(`new Number(${NUMBER})`, () => expect(isPrimitive(NUMBER_NEW_INSTANCE, 'number')).toBe(TRUE)); + }); + + // string + describe(`string`, () => { + it(`${STRING}`, () => { + expect(resultTRUE.firstName).toBe(STRING); + expect(resultFALSE.firstName).toBeUndefined(); + }); + // it(`String(${STRING})`, () => expect(isPrimitive(STRING_INSTANCE, 'string')).toBe(TRUE)); + // it(`new String(${STRING})`, () => expect(isPrimitive(STRING_NEW_INSTANCE, 'string')).toBe(TRUE)); + }); + + // symbol + describe(`symbol`, () => { + // it(`Symbol(${NUMBER})`, () => expect(isPrimitive(SYMBOL_NUMBER, 'symbol')).toBe(TRUE)); + // it(`Symbol(${STRING})`, () => expect(isPrimitive(SYMBOL_STRING, 'symbol')).toBe(TRUE)); + }); + + // undefined + // it(`${UNDEFINED}`, () => expect(isPrimitive(UNDEFINED, 'undefined')).toBe(TRUE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-primitive.spec.ts b/packages/type/src/is/test/is-primitive.spec.ts new file mode 100644 index 00000000..ee001540 --- /dev/null +++ b/packages/type/src/is/test/is-primitive.spec.ts @@ -0,0 +1,89 @@ +// Function. +import { isPrimitive } from '../lib/is-primitive.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, TRUE_INSTANCE, FALSE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW, ObjectTwo, ObjectOne } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +describe(`isPrimitive`, () => { + // Defined. + it('is DEFINED', () => expect(isPrimitive).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + // it('callback', () => { + // isPrimitive('test', (result: boolean) => { + // expect(result).toBe(TRUE); + // return result; + // }); + // }); + + // ... instance. + // describe(`instance`, () => it(`Class`, () => expect(isPrimitive(CLASS, Class)).toBe(TRUE))); + + // ... function. + // describe(`function`, () => { + // it(`${FUNCTION}`, () => expect(isPrimitive(FUNCTION, 'function')).toBe(TRUE)); + // it(`${CLASS}`, () => expect(isPrimitive(Class, 'function')).toBe(TRUE)); + // }); + + // ... objects. + // describe('object', () => { + // it(`CLASS`, () => expect(isPrimitive(CLASS, 'object')).toBe(TRUE)); + // it(`OBJECT_ONE`, () => expect(isPrimitive(OBJECT_ONE, 'object')).toBe(TRUE)); + // it(`OBJECT_ONE_NEW`, () => expect(isPrimitive(OBJECT_TWO, 'object')).toBe(TRUE)); + // it(`new Object(OBJECT_ONE_NEW)`, () => expect(isPrimitive(OBJECT_ONE_NEW, 'object')).toBe(TRUE)); + // it(`new Object(OBJECT_TWO_NEW)`, () => expect(isPrimitive(OBJECT_TWO_NEW, 'object')).toBe(TRUE)); + // }); + + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isPrimitive(BIGINT, 'bigint')).toBe(TRUE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isPrimitive(BIGINT_INSTANCE, 'bigint')).toBe(TRUE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isPrimitive(TRUE, 'boolean')).toBe(TRUE)); + it(`${FALSE}`, () => expect(isPrimitive(FALSE, 'boolean')).toBe(TRUE)); + it(`${FALSE_EXPECTATION}`, () => expect(isPrimitive(TRUE_INSTANCE, 'boolean')).toBe(TRUE)); + it(`${TRUE_EXPECTATION}`, () => expect(isPrimitive(FALSE_INSTANCE, 'boolean')).toBe(TRUE)); + }); + + // null + it(`${NULL}`, () => expect(isPrimitive(NULL, 'null')).toBe(TRUE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isPrimitive(NUMBER, 'number')).toBe(TRUE)); + it(`Number(${NUMBER})`, () => expect(isPrimitive(NUMBER_INSTANCE, 'number')).toBe(TRUE)); + it(`new Number(${NUMBER})`, () => expect(isPrimitive(NUMBER_NEW_INSTANCE, 'number')).toBe(TRUE)); + }); + + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isPrimitive(STRING, 'string')).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isPrimitive(STRING_INSTANCE, 'string')).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isPrimitive(STRING_NEW_INSTANCE, 'string')).toBe(TRUE)); + }); + + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isPrimitive(SYMBOL_NUMBER, 'symbol')).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isPrimitive(SYMBOL_STRING, 'symbol')).toBe(TRUE)); + }); + + // undefined + it(`${UNDEFINED}`, () => expect(isPrimitive(UNDEFINED, 'undefined')).toBe(TRUE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-string-object.spec.ts b/packages/type/src/is/test/is-string-object.spec.ts new file mode 100644 index 00000000..365f2a1d --- /dev/null +++ b/packages/type/src/is/test/is-string-object.spec.ts @@ -0,0 +1,90 @@ +// Function. +import { isStringObject } from '../lib/is-string-object.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +/** + * Checks + * ✓ typeof === 'object' && instanceof String === true && instanceof Object === true + */ +describe(`isStringObject`, () => { + // Defined. + it('is DEFINED', () => expect(isStringObject).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isStringObject(STRING_NEW_INSTANCE, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isStringObject(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isStringObject(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isStringObject(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isStringObject(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isStringObject(CLASS)).toBe(FALSE)); + it(`OBJECT_ONE`, () => expect(isStringObject(OBJECT_ONE)).toBe(FALSE)); + it(`OBJECT_TWO`, () => expect(isStringObject(OBJECT_TWO)).toBe(FALSE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isStringObject(OBJECT_ONE_NEW)).toBe(FALSE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isStringObject(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isStringObject(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isStringObject(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isStringObject(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isStringObject(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isStringObject(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isStringObject(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isStringObject(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isStringObject(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isStringObject(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isStringObject(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isStringObject(STRING)).toBe(FALSE)); + it(`String(${STRING})`, () => expect(isStringObject(STRING_INSTANCE)).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(isStringObject(STRING_NEW_INSTANCE)).toBe(TRUE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isStringObject(SYMBOL_NUMBER)).toBe(FALSE)); + it(`Symbol(${STRING})`, () => expect(isStringObject(SYMBOL_STRING)).toBe(FALSE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isStringObject(UNDEFINED)).toBe(FALSE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-string-type.spec.ts b/packages/type/src/is/test/is-string-type.spec.ts new file mode 100644 index 00000000..c8675482 --- /dev/null +++ b/packages/type/src/is/test/is-string-type.spec.ts @@ -0,0 +1,90 @@ +// Function. +import { isStringType } from '../lib/is-string-type.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +/** + * Checks + * ✓ typeof === 'string' && instanceof String === false && instanceof Object === false + */ +describe(`isStringType`, () => { + // Defined. + it('is DEFINED', () => expect(isStringType).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isStringType(STRING, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isStringType(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isStringType(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isStringType(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isStringType(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isStringType(CLASS)).toBe(FALSE)); + it(`OBJECT_ONE`, () => expect(isStringType(OBJECT_ONE)).toBe(FALSE)); + it(`OBJECT_TWO`, () => expect(isStringType(OBJECT_TWO)).toBe(FALSE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isStringType(OBJECT_ONE_NEW)).toBe(FALSE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isStringType(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isStringType(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isStringType(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isStringType(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isStringType(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isStringType(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isStringType(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isStringType(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isStringType(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isStringType(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isStringType(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isStringType(STRING)).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isStringType(STRING_INSTANCE)).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isStringType(STRING_NEW_INSTANCE)).toBe(FALSE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isStringType(SYMBOL_NUMBER)).toBe(FALSE)); + it(`Symbol(${STRING})`, () => expect(isStringType(SYMBOL_STRING)).toBe(FALSE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isStringType(UNDEFINED)).toBe(FALSE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-string.spec.ts b/packages/type/src/is/test/is-string.spec.ts index c46e0b11..00728cc8 100644 --- a/packages/type/src/is/test/is-string.spec.ts +++ b/packages/type/src/is/test/is-string.spec.ts @@ -1,72 +1,91 @@ -/** - * Checks - * ✓ typeof === 'string' && instanceof String === false && instanceof Object === false - * ✓ typeof === 'object' && instanceof String === true && instanceof Object === true - */ - -import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE } from './variables/boolean.const'; -import { BIGINT, BIGINT_INSTANCE } from './variables/big-int.const'; +// Function. +import { isString } from '../lib/is-string.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; import { FUNCTION } from './variables/function.const'; -import { notDefined } from './variables.const'; import { NULL } from './variables/null.const'; import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; -import { OBJECT_ONE, OBJECT_TWO } from './variables/object.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; import { UNDEFINED } from './variables/undefined.const'; -import { isString } from '../lib/is-string.func'; -describe('isString', () => { - // TRUE - it('is DEFINED', () => { - expect(isString).toBeDefined(); - }); - it(`'string' | String`, () => { - expect(isString(STRING)).toBeTruthy(); - expect(isString(STRING_INSTANCE)).toBeTruthy(); - expect(isString(STRING_NEW_INSTANCE)).toBeTruthy(); - }); +/** + * Checks + * ✓ typeof === 'string' && instanceof String === false && instanceof Object === false + * ✓ typeof === 'object' && instanceof String === true && instanceof Object === true + */ +describe(`isString`, () => { + // Defined. + it('is DEFINED', () => expect(isString).toBeDefined()); - // FALSE - it(`'bigint'`, () => { - expect(isString(BIGINT)).toBeFalsy(); - expect(isString(BIGINT_INSTANCE)).toBeFalsy(); - }); - it(`'boolean' | Boolean`, () => { - expect(isString(FALSE)).toBeFalsy(); - expect(isString(TRUE)).toBeFalsy(); - expect(isString(FALSE_INSTANCE)).toBeFalsy(); - expect(isString(TRUE_INSTANCE)).toBeFalsy(); - expect(isString(Boolean(false))).toBeFalsy(); - expect(isString(Boolean(true))).toBeFalsy(); - }); - it(`Class | CLASS`, () => { - expect(isString(Class)).toBeFalsy(); - expect(isString(CLASS)).toBeFalsy(); - }); - it(`'function' | Function`, () => { - expect(isString(FUNCTION)).toBeFalsy(); - }); - it(`null | NULL`, () => { - expect(isString(null)).toBeFalsy(); - expect(isString(NULL)).toBeFalsy(); - }); - it(`'number' | Number`, () => { - expect(isString(NUMBER)).toBeFalsy(); - expect(isString(NUMBER_INSTANCE)).toBeFalsy(); - expect(isString(NUMBER_NEW_INSTANCE)).toBeFalsy(); - }); - it(`'object' | Object`, () => { - expect(isString(OBJECT_ONE)).toBeFalsy(); - expect(isString(OBJECT_TWO)).toBeFalsy(); - }); - it(`'symbol'`, () => { - expect(isString(SYMBOL_NUMBER)).toBeFalsy(); - expect(isString(SYMBOL_STRING)).toBeFalsy(); - }); - it(`'undefined'`, () => { - expect(isString(notDefined)).toBeFalse(); - expect(isString(UNDEFINED)).toBeFalse(); + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isString(STRING, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isString(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isString(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isString(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isString(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isString(CLASS)).toBe(FALSE)); + it(`OBJECT_ONE`, () => expect(isString(OBJECT_ONE)).toBe(FALSE)); + it(`OBJECT_TWO`, () => expect(isString(OBJECT_TWO)).toBe(FALSE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isString(OBJECT_ONE_NEW)).toBe(FALSE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isString(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isString(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isString(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isString(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isString(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isString(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isString(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isString(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isString(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isString(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isString(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isString(STRING)).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isString(STRING_INSTANCE)).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isString(STRING_NEW_INSTANCE)).toBe(TRUE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isString(SYMBOL_NUMBER)).toBe(FALSE)); + it(`Symbol(${STRING})`, () => expect(isString(SYMBOL_STRING)).toBe(FALSE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isString(UNDEFINED)).toBe(FALSE)); + }); }); }); diff --git a/packages/type/src/is/test/is-symbol.spec.ts b/packages/type/src/is/test/is-symbol.spec.ts index f9fb33b6..7b231910 100644 --- a/packages/type/src/is/test/is-symbol.spec.ts +++ b/packages/type/src/is/test/is-symbol.spec.ts @@ -1,14 +1,90 @@ +// Function. +import { isSymbol } from '../lib/is-symbol.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + /** * Checks * ✓ typeof === 'symbol' */ +describe(`isSymbol`, () => { + // Defined. + it('is DEFINED', () => expect(isSymbol).toBeDefined()); -import { isSymbol } from '../lib/is-symbol.func'; + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isSymbol(SYMBOL_NUMBER, (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isSymbol(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isSymbol(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isSymbol(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isSymbol(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isSymbol(CLASS)).toBe(FALSE)); + it(`OBJECT_ONE`, () => expect(isSymbol(OBJECT_ONE)).toBe(FALSE)); + it(`OBJECT_TWO`, () => expect(isSymbol(OBJECT_TWO)).toBe(FALSE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isSymbol(OBJECT_ONE_NEW)).toBe(FALSE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isSymbol(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isSymbol(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isSymbol(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isSymbol(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isSymbol(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isSymbol(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isSymbol(FALSE_INSTANCE)).toBe(FALSE)); + }); -describe('isSymbol', () => { - // TRUE - it('is DEFINED', () => { - expect(isSymbol).toBeDefined(); - }); + // null + it(`${NULL}`, () => expect(isSymbol(NULL)).toBe(FALSE)); + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isSymbol(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isSymbol(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isSymbol(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isSymbol(STRING)).toBe(FALSE)); + it(`String(${STRING})`, () => expect(isSymbol(STRING_INSTANCE)).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(isSymbol(STRING_NEW_INSTANCE)).toBe(FALSE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isSymbol(SYMBOL_NUMBER)).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isSymbol(SYMBOL_STRING)).toBe(TRUE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isSymbol(UNDEFINED)).toBe(FALSE)); + }); + }); }); diff --git a/packages/type/src/is/test/is-type.spec.ts b/packages/type/src/is/test/is-type.spec.ts new file mode 100644 index 00000000..ccebd1c3 --- /dev/null +++ b/packages/type/src/is/test/is-type.spec.ts @@ -0,0 +1,93 @@ +// Function. +import { isType } from '../lib/is-type.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, TRUE_INSTANCE, FALSE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, OBJECT_ONE_NEW, OBJECT_TWO_NEW, ObjectTwo, ObjectOne } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +describe(`isType`, () => { + // Defined. + it('is DEFINED', () => expect(isType).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isType(STRING, 'string', (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + isType(NUMBER, 'number', (result: boolean) => { + expect(result).toBe(TRUE); + return result; + }); + }); + + // ... instance. + describe(`instance`, () => it(`Class`, () => expect(isType(CLASS, Class)).toBe(TRUE))); + + // ... function. + describe(`function`, () => { + it(`${FUNCTION}`, () => expect(isType(FUNCTION, 'function')).toBe(TRUE)); + it(`${CLASS}`, () => expect(isType(Class, 'function')).toBe(TRUE)); + }); + + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isType(CLASS, 'object')).toBe(TRUE)); + it(`OBJECT_ONE`, () => expect(isType(OBJECT_ONE, 'object')).toBe(TRUE)); + it(`OBJECT_ONE_NEW`, () => expect(isType(OBJECT_TWO, 'object')).toBe(TRUE)); + it(`new Object(OBJECT_ONE_NEW)`, () => expect(isType(OBJECT_ONE_NEW, 'object')).toBe(TRUE)); + it(`new Object(OBJECT_TWO_NEW)`, () => expect(isType(OBJECT_TWO_NEW, 'object')).toBe(TRUE)); + }); + + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isType(BIGINT, 'bigint')).toBe(TRUE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isType(BIGINT_INSTANCE, 'bigint')).toBe(TRUE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isType(TRUE, 'boolean')).toBe(TRUE)); + it(`${FALSE}`, () => expect(isType(FALSE, 'boolean')).toBe(TRUE)); + it(`${FALSE_EXPECTATION}`, () => expect(isType(TRUE_INSTANCE, 'boolean')).toBe(TRUE)); + it(`${TRUE_EXPECTATION}`, () => expect(isType(FALSE_INSTANCE, 'boolean')).toBe(TRUE)); + }); + + // null + it(`${NULL}`, () => expect(isType(NULL, 'null')).toBe(TRUE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isType(NUMBER, 'number')).toBe(TRUE)); + it(`Number(${NUMBER})`, () => expect(isType(NUMBER_INSTANCE, 'number')).toBe(TRUE)); + it(`new Number(${NUMBER})`, () => expect(isType(NUMBER_NEW_INSTANCE, 'number')).toBe(TRUE)); + }); + + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isType(STRING, 'string')).toBe(TRUE)); + it(`String(${STRING})`, () => expect(isType(STRING_INSTANCE, 'string')).toBe(TRUE)); + it(`new String(${STRING})`, () => expect(isType(STRING_NEW_INSTANCE, 'string')).toBe(TRUE)); + }); + + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isType(SYMBOL_NUMBER, 'symbol')).toBe(TRUE)); + it(`Symbol(${STRING})`, () => expect(isType(SYMBOL_STRING, 'symbol')).toBe(TRUE)); + }); + + // undefined + it(`${UNDEFINED}`, () => expect(isType(UNDEFINED, 'undefined')).toBe(TRUE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is-undefined.spec.ts b/packages/type/src/is/test/is-undefined.spec.ts new file mode 100644 index 00000000..5c74285a --- /dev/null +++ b/packages/type/src/is/test/is-undefined.spec.ts @@ -0,0 +1,92 @@ +// Function. +import { isUndefined } from '../lib/is-undefined.func'; +// Variables. +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from './variables/big-int.const'; +import { Class, CLASS } from './variables/class.const'; +import { FALSE, TRUE, FALSE_INSTANCE, TRUE_INSTANCE, FALSE_EXPECTATION, TRUE_EXPECTATION } from './variables/boolean.const'; +import { FUNCTION } from './variables/function.const'; +import { NULL } from './variables/null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './variables/number.const'; +import { OBJECT_ONE, OBJECT_TWO, ObjectOne, ObjectTwo, OBJECT_ONE_NEW, OBJECT_TWO_NEW } from './variables/object.const'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './variables/string.const'; +import { SYMBOL_NUMBER, SYMBOL_STRING } from './variables/symbol.const'; +import { UNDEFINED } from './variables/undefined.const'; + +/** + * Checks + * ✓ typeOf() = 'undefined + * ✓ typeof === 'undefined' + * ✓ value === undefined + */ +describe(`isUndefined`, () => { + // Defined. + it('is DEFINED', () => expect(isUndefined).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + it('callback', () => { + isUndefined(STRING, (result: boolean) => { + expect(result).toBe(FALSE); + return result; + }); + }); + + // ... arrays. + describe(`array`, () => { + // it(`${FUNCTION}`, () => expect(isUndefined(FUNCTION, 'function')).toBe(FALSE)); + // it(`${Class}`, () => expect(isUndefined(Class, 'function')).toBe(FALSE)); + }); + // ... function. + describe(`function`, () => { + it(`FUNCTION`, () => expect(isUndefined(FUNCTION)).toBe(FALSE)); + it(`Class`, () => expect(isUndefined(Class)).toBe(FALSE)); + }); + // ... objects. + describe('object', () => { + it(`CLASS`, () => expect(isUndefined(CLASS)).toBe(FALSE)); + it(`OBJECT_ONE`, () => expect(isUndefined(OBJECT_ONE)).toBe(FALSE)); + it(`OBJECT_TWO`, () => expect(isUndefined(OBJECT_TWO)).toBe(FALSE)); + it(`new Object(OBJECT_ONE_NEW})`, () => expect(isUndefined(OBJECT_ONE_NEW)).toBe(FALSE)); + it(`new Object(OBJECT_TWO_NEW})`, () => expect(isUndefined(OBJECT_TWO_NEW)).toBe(FALSE)); + }); + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => expect(isUndefined(BIGINT)).toBe(FALSE)); + it(`${BIGINT_EXPECTATION}`, () => expect(isUndefined(BIGINT_INSTANCE)).toBe(FALSE)); + }); + + // boolean + describe(`boolean`, () => { + it(`${TRUE}`, () => expect(isUndefined(TRUE)).toBe(FALSE)); + it(`${FALSE}`, () => expect(isUndefined(FALSE)).toBe(FALSE)); + it(`${FALSE_EXPECTATION}`, () => expect(isUndefined(TRUE_INSTANCE)).toBe(FALSE)); + it(`${TRUE_EXPECTATION}`, () => expect(isUndefined(FALSE_INSTANCE)).toBe(FALSE)); + }); + + // null + it(`${NULL}`, () => expect(isUndefined(NULL)).toBe(FALSE)); + + // number + describe(`number`, () => { + it(`${NUMBER}`, () => expect(isUndefined(NUMBER)).toBe(FALSE)); + it(`Number(${NUMBER})`, () => expect(isUndefined(NUMBER_INSTANCE)).toBe(FALSE)); + it(`new Number(${NUMBER})`, () => expect(isUndefined(NUMBER_NEW_INSTANCE)).toBe(FALSE)); + }); + // string + describe(`string`, () => { + it(`${STRING}`, () => expect(isUndefined(STRING)).toBe(FALSE)); + it(`String(${STRING})`, () => expect(isUndefined(STRING_INSTANCE)).toBe(FALSE)); + it(`new String(${STRING})`, () => expect(isUndefined(STRING_NEW_INSTANCE)).toBe(FALSE)); + }); + // symbol + describe(`symbol`, () => { + it(`Symbol(${NUMBER})`, () => expect(isUndefined(SYMBOL_NUMBER)).toBe(FALSE)); + it(`Symbol(${STRING})`, () => expect(isUndefined(SYMBOL_STRING)).toBe(FALSE)); + }); + // undefined + it(`${UNDEFINED}`, () => expect(isUndefined(UNDEFINED)).toBe(TRUE)); + }); + }); +}); diff --git a/packages/type/src/is/test/is.spec.ts b/packages/type/src/is/test/is.spec.ts index 8fdd2f3b..e070eaa0 100644 --- a/packages/type/src/is/test/is.spec.ts +++ b/packages/type/src/is/test/is.spec.ts @@ -1,9 +1,29 @@ import { is } from '../lib/is.object'; -describe('is.', () => { - // TRUE - it('is DEFINED', () => { - expect(is).toBeDefined(); +describe('`is`', () => { + describe('DEFINED', () => { + it('is', () => expect(is).toBeDefined()); + it('is.array()', () => expect(is.array).toBeDefined()); + it('is.boolean()', () => expect(is.boolean).toBeDefined()); + it('is.booleanObject()', () => expect(is.booleanObject).toBeDefined()); + it('is.booleanType()', () => expect(is.booleanType).toBeDefined()); + it('is.defined()', () => expect(is.defined).toBeDefined()); + it('is.function()', () => expect(is.function).toBeDefined()); + it('is.instance()', () => expect(is.instance).toBeDefined()); + it('is.key()', () => expect(is.key).toBeDefined()); + it('is.null()', () => expect(is.null).toBeDefined()); + it('is.number()', () => expect(is.number).toBeDefined()); + it('is.numberObject()', () => expect(is.numberObject).toBeDefined()); + it('is.numberType()', () => expect(is.numberType).toBeDefined()); + it('is.object()', () => expect(is.object).toBeDefined()); + it('is.objectKey()', () => expect(is.objectKey).toBeDefined()); + it('is.primitive()', () => expect(is.primitive).toBeDefined()); + it('is.string()', () => expect(is.string).toBeDefined()); + it('is.stringObject()', () => expect(is.stringObject).toBeDefined()); + it('is.stringType()', () => expect(is.stringType).toBeDefined()); + it('is.symbol()', () => expect(is.symbol).toBeDefined()); + it('is.type()', () => expect(is.type).toBeDefined()); + it('is.undefined()', () => expect(is.undefined).toBeDefined()); }); }); diff --git a/packages/type/src/is/test/variables.const.ts b/packages/type/src/is/test/variables.const.ts deleted file mode 100644 index 3fce92af..00000000 --- a/packages/type/src/is/test/variables.const.ts +++ /dev/null @@ -1,8 +0,0 @@ -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures -/** - * Check typeof === 'undefined', 'boolean', 'function', 'number', 'object', 'string', 'bigint', 'symbol' - * Check instanceof String, Boolean, Number, Function, Object - */ -export let notDefined: any; // typeof === 'undefined' - - diff --git a/packages/type/src/is/test/variables/array.const.ts b/packages/type/src/is/test/variables/array.const.ts index 11daf6c2..a7b88bfb 100644 --- a/packages/type/src/is/test/variables/array.const.ts +++ b/packages/type/src/is/test/variables/array.const.ts @@ -1,13 +1,17 @@ -import { FALSE, TRUE, TRUE_INSTANCE, FALSE_INSTANCE } from './boolean.const'; -import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './number.const'; -import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './string.const'; +// Variables. import { BIGINT, BIGINT_INSTANCE } from './big-int.const'; -import { NULL } from './null.const'; import { Class } from './class.const'; +import { FALSE, TRUE, TRUE_INSTANCE, FALSE_INSTANCE } from './boolean.const'; +import { NULL } from './null.const'; +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from './number.const'; import { ObjectOne, ObjectTwo } from './object.const'; -import { Func } from '../../../type/func.type'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from './string.const'; import { UNDEFINED } from './undefined.const'; -import { notDefined } from '../variables.const'; + +// Type. +import { Func } from '../../../type/func.type'; +import { notDefined } from './not-defined.const'; + // Arrays. // Array with `bigint`. export const ARRAY_BIGINT: Array = [BIGINT, BIGINT_INSTANCE, 9007199254740991n]; diff --git a/packages/type/src/is/test/variables/big-int.const.ts b/packages/type/src/is/test/variables/big-int.const.ts index fb37af3c..e66af580 100644 --- a/packages/type/src/is/test/variables/big-int.const.ts +++ b/packages/type/src/is/test/variables/big-int.const.ts @@ -13,3 +13,5 @@ export const BIGINT: any = 9007199254740991n; // typeof === 'bigint' * instanceof Object === false */ export const BIGINT_INSTANCE: any = BigInt('9007199254740991'); // typeof === 'bigint' + +export const BIGINT_EXPECTATION = `BigInt('9007199254740991')`; diff --git a/packages/type/src/is/test/variables/boolean.const.ts b/packages/type/src/is/test/variables/boolean.const.ts index 79fa33b2..d17bedd2 100644 --- a/packages/type/src/is/test/variables/boolean.const.ts +++ b/packages/type/src/is/test/variables/boolean.const.ts @@ -18,5 +18,8 @@ export const TRUE: any = true; // typeof === 'boolean' * instanceof Boolean === true * instanceof Object === true */ -export const FALSE_INSTANCE: any = new Boolean(false); // instanceof Boolean -export const TRUE_INSTANCE: any = new Boolean(true); // instanceof Boolean +export const FALSE_INSTANCE: any = new Boolean(FALSE); // instanceof Boolean +export const TRUE_INSTANCE: any = new Boolean(TRUE); // instanceof Boolean + +export const FALSE_EXPECTATION = `new Boolean(${FALSE})`; +export const TRUE_EXPECTATION = `new Boolean(${TRUE})`; diff --git a/packages/type/src/is/test/variables/class.const.ts b/packages/type/src/is/test/variables/class.const.ts index 9cc98c59..70145877 100644 --- a/packages/type/src/is/test/variables/class.const.ts +++ b/packages/type/src/is/test/variables/class.const.ts @@ -1,4 +1,6 @@ import { STRING } from './string.const'; +import { NUMBER } from './number.const'; +import { SYMBOL_STRING, SYMBOL_NUMBER } from './symbol.const'; /** * typeof === 'function' @@ -7,8 +9,30 @@ import { STRING } from './string.const'; * instanceof Object === true */ export class Class { - x = 5; + + 1030405027 = 'my new number'; + 5 = 'my number'; + + firstName = 'My name'; + surname = 'Surname'; + + x = NUMBER; y = STRING; + + get [NUMBER](): number { + return this.x; + } + get [STRING](): string { + return this.y; + } + + get [SYMBOL_NUMBER](): number { + return this.x; + } + + get [SYMBOL_STRING](): string { + return this.y; + } } /** @@ -18,3 +42,19 @@ export class Class { * instanceof Object === true */ export const CLASS = new Class(); + + +export class Person { + firstName = ''; + surname = ''; + age = 15; +} + +export class PersonCopy { + firstName = ''; + surname = ''; + age = 15; +} + +export const PERSON: Person = new Person(); +export const PERSON_COPY: PersonCopy = new PersonCopy(); diff --git a/packages/type/src/is/test/variables/not-defined.const.ts b/packages/type/src/is/test/variables/not-defined.const.ts new file mode 100644 index 00000000..d0483860 --- /dev/null +++ b/packages/type/src/is/test/variables/not-defined.const.ts @@ -0,0 +1 @@ +export let notDefined: any; // typeof === 'undefined' diff --git a/packages/type/src/is/test/variables/number.const.ts b/packages/type/src/is/test/variables/number.const.ts index e1119823..eccb1374 100644 --- a/packages/type/src/is/test/variables/number.const.ts +++ b/packages/type/src/is/test/variables/number.const.ts @@ -17,7 +17,7 @@ export const NUMBER: any = 10304050; * instanceof Number === false * instanceof Object === false */ -export const NUMBER_INSTANCE: any = Number(10304050); +export const NUMBER_INSTANCE: any = Number(NUMBER); /** * typeof === 'number' @@ -25,4 +25,4 @@ export const NUMBER_INSTANCE: any = Number(10304050); * instanceof Number === true * instanceof Object === true */ -export const NUMBER_NEW_INSTANCE: any = new Number(10304050); +export const NUMBER_NEW_INSTANCE: any = new Number(NUMBER); diff --git a/packages/type/src/is/test/variables/object.const.ts b/packages/type/src/is/test/variables/object.const.ts index 7cf76366..3088398c 100644 --- a/packages/type/src/is/test/variables/object.const.ts +++ b/packages/type/src/is/test/variables/object.const.ts @@ -1,4 +1,14 @@ -export interface ObjectOne { x: number; } +import { SYMBOL_NUMBER, SYMBOL_STRING } from './symbol.const'; +import { STRING } from './string.const'; +import { NUMBER } from './number.const'; +export interface ObjectOne { + 'key as string'?: boolean; + 1030405027?: string; + 5?: string; + [SYMBOL_NUMBER]?: string; + [SYMBOL_STRING]?: number; + x: number; +} export interface ObjectTwo { x: string; y: number; } /** * @example https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects @@ -12,7 +22,16 @@ export interface ObjectTwo { x: string; y: number; } * instanceof String === false * instanceof Symbol === false */ -export const OBJECT_ONE: ObjectOne = { x: 3000 }; +export const OBJECT_ONE: ObjectOne = { + 'key as string': true, + 1030405027: 'key is number', + 5: 'key is also number', + [NUMBER]: 'key is number', + [STRING]: 'key is string', + [SYMBOL_NUMBER]: 'key is symbol number', + [SYMBOL_STRING]: 6, + x: 3000 +}; /** * typeof === 'object' diff --git a/packages/type/src/is/test/variables/symbol.const.ts b/packages/type/src/is/test/variables/symbol.const.ts index 40ba9a8b..72ba6f5c 100644 --- a/packages/type/src/is/test/variables/symbol.const.ts +++ b/packages/type/src/is/test/variables/symbol.const.ts @@ -13,5 +13,5 @@ import { STRING } from './string.const'; * instanceof String === false * instanceof Symbol === false */ -export const SYMBOL_NUMBER: any = Symbol(NUMBER); -export const SYMBOL_STRING: any = Symbol(STRING); +export const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER); +export const SYMBOL_STRING: unique symbol = Symbol(STRING); diff --git a/packages/type/src/is/type/is-array.type.ts b/packages/type/src/is/type/is-array.type.ts index fd118f1a..375e273a 100644 --- a/packages/type/src/is/type/is-array.type.ts +++ b/packages/type/src/is/type/is-array.type.ts @@ -1 +1,2 @@ -export type IsArray = (value: any) => value is Array; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsArray = (value: any, callback?: ResultCallback) => value is Array; diff --git a/packages/type/src/is/type/is-big-int.type.ts b/packages/type/src/is/type/is-big-int.type.ts index 7242741a..8bf35b21 100644 --- a/packages/type/src/is/type/is-big-int.type.ts +++ b/packages/type/src/is/type/is-big-int.type.ts @@ -1 +1,2 @@ -export type IsBigInt = (value: any) => value is bigint; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsBigInt = (value: any, callback?: ResultCallback) => value is bigint; diff --git a/packages/type/src/is/type/is-boolean-object.type.ts b/packages/type/src/is/type/is-boolean-object.type.ts new file mode 100644 index 00000000..6a7068cb --- /dev/null +++ b/packages/type/src/is/type/is-boolean-object.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type IsBooleanObject = (value: any, callback?: ResultCallback) => value is boolean; diff --git a/packages/type/src/is/type/is-boolean-type.type.ts b/packages/type/src/is/type/is-boolean-type.type.ts new file mode 100644 index 00000000..66b74f80 --- /dev/null +++ b/packages/type/src/is/type/is-boolean-type.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type IsBooleanType = (value: any, callback?: ResultCallback) => value is boolean; diff --git a/packages/type/src/is/type/is-boolean.type.ts b/packages/type/src/is/type/is-boolean.type.ts index 544d315c..1a7dc6fb 100644 --- a/packages/type/src/is/type/is-boolean.type.ts +++ b/packages/type/src/is/type/is-boolean.type.ts @@ -1 +1,2 @@ -export type IsBoolean = (value: any) => value is boolean; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsBoolean = (value: any, callback?: ResultCallback) => value is boolean; diff --git a/packages/type/src/is/type/is-defined.type.ts b/packages/type/src/is/type/is-defined.type.ts index 1a5fcb73..14732e2b 100644 --- a/packages/type/src/is/type/is-defined.type.ts +++ b/packages/type/src/is/type/is-defined.type.ts @@ -1 +1,2 @@ -export type IsDefined = (value: unknown) => boolean; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsDefined = (value: unknown, callback?: ResultCallback) => boolean; diff --git a/packages/type/src/is/type/is-function.type.ts b/packages/type/src/is/type/is-function.type.ts index 432a24f5..4e6db5b2 100644 --- a/packages/type/src/is/type/is-function.type.ts +++ b/packages/type/src/is/type/is-function.type.ts @@ -1,2 +1,3 @@ import { Func } from '../../type/func.type'; -export type IsFunction = (value: any) => value is Func; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsFunction = (value: any, callback?: ResultCallback) => value is Func; diff --git a/packages/type/src/is/type/is-instance.type.ts b/packages/type/src/is/type/is-instance.type.ts index 6ad50878..8ea5eb84 100644 --- a/packages/type/src/is/type/is-instance.type.ts +++ b/packages/type/src/is/type/is-instance.type.ts @@ -1,2 +1,3 @@ import { Constructor } from '../../type/constructor.type'; -export type IsInstance = (value: any, instance: Constructor) => value is Obj; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsInstance = (value: any, instance: Constructor, callback?: ResultCallback) => value is Obj; diff --git a/packages/type/src/is/type/is-key.type.ts b/packages/type/src/is/type/is-key.type.ts new file mode 100644 index 00000000..98fa4ded --- /dev/null +++ b/packages/type/src/is/type/is-key.type.ts @@ -0,0 +1,3 @@ +import { Key } from '../../type/key.type'; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsKey = (value: any, callback?: ResultCallback) => value is Key; diff --git a/packages/type/src/is/type/is-null.type.ts b/packages/type/src/is/type/is-null.type.ts index ec9502fb..dc464b0b 100644 --- a/packages/type/src/is/type/is-null.type.ts +++ b/packages/type/src/is/type/is-null.type.ts @@ -1 +1,2 @@ -export type IsNull = (value: any) => value is null; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsNull = (value: any, callback?: ResultCallback) => value is null; diff --git a/packages/type/src/is/type/is-number-object.type.ts b/packages/type/src/is/type/is-number-object.type.ts new file mode 100644 index 00000000..9e894dd1 --- /dev/null +++ b/packages/type/src/is/type/is-number-object.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type IsNumberObject = (value: any, callback?: ResultCallback) => value is number; diff --git a/packages/type/src/is/type/is-number-type.type.ts b/packages/type/src/is/type/is-number-type.type.ts new file mode 100644 index 00000000..ed9df0db --- /dev/null +++ b/packages/type/src/is/type/is-number-type.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type IsNumberType = (value: any, callback?: ResultCallback) => value is number; diff --git a/packages/type/src/is/type/is-number.type.ts b/packages/type/src/is/type/is-number.type.ts index 6bd36895..d959f5b8 100644 --- a/packages/type/src/is/type/is-number.type.ts +++ b/packages/type/src/is/type/is-number.type.ts @@ -1 +1,2 @@ -export type IsNumber = (value: any) => value is number; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsNumber = (value: any, callback?: ResultCallback) => value is number; diff --git a/packages/type/src/is/type/is-object-key.type.ts b/packages/type/src/is/type/is-object-key.type.ts new file mode 100644 index 00000000..413db995 --- /dev/null +++ b/packages/type/src/is/type/is-object-key.type.ts @@ -0,0 +1,3 @@ +import { Key } from '../../type/key.type'; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsObjectKey = (value: any, key: Key | Key[], callback?: ResultCallback) => value is Type; diff --git a/packages/type/src/is/type/is-object.type.ts b/packages/type/src/is/type/is-object.type.ts index 6542dbc2..7cc12901 100644 --- a/packages/type/src/is/type/is-object.type.ts +++ b/packages/type/src/is/type/is-object.type.ts @@ -1 +1,2 @@ -export type IsObject = (value: any, key?: string) => value is Obj; +import { Key } from '../../type/key.type'; +export type IsObject = (value: any, key?: Key) => value is Obj; diff --git a/packages/type/src/is/type/is-primitive.type.ts b/packages/type/src/is/type/is-primitive.type.ts index c951bbbe..f0aa4033 100644 --- a/packages/type/src/is/type/is-primitive.type.ts +++ b/packages/type/src/is/type/is-primitive.type.ts @@ -1,2 +1,4 @@ +import { Primitive } from '../../type/primitive.type'; import { Primitives } from '../../type/primitives.type'; -export type IsPrimitive = (value: any, type: Primitives) => value is Type; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsPrimitive = (value: any, type: Primitives, callback?: ResultCallback) => value is T; diff --git a/packages/type/src/is/type/is-string-object.type.ts b/packages/type/src/is/type/is-string-object.type.ts new file mode 100644 index 00000000..62019501 --- /dev/null +++ b/packages/type/src/is/type/is-string-object.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type IsStringObject = (value: any, callback?: ResultCallback) => value is string; diff --git a/packages/type/src/is/type/is-string-type.type.ts b/packages/type/src/is/type/is-string-type.type.ts new file mode 100644 index 00000000..bf8b4e4e --- /dev/null +++ b/packages/type/src/is/type/is-string-type.type.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../../type/result-callback.type'; +export type IsStringType = (value: any, callback?: ResultCallback) => value is string; diff --git a/packages/type/src/is/type/is-string.type.ts b/packages/type/src/is/type/is-string.type.ts index 996c5927..03876e5b 100644 --- a/packages/type/src/is/type/is-string.type.ts +++ b/packages/type/src/is/type/is-string.type.ts @@ -1 +1,2 @@ -export type IsString = (value: any) => value is string; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsString = (value: any, callback?: ResultCallback) => value is string; diff --git a/packages/type/src/is/type/is-symbol.type.ts b/packages/type/src/is/type/is-symbol.type.ts index ad5209ff..a3466e95 100644 --- a/packages/type/src/is/type/is-symbol.type.ts +++ b/packages/type/src/is/type/is-symbol.type.ts @@ -1 +1,2 @@ -export type IsSymbol = (value: any) => value is symbol; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsSymbol = (value: any, callback?: ResultCallback) => value is symbol; diff --git a/packages/type/src/is/type/is-type.type.ts b/packages/type/src/is/type/is-type.type.ts index 546a16cd..b62b760d 100644 --- a/packages/type/src/is/type/is-type.type.ts +++ b/packages/type/src/is/type/is-type.type.ts @@ -1,2 +1,4 @@ +import { ResultCallback } from '../../type/result-callback.type'; +import { Type } from '../../type/type.type'; import { Types } from '../../type/types.type'; -export type IsType = (value: any, type: Types) => value is Type; +export type IsType = (value: any, type: Types, callback?: ResultCallback) => value is T; diff --git a/packages/type/src/is/type/is-undefined.type.ts b/packages/type/src/is/type/is-undefined.type.ts index cd0babd5..e78d5a70 100644 --- a/packages/type/src/is/type/is-undefined.type.ts +++ b/packages/type/src/is/type/is-undefined.type.ts @@ -1 +1,2 @@ -export type IsUndefined = (value: any) => value is undefined; +import { ResultCallback } from '../../type/result-callback.type'; +export type IsUndefined = (value: any, callback?: ResultCallback) => value is undefined; diff --git a/packages/type/src/lib/bigint-object.class.ts b/packages/type/src/lib/bigint-object.class.ts new file mode 100644 index 00000000..186756f9 --- /dev/null +++ b/packages/type/src/lib/bigint-object.class.ts @@ -0,0 +1,11 @@ +import { PrimitiveObject } from './primitive-object.class'; + +export class BigIntObject { + static set set(value: any) { + PrimitiveObject.bigint = BigInt(value); + } + static get get(): BigInt { + return PrimitiveObject.bigint; + } +} + diff --git a/packages/type/src/lib/boolean-object.class.ts b/packages/type/src/lib/boolean-object.class.ts new file mode 100644 index 00000000..632f34b7 --- /dev/null +++ b/packages/type/src/lib/boolean-object.class.ts @@ -0,0 +1,14 @@ +import { PrimitiveObject } from './primitive-object.class'; + +export class BooleanObject { + /** + * `false` when empty, 0, null, '', false + * `true` when 'true', 'false', 'Su Lin whatever', [], {}, true + */ + static set set(value: any) { + PrimitiveObject.boolean = new Boolean(value); + } + static get get(): Boolean { + return PrimitiveObject.boolean; + } +} diff --git a/packages/type/src/lib/number-object.class.ts b/packages/type/src/lib/number-object.class.ts new file mode 100644 index 00000000..30f19bdb --- /dev/null +++ b/packages/type/src/lib/number-object.class.ts @@ -0,0 +1,10 @@ +import { PrimitiveObject } from './primitive-object.class'; + +export class NumberObject { + static set set(value: any) { + PrimitiveObject.number = new Number(value); + } + static get get(): Number { + return PrimitiveObject.number; + } +} diff --git a/packages/type/src/lib/primitive-object.class.ts b/packages/type/src/lib/primitive-object.class.ts new file mode 100644 index 00000000..6aeea8f5 --- /dev/null +++ b/packages/type/src/lib/primitive-object.class.ts @@ -0,0 +1,7 @@ +export class PrimitiveObject { + static bigint: BigInt; + static boolean: Boolean; + static number: Number; + static string: String; + static symbol: Symbol; +} diff --git a/packages/type/src/lib/result-callback.func.ts b/packages/type/src/lib/result-callback.func.ts new file mode 100644 index 00000000..b917ba76 --- /dev/null +++ b/packages/type/src/lib/result-callback.func.ts @@ -0,0 +1,2 @@ +import { ResultCallback } from '../type/result-callback.type'; +export const resultCallback: ResultCallback = (result: boolean): boolean => result; diff --git a/packages/type/src/lib/string-object.class.ts b/packages/type/src/lib/string-object.class.ts new file mode 100644 index 00000000..234536bb --- /dev/null +++ b/packages/type/src/lib/string-object.class.ts @@ -0,0 +1,10 @@ +import { PrimitiveObject } from './primitive-object.class'; + +export class StringObject { + static set set(value: any) { + PrimitiveObject.string = new String(value); + } + static get get(): String { + return PrimitiveObject.string; + } +} diff --git a/packages/type/src/lib/symbol-object.class.ts b/packages/type/src/lib/symbol-object.class.ts new file mode 100644 index 00000000..a45e96c3 --- /dev/null +++ b/packages/type/src/lib/symbol-object.class.ts @@ -0,0 +1,10 @@ +import { PrimitiveObject } from './primitive-object.class'; + +export class SymbolObject { + static set set(value: string | number | undefined) { + PrimitiveObject.symbol = Symbol(value); + } + static get get(): Symbol { + return PrimitiveObject.symbol; + } +} diff --git a/packages/type/src/public-api.ts b/packages/type/src/public-api.ts index cf993bb3..65c6856b 100644 --- a/packages/type/src/public-api.ts +++ b/packages/type/src/public-api.ts @@ -8,13 +8,20 @@ export { guard } from './guard'; // `guard` functions. export { guardArray, + guardBigInt, + guardBoolean, guardFunction, + guardInstance, + guardKey, + guardNull, guardNumber, guardObject, guardObjectKey, guardPrimitive, guardString, - guardType + guardSymbol, + guardType, + guardUndefined } from './guard'; // `are` object and functions. @@ -31,14 +38,22 @@ export { isArray, isBigInt, isBoolean, + isBooleanObject, + isBooleanType, isDefined, isFunction, isInstance, + isKey, isNull, isNumber, + isNumberObject, + isNumberType, isObject, + isObjectKey, isPrimitive, isString, + isStringObject, + isStringType, isSymbol, isType, isUndefined @@ -56,4 +71,4 @@ export { } from './is/not'; // Types. -export { Constructor, CycleHook, Func, Primitive, Primitives, Types } from './type'; +export { Constructor, CycleHook, Func, Key, Primitive, Primitives, ResultCallback, Type, Types } from './type'; diff --git a/packages/type/src/test/bigint-object.spec.ts b/packages/type/src/test/bigint-object.spec.ts new file mode 100644 index 00000000..e7221145 --- /dev/null +++ b/packages/type/src/test/bigint-object.spec.ts @@ -0,0 +1,27 @@ +import { BIGINT, BIGINT_EXPECTATION, BIGINT_INSTANCE } from '../is/test/variables/big-int.const'; +import { BigIntObject } from '../lib/bigint-object.class'; + + +describe(`BigIntObject`, () => { + // Defined. + it('is DEFINED', () => expect(BigIntObject).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`bigint`, () => { + it(`${BIGINT}`, () => { + BigIntObject.set = BIGINT; + expect(BigIntObject.get).toEqual(BIGINT); + }); + it(`${BIGINT_EXPECTATION}`, () => { + BigIntObject.set = BIGINT_INSTANCE; + expect(BigIntObject.get).toEqual(BIGINT_INSTANCE); + }); + }); + }); + }); +}); diff --git a/packages/type/src/test/boolean-object.spec.ts b/packages/type/src/test/boolean-object.spec.ts new file mode 100644 index 00000000..6f2baf11 --- /dev/null +++ b/packages/type/src/test/boolean-object.spec.ts @@ -0,0 +1,51 @@ +import { TRUE, TRUE_EXPECTATION, TRUE_INSTANCE, FALSE, FALSE_INSTANCE, FALSE_EXPECTATION } from '../is/test/variables/boolean.const'; +import { BooleanObject } from '../lib/boolean-object.class'; + + +describe(`BooleanObject`, () => { + // Defined. + it('is DEFINED', () => expect(BooleanObject).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + + // ... primitives. + describe(`primitive`, () => { + // bigint + describe(`boolean`, () => { + + it(`${TRUE}`, () => { + BooleanObject.set = TRUE; + expect(BooleanObject.get).not.toEqual(FALSE_INSTANCE); + expect(BooleanObject.get.valueOf()).toBeInstanceOf(Boolean); + expect(BooleanObject.get.valueOf()).toBeTruthy(); + expect(BooleanObject.get.valueOf()).toBe(TRUE); + }); + + it(`${TRUE_EXPECTATION}`, () => { + BooleanObject.set = TRUE_INSTANCE; + expect(BooleanObject.get.valueOf()).toBeInstanceOf(Boolean); + expect(BooleanObject.get.valueOf()).toBeTrue(); + expect(BooleanObject.get.valueOf()).toBeTruthy(); + expect(BooleanObject.get.valueOf()).toBe(TRUE); + }); + + it(`${FALSE}`, () => { + BooleanObject.set = FALSE; + expect(BooleanObject.get).not.toEqual(TRUE_INSTANCE); + expect(BooleanObject.get.valueOf()).toBeInstanceOf(Boolean); + expect(BooleanObject.get.valueOf()).toBeFalse(); + expect(BooleanObject.get.valueOf()).toBe(FALSE); + }); + + it(`${FALSE_EXPECTATION}`, () => { + BooleanObject.set = FALSE_INSTANCE; + expect(BooleanObject.get.valueOf().valueOf()).toBeInstanceOf(Boolean); + expect(BooleanObject.get.valueOf().valueOf()).toBeTrue(); + expect(BooleanObject.get.valueOf().valueOf()).toBeTruthy(); + expect(BooleanObject.get.valueOf().valueOf()).toBe(TRUE); + }); + }); + }); + }); +}); diff --git a/packages/type/src/test/number-object.spec.ts b/packages/type/src/test/number-object.spec.ts new file mode 100644 index 00000000..381857fc --- /dev/null +++ b/packages/type/src/test/number-object.spec.ts @@ -0,0 +1,27 @@ +import { NUMBER, NUMBER_INSTANCE, NUMBER_NEW_INSTANCE } from '../is/test/variables/number.const'; +import { NumberObject } from '../lib/number-object.class'; + + +describe(`NumberObject`, () => { + // Defined. + it('is DEFINED', () => expect(NumberObject).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + + // ... primitives. + describe(`primitive`, () => { + // number + describe(`number`, () => { + NumberObject.set = NUMBER; + it(`${NUMBER}`, () => expect(NumberObject.get).toEqual(NUMBER)); + + NumberObject.set = NUMBER_INSTANCE; + it(`Number(${NUMBER})`, () => expect(NumberObject.get).toEqual(NUMBER_INSTANCE)); + + NumberObject.set = NUMBER_NEW_INSTANCE; + it(`new Number(${NUMBER})`, () => expect(NumberObject.get).toEqual(NUMBER_NEW_INSTANCE)); + }); + }); + }); +}); diff --git a/packages/type/src/test/string-object.spec.ts b/packages/type/src/test/string-object.spec.ts new file mode 100644 index 00000000..37606814 --- /dev/null +++ b/packages/type/src/test/string-object.spec.ts @@ -0,0 +1,27 @@ +import { StringObject } from '../lib/string-object.class'; +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../is/test/variables/string.const'; + + +describe(`StringObject`, () => { + // Defined. + it('is DEFINED', () => expect(StringObject).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + + // ... primitives. + describe(`primitive`, () => { + // string + describe(`string`, () => { + StringObject.set = STRING; + it(`${STRING}`, () => expect(StringObject.get).toEqual(STRING)); + + StringObject.set = STRING_INSTANCE; + it(`Number(${STRING})`, () => expect(StringObject.get).toEqual(STRING_INSTANCE)); + + StringObject.set = STRING_NEW_INSTANCE; + it(`new Number(${STRING})`, () => expect(StringObject.get).toEqual(STRING_NEW_INSTANCE)); + }); + }); + }); +}); diff --git a/packages/type/src/test/symbol-object.spec.ts b/packages/type/src/test/symbol-object.spec.ts new file mode 100644 index 00000000..71db2393 --- /dev/null +++ b/packages/type/src/test/symbol-object.spec.ts @@ -0,0 +1,21 @@ +import { STRING, STRING_INSTANCE, STRING_NEW_INSTANCE } from '../is/test/variables/string.const'; +import { SymbolObject } from '../lib/symbol-object.class'; +import { SYMBOL_NUMBER } from '../is/test/variables/symbol.const'; + +describe(`SymbolObject`, () => { + // Defined. + it('is DEFINED', () => expect(SymbolObject).toBeDefined()); + + // Checks ... + describe(`checks`, () => { + + // ... primitives. + describe(`primitive`, () => { + // string + describe(`symbol`, () => { + SymbolObject.set = STRING; + it(`Symbol(${STRING})`, () => expect(SymbolObject.get).toBeDefined()); + }); + }); + }); +}); diff --git a/packages/type/src/type/constructor.type.ts b/packages/type/src/type/constructor.type.ts index 843ee122..5b3885dd 100644 --- a/packages/type/src/type/constructor.type.ts +++ b/packages/type/src/type/constructor.type.ts @@ -1 +1,4 @@ +/** + * Class type. + */ export type Constructor = new (...args: any[]) => Type; diff --git a/packages/type/src/type/func.type.ts b/packages/type/src/type/func.type.ts index 633f2266..9d3d92a4 100644 --- a/packages/type/src/type/func.type.ts +++ b/packages/type/src/type/func.type.ts @@ -1 +1,4 @@ +/** + * Function type. + */ export type Func = (...param: any) => any; diff --git a/packages/type/src/type/index.ts b/packages/type/src/type/index.ts index fdcd110b..1a88d7a5 100644 --- a/packages/type/src/type/index.ts +++ b/packages/type/src/type/index.ts @@ -1,6 +1,9 @@ export { Constructor } from './constructor.type'; export { CycleHook } from './cycle-hook.type'; export { Func } from './func.type'; +export { Key } from './key.type'; export { Primitive } from './primitive.type'; export { Primitives } from './primitives.type'; +export { ResultCallback } from './result-callback.type'; +export { Type } from './type.type'; export { Types } from './types.type'; diff --git a/packages/type/src/type/key.type.ts b/packages/type/src/type/key.type.ts new file mode 100644 index 00000000..0e37731d --- /dev/null +++ b/packages/type/src/type/key.type.ts @@ -0,0 +1,4 @@ +/** + * Name of the `object` property. + */ +export type Key = number | string | symbol; diff --git a/packages/type/src/type/primitive.type.ts b/packages/type/src/type/primitive.type.ts index b68d2179..86588765 100644 --- a/packages/type/src/type/primitive.type.ts +++ b/packages/type/src/type/primitive.type.ts @@ -1 +1,4 @@ +/** + * All primitives. + */ export type Primitive = boolean | bigint | null | number | string | symbol | undefined; diff --git a/packages/type/src/type/primitives.type.ts b/packages/type/src/type/primitives.type.ts index 18f0165a..c09ad762 100644 --- a/packages/type/src/type/primitives.type.ts +++ b/packages/type/src/type/primitives.type.ts @@ -1 +1,4 @@ -export type Primitives = 'bigint' | 'boolean' | 'number' | 'symbol' | 'string' | 'undefined'; +/** + * All primitives as `string`. + */ +export type Primitives = 'bigint' | 'boolean' | 'null' | 'number' | 'string' | 'symbol' | 'undefined'; diff --git a/packages/type/src/type/result-callback.type.ts b/packages/type/src/type/result-callback.type.ts new file mode 100644 index 00000000..daacc602 --- /dev/null +++ b/packages/type/src/type/result-callback.type.ts @@ -0,0 +1 @@ +export type ResultCallback = (result: boolean) => boolean; diff --git a/packages/type/src/type/type.type.ts b/packages/type/src/type/type.type.ts new file mode 100644 index 00000000..db8ab3c9 --- /dev/null +++ b/packages/type/src/type/type.type.ts @@ -0,0 +1,6 @@ +import { Func } from './func.type'; +import { Primitive } from './primitive.type'; +/** + * Main types. + */ +export type Type = Func | object | Primitive; diff --git a/packages/type/src/type/types.type.ts b/packages/type/src/type/types.type.ts index 31ee2908..dae586b8 100644 --- a/packages/type/src/type/types.type.ts +++ b/packages/type/src/type/types.type.ts @@ -1,3 +1,6 @@ import { Constructor } from './constructor.type'; import { Primitives } from './primitives.type'; +/** + * Main types as `string` values. + */ export type Types = Constructor | 'function' | 'object' | Primitives;