Correct is.any() and is.all() methods
This commit is contained in:
parent
ce367d3284
commit
23b1e225b4
3 changed files with 24 additions and 121 deletions
109
index.js
109
index.js
|
|
@ -169,118 +169,25 @@ const isEmptyMapOrSet = x => (is.map(x) || is.set(x)) && x.size === 0;
|
||||||
|
|
||||||
is.empty = x => !x || isEmptyStringOrArray(x) || isEmptyObject(x) || isEmptyMapOrSet(x);
|
is.empty = x => !x || isEmptyStringOrArray(x) || isEmptyObject(x) || isEmptyMapOrSet(x);
|
||||||
|
|
||||||
const isType = (value, type) => {
|
is.any = (predicate, ...values) => {
|
||||||
switch (String(type).toLowerCase()) {
|
|
||||||
case 'undefined':
|
|
||||||
return is.undefined(value);
|
|
||||||
case 'null':
|
|
||||||
return is.null(value);
|
|
||||||
case 'string':
|
|
||||||
return is.string(value);
|
|
||||||
case 'number':
|
|
||||||
return is.number(value);
|
|
||||||
case 'boolean':
|
|
||||||
return is.boolean(value);
|
|
||||||
case 'symbol':
|
|
||||||
return is.symbol(value);
|
|
||||||
case 'array':
|
|
||||||
return is.array(value);
|
|
||||||
case 'function':
|
|
||||||
return is.function(value);
|
|
||||||
case 'buffer':
|
|
||||||
return is.buffer(value);
|
|
||||||
case 'object':
|
|
||||||
return is.object(value);
|
|
||||||
case 'regexp':
|
|
||||||
return is.regExp(value);
|
|
||||||
case 'date':
|
|
||||||
return is.date(value);
|
|
||||||
case 'error':
|
|
||||||
return is.error(value);
|
|
||||||
case 'nativepromise':
|
|
||||||
return is.nativePromise(value);
|
|
||||||
case 'promise':
|
|
||||||
return is.promise(value);
|
|
||||||
case 'generator':
|
|
||||||
return is.generator(value);
|
|
||||||
case 'generatorfunction':
|
|
||||||
return is.generatorFunction(value);
|
|
||||||
case 'map':
|
|
||||||
return is.map(value);
|
|
||||||
case 'set':
|
|
||||||
return is.set(value);
|
|
||||||
case 'weakmap':
|
|
||||||
return is.weakMap(value);
|
|
||||||
case 'weakset':
|
|
||||||
return is.weakSet(value);
|
|
||||||
case 'int8array':
|
|
||||||
return is.int8Array(value);
|
|
||||||
case 'uint8array':
|
|
||||||
return is.uint8Array(value);
|
|
||||||
case 'uint8clampedarray':
|
|
||||||
return is.uint8ClampedArray(value);
|
|
||||||
case 'int16array':
|
|
||||||
return is.int16Array(value);
|
|
||||||
case 'uint16array':
|
|
||||||
return is.uint16Array(value);
|
|
||||||
case 'int32array':
|
|
||||||
return is.int32Array(value);
|
|
||||||
case 'uint32array':
|
|
||||||
return is.uint32Array(value);
|
|
||||||
case 'float32array':
|
|
||||||
return is.float32Array(value);
|
|
||||||
case 'float64arrat':
|
|
||||||
return is.float64Array(value);
|
|
||||||
case 'arraybuffer':
|
|
||||||
return is.arrayBuffer(value);
|
|
||||||
case 'sharedarraybuffer':
|
|
||||||
return is.sharedArrayBuffer(value);
|
|
||||||
case 'dataview':
|
|
||||||
return is.dataView(value);
|
|
||||||
case 'nan':
|
|
||||||
return is.nan(value);
|
|
||||||
case 'nullorundefined':
|
|
||||||
return is.nullOrUndefined(value);
|
|
||||||
case 'primitive':
|
|
||||||
return is.primitive(value);
|
|
||||||
case 'integer':
|
|
||||||
return is.integer(value);
|
|
||||||
case 'plainobject':
|
|
||||||
return is.plainObject(value);
|
|
||||||
case 'iterable':
|
|
||||||
return is.iterable(value);
|
|
||||||
case 'class':
|
|
||||||
return is.class(value);
|
|
||||||
case 'typedarray':
|
|
||||||
return is.typedArray(value);
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
is.any = (values, types) => {
|
|
||||||
let ret = false;
|
let ret = false;
|
||||||
|
|
||||||
values.forEach(value => {
|
values.forEach(value => {
|
||||||
types.forEach(type => {
|
if (predicate(value)) {
|
||||||
if (isType(value, type)) {
|
ret = true;
|
||||||
ret = true;
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
is.all = (values, types) => {
|
is.all = (predicate, ...values) => {
|
||||||
let ret = true;
|
let ret = true;
|
||||||
|
|
||||||
values.forEach(value => {
|
values.forEach(value => {
|
||||||
types.forEach(type => {
|
if (!predicate(value)) {
|
||||||
if (!isType(value, type)) {
|
ret = false;
|
||||||
ret = false;
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
||||||
16
readme.md
16
readme.md
|
|
@ -155,27 +155,27 @@ Check if `value` is `Infinity` or `-Infinity`.
|
||||||
Returns `true` if `value` is falsy or an empty string, array, object, map, or set.
|
Returns `true` if `value` is falsy or an empty string, array, object, map, or set.
|
||||||
|
|
||||||
|
|
||||||
##### .any(values, types)
|
##### .any(predicate, ...values)
|
||||||
|
|
||||||
Returns `true` if **any** of `values`'s (array) types are in `types` (array).
|
Returns `true` if **any** of the input `values` returns true in the `predicate`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
is.any(['🦄', {}], ['string', 'boolean']);
|
is.any(is.string, {}, true, '🦄');
|
||||||
//=> true
|
//=> true
|
||||||
|
|
||||||
is.any([[], {}], ['null', 'integer']);
|
is.any(is.boolean, 'unicorns', [], new Map());
|
||||||
//=> false
|
//=> false
|
||||||
```
|
```
|
||||||
|
|
||||||
##### .all(values, types)
|
##### .all(predicate, ...values)
|
||||||
|
|
||||||
Returns `true` if **all** of `values`'s (array) types are in `types` (array).
|
Returns `true` if **all** of the input `values` returns true in the `predicate`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
is.all([new Set(), new Map(), {}], ['object']);
|
is.all(is.object, {}, new Map(), new Set());
|
||||||
//=> true
|
//=> true
|
||||||
|
|
||||||
is.all([false, '🦄'], ['string', 'array']);
|
is.all(is.string, '🦄', [], 'unicorns');
|
||||||
//=> false
|
//=> false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
20
test.js
20
test.js
|
|
@ -404,19 +404,15 @@ test('is.empty', t => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.any', t => {
|
test('is.any', t => {
|
||||||
t.true(m.any([null, false], ['null', 'object']));
|
t.true(m.any(m.string, {}, true, '🦄'));
|
||||||
t.true(m.any(['🦄', []], ['array', 'boolean']));
|
t.true(m.any(m.object, false, {}, 'unicorns'));
|
||||||
t.true(m.any([{}, []], ['object', 'string']));
|
t.false(m.any(m.boolean, '🦄', [], 3));
|
||||||
t.false(m.any([null, []], ['string', 'set']));
|
t.false(m.any(m.integer, true, 'lol', {}));
|
||||||
t.false(m.any([new Set(), true], ['map', 'string']));
|
|
||||||
t.false(m.any(['🦄', null], ['set', 'boolean']));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.all', t => {
|
test('is.all', t => {
|
||||||
t.true(m.all([() => {}, {}], ['object']));
|
t.true(m.all(m.object, {}, new Set(), new Map()));
|
||||||
t.true(m.all(['🦄', 'unicorns'], ['string']));
|
t.true(m.all(m.boolean, true, false));
|
||||||
t.true(m.any([new Set()], ['object', 'set']));
|
t.false(m.all(m.string, '🦄', []));
|
||||||
t.false(m.any([null, []], ['string', 'set']));
|
t.false(m.all(m.set, new Map(), {}));
|
||||||
t.false(m.any([new Set(), true], ['map', 'string']));
|
|
||||||
t.false(m.any(['🦄', null], ['set', 'boolean']));
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue