GitXplorerGitXplorer
b

Enumerable.js

public
8 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
ed772e25ff9e37afb0e74694c68048cfa5bf145d

add integ icon [skip ci]

bbioball committed 10 years ago
Unverified
8c9c8be2f491e54debae9075961ccd487e1b334e

add mocha dep

bbioball committed 10 years ago
Unverified
768fb7d38c1034db7cd5449d75e59c74deb0c192

add travis.yml

bbioball committed 10 years ago
Unverified
72c86e92a8e91fc3e2640e0431c8aff1e056eb27

codeify the first annotation

bbioball committed 11 years ago
Unverified
f85bb8db2b03bbfa43e3c59bae43ba53dd87a391

minor formatting fix on enumerable.count

bbioball committed 11 years ago
Unverified
4d90b0c0596e30fc3c84dcb20227a57d63258ff2

Move annotated source location

bbioball committed 11 years ago

README

The README file for this repository.

Enumerable.js

Build Status

utility functions for all types of collections

When you use a JavaScript array or hash table, you have so many ways to get utility functions at your disposal through libraries like Underscore, Zepto, jQuery, or natively in ECMAScript5. Why can't we have these same utility functions just as easily for other data structures that can be iterated over? Say, a linked list, or a tree, or a graph, or that newest thing that your monkey came up with?

This library is heavily inspired by Ruby's Enumerable module, and borrows great deal of its functionality.

Note: This is not meant to be a standalone library, but to be used in conjunction with another data type that has a defineable .each method.

Annotated source

Installation

As an NPM module npm install enumerable-js

As a bower package bower install enumerable-js

From Github git clone git@github.com:bioball/Enumerable.js.git

Quickstart

1. Have a collection of some sort

// contrived example
var LinkedList = function(){
  this.head = null;
  this.tail = null;
};

LinkedList.prototype.add = function(value){
  var node = {
    value: value,
    next: null
  };
  if(!this.head){
    this.head = this.tail = node;
  } else {
    this.tail.next = node;
    this.tail = node;
  }
};

2. Extend that thing with enumerable

// option 1: use enumerable.extend
enumerable.extend(LinkedList.prototype);

// option 2: use Object#create. NOTE: In this case, doing so will will completely 
// reassign what LinkedList.prototype is. Here, we would need to define 
// LinkedList#add again after this.

LinkedList.prototype = Object.create(enumerable);

3. Define what .each means

LinkedList.prototype.each = function(callback, context){
  var node = this.head;
  while(node){
    callback.call(context, node);
    node = node.next;
  }
};

4. Stand in awe of all the awesome utility functions you suddenly have

var list = new LinkedList();
list.add(5);
list.add(9);
list.add(15);

var sum = list.reduce(function(sum, node){ return sum + node.value; }, 0);
console.log(sum);
// => 27   ¯\_(ツ)_/¯

Supported functions

Returns as specified

  • reduce / inject / foldl
  • first

Returns an array

  • filter / select / findAll
  • mapToArray
  • toArray
  • sort
  • partition

Returns the collection

  • mapInPlace
  • eachCons
  • eachSlice
  • eachUntilN
  • reverseEach
  • cycle

Returns an item in the collection

  • find
  • max
  • min
  • detect

Returns a boolean

  • all
  • any
  • none
  • hasN

Returns an integer

  • count

*Want to contribute? Please send in pull requests! However, please also have test coverage in your pull requests.