A Node.js module that tests a value’s type against a string like 'positive integer'
or 'non-empty map'
.
npm install isit --save
When calling isit()
, the first argument is a space-separated string of type tests, and the second argument is the value to be tested. isit()
returns true only if all tests pass.
const isit = require('isit')
isit('non-empty array', [1, 2, 3]) // true
isit('empty map', new Map()) // true
isit('positive integer', 1) // true
Available tests are listed in the “Type Tests” section below. Anything that does not match one of the available tests will be considered a class name (example: map
in the above code block).
A test can be individually negated by prefixing it with non-
or !
, as in:
const isit = require('isit')
isit('non-empty array', [1, 2, 3]) // true
isit('array !empty', [1, 2, 3]) // true
isit('empty non-array', '') // true
If you omit the second argument, a function is returned which runs the test provided in the first argument.
const isObject = require('isit')('non-array object')
isObject({}) // true
isObject([]) // false
All tests are also available as member functions of isit
, allowing you to run a single test like so:
const isit = require('isit')
isit.array([]) // true
const isString = require('isit').string
isString('test') // true
Here is the complete list:
arguments
/args
array
blank
boolean
/bool
boolish
buffer
collection
empty
false
falsey
float
finite
function
generator
infinity
integer
/int
iterable
nan
negative
nil
null
number
numberish
numeric
object
objectbased
plain
positive
primitive
scalar
string
stringish
symbol
true
truthy
typedarray
undefined
/undef
Value: | undefined undef |
null |
nil |
---|---|---|---|
undefined |
✔ | ✔ | |
null |
✔ | ✔ |
Value Type: | primitive |
scalar |
---|---|---|
Undefined | ✔ | |
Null | ✔ | |
Boolean | ✔ | ✔ |
Number | ✔ | ✔ |
String | ✔ | ✔ |
Symbol | ✔ | ✔ |
Object | ||
Function |
Value: | boolean bool |
boolish |
---|---|---|
true |
✔ | ✔ |
false |
✔ | ✔ |
'true' string |
✔ | |
'false' string |
✔ | |
new Boolean(true) |
✔ | |
new Boolean(false) |
✔ |
Value: | true |
truthy |
---|---|---|
true |
✔ | ✔ |
new Boolean(true) |
✔ | ✔ |
'true' string |
✔ | ✔ |
'false' string |
||
1 |
✔ | |
'test' |
✔ | |
[] |
✔ |
Value: | false |
falsey |
---|---|---|
false |
✔ | ✔ |
new Boolean(false) |
✔ | ✔ |
'false' string |
✔ | ✔ |
0 |
✔ | |
'' |
✔ |
Every empty-checker out there assesses “emptiness” a bit differently. For our purposes, an empty value is one which contains no useful information except the absence of a value. Therefore, unlike many similar functions, the empty
test does not consider 0
, false
, or zero-parameter functions to be “empty,” because these can often be intended as actual values.
Value: | empty |
---|---|
undefined |
✔ |
null |
✔ |
NaN |
✔ |
0 |
|
false |
|
'' |
✔ |
{} |
✔ |
[] |
✔ |
() => {} |
|
new Error() |
|
new Map() |
✔ |
new Set() |
✔ |
The blank
test is the same as empty
except it also returns true
for strings that consist only of whitespace.
Value: | function |
generator |
---|---|---|
function () {} |
✔ | |
() => {} |
✔ | |
function* () {} |
✔ | ✔ |
Value: | number |
numberish |
numeric |
---|---|---|---|
0 |
✔ | ✔ | ✔ |
1.23 |
✔ | ✔ | ✔ |
new Number(1) |
✔ | ✔ | |
'1' |
✔ | ||
'1e3' |
✔ | ||
NaN |
Value: | positive |
negative |
---|---|---|
Infinity |
✔ | |
123.45 |
✔ | |
0 |
✔ | |
-0 |
✔ | |
-123.45 |
✔ | |
-Infinity |
✔ |
JavaScript considers the number zero to be either positively or negatively signed; therefore, positive
reports true
for 0
. If you want to exclude zero, consider using a simple x>0
test instead.
More Number Tests:
nan
integer
/int
float
finite
infinity
Although functions are technically objects in JavaScript, they are often considered a separate category because the typeof
operator gives them their own type. Use objectbased
if you want to include functions.
Value: | object |
objectbased |
---|---|---|
{} |
✔ | ✔ |
() => {} |
✔ |
Value: | object |
plain |
array |
---|---|---|---|
new Date() |
✔ | ||
{} |
✔ | ✔ | |
[] |
✔ | ✔ |
The object
test returns true
for arrays, because arrays are objects in JavaScript. If you want to exclude arrays, test against 'non-array object'
instead.
arguments
or args
returns true if the value is an Arguments object.
The buffer
test returns true if the value is a Node.js or Browserify Buffer.
The collection
test returns true if the value is an instance of one of:
- Map
- Set
- WeakMap
- WeakSet
The iterable
test returns true if for...of
can be used to iterate through the value.
The typedarray
test returns true if the value is an instance of one of:
- Int8Array
- Uint8Array
- Uint8ClampedArray
- Int16Array
- Uint16Array
- Int32Array
- Uint32Array
- Float32Array
- Float64Array
Value: | string |
stringish |
---|---|---|
'' |
✔ | ✔ |
'test' |
✔ | ✔ |
new String('test') |
✔ |
The symbol
test returns true if the value is a symbol.
You can use isit.a
or isit.an
to see if an object is an instance of a given class.
You can provide the class itself or the class name as a case-insensitive string.
const isit = require('isit')
isit.a(Date, new Date()) // true
isit.a('date', new Date()) // true
isit.an(Error, new Error()) // true
isit.an('error', new Error()) // true
// Returns true because TypeError extends Error
isit.an(Error, new TypeError()) // true
You can also check if a given value is an instance of any one of a list of classes:
const isit = require('isit')
isit.a([TypeError, ReferenceError], new TypeError()) // true
isit.a('TypeError ReferenceError', new TypeError()) // true
You can add new tests, or override existing ones, by requiring isit/x
and calling it as a function with the additional tests as an object argument. For example:
const isit = require('isit/x')({
zero: value => value === 0
})
isit('negative zero', -0) // true
isit('non-zero integer', 1) // true