You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

4 years ago
  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. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.14.0
  9. * @category Function
  10. * @sig Number -> (a -> b) -> (a -> c)
  11. * @param {Number} length The arity for the returned function.
  12. * @param {Function} fn The function to uncurry.
  13. * @return {Function} A new function.
  14. * @see R.curry
  15. * @example
  16. *
  17. * const addFour = a => b => c => d => a + b + c + d;
  18. *
  19. * const uncurriedAddFour = R.uncurryN(4, addFour);
  20. * uncurriedAddFour(1, 2, 3, 4); //=> 10
  21. */
  22. var uncurryN =
  23. /*#__PURE__*/
  24. _curry2(function uncurryN(depth, fn) {
  25. return curryN(depth, function () {
  26. var currentDepth = 1;
  27. var value = fn;
  28. var idx = 0;
  29. var endIdx;
  30. while (currentDepth <= depth && typeof value === 'function') {
  31. endIdx = currentDepth === depth ? arguments.length : idx + value.length;
  32. value = value.apply(this, Array.prototype.slice.call(arguments, idx, endIdx));
  33. currentDepth += 1;
  34. idx = endIdx;
  35. }
  36. return value;
  37. });
  38. });
  39. export default uncurryN;