@sindresorhus/is
Advanced tools
Comparing version 0.1.0 to 0.2.0
127
index.js
'use strict'; | ||
const util = require('util'); | ||
const toString = Object.prototype.toString; | ||
const getObjectType = x => toString.call(x).slice(8, -1); | ||
const isOfType = type => x => typeof x === type; // eslint-disable-line valid-typeof | ||
const isObjectOfType = type => x => getObjectType(x) === type; | ||
@@ -32,3 +36,3 @@ const is = value => { | ||
if (type === 'function') { | ||
if (is.function(value)) { | ||
return 'Function'; | ||
@@ -57,70 +61,64 @@ } | ||
is.undefined = x => typeof x === 'undefined'; | ||
is.undefined = isOfType('undefined'); | ||
is.null = x => x === null; | ||
is.string = x => typeof x === 'string'; | ||
is.number = x => typeof x === 'number'; | ||
is.boolean = x => typeof x === 'boolean'; | ||
is.symbol = x => typeof x === 'symbol'; | ||
is.string = isOfType('string'); | ||
is.number = isOfType('number'); | ||
is.boolean = isOfType('boolean'); | ||
is.symbol = isOfType('symbol'); | ||
is.array = Array.isArray; | ||
is.function = x => typeof x === 'function'; | ||
is.function = isOfType('function'); | ||
is.buffer = Buffer.isBuffer; | ||
is.object = x => { | ||
const type = typeof x; | ||
return x !== null && (type === 'object' || type === 'function'); | ||
}; | ||
const isObject = x => typeof x === 'object'; | ||
is.nativePromise = x => getObjectType(x) === 'Promise'; | ||
is.object = x => !is.nullOrUndefined(x) && (is.function(x) || isObject(x)); | ||
is.promise = x => { | ||
return is.nativePromise(x) || | ||
( | ||
x !== null && | ||
typeof x === 'object' && | ||
typeof x.then === 'function' && | ||
typeof x.catch === 'function' | ||
); | ||
}; | ||
is.nativePromise = isObjectOfType('Promise'); | ||
is.regExp = x => getObjectType(x) === 'RegExp'; | ||
is.date = x => getObjectType(x) === 'Date'; | ||
is.error = x => getObjectType(x) === 'Error'; | ||
is.map = x => getObjectType(x) === 'Map'; | ||
is.set = x => getObjectType(x) === 'Set'; | ||
is.weakMap = x => getObjectType(x) === 'WeakMap'; | ||
is.weakSet = x => getObjectType(x) === 'WeakSet'; | ||
const hasPromiseAPI = x => | ||
!is.null(x) && | ||
isObject(x) && | ||
is.function(x.then) && | ||
is.function(x.catch); | ||
is.int8Array = x => getObjectType(x) === 'Int8Array'; | ||
is.uint8Array = x => getObjectType(x) === 'Uint8Array'; | ||
is.uint8ClampedArray = x => getObjectType(x) === 'Uint8ClampedArray'; | ||
is.int16Array = x => getObjectType(x) === 'Int16Array'; | ||
is.uint16Array = x => getObjectType(x) === 'Uint16Array'; | ||
is.int32Array = x => getObjectType(x) === 'Int32Array'; | ||
is.uint32Array = x => getObjectType(x) === 'Uint32Array'; | ||
is.float32Array = x => getObjectType(x) === 'Float32Array'; | ||
is.float64Array = x => getObjectType(x) === 'Float64Array'; | ||
is.promise = x => is.nativePromise(x) || hasPromiseAPI(x); | ||
is.arrayBuffer = x => getObjectType(x) === 'ArrayBuffer'; | ||
is.generator = x => is.iterable(x) && is.function(x.next) && is.function(x.throw); | ||
is.sharedArrayBuffer = x => { | ||
try { | ||
return getObjectType(x) === 'SharedArrayBuffer'; | ||
} catch (err) { | ||
return false; | ||
} | ||
}; | ||
// TODO: Change to use `isObjectOfType` once Node.js 6 or higher is targeted | ||
is.generatorFunction = x => is.function(x) && is.function(x.constructor) && x.constructor.name === 'GeneratorFunction'; | ||
is.regExp = isObjectOfType('RegExp'); | ||
is.date = isObjectOfType('Date'); | ||
is.error = isObjectOfType('Error'); | ||
is.map = isObjectOfType('Map'); | ||
is.set = isObjectOfType('Set'); | ||
is.weakMap = isObjectOfType('WeakMap'); | ||
is.weakSet = isObjectOfType('WeakSet'); | ||
is.int8Array = isObjectOfType('Int8Array'); | ||
is.uint8Array = isObjectOfType('Uint8Array'); | ||
is.uint8ClampedArray = isObjectOfType('Uint8ClampedArray'); | ||
is.int16Array = isObjectOfType('Int16Array'); | ||
is.uint16Array = isObjectOfType('Uint16Array'); | ||
is.int32Array = isObjectOfType('Int32Array'); | ||
is.uint32Array = isObjectOfType('Uint32Array'); | ||
is.float32Array = isObjectOfType('Float32Array'); | ||
is.float64Array = isObjectOfType('Float64Array'); | ||
is.arrayBuffer = isObjectOfType('ArrayBuffer'); | ||
is.sharedArrayBuffer = isObjectOfType('SharedArrayBuffer'); | ||
is.nan = Number.isNaN; | ||
is.nullOrUndefined = x => x === null || typeof x === 'undefined'; | ||
is.nullOrUndefined = x => is.null(x) || is.undefined(x); | ||
is.primitive = x => { | ||
const type = typeof x; | ||
return x === null || | ||
type === 'undefined' || | ||
type === 'string' || | ||
type === 'number' || | ||
type === 'boolean' || | ||
type === 'symbol'; | ||
}; | ||
const primitiveTypes = new Set([ | ||
'undefined', | ||
'string', | ||
'number', | ||
'boolean', | ||
'symbol' | ||
]); | ||
is.primitive = x => is.null(x) || primitiveTypes.has(typeof x); | ||
@@ -135,7 +133,9 @@ is.integer = Number.isInteger; | ||
(prototype = Object.getPrototypeOf(x), prototype === null || | ||
prototype === Object.getPrototypeOf({})); | ||
prototype === Object.getPrototypeOf({})); | ||
}; | ||
is.iterable = x => !is.null(x) && !is.undefined(x) && typeof x[Symbol.iterator] === 'function'; | ||
is.iterable = x => !is.nullOrUndefined(x) && is.function(x[Symbol.iterator]); | ||
is.class = x => is.function(x) && x.toString().startsWith('class '); | ||
const typedArrayTypes = new Set([ | ||
@@ -154,2 +154,15 @@ 'Int8Array', | ||
is.inRange = (x, range) => { | ||
if (is.number(range)) { | ||
return x >= Math.min(0, range) && x <= Math.max(range, 0); | ||
} | ||
if (is.array(range) && range.length === 2) { | ||
// TODO: Use spread operator here when targeting Node.js 6 or higher | ||
return x >= Math.min.apply(null, range) && x <= Math.max.apply(null, range); | ||
} | ||
throw new TypeError(`Invalid range: ${util.inspect(range)}`); | ||
}; | ||
module.exports = is; |
{ | ||
"name": "@sindresorhus/is", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Type check values: `is.string('🦄') //=> true`", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -81,2 +81,8 @@ # is [![Build Status](https://travis-ci.org/sindresorhus/is.svg?branch=master)](https://travis-ci.org/sindresorhus/is) | ||
##### .generator(value) | ||
Returns `true` for any object that implements its own `.next()` and `.throw()` methods and has a function definition for `Symbol.iterator`. | ||
##### .generatorFunction(value) | ||
##### .map(value) | ||
@@ -119,5 +125,27 @@ ##### .set(value) | ||
##### .iterable(value) | ||
##### .class(value) | ||
Returns `true` for instances created by a ES2015 class. | ||
##### .typedArray(value) | ||
##### .inRange(value, range) | ||
Check if `value` (number) is in the given `range`. The range is an array of two values, lower bound and upper bound, in no specific order. | ||
```js | ||
is.inRange(3, [0, 5]); | ||
is.inRange(3, [5, 0]); | ||
is.inRange(0, [-2, 2]); | ||
``` | ||
##### .inRange(value, upperBound) | ||
Check if `value` (number) is in the range of `0` to `upperBound`. | ||
```js | ||
is.inRange(3, 10); | ||
``` | ||
## FAQ | ||
@@ -152,4 +180,10 @@ | ||
## Created by | ||
- [Sindre Sorhus](https://github.com/sindresorhus) | ||
- [Giora Guttsait](https://github.com/gioragutt) | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
MIT |
10639
127
187