Enumerable
Enumerable mixin.
users
Installation
$ component install component/enumerable
API
- mixin()
- .each()
- .map()
- .select()
- .unique()
- .reject()
- .compact()
- .find()
- .findLast()
- .none()
- .any()
- .count()
- .indexOf()
- .has()
- .grep()
- .reduce()
- .max()
- .sum()
- .first()
- .last()
- .inGroupsOf()
- .at()
- .value()
mixin()
Mixin to obj
.
var Enumerable = ; ;
.each(fn:Function)
Iterate each value and invoke fn(val, i)
.
users
.map(fn:Function)
Map each return value from fn(val, i)
.
Passing a callback function:
users
Passing a property string:
users
.select(fn:Function|String)
Select all values that return a truthy value of fn(val, i)
.
users
With a property:
items
.unique()
Select all unique values.
nums
.reject(fn:Function|String|Mixed)
Reject all values that return a truthy value of fn(val, i)
.
Rejecting using a callback:
users
Rejecting with a property:
items
Rejecting values via ==
:
data users
.compact()
Reject null
and undefined
.
1 null 5 undefinedcompact // => [1,5]
.find(fn:Function)
Return the first value when fn(val, i)
is truthy,
otherwise return undefined
.
users
.findLast(fn:Function)
Return the last value when fn(val, i)
is truthy,
otherwise return undefined
.
users
.none(fn:Function|String)
Assert that none of the invocations of fn(val, i)
are truthy.
For example ensuring that no pets are admins:
pets pets
.any(fn:Function)
Assert that at least one invocation of fn(val, i)
is truthy.
For example checking to see if any pets are ferrets:
pets
.count(fn:Function)
Count the number of times fn(val, i)
returns true.
var n = pets
.indexOf(obj:Mixed)
Determine the indexof obj
or return -1
.
.has(obj:Mixed)
Check if obj
is present in this enumerable.
.grep(re:RegExp)
Grep values using the given re
.
users
.reduce(fn:Function, [val]:Mixed)
Reduce with fn(accumulator, val, i)
using
optional init
value defaulting to the first
enumerable value.
.max(fn:Function|String)
Determine the max value.
With a callback function:
pets
With property strings:
pets
With immediate values:
nums
.sum(fn:Function|String)
Determine the sum.
With a callback function:
pets
With property strings:
pets
With immediate values:
nums
.first([n]:Number|Function)
Return the first value, or first n
values.
.last([n]:Number|Function)
Return the last value, or last n
values.
.inGroupsOf(n:Number)
Return values in groups of n
.
.at(i:Number)
Return the value at the given index.
.value()
Return the enumerable value.
License
MIT