-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harriet Lawrence
committed
Feb 13, 2015
0 parents
commit 394860d
Showing
7 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
components | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; }) | ||
); | ||
}); | ||
}); |