Collection of small javascript type check functions.
npm install @spaceavocado/type-checknpm install -D @spaceavocado/type-check` or `yarn add @spaceavocado/type-check -D`
Usage
The library is built as Exported as CommonJS module and as ESM module.
$3
`javascript
import tc from '@spaceavocado/type-check';
tc.isFunction(value);
`
The tc object has not property which holds negated version of all functions. e.g. tc.isString(), tc.not.isString().
$3
`javascript
import {isFunction, isArray} from '@spaceavocado/type-check';
`
Type Check Functions
$3
`javascript
tc.isString('value');
// => true
tc.not.isString('value');
// => false
`
$3
Supports string and array empty check, any other types throws an error (tc.TypeCheckError).
`javascript
// String value
tc.isEmpty('');
// => true
// Array value
tc.isEmpty([]);
// => true
tc.isEmpty({});
// => throws tc.TypeCheckError
// String value
tc.not.isEmpty('');
// => false
// Array value
tc.not.isEmpty([]);
// => false
`
$3
`javascript
tc.isNumber(5);
// => true
tc.not.isNumber(5);
// => false
`
$3
`javascript
tc.isFunction(() => {});
// => true
tc.not.isFunction(() => {});
// => false
`
$3
`javascript
tc.isObject({});
// => true
tc.not.isObject({});
// => false
`
$3
`javascript
tc.isError(new Error('error message'));
// => true
tc.not.isError(new Error('error message'));
// => false
`
$3
`javascript
tc.isNullOrUndefined(undefined);
// => true
tc.isNullOrUndefined(null);
// => true
tc.not.isNullOrUndefined(undefined);
// => false
`
$3
`javascript
tc.isArray([]);
// => true
tc.not.isArray([]);
// => false
`
$3
`javascript
tc.isSymbol(new Symbol());
// => true
tc.not.isSymbol(new Symbol());
// => false
`
$3
`javascript
tc.isPromise(new Promise((resolve, reject) => {}));
// => true
tc.not.isPromise(new Promise((resolve, reject) => {}));
// => false
`
$3
`javascript
const myEnum = {
A: 'A',
B: 'B',
}
tc.isEnumKey(myEnum.A, myEnum);
// => true
tc.isEnumKey('C', myEnum);
// => false
tc.not.isEnumKey(myEnum.A, myEnum);
// => false
``