mirror of https://github.com/lukechilds/ow.git
committed by
Sindre Sorhus
8 changed files with 76 additions and 146 deletions
@ -0,0 +1,27 @@ |
|||||
|
type Collection = Set<any> | Map<any, any> | WeakSet<any> | WeakMap<any, any>; |
||||
|
|
||||
|
/** |
||||
|
* Retrieve the missing values in a collection based on an array of items. |
||||
|
* |
||||
|
* @hidden |
||||
|
* @param source Source collection to search through. |
||||
|
* @param items Items to search for. |
||||
|
* @param maxValues Maximum number of values after the search process is stopped. (Default: 5) |
||||
|
*/ |
||||
|
export default (source: Collection, items: any[], maxValues = 5) => { |
||||
|
const missingValues: any[] = []; |
||||
|
|
||||
|
for (const value of items) { |
||||
|
if (source.has(value)) { |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
missingValues.push(value); |
||||
|
|
||||
|
if (missingValues.length === maxValues) { |
||||
|
return missingValues; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return missingValues.length === 0 ? true : missingValues; |
||||
|
}; |
@ -0,0 +1,21 @@ |
|||||
|
import ow from '../..'; |
||||
|
import {Predicate} from '../predicates/predicate'; |
||||
|
|
||||
|
/** |
||||
|
* Test all the values in the collection against a provided predicate. |
||||
|
* |
||||
|
* @hidden |
||||
|
* @param source Source collection to test. |
||||
|
* @param predicate Predicate to test every item in the source collection against. |
||||
|
*/ |
||||
|
export default (source: IterableIterator<any> | Set<any>, predicate: Predicate): boolean | string => { |
||||
|
try { |
||||
|
for (const item of source) { |
||||
|
ow(item, predicate); |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} catch (err) { |
||||
|
return err.message; |
||||
|
} |
||||
|
}; |
Loading…
Reference in new issue