isit
A Node.js module that tests a value’s type against a string like 'positive integer'
or 'non-empty map'
.
Installation
npm install isit --save
Usage
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 = // true // true // 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).
Negation
A test can be individually negated by prefixing it with non-
or !
, as in:
const isit = // true // true // true
Create Curried Functions
If you omit the second argument, a function is returned which runs the test provided in the first argument.
const isObject = 'non-array object' // true // false
Individual Test Functions
All tests are also available as member functions of isit
, allowing you to run a single test like so:
const isit = isit // true const isString = string // true
Type Tests
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
Undefined & Null
Value: | undefined undef |
null |
nil |
---|---|---|---|
undefined |
✔ | ✔ | |
null |
✔ | ✔ |
Primitives & Scalars
Value Type: | primitive |
scalar |
---|---|---|
Undefined | ✔ | |
Null | ✔ | |
Boolean | ✔ | ✔ |
Number | ✔ | ✔ |
String | ✔ | ✔ |
Symbol | ✔ | ✔ |
Object | ||
Function |
Booleans
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 |
✔ | |
'' |
✔ |
Empty Values
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() |
✔ |
Blank Values
The blank
test is the same as empty
except it also returns true
for strings that consist only of whitespace.
Functions
Value: | function |
generator |
---|---|---|
function () {} |
✔ | |
() => {} |
✔ | |
function* () {} |
✔ | ✔ |
Numbers
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
Objects
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
arguments
or args
returns true if the value is an Arguments object.
Buffers
The buffer
test returns true if the value is a Node.js or Browserify Buffer.
Collections
The collection
test returns true if the value is an instance of one of:
- Map
- Set
- WeakMap
- WeakSet
Iterables
The iterable
test returns true if for...of
can be used to iterate through the value.
Typed Arrays
The typedarray
test returns true if the value is an instance of one of:
- Int8Array
- Uint8Array
- Uint8ClampedArray
- Int16Array
- Uint16Array
- Int32Array
- Uint32Array
- Float32Array
- Float64Array
Strings
Value: | string |
stringish |
---|---|---|
'' |
✔ | ✔ |
'test' |
✔ | ✔ |
new String('test') |
✔ |
Symbols
The symbol
test returns true if the value is a symbol.
Class Testing
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 = isit // trueisit // true isit // trueisit // true // Returns true because TypeError extends Errorisit // true
You can also check if a given value is an instance of any one of a list of classes:
const isit = isit // trueisit // true
Advanced Usage
Adding or Overriding Tests
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 = value === 0 // true // true