Ramda Adjunct 1.14.0

weave.js

  1. import { curryN } from 'ramda';
  2. /**
  3. * Weaves a configuration into function returning the runnable monad like `Reader` or `Free`.
  4. * This allows us to pre-bind the configuration in advance and use the weaved function
  5. * without need to explicitly pass the configuration on every call.
  6. *
  7. * @func weave
  8. * @memberOf RA
  9. * @since {@link https://char0n.github.io/ramda-adjunct/1.7.0|v1.7.0}
  10. * @category Function
  11. * @sig (*... -> *) -> * -> (*... -> *)
  12. * @param {Function} fn The function to weave
  13. * @param {*} config The configuration to weave into fn
  14. * @return {Function} Auto-curried weaved function
  15. * @example
  16. *
  17. * const { Reader: reader } = require('monet');
  18. *
  19. * const log = value => reader(
  20. * config => config.log(value)
  21. * );
  22. *
  23. * // no weaving
  24. * log('test').run(console); //=> prints 'test'
  25. *
  26. * // weaving
  27. * const wlog = RA.weave(log, console);
  28. * wlog('test'); //=> prints 'test'
  29. */
  30. const weave = curryN(2, (fn, config) => curryN(fn.length, (...args) => fn(...args).run(config)));
  31. export default weave;