Add is.any() and is.all() methods

This commit is contained in:
Kodie Grantham 2017-10-06 12:03:30 -05:00
parent 6268253ec6
commit ce367d3284
3 changed files with 159 additions and 0 deletions

117
index.js
View file

@ -169,4 +169,121 @@ const isEmptyMapOrSet = x => (is.map(x) || is.set(x)) && x.size === 0;
is.empty = x => !x || isEmptyStringOrArray(x) || isEmptyObject(x) || isEmptyMapOrSet(x);
const isType = (value, type) => {
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;
values.forEach(value => {
types.forEach(type => {
if (isType(value, type)) {
ret = true;
}
});
});
return ret;
};
is.all = (values, types) => {
let ret = true;
values.forEach(value => {
types.forEach(type => {
if (!isType(value, type)) {
ret = false;
}
});
});
return ret;
};
module.exports = is;

View file

@ -155,6 +155,30 @@ Check if `value` is `Infinity` or `-Infinity`.
Returns `true` if `value` is falsy or an empty string, array, object, map, or set.
##### .any(values, types)
Returns `true` if **any** of `values`'s (array) types are in `types` (array).
```js
is.any(['🦄', {}], ['string', 'boolean']);
//=> true
is.any([[], {}], ['null', 'integer']);
//=> false
```
##### .all(values, types)
Returns `true` if **all** of `values`'s (array) types are in `types` (array).
```js
is.all([new Set(), new Map(), {}], ['object']);
//=> true
is.all([false, '🦄'], ['string', 'array']);
//=> false
```
## FAQ
### Why yet another type checking module?

18
test.js
View file

@ -402,3 +402,21 @@ test('is.empty', t => {
tempSet.add(1);
t.false(m.empty(tempSet));
});
test('is.any', t => {
t.true(m.any([null, false], ['null', 'object']));
t.true(m.any(['🦄', []], ['array', 'boolean']));
t.true(m.any([{}, []], ['object', 'string']));
t.false(m.any([null, []], ['string', 'set']));
t.false(m.any([new Set(), true], ['map', 'string']));
t.false(m.any(['🦄', null], ['set', 'boolean']));
});
test('is.all', t => {
t.true(m.all([() => {}, {}], ['object']));
t.true(m.all(['🦄', 'unicorns'], ['string']));
t.true(m.any([new Set()], ['object', 'set']));
t.false(m.any([null, []], ['string', 'set']));
t.false(m.any([new Set(), true], ['map', 'string']));
t.false(m.any(['🦄', null], ['set', 'boolean']));
});