Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Commit

Permalink
tests against native reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Johnson committed Nov 8, 2012
1 parent 4011117 commit 346d59f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
components
build
node_modules
9 changes: 7 additions & 2 deletions Makefile
Expand Up @@ -6,6 +6,11 @@ components: component.json
@component install --dev

clean:
rm -fr build components template.js
rm -fr build components

.PHONY: clean
test:
@./node_modules/.bin/mocha \
--require should \
--reporter spec

.PHONY: clean test
4 changes: 2 additions & 2 deletions component.json
@@ -1,8 +1,8 @@
{
"name": "reduce",
"repo": "redventures/reduce",
"description": "array reduce",
"version": "0.0.0",
"description": "Array reduce component",
"version": "0.0.1",
"keywords": ["array", "reduce"],
"dependencies": {},
"development": {},
Expand Down
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -9,15 +9,15 @@
* TODO: combatible error handling?
*/

module.exports = function(arr, fn, initial){
module.exports = function(arr, fn, initial){
var idx = 0;
var len = arr.length;
var curr = arguments.length == 3
? initial
: arr[0];
: arr[idx++];

while (++idx < len) {
curr = fn.call(null, curr, arr[idx], idx, arr);
while (idx < len) {
curr = fn.call(null, curr, arr[idx], ++idx, arr);
}

return curr;
Expand Down
14 changes: 14 additions & 0 deletions package.json
@@ -0,0 +1,14 @@
{
"name": "reduce",
"description": "Array reduce component",
"version": "0.0.1",
"devDependencies": {
"mocha": "*",
"should": "*"
},
"component": {
"scripts": {
"reduce": "index.js"
}
}
}
49 changes: 49 additions & 0 deletions test/reduce.js
@@ -0,0 +1,49 @@

var reduce = require('..');

describe('reduce', function(){

describe('when adding prev and current', function(){
it('should be sum all the values', function(){
var numbers = [2,2,2];
var fn = function(prev, curr){
return prev + curr;
};

var a = numbers.reduce(fn);
var b = reduce(numbers, fn);

a.should.equal(6);
b.should.equal(a);
});
});

describe('when passing in an initial value', function(){
it('should default to it', function(){
var items = [];
var fn = function(prev){
return prev;
};

var a = items.reduce(fn, 'foo');
var b = reduce(items, fn, 'foo');

a.should.equal('foo');
b.should.equal(a);
});

it('should start with it', function(){
var items = [10, 10];
var fn = function(prev, curr){
return prev + curr;
};

var a = items.reduce(fn, 10);
var b = reduce(items, fn, 10);

a.should.equal(30);
b.should.equal(a);
});
});

});

0 comments on commit 346d59f

Please sign in to comment.