parent
26ca195302
commit
4850b97143
2 changed files with 53 additions and 62 deletions
91
index.js
91
index.js
|
|
@ -1,6 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
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 => 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
|
||||||
|
|
@ -59,69 +60,55 @@ is.string = x => typeof x === 'string';
|
||||||
is.number = x => typeof x === 'number';
|
is.number = x => typeof x === 'number';
|
||||||
is.boolean = x => typeof x === 'boolean';
|
is.boolean = x => typeof x === 'boolean';
|
||||||
is.symbol = x => typeof x === 'symbol';
|
is.symbol = x => typeof x === 'symbol';
|
||||||
|
is.nullOrUndefined = x => is.null(x) || is.undefined(x);
|
||||||
|
|
||||||
|
const primitiveTypes = new Set([
|
||||||
|
'undefined',
|
||||||
|
'string',
|
||||||
|
'number',
|
||||||
|
'boolean',
|
||||||
|
'symbol'
|
||||||
|
]);
|
||||||
|
is.primitive = x => is.null(x) || primitiveTypes.has(typeof x);
|
||||||
|
|
||||||
is.array = Array.isArray;
|
is.array = Array.isArray;
|
||||||
is.function = x => typeof x === 'function';
|
is.function = x => typeof x === 'function';
|
||||||
is.buffer = Buffer.isBuffer;
|
is.buffer = Buffer.isBuffer;
|
||||||
|
|
||||||
is.object = x => {
|
is.object = x => !is.nullOrUndefined(x) && (is.function(x) || typeof x === 'object');
|
||||||
const type = typeof x;
|
|
||||||
return x !== null && (type === 'object' || type === 'function');
|
|
||||||
};
|
|
||||||
|
|
||||||
is.nativePromise = x => getObjectType(x) === 'Promise';
|
is.nativePromise = isOfType('Promise');
|
||||||
|
|
||||||
is.promise = x => {
|
const hasPromiseAPI = x =>
|
||||||
return is.nativePromise(x) ||
|
!is.null(x) &&
|
||||||
(
|
typeof x === 'object' &&
|
||||||
x !== null &&
|
is.function(x.then) &&
|
||||||
typeof x === 'object' &&
|
is.function(x.catch);
|
||||||
typeof x.then === 'function' &&
|
|
||||||
typeof x.catch === 'function'
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
is.regExp = x => getObjectType(x) === 'RegExp';
|
is.promise = x => is.nativePromise(x) || hasPromiseAPI(x);
|
||||||
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';
|
|
||||||
|
|
||||||
is.int8Array = x => getObjectType(x) === 'Int8Array';
|
is.regExp = isOfType('RegExp');
|
||||||
is.uint8Array = x => getObjectType(x) === 'Uint8Array';
|
is.date = isOfType('Date');
|
||||||
is.uint8ClampedArray = x => getObjectType(x) === 'Uint8ClampedArray';
|
is.error = isOfType('Error');
|
||||||
is.int16Array = x => getObjectType(x) === 'Int16Array';
|
is.map = isOfType('Map');
|
||||||
is.uint16Array = x => getObjectType(x) === 'Uint16Array';
|
is.set = isOfType('Set');
|
||||||
is.int32Array = x => getObjectType(x) === 'Int32Array';
|
is.weakMap = isOfType('WeakMap');
|
||||||
is.uint32Array = x => getObjectType(x) === 'Uint32Array';
|
is.weakSet = isOfType('WeakSet');
|
||||||
is.float32Array = x => getObjectType(x) === 'Float32Array';
|
|
||||||
is.float64Array = x => getObjectType(x) === 'Float64Array';
|
|
||||||
|
|
||||||
is.arrayBuffer = x => getObjectType(x) === 'ArrayBuffer';
|
is.int8Array = isOfType('Int8Array');
|
||||||
|
is.uint8Array = isOfType('Uint8Array');
|
||||||
|
is.uint8ClampedArray = isOfType('Uint8ClampedArray');
|
||||||
|
is.int16Array = isOfType('Int16Array');
|
||||||
|
is.uint16Array = isOfType('Uint16Array');
|
||||||
|
is.int32Array = isOfType('Int32Array');
|
||||||
|
is.uint32Array = isOfType('Uint32Array');
|
||||||
|
is.float32Array = isOfType('Float32Array');
|
||||||
|
is.float64Array = isOfType('Float64Array');
|
||||||
|
|
||||||
is.sharedArrayBuffer = x => {
|
is.arrayBuffer = isOfType('ArrayBuffer');
|
||||||
try {
|
is.sharedArrayBuffer = isOfType('SharedArrayBuffer');
|
||||||
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.primitive = x => {
|
|
||||||
const type = typeof x;
|
|
||||||
return x === null ||
|
|
||||||
type === 'undefined' ||
|
|
||||||
type === 'string' ||
|
|
||||||
type === 'number' ||
|
|
||||||
type === 'boolean' ||
|
|
||||||
type === 'symbol';
|
|
||||||
};
|
|
||||||
|
|
||||||
is.integer = Number.isInteger;
|
is.integer = Number.isInteger;
|
||||||
|
|
||||||
is.plainObject = x => {
|
is.plainObject = x => {
|
||||||
|
|
@ -133,9 +120,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',
|
||||||
|
|
|
||||||
24
test.js
24
test.js
|
|
@ -10,7 +10,11 @@ const ErrorSubclassFixture = class extends Error {};
|
||||||
const types = new Map([
|
const types = new Map([
|
||||||
['undefined', undefined],
|
['undefined', undefined],
|
||||||
['null', null],
|
['null', null],
|
||||||
['string', '🦄'],
|
['string', [
|
||||||
|
'🦄',
|
||||||
|
'hello world',
|
||||||
|
''
|
||||||
|
]],
|
||||||
['number', [
|
['number', [
|
||||||
6,
|
6,
|
||||||
1.4,
|
1.4,
|
||||||
|
|
@ -247,9 +251,7 @@ test('is.primitive', t => {
|
||||||
Symbol('🦄')
|
Symbol('🦄')
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const el of primitives) {
|
primitives.forEach(el => t.true(m.primitive(el)));
|
||||||
t.true(m.primitive(el));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.integer', t => {
|
test('is.integer', t => {
|
||||||
|
|
@ -320,13 +322,15 @@ test('is.inRange', t => {
|
||||||
|
|
||||||
t.true(m.inRange(x, 10));
|
t.true(m.inRange(x, 10));
|
||||||
t.true(m.inRange(0, 0));
|
t.true(m.inRange(0, 0));
|
||||||
|
t.true(m.inRange(-2, -3));
|
||||||
t.false(m.inRange(x, 2));
|
t.false(m.inRange(x, 2));
|
||||||
|
t.false(m.inRange(-3, -2));
|
||||||
|
|
||||||
t.throws(() => {
|
t.throws(() =>
|
||||||
t.true(m.inRange(0));
|
t.true(m.inRange(0))
|
||||||
});
|
);
|
||||||
|
|
||||||
t.throws(() => {
|
t.throws(() =>
|
||||||
t.true(m.inRange(0, [5]));
|
t.true(m.inRange(0, [5]))
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue