Enumerable mixin.
users
.map('friends')
.select('age > 20')
.map('name.first')
.select(/^T/)
$ component install component/enumerable
Nearly all methods utilize the to-function
component, which converts the argument passed to a function. For example
.map('name.first')
expands to a function effectively defined as return obj.name.first
,
likewise .select(/^Tobi/)
expands to return /^Tobi/.test(str)
and so on. For details
check out to-function's Readme and familiarize yourself since all of that is applicable
to Enumerable.
- mixin()
- .each()
- .map()
- .select()
- .unique()
- .reject()
- .compact()
- .find()
- .findLast()
- .none()
- .any()
- .count()
- .indexOf()
- .has()
- .reduce()
- .max()
- .sum()
- .first()
- .last()
- .inGroupsOf()
- .at()
- .value()
Mixin to obj
.
var Enumerable = require('enumerable');
Enumerable(Something.prototype);
Iterate each value and invoke fn(val, i)
.
users.each(function(val, i){
})
Map each return value from fn(val, i)
.
Passing a callback function:
users.map(function(user){
return user.name.first
})
Passing a property string:
users.map('name.first')
Select all values that return a truthy value of fn(val, i)
.
users.select(function(user){
return user.age > 20
})
With a property:
items.select('complete')
Select all unique values.
nums.unique()
Reject all values that return a truthy value of fn(val, i)
.
Rejecting using a callback:
users.reject(function(user){
return user.age < 20
})
Rejecting with a property:
items.reject('complete')
Rejecting values via ==
:
data.reject(null)
users.reject(toni)
Reject null
and undefined
.
[1, null, 5, undefined].compact()
// => [1,5]
Return the first value when fn(val, i)
is truthy,
otherwise return undefined
.
users.find(function(user){
return user.role == 'admin'
})
Return the last value when fn(val, i)
is truthy,
otherwise return undefined
.
users.findLast(function(user){
return user.role == 'admin'
})
Assert that none of the invocations of fn(val, i)
are truthy.
For example ensuring that no pets are admins:
pets.none(function(p){ return p.admin })
pets.none('admin')
Assert that at least one invocation of fn(val, i)
is truthy.
For example checking to see if any pets are ferrets:
pets.any(function(pet){
return pet.species == 'ferret'
})
Count the number of times fn(val, i)
returns true.
var n = pets.count(function(pet){
return pet.species == 'ferret'
})
Determine the indexof obj
or return -1
.
Check if obj
is present in this enumerable.
Reduce with fn(accumulator, val, i)
using
optional init
value defaulting to the first
enumerable value.
Determine the max value.
With a callback function:
pets.max(function(pet){
return pet.age
})
With property strings:
pets.max('age')
With immediate values:
nums.max()
Determine the sum.
With a callback function:
pets.sum(function(pet){
return pet.age
})
With property strings:
pets.sum('age')
With immediate values:
nums.sum()
Return the first value, or first n
values.
Return the last value, or last n
values.
Return values in groups of n
.
Return the value at the given index.
Return the enumerable value.
MIT