Add is.any() and is.all() methods (#19)
This commit is contained in:
parent
75ac3cd574
commit
651f434eab
3 changed files with 81 additions and 0 deletions
27
index.js
27
index.js
|
|
@ -181,4 +181,31 @@ const isEmptyMapOrSet = x => (is.map(x) || is.set(x)) && x.size === 0;
|
|||
|
||||
is.empty = x => !x || isEmptyStringOrArray(x) || isEmptyObject(x) || isEmptyMapOrSet(x);
|
||||
|
||||
const predicateOnArray = (method, predicate, values) => {
|
||||
// `values` is the calling function's "arguments object".
|
||||
// We have to do it this way to keep node v4 support.
|
||||
// So here we convert it to an array and slice off the first item.
|
||||
values = Array.prototype.slice.call(values, 1);
|
||||
|
||||
if (is.function(predicate) === false) {
|
||||
throw new TypeError(`Invalid predicate: ${util.inspect(predicate)}`);
|
||||
}
|
||||
|
||||
if (values.length === 0) {
|
||||
throw new TypeError(`Invalid number of values`);
|
||||
}
|
||||
|
||||
return method.call(values, predicate);
|
||||
};
|
||||
|
||||
// We have to use anonymous functions for the any() and all() methods
|
||||
// to get the arguments since we can't use rest parameters in node v4.
|
||||
is.any = function (predicate) {
|
||||
return predicateOnArray(Array.prototype.some, predicate, arguments);
|
||||
};
|
||||
|
||||
is.all = function (predicate) {
|
||||
return predicateOnArray(Array.prototype.every, predicate, arguments);
|
||||
};
|
||||
|
||||
module.exports = is;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue