/
some.js
48 lines (40 loc) · 1.22 KB
/
some.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* Check if predicate return truthy for any element.
*
* |Name |Desc |
* |---------|----------------------------------------------|
* |obj |Collection to iterate over |
* |predicate|Function to invoked per iteration |
* |ctx |Predicate context |
* |return |True if any element passes the predicate check|
*/
/* example
* some([2, 5], function(val) {
* return val % 2 === 0;
* }); // -> true
*/
/* module
* env: all
*/
/* typescript
* export declare function some<T>(
* list: types.List<T>,
* iterator?: types.ListIterator<T, boolean>,
* context?: any
* ): boolean;
* export declare function some<T>(
* object: types.Dictionary<T>,
* iterator?: types.ObjectIterator<T, boolean>,
* context?: any
* ): boolean;
*/
_('safeCb isArrLike keys types');
exports = function(obj, predicate, ctx) {
predicate = safeCb(predicate, ctx);
const _keys = !isArrLike(obj) && keys(obj);
const len = (_keys || obj).length;
for (let i = 0; i < len; i++) {
const key = _keys ? _keys[i] : i;
if (predicate(obj[key], key, obj)) return true;
}
return false;
};