Comparing version 2.3.0 to 2.4.0
22
index.js
@@ -390,2 +390,24 @@ var abbott = require('abbott'); | ||
righto.reduce = function(values, reducer, seed){ | ||
if(!values || !values.reduce){ | ||
throw new Error('values was not a reduceable object (like an array)'); | ||
} | ||
values = values.slice(); | ||
if(arguments.length < 3){ | ||
seed = righto(values.shift()); | ||
} | ||
return values.reduce(function(previous, next){ | ||
if(reducer){ | ||
return righto(function(previous, done){ | ||
reducer(previous, next)(done); | ||
}, previous); | ||
} | ||
return righto(done => next(done), righto.after(righto.from(previous))); | ||
}, seed); | ||
}; | ||
righto.from = function(value){ | ||
@@ -392,0 +414,0 @@ if(isRighto(value)){ |
{ | ||
"name": "righto", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -250,3 +250,58 @@ # Righto | ||
## All | ||
righto.reduce takes N tasks, or an Array of tasks as the first argument, | ||
resolves them from left-to-right, optionally passing the result of the last, and the next task to a reducer. | ||
If no reducer is passed, the tasks will be resolved in series, and the final tasks result will be passed as the result from reduce. | ||
If a reducer is used, a seed can optionally be passed as the third parameter. | ||
No reducer passed: | ||
```javascript | ||
function a(callback){ | ||
aCalled = true; | ||
t.pass('a called'); | ||
callback(null, 1); | ||
} | ||
function b(callback){ | ||
t.ok(aCalled, 'b called after a'); | ||
callback(null, 2); | ||
} | ||
var result = righto.reduce([a, b]); | ||
result(function(error, finalResult){ | ||
// finalResult === 2 | ||
}); | ||
``` | ||
With a custom reducer, and seed. | ||
``` | ||
function a(last, callback){ | ||
aCalled = true; | ||
t.pass('a called'); | ||
callback(null, last); | ||
} | ||
function b(last, callback){ | ||
t.ok(aCalled, 'b called after a'); | ||
callback(null, last + 2); | ||
} | ||
// Passes previous eventual result to next reducer call. | ||
var result = righto.reduce( | ||
[a, b], | ||
function(result, next){ // Reducer | ||
return righto(next, result); | ||
}, | ||
5 // Seed | ||
); | ||
result(function(error, finalResult){ | ||
// finalResult === 7 | ||
}); | ||
``` | ||
## Sync | ||
@@ -253,0 +308,0 @@ |
@@ -350,2 +350,89 @@ var test = require('tape'), | ||
test('righto.reduce', function(t){ | ||
t.plan(4); | ||
var aCalled; | ||
function a(callback){ | ||
aCalled = true; | ||
t.pass('a called'); | ||
callback(null, 1); | ||
} | ||
function b(callback){ | ||
t.ok(aCalled, 'b called after a'); | ||
callback(null, 2); | ||
} | ||
var result = righto.reduce([a, b]); | ||
result(function(error, finalResult){ | ||
t.notOk(error, 'no error'); | ||
t.equal(finalResult, 2, 'Got correct final result'); | ||
}); | ||
}); | ||
test('righto.reduce eventuals', function(t){ | ||
t.plan(4); | ||
var aCalled; | ||
var a = righto(function(callback){ | ||
aCalled = true; | ||
t.pass('a called'); | ||
callback(null, 1); | ||
}); | ||
var b = righto(function(callback){ | ||
t.ok(aCalled, 'b called after a'); | ||
callback(null, 2); | ||
}); | ||
var result = righto.reduce([a, b]); | ||
result(function(error, finalResult){ | ||
t.notOk(error, 'no error'); | ||
t.equal(finalResult, 2, 'Got correct final result'); | ||
}); | ||
}); | ||
test('righto.reduce custom reducer', function(t){ | ||
t.plan(4); | ||
var aCalled; | ||
function a(last, callback){ | ||
aCalled = true; | ||
t.pass('a called'); | ||
callback(null, last); | ||
} | ||
function b(last, callback){ | ||
t.ok(aCalled, 'b called after a'); | ||
callback(null, last + 2); | ||
} | ||
var result = righto.reduce([a, b], function(result, next){ | ||
return righto(next, result); | ||
}, 5); | ||
result(function(error, finalResult){ | ||
t.notOk(error, 'no error'); | ||
t.deepEqual(finalResult, 7, 'Got correct final result'); | ||
}); | ||
}); | ||
test('righto.reduce with values custom reducer', function(t){ | ||
t.plan(2); | ||
var result = righto.reduce([1, 2, 3], function(result, next){ | ||
return righto.sync((a) => a + next, result); | ||
}, 5); | ||
result(function(error, finalResult){ | ||
t.notOk(error, 'no error'); | ||
t.deepEqual(finalResult, 11, 'Got correct final result'); | ||
}); | ||
}); | ||
test('righto().get(key)', function(t){ | ||
@@ -352,0 +439,0 @@ t.plan(4); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62079
1608
690