/
enumerable.js
54 lines (51 loc) · 1.75 KB
/
enumerable.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
49
50
51
52
53
54
// Inspired by http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l54
/**
* Enumerable.findIndex() -> Number | undefined
* - iterator(Function): Iterator. Called for each value of a "collection" with value and index
* - context(Object): Optional object to be used as a context of iterator
*
* Returns index of a first value in "collection" which satisfies iterator
*
* $R(1,10).findIndex(function(v) { return v % 5 == 0 }); // => 4
* $R(1,10).findIndex(function(v) { return v % 25 == 0 }); // => undefined
*
* // Get all <td> of <th.foo>
* var index = $$('th').findIndex(function(el) { return el.hasClassName('foo') });
* $$('td:nth-child(' + index + ')');
*
**/
Enumerable.findIndex = function(iterator, context) {
var result;
this.each(function(value, index) {
if (iterator.call(context || iterator, value, index)) {
result = index;
throw $break;
}
})
return result;
}
/**
* Enumerable.count() -> Number
* - iterator(Function): Iterator. Called for each value of a "collection" with value and index
* - context(Object): Optional object to be used as a context of iterator
*
* Returns a number representing how many values in "collection" iterator satisfies
*
* // how many of 1 is in the array?
* ["bar", 1, "foo", 2, 1].count(1); // => 2
*
* // how many could be converted to number?
* ["bar", 1, "foo", 2, 3].count(function(v) { return parseInt(v) }); // => 3
*
* // how many are strings?
* ["foo", "bar", 1, 2, 3, "baz"].count(Object.isString); // => 3
*
**/
Enumerable.count = function(iterator, context) {
var result = 0;
this.each(function(value, index) {
if (iterator.call ? iterator.call(context || iterator, value, index) : (iterator == value))
++result;
})
return result;
}