README
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';
// positives
check.isUndefined(undefined) // true
check.isNull(null) // true
check.isBoolean(true) // true
check.isNumber(0) // true
check.isNumber(parseInt('x')) // true (NaN is a number)
check.isNumber(1 / 0) // true (+Infinity is a number)
check.isString('') // true
check.isArray([]) // true
check.isObject({}) // true
check.isFunction(() => {}) // true
(function () {
check.isArguments(arguments) // true
})()
check.isRegExp(/.*/) // true
check.isDate(new Date()) // true
// negatives
check.isNotUndefined(undefined) // false
check.isNotNull(null) // false
check.isNotBoolean(true) // false
check.isNotNumber(0) // false
check.isNotNumber(parseInt('x')) // false (NaN is a number)
check.isNotNumber(1 / 0) // false (+Infinity is a number)
check.isNotString('') // false
check.isNotArray([]) // false
check.isNotObject({}) // false
check.isNotFunction(() => {}) // false
(function () {
check.isNotArguments(arguments) // false
})()
check.isNotRegExp(/.*/) // false
check.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
and isIterable(test)isNotIterable(test)
TODO: add sample code
Checks for array-like objects:
TODO: add explanation
and isArrayLike(test)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) // true
check.isMap(new Map()) // true
check.getType(new Set()) // 'set'
check.isError(new Error()) // true
check.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