Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Harriet Lawrence committed Feb 13, 2015
0 parents commit 394860d
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
components
build
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

build: components
@component build --dev

clean:
@rm -rf node_modules build components

components: component.json
@component install --dev

node_modules: package.json
@npm install

test: node_modules build
@./node_modules/.bin/mocha --reporter spec
@component test phantom

.PHONY: clean test
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# Filter

Iterate over elements of a collection, and return an array of all elements the predicate returns truthy for.

## Installation

$ component install harrietgrace/filter
$ npm install harrietgrace/filter

## License

MIT
16 changes: 16 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "filter",
"version": "0.1.0",
"repo": "harrietgrace/filter",
"description": "Iterate over elements of a collection, and return an array of all elements the predicate returns truthy for.",
"keywords": [],
"license": "MIT",
"main": "lib/index.js",
"scripts": [
"lib/index.js"
],
"dependencies": {},
"development": {
"component/assert": "*"
}
}
25 changes: 25 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Expose `filter`.
*/

module.exports = filter;

/**
* Return an array of all elements the predicate returns truthy for.
*
* @param {Array|Object|String} collection
* @param {Function} predicate
* @return {Array}
*/

function filter(collection, predicate){
var results = [];

if (collection == null) return results;

for (var i = 0; i < collection.length; i +=1) {
if (predicate(collection[i])) results.push(collection[i]);
}

return results;
}
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "filter",
"version": "0.1.0",
"repository": "git://github.com/harrietgrace/filter.git",
"description": "Iterate over elements of a collection, and return an array of all elements the predicate returns truthy for.",
"keywords": [],
"license": "MIT",
"main": "lib/index.js",
"dependencies": {},
"devDependencies": {
"mocha": "1.x"
}
}
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
try {
var filter = require('filter');
} catch (e) {
var filter = require('..');
}

var assert = require('assert');

describe('filter', function(){
it('should return the filtered keys', function(){
var arr = [ 1, 2, 3, 4, 5 ];
assert.deepEqual(
[ 2, 4 ],
filter(arr, function(value){ if( value == 2 || value == 4 ) return value; })
);
});

it('should return an empty array', function(){
var arr = [ 1, 2, 3, 4, 5 ];
assert.deepEqual(
[],
filter(arr, function(value){ if( value == 'a' ) return value; })
);
});

it('should return all the elements', function(){
var arr = [ 1, 2, 3, 4, 5 ];
assert.deepEqual(
[ 1, 2, 3, 4, 5 ],
filter(arr, function(value){ if( value ) return value; })
);
});
});

0 comments on commit 394860d

Please sign in to comment.