Skip to content

Commit

Permalink
feat(weave): add support for auto-currying returned function
Browse files Browse the repository at this point in the history
- credits to Michal Kuk for proposing this enhancement

Closes #78
  • Loading branch information
char0n committed May 20, 2017
1 parent 7ac3f35 commit 7fcf7a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/weave.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { curryN } from 'ramda';
* @sig (*... -> *) -> * -> (*... -> *)
* @param {Function} fn The function to weave
* @param {*} config The configuration to weave into fn
* @return {Function}
* @return {Function} Auto-curried weaved function
* @example
*
* const { Reader: reader } = require('monet');
Expand All @@ -28,6 +28,6 @@ import { curryN } from 'ramda';
* const wlog = RA.weave(log, console);
* wlog('test'); //=> prints 'test'
*/
const weave = curryN(2, (fn, config) => (...args) => fn(...args).run(config));
const weave = curryN(2, (fn, config) => curryN(fn.length, (...args) => fn(...args).run(config)));

export default weave;
41 changes: 38 additions & 3 deletions test/weave.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { curryN } from 'ramda';
import { sum, curry } from 'ramda';
import { Reader as reader } from 'monet';

import RA from '../src/index';
Expand All @@ -8,6 +8,8 @@ import eq from './shared/eq';
describe('weave', function() {
const unaryReader = a => reader(config => config + a);
const binaryReader = (a, b) => reader(config => config + a + b);
const variadicReader = (...args) => reader(config => sum(args) + config);
const mixedReader = (a, b, ...args) => reader(config => sum(args.concat(a, b, config)));

it('tests weaving', function() {
const wunaryReader = RA.weave(unaryReader, 1);
Expand All @@ -25,10 +27,43 @@ describe('weave', function() {
eq(wbinaryReader2(2, 3), 6);
});

it('tests curried weaving', function() {
const wbinaryReader = curryN(2, RA.weave(binaryReader, 1));
it('tests auto-curring on fixed function signature', function() {
const wbinaryReader = RA.weave(binaryReader, 1);

eq(wbinaryReader(2, 3), 6);
eq(wbinaryReader(2)(3), 6);
});

it('tests auto-curring on curried fixed function signature', function() {
const wbinaryReader = RA.weave(curry(binaryReader), 1);

eq(wbinaryReader(2, 3), 6);
eq(wbinaryReader(2)(3), 6);
});

it('tests auto-curring on variadic function signature', function() {
const wvariadicReader = RA.weave(variadicReader, 1);

eq(wvariadicReader(1, 2, 3), 7);
});

it('tests auto-curring on curried variadic function signature', function() {
const wvariadicReader = RA.weave(curry(variadicReader), 1);

eq(wvariadicReader(1, 2, 3), 7);
});

it('tests auto-curring on mixed function signature', function() {
const wmixedReader = RA.weave(mixedReader, 1);

eq(wmixedReader(2, 3, 4), 10);
eq(wmixedReader(2)(3, 4), 10);
});

it('tests auto-curring on curried mixed function signature', function() {
const wmixedReader = RA.weave(curry(mixedReader), 1);

eq(wmixedReader(2, 3, 4), 10);
eq(wmixedReader(2)(3, 4), 10);
});
});

0 comments on commit 7fcf7a9

Please sign in to comment.