No description
Find a file
2017-09-23 16:19:58 +01:00
.editorconfig Init 2017-09-22 00:44:27 +07:00
.gitattributes Init 2017-09-22 00:44:27 +07:00
.gitignore Init 2017-09-22 00:44:27 +07:00
.npmrc Init 2017-09-22 00:44:27 +07:00
.travis.yml Init 2017-09-22 00:44:27 +07:00
header.gif Init 2017-09-22 00:44:27 +07:00
index.js Rename to inRange and reuse the Math module 2017-09-23 16:19:02 +01:00
license Init 2017-09-22 00:44:27 +07:00
package.json 0.1.0 2017-09-22 00:54:26 +07:00
readme.md Update readme to document inRange method 2017-09-23 16:19:58 +01:00
test.js Rename to inRange and reuse the Math module 2017-09-23 16:19:02 +01:00

is Build Status

Type check values: is.string('🦄') //=> true

Install

$ npm install @sindresorhus/is

Usage

const is = require('@sindresorhus/is');

is('🦄');
//=> 'string'

is(new Map());
//=> 'Map'

is.number(6);
//=> true

API

is(value)

Returns the type of value.

Primitives are lowercase and object types are camelcase.

Example:

  • 'undefined'
  • 'null'
  • 'string'
  • 'symbol'
  • 'Array'
  • 'Function'
  • 'Object'

Note: It will throw if you try to feed it object-wrapped primitives, as that's a bad practice. For example new String('foo').

is.{method}

All the below methods accept a value and returns a boolean for whether the value is of the desired type.

Primitives

.undefined(value)
.null(value)
.string(value)
.number(value)
.boolean(value)
.symbol(value)

Built-in types

.array(value)
.function(value)
.buffer(value)
.object(value)

Keep in mind that functions are objects too.

.regExp(value)
.date(value)
.error(value)
.nativePromise(value)
.promise(value)

Returns true for any object with a .then() and .catch() method. Prefer this one over .nativePromise() as you usually want to allow userland promise implementations too.

.map(value)
.set(value)
.weakMap(value)
.weakSet(value)

Typed arrays

.int8Array(value)
.uint8Array(value)
.uint8ClampedArray(value)
.int16Array(value)
.uint16Array(value)
.int32Array(value)
.uint32Array(value)
.float32Array(value)
.float64Array(value)

Structured data

.arrayBuffer(value)
.sharedArrayBuffer(value)
.dataView(value)

Miscellaneous

.nan(value)
.nullOrUndefined(value)
.primitive(value)

JavaScript primitives are as follows: null, undefined, string, number, boolean, symbol.

.integer(value)
.plainObject(value)

An object is plain if it's created by either {}, new Object(), or Object.create(null).

.iterable(value)
.typedArray(value)
.inRange(value, range)

Check to see if the value is within a given range, for example is.inRange(3, [0, 5]). The range supplied can be an array of any size, though this will only check for the minimum and maximum values in it.

FAQ

Why yet another type checking module?

There are hundreds of type checking modules on npm, unfortunately, I couldn't find any that fit my needs:

  • Includes both type methods and ability to get the type
  • Types of primitives returned as lowercase and object types as camelcase
  • Covers all built-ins
  • Unsurprising behavior
  • Well-maintained
  • Comprehensive test suite

For the ones I found, pick 3 of these.

The most common mistakes I noticed in these modules was using instanceof for type checking, forgetting that functions are objects, and omitting symbol as a primitive.

License

MIT © Sindre Sorhus