Comparing version 1.7.1 to 1.8.0
29
index.js
@@ -17,2 +17,6 @@ var foreign = require('foreign'), | ||
function isTake(x){ | ||
return x && typeof x === 'object' && '__take__' in x; | ||
} | ||
function slice(list, start, end){ | ||
@@ -39,6 +43,7 @@ return Array.prototype.slice.call(list, start, end); | ||
if(Array.isArray(task) && isRighto(task[0]) && !isRighto(task[1])){ | ||
return task[0](function(error){ | ||
function take(targetTask){ | ||
var keys = slice(arguments, 1); | ||
return targetTask(function(error){ | ||
var args = slice(arguments, 1); | ||
done(error, task.slice(1).map(function(key){ | ||
done(error, keys.map(function(key){ | ||
return args[key]; | ||
@@ -49,2 +54,10 @@ })); | ||
if(Array.isArray(task) && isRighto(task[0]) && !isRighto(task[1])){ | ||
return take.apply(null, task); | ||
} | ||
if(isTake(task)){ | ||
return take.apply(null, task.__take__); | ||
} | ||
return done(null, [task]); | ||
@@ -229,2 +242,10 @@ } | ||
righto.take = function(){ | ||
return {__take__: slice(arguments)}; | ||
}; | ||
righto.after = function(task){ | ||
return {__take__: [task]}; | ||
}; | ||
righto.resolve = function(object, deep){ | ||
@@ -266,3 +287,3 @@ if(isRighto(object)){ | ||
return righto.apply(null, [resolveIterator(fn)].concat(args)); | ||
} | ||
}; | ||
@@ -269,0 +290,0 @@ righto.value = function(){ |
{ | ||
"name": "righto", | ||
"version": "1.7.1", | ||
"version": "1.8.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -152,3 +152,3 @@ # Righto | ||
## Multiple results | ||
## Take / Multiple results | ||
@@ -183,6 +183,22 @@ By default, dependant tasks are passed only the first result of a dependency `righto`. eg: | ||
```javascript | ||
// foo() and bar() as defined above... | ||
var getBar = righto(bar, righto.take(getFoo, 0, 2)); // Take result 0, and result 2, from getFoo | ||
getBar(function(error, result){ | ||
// ...1 second later... | ||
result -> 'first third'; | ||
}); | ||
``` | ||
## After | ||
Sometimes you need a task to run after another has succeeded, but you don't need its results, | ||
righto.after(task) can be used to achieve this: | ||
```javascript | ||
function foo(callback){ | ||
setTimeout(function(){ | ||
callback(null, 'first', 'second', 'third'); | ||
callback(null, 'first result'); | ||
@@ -194,11 +210,10 @@ }, 1000); | ||
function bar(a, b callback){ | ||
callback(null, [a, b].join(' ')); | ||
function bar(callback){ | ||
callback(null, 'second result'); | ||
} | ||
var getBar = righto(bar, [getFoo, 0, 2]); // Take result 0, and result 2, from getFoo | ||
var getBar = righto(bar, righto.after(getFoo)); // wait for foo before running bar. | ||
getBar(function(error, result){ | ||
// ...1 second later... | ||
result -> 'first third'; | ||
result -> 'second result'; | ||
}); | ||
@@ -239,3 +254,26 @@ ``` | ||
``` | ||
## Sync | ||
Synchronous functions can be used to create righto tasks using righto.sync: | ||
```javascript | ||
var someNumber = righto(function(done){ | ||
setTimeout(function(){ | ||
done(null, 5); | ||
}, 1000); | ||
} | ||
function addFive(value){ | ||
return value + 5; | ||
} | ||
var syncTask = righto.sync(addFive, someNumber); | ||
syncTask(function(error, result){ | ||
result; // -> 10 | ||
}); | ||
``` | ||
## Value (passing resolveables as unresolved arguments) | ||
@@ -242,0 +280,0 @@ |
@@ -75,2 +75,54 @@ var test = require('tape'), | ||
test('ignored deps with take', function(t){ | ||
t.plan(2); | ||
function bar(callback){ | ||
asyncify(function(){ | ||
callback(null, 2, 3); | ||
}); | ||
} | ||
function foo(callback){ | ||
asyncify(function(){ | ||
callback(null, 'hello'); | ||
}); | ||
} | ||
var getBar = righto(bar); | ||
var getFoo = righto(foo, righto.take(getBar)); | ||
getFoo(function(error, result){ | ||
t.notOk(error, 'no error'); | ||
t.equal(result, 'hello', 'Got correct result'); | ||
}); | ||
}); | ||
test('ignored deps with after', function(t){ | ||
t.plan(2); | ||
function bar(callback){ | ||
asyncify(function(){ | ||
callback(null, 2, 3); | ||
}); | ||
} | ||
function foo(callback){ | ||
asyncify(function(){ | ||
callback(null, 'hello'); | ||
}); | ||
} | ||
var getBar = righto(bar); | ||
var getFoo = righto(foo, righto.after(getBar)); | ||
getFoo(function(error, result){ | ||
t.notOk(error, 'no error'); | ||
t.equal(result, 'hello', 'Got correct result'); | ||
}); | ||
}); | ||
test('multiple deps', function(t){ | ||
@@ -101,2 +153,27 @@ t.plan(2); | ||
test('multiple deps with take', function(t){ | ||
t.plan(2); | ||
function bar(callback){ | ||
asyncify(function(){ | ||
callback(null, 2, 3); | ||
}); | ||
} | ||
function foo(a, b, callback){ | ||
asyncify(function(){ | ||
callback(null, a - b); | ||
}); | ||
} | ||
var getBar = righto(bar); | ||
var getFoo = righto(foo, righto.take(getBar, 0, 1)); | ||
getFoo(function(error, result){ | ||
t.notOk(error, 'no error'); | ||
t.equal(result, -1, 'Got correct result'); | ||
}); | ||
}); | ||
test('multiple deps repeated', function(t){ | ||
@@ -127,2 +204,27 @@ t.plan(2); | ||
test('multiple deps repeated with take', function(t){ | ||
t.plan(2); | ||
function bar(callback){ | ||
asyncify(function(){ | ||
callback(null, 2, 3); | ||
}); | ||
} | ||
function foo(a, b, callback){ | ||
asyncify(function(){ | ||
callback(null, a - b); | ||
}); | ||
} | ||
var getBar = righto(bar); | ||
var getFoo = righto(foo, righto.take(getBar, 1, 1)); | ||
getFoo(function(error, result){ | ||
t.notOk(error, 'no error'); | ||
t.equal(result, 0, 'Got correct result'); | ||
}); | ||
}); | ||
test('multiple deps reordered', function(t){ | ||
@@ -153,2 +255,27 @@ t.plan(2); | ||
test('multiple deps reordered with take', function(t){ | ||
t.plan(2); | ||
function bar(callback){ | ||
asyncify(function(){ | ||
callback(null, 2, 3); | ||
}); | ||
} | ||
function foo(a, b, callback){ | ||
asyncify(function(){ | ||
callback(null, a - b); | ||
}); | ||
} | ||
var getBar = righto(bar); | ||
var getFoo = righto(foo, righto.take(getBar, 1, 0)); | ||
getFoo(function(error, result){ | ||
t.notOk(error, 'no error'); | ||
t.equal(result, 1, 'Got correct result'); | ||
}); | ||
}); | ||
test('multiple deps, default result', function(t){ | ||
@@ -155,0 +282,0 @@ t.plan(2); |
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
579588
8644
461