istypes
istypes
is a library that allows type-checking of javascript instances and also a way to group a list of items by type.
Installation
$ npm install --save istypes
or
$ yarn add istypes
Usage
Type checking API:
All samples are included in tests to guarantee their validity
import { check } from 'istypes'; // positivescheck.isUndefined(undefined) // truecheck.isNull(null) // truecheck.isBoolean(true) // truecheck.isNumber(0) // truecheck.isNumber(parseInt('x')) // true (NaN is a number)check.isNumber(1 / 0) // true (+Infinity is a number)check.isString('') // truecheck.isArray([]) // truecheck.isObject({}) // truecheck.isFunction(() => {}) // true(function () { check.isArguments(arguments) // true})()check.isRegExp(/.*/) // truecheck.isDate(new Date()) // true // negativescheck.isNotUndefined(undefined) // falsecheck.isNotNull(null) // falsecheck.isNotBoolean(true) // falsecheck.isNotNumber(0) // falsecheck.isNotNumber(parseInt('x')) // false (NaN is a number)check.isNotNumber(1 / 0) // false (+Infinity is a number)check.isNotString('') // falsecheck.isNotArray([]) // falsecheck.isNotObject({}) // falsecheck.isNotFunction(() => {}) // false(function () { check.isNotArguments(arguments) // false})()check.isNotRegExp(/.*/) // falsecheck.isNotDate(new Date()) // false
Checks for primitives:
isPrimitive(test)
A test for primitive-ness as found on stackoverflow:
console.log([ // promitives : undefined, null, true, false, 0, parseInt('x'), 1 / 0, -1 / 0, '', Symbol.iterator, // complex: [], {}, function () { }, arguments, /$/, new Date(), new Error('')].map(check.isPrimitive));
Outputs:
[ true, // undefined, true, // null, true, // true, true, // false, true, // 0, true, // parseInt('x'), true, // 1 / 0, true, // -1 / 0, true, // '', true, // Symbol.iterator, false, false, false, false, false, false, false ]
isNotPrimitive(test)
Returns exactly opposite of isPrimitive(test)
Checks for iterables:
TODO: add explanation
isIterable(test)
and isNotIterable(test)
TODO: add sample code
Checks for array-like objects:
TODO: add explanation
isArrayLike(test)
and isNotArrayLike(test)
TODO: add sample code
Type extraction
console.log([ undefined, null, false, 0, '', [], {}, function () { }, arguments, /$/, new Date()].map(check.getType))
Outputs:
[ 'undefined', 'null', 'boolean', 'number', 'string', 'array', 'object', 'function', 'arguments', 'regexp', 'date' ]
Extensibility
Checks are available even for types not included in the library:
import { check as checkGen } from 'istypes'; const check = checkGen(new Map(), new Set(), new Error(), Symbol.iterator, Buffer.from('')); check.isUndefined(undefined) // truecheck.isMap(new Map()) // truecheck.getType(new Set()) // 'set'check.isError(new Error()) // truecheck.isSymbol(Symbol.species) // true// here is a small catch with Buffer:check.isUint8Array(Buffer.from('123')) // true, after all, Buffer is a type 'Uint8Array'check.getType(Buffer.from('456')) // 'uint8array'
Methods getType(input)
, isPrimitive(test)
, and isNotPrimitive(test)
are not affected by extensibility.
Grouping by type:
A convenience method to stack values of an array-like object (e.g. arguments
) under the keys named after items' types in a simple object.
import { groupByType } from 'istypes'; function (/* variable signature, name? : string, options? : object, callback? : function */) { const grouped = groupByType(argusments); if (grouped.string) { // name provided } if (grouped.object) { // options provided } if (grouped['function']) { // callback provided } // ...}
Contributing
Please use the issues page to report a bug or request a feature.
Stay in Touch
License
Author
Andrew Revinsky