Socket
Book a DemoInstallSign in
Socket

enumerable-component

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enumerable-component

Enumerable mixin.

latest
npmnpm
Version
0.3.1
Version published
Maintainers
1
Created
Source

Enumerable

Enumerable mixin.

users
  .map('friends')
  .select('age > 20')
  .map('name.first')
  .grep(/^T/)

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 = require('enumerable');
 Enumerable(Something.prototype);

.each(fn:Function)

Iterate each value and invoke fn(val, i).

 users.each(function(val, i){
   
 })

.map(fn:Function)

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(fn:Function|String)

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')

.unique()

Select all unique values.

 nums.unique()

.reject(fn:Function|String|Mixed)

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)

.compact()

Reject null and undefined.

 [1, null, 5, undefined].compact()
 // => [1,5]

.find(fn:Function)

Return the first value when fn(val, i) is truthy, otherwise return undefined.

 users.find(function(user){
   return user.role == 'admin'
 })

.findLast(fn:Function)

Return the last value when fn(val, i) is truthy, otherwise return undefined.

 users.findLast(function(user){
   return user.role == 'admin'
 })

.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.none(function(p){ return p.admin })
 pets.none('admin')

.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.any(function(pet){
   return pet.species == 'ferret'
 })

.count(fn:Function)

Count the number of times fn(val, i) returns true.

 var n = pets.count(function(pet){
   return pet.species == 'ferret'
 })

.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.map('name').grep(/^tobi/i)

.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.max(function(pet){
   return pet.age
 })

With property strings:

 pets.max('age')

With immediate values:

 nums.max()

.sum(fn:Function|String)

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()

.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

FAQs

Package last updated on 06 Dec 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts