Batman.Enumerable
Batman.Enumerable is a mixin that defines methods available for every
enumerable type (Batman.Set and Batman.Hash being the main two).
Note that where the signature of the callback functions is defined, element
is the value of the current element being iterated over, and value is
null—except in the case of Batman.Hash where element is the key and
value is the value.
-
length : number
The number of elements.
-
forEach(func)
Calls
funconce for each element.funcreceives the arguments(element, value, this)test "forEach runs n times", -> set = new Batman.Set('a', 'b') count = 0 set.forEach -> count++ equal count, 2 -
map(func[, context = Batman.container]) : Array
Calls
funconce for each element, and returns an array composed of the return value at each iteration.funcreceives the arguments(element, value, this)test "forEach runs n times", -> set = new Batman.Set(1, 2) deepEqual [2, 3], set.map (x) -> x + 1 -
mapToProperty(key : string) : Array
Returns an array composed of the property specified of each item.
funcreceives the arguments(element, value, this)test "mapToProperty pulls out the property specified", -> set = new Batman.Set({key: 'a'}, {key: 'b'}) deepEqual ['a', 'b'], set.mapToProperty('key') -
every(func[, context = Batman.container]) : boolean
Calls
funconce for each element, and returns true iffuncreturns true for every iteration.funcreceives the arguments(element, value, this)test "every is false when any element doesn't satisfy the selector function", -> set = new Batman.Set(true, false, true) equal false, set.every (x) -> x -
some(func[, context = Batman.container]) : boolean
Calls
funconce for each element, and returns true iffuncreturns true for any iteration.funcreceives the arguments(element, value, this)test "some is true when any element satisfies the selector function", -> set = new Batman.Set(true, false, true) equal true, set.some (x) -> x -
reduce(func[, accumulator]) : Array
Calls
funconce for each element, accumulating the result as it goes along. If you pass your ownaccumulatorit will retain its initial value, otherwise the first element will become the initial accumulator value.funcreceives the arguments(accumulator, element, value, index, this)test "reduce can implement a sum", -> set = new Batman.Set(1, 2, 3) equal 6, set.reduce (accumulator, x) -> accumulator + x -
filter(func) : Object
Creates a new instance of the current type, and adds every element for which
funcreturns true.funcreceives the arguments(element, value, this)test "filter strips out the crud", -> set = new Batman.Set('wheat', 'crud', 'grain') deepEqual ['wheat', 'grain'], (set.filter (x) -> x != 'crud').toArray() -
count(func) : number
Returns the number of elements for which
funcreturns true, or the total number of elements if you don't pass afunc.funcreceives the arguments(element, value, this)test "count counts", -> set = new Batman.Set(1, 2, 3) equal 3, set.count() equal 2, set.count (x) -> x > 1 -
Implementing your own `Batman.Enumerable`
To make these methods available for a new type, all you need to do is define
forEach()andlength. Then, in your type, you can justBatman.extend @prototype, Batman.Enumerableto copy theEnumerablemethods onto your prototype.
