Functional application: Allow any granularity of arguments (currying, essentially).
This commit is contained in:
parent
04bbe30a00
commit
d7c117619a
3 changed files with 17 additions and 31 deletions
13
lib/index.js
13
lib/index.js
|
@ -339,23 +339,22 @@ Channel.select = methodPromises =>
|
|||
// functional interface allowing full or partial application
|
||||
//
|
||||
// Channel.slice(10, Infinity, channel)
|
||||
//
|
||||
// or
|
||||
//
|
||||
// const skipTen = Channel.slice(10, Infinity)
|
||||
// skipTen(channel)
|
||||
// Channel.slice(10, Infinity)(channel)
|
||||
// Channel.slice(10)(Infinity)(channel)
|
||||
|
||||
const channel = Channel();
|
||||
const methods = Object.keys(channel).concat(Object.keys(channel.readOnly()));
|
||||
|
||||
methods.forEach(method => {
|
||||
Channel[method] = (...args) => {
|
||||
const bound = function(...args) {
|
||||
const arity = method === `slice` ? 3 : channel[method].length;
|
||||
|
||||
return args.length >= arity
|
||||
? args[arity - 1][method](...args.slice(0, arity - 1))
|
||||
: channel => channel[method](...args);
|
||||
: bound.bind(this, ...args);
|
||||
};
|
||||
|
||||
Channel[method] = bound;
|
||||
});
|
||||
|
||||
module.exports = Object.freeze(Channel);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue