Skip to content

Commit

Permalink
feat: add weave
Browse files Browse the repository at this point in the history
Closes #65
  • Loading branch information
char0n committed May 17, 2017
1 parent 9cd0a77 commit 118daf9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ declare namespace RamdaAdjunct {
*/
resetToDefault(defaultOptions: Object, options: Object): Object; // alias of mergeRight
resetToDefault(defaultOptions: Object): (options: Object) => Object; // alias of mergeRight

/**
* Weave a configuration into function returning the runnable monad like `Reader` or `Free`.
*/
weave(fn: Function, config: any): Function;
weave(fn: Function): (config: any) => Function;
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import noop from './noop';
import liftFN from './liftFN';
import liftF from './liftF';
import cata from './cata';
import weave from './weave';
// List
import pickIndexes from './pickIndexes';
import list from './list';
Expand Down Expand Up @@ -102,6 +103,7 @@ export { default as noop } from './noop';
export { default as liftFN } from './liftFN';
export { default as liftF } from './liftF';
export { default as cata } from './cata';
export { default as weave } from './weave';
// List
export { default as pickIndexes } from './pickIndexes';
export { default as list } from './list';
Expand Down Expand Up @@ -167,6 +169,7 @@ const RA = {
liftFN,
liftF,
cata,
weave,
// List
pickIndexes,
list,
Expand Down
31 changes: 31 additions & 0 deletions src/weave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { curryN } from 'ramda';

/**
* Weave a configuration into function returning the runnable monad like `Reader` or `Free`.
*
* @func weave
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/1.7.0|v1.7.0}
* @category Function
* @sig (*... -> *) -> * -> (*... -> *)
* @param {Function} fn The function to weave
* @param {*} config The configuration to weave into fn
* @return {Function}
* @example
*
* const { Reader: reader } = require('monet');
*
* const log = value => reader(
* config => config.log(value)
* );
*
* // no weaving
* log('test').run(console); //=> prints 'test'
*
* // weaving
* const wlog = RA.weave(log, console);
* wlog('test'); //=> prints 'test'
*/
const weave = curryN(2, (fn, config) => (...args) => fn(...args).run(config));

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

import RA from '../src/index';
import eq from './shared/eq';


describe('weave', function() {
const unaryReader = a => reader(config => config + a);
const binaryReader = (a, b) => reader(config => config + a + b);

it('tests weaving', function() {
const wunaryReader = RA.weave(unaryReader, 1);
const wbinaryReader = RA.weave(binaryReader, 1);

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

it('tests currying', function() {
const wbinaryReader1 = RA.weave(binaryReader, 1);
const wbinaryReader2 = RA.weave(binaryReader)(1);

eq(wbinaryReader1(2, 3), 6);
eq(wbinaryReader2(2, 3), 6);
});

it('tests curried weaving', function() {
const wbinaryReader = curryN(2, RA.weave(binaryReader, 1));

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

0 comments on commit 118daf9

Please sign in to comment.