| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | var apply = require('./_apply'), |
| 2 | createCtor = require('./_createCtor'), |
| 3 | createHybrid = require('./_createHybrid'), |
| 4 | createRecurry = require('./_createRecurry'), |
| 5 | getHolder = require('./_getHolder'), |
| 6 | replaceHolders = require('./_replaceHolders'), |
| 7 | root = require('./_root'); |
| 8 | |
| 9 | /** |
| 10 | * Creates a function that wraps `func` to enable currying. |
| 11 | * |
| 12 | * @private |
| 13 | * @param {Function} func The function to wrap. |
| 14 | * @param {number} bitmask The bitmask flags. See `createWrap` for more details. |
| 15 | * @param {number} arity The arity of `func`. |
| 16 | * @returns {Function} Returns the new wrapped function. |
| 17 | */ |
| 18 | function createCurry(func, bitmask, arity) { |
| 19 | var Ctor = createCtor(func); |
| 20 | |
| 21 | function wrapper() { |
| 22 | var length = arguments.length, |
| 23 | args = Array(length), |
| 24 | index = length, |
| 25 | placeholder = getHolder(wrapper); |
| 26 | |
| 27 | while (index--) { |
| 28 | args[index] = arguments[index]; |
| 29 | } |
| 30 | var holders = (length < 3 && args[0] !== placeholder && args[length - 1] !== placeholder) |
| 31 | ? [] |
| 32 | : replaceHolders(args, placeholder); |
| 33 | |
| 34 | length -= holders.length; |
| 35 | if (length < arity) { |
| 36 | return createRecurry( |
| 37 | func, bitmask, createHybrid, wrapper.placeholder, undefined, |
| 38 | args, holders, undefined, undefined, arity - length); |
| 39 | } |
| 40 | var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func; |
| 41 | return apply(fn, this, args); |
| 42 | } |
| 43 | return wrapper; |
| 44 | } |
| 45 | |
| 46 | module.exports = createCurry; |