uncurryN.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import _curry2 from "./internal/_curry2.js";
  2. import curryN from "./curryN.js";
  3. /**
  4. * Returns a function of arity `n` from a (manually) curried function.
  5. * Note that, the returned function is actually a ramda style
  6. * curryied function, which can accept one or more arguments in each
  7. * function calling.
  8. *
  9. * @func
  10. * @memberOf R
  11. * @since v0.14.0
  12. * @category Function
  13. * @sig Number -> (a -> b -> c ... -> z) -> ((a -> b -> c ...) -> z)
  14. * @param {Number} length The arity for the returned function.
  15. * @param {Function} fn The function to uncurry.
  16. * @return {Function} A new function.
  17. * @see R.curry, R.curryN
  18. * @example
  19. *
  20. * const addFour = a => b => c => d => a + b + c + d;
  21. *
  22. * const uncurriedAddFour = R.uncurryN(4, addFour);
  23. * uncurriedAddFour(1, 2, 3, 4); //=> 10
  24. */
  25. var uncurryN =
  26. /*#__PURE__*/
  27. _curry2(function uncurryN(depth, fn) {
  28. return curryN(depth, function () {
  29. var currentDepth = 1;
  30. var value = fn;
  31. var idx = 0;
  32. var endIdx;
  33. while (currentDepth <= depth && typeof value === 'function') {
  34. endIdx = currentDepth === depth ? arguments.length : idx + value.length;
  35. value = value.apply(this, Array.prototype.slice.call(arguments, idx, endIdx));
  36. currentDepth += 1;
  37. idx = endIdx;
  38. }
  39. return value;
  40. });
  41. });
  42. export default uncurryN;