🔀 🔨 merging and quick fixes
This commit is contained in:
commit
23caa33a5a
1 changed files with 66 additions and 59 deletions
125
index.js
125
index.js
|
|
@ -1,6 +1,10 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
const toString = Object.prototype.toString;
|
const toString = Object.prototype.toString;
|
||||||
const getObjectType = x => toString.call(x).slice(8, -1);
|
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;
|
||||||
|
|
||||||
const is = value => {
|
const is = value => {
|
||||||
if (value == null) { // eslint-disable-line no-eq-null, eqeqeq
|
if (value == null) { // eslint-disable-line no-eq-null, eqeqeq
|
||||||
|
|
@ -29,7 +33,7 @@ const is = value => {
|
||||||
return 'symbol';
|
return 'symbol';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'function') {
|
if (is.function(value)) {
|
||||||
return 'Function';
|
return 'Function';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,78 +57,68 @@ const is = value => {
|
||||||
return 'Object';
|
return 'Object';
|
||||||
};
|
};
|
||||||
|
|
||||||
is.undefined = x => typeof x === 'undefined';
|
is.undefined = isOfType('undefined');
|
||||||
is.null = x => x === null;
|
is.null = x => x === null;
|
||||||
is.string = x => typeof x === 'string';
|
is.string = isOfType('string');
|
||||||
is.number = x => typeof x === 'number';
|
is.number = isOfType('number');
|
||||||
is.boolean = x => typeof x === 'boolean';
|
is.boolean = isOfType('boolean');
|
||||||
is.symbol = x => typeof x === 'symbol';
|
is.symbol = isOfType('symbol');
|
||||||
|
|
||||||
is.array = Array.isArray;
|
is.array = Array.isArray;
|
||||||
is.function = x => typeof x === 'function';
|
is.function = isOfType('function');
|
||||||
is.buffer = Buffer.isBuffer;
|
is.buffer = Buffer.isBuffer;
|
||||||
|
|
||||||
is.object = x => {
|
const isObject = x => typeof x === 'object';
|
||||||
const type = typeof x;
|
|
||||||
return x !== null && (type === 'object' || type === 'function');
|
|
||||||
};
|
|
||||||
|
|
||||||
is.nativePromise = x => getObjectType(x) === 'Promise';
|
is.object = x => !is.nullOrUndefined(x) && (is.function(x) || isObject(x));
|
||||||
|
|
||||||
is.promise = x => {
|
is.nativePromise = isObjectOfType('Promise');
|
||||||
return is.nativePromise(x) ||
|
|
||||||
(
|
const hasPromiseAPI = x =>
|
||||||
x !== null &&
|
!is.null(x) &&
|
||||||
typeof x === 'object' &&
|
isObject(x) &&
|
||||||
typeof x.then === 'function' &&
|
is.function(x.then) &&
|
||||||
typeof x.catch === 'function'
|
is.function(x.catch);
|
||||||
);
|
|
||||||
};
|
is.promise = x => is.nativePromise(x) || hasPromiseAPI(x);
|
||||||
|
|
||||||
is.generator = x => is.iterable(x) && is.function(x.next) && is.function(x.throw);
|
is.generator = x => is.iterable(x) && is.function(x.next) && is.function(x.throw);
|
||||||
|
|
||||||
is.generatorFunction = x => getObjectType(x) === 'GeneratorFunction';
|
// Change to use `isObjectOfType` once Node 4.x.x LTS is dropped
|
||||||
|
is.generatorFunction = x => x.constructor.name === 'GeneratorFunction';
|
||||||
|
|
||||||
is.regExp = x => getObjectType(x) === 'RegExp';
|
is.regExp = isObjectOfType('RegExp');
|
||||||
is.date = x => getObjectType(x) === 'Date';
|
is.date = isObjectOfType('Date');
|
||||||
is.error = x => getObjectType(x) === 'Error';
|
is.error = isObjectOfType('Error');
|
||||||
is.map = x => getObjectType(x) === 'Map';
|
is.map = isObjectOfType('Map');
|
||||||
is.set = x => getObjectType(x) === 'Set';
|
is.set = isObjectOfType('Set');
|
||||||
is.weakMap = x => getObjectType(x) === 'WeakMap';
|
is.weakMap = isObjectOfType('WeakMap');
|
||||||
is.weakSet = x => getObjectType(x) === 'WeakSet';
|
is.weakSet = isObjectOfType('WeakSet');
|
||||||
|
|
||||||
is.int8Array = x => getObjectType(x) === 'Int8Array';
|
is.int8Array = isObjectOfType('Int8Array');
|
||||||
is.uint8Array = x => getObjectType(x) === 'Uint8Array';
|
is.uint8Array = isObjectOfType('Uint8Array');
|
||||||
is.uint8ClampedArray = x => getObjectType(x) === 'Uint8ClampedArray';
|
is.uint8ClampedArray = isObjectOfType('Uint8ClampedArray');
|
||||||
is.int16Array = x => getObjectType(x) === 'Int16Array';
|
is.int16Array = isObjectOfType('Int16Array');
|
||||||
is.uint16Array = x => getObjectType(x) === 'Uint16Array';
|
is.uint16Array = isObjectOfType('Uint16Array');
|
||||||
is.int32Array = x => getObjectType(x) === 'Int32Array';
|
is.int32Array = isObjectOfType('Int32Array');
|
||||||
is.uint32Array = x => getObjectType(x) === 'Uint32Array';
|
is.uint32Array = isObjectOfType('Uint32Array');
|
||||||
is.float32Array = x => getObjectType(x) === 'Float32Array';
|
is.float32Array = isObjectOfType('Float32Array');
|
||||||
is.float64Array = x => getObjectType(x) === 'Float64Array';
|
is.float64Array = isObjectOfType('Float64Array');
|
||||||
|
|
||||||
is.arrayBuffer = x => getObjectType(x) === 'ArrayBuffer';
|
is.arrayBuffer = isObjectOfType('ArrayBuffer');
|
||||||
|
is.sharedArrayBuffer = isObjectOfType('SharedArrayBuffer');
|
||||||
is.sharedArrayBuffer = x => {
|
|
||||||
try {
|
|
||||||
return getObjectType(x) === 'SharedArrayBuffer';
|
|
||||||
} catch (err) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
is.nan = Number.isNaN;
|
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 primitiveTypes = new Set([
|
||||||
const type = typeof x;
|
'undefined',
|
||||||
return x === null ||
|
'string',
|
||||||
type === 'undefined' ||
|
'number',
|
||||||
type === 'string' ||
|
'boolean',
|
||||||
type === 'number' ||
|
'symbol'
|
||||||
type === 'boolean' ||
|
]);
|
||||||
type === 'symbol';
|
is.primitive = x => is.null(x) || primitiveTypes.has(typeof x);
|
||||||
};
|
|
||||||
|
|
||||||
is.integer = Number.isInteger;
|
is.integer = Number.isInteger;
|
||||||
|
|
||||||
|
|
@ -137,9 +131,9 @@ is.plainObject = x => {
|
||||||
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 => typeof x === 'function' && x.toString().startsWith('class ');
|
is.class = x => is.function(x) && x.toString().startsWith('class ');
|
||||||
|
|
||||||
const typedArrayTypes = new Set([
|
const typedArrayTypes = new Set([
|
||||||
'Int8Array',
|
'Int8Array',
|
||||||
|
|
@ -154,4 +148,17 @@ const typedArrayTypes = new Set([
|
||||||
]);
|
]);
|
||||||
is.typedArray = x => typedArrayTypes.has(getObjectType(x));
|
is.typedArray = x => typedArrayTypes.has(getObjectType(x));
|
||||||
|
|
||||||
|
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;
|
module.exports = is;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue