UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

95 lines (94 loc) 4.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const toInteger_1 = __importDefault(require("../toInteger")); const nativeMax_1 = __importDefault(require("./nativeMax")); const getData_1 = __importDefault(require("./getData")); const CONSTANTS_1 = require("./CONSTANTS"); const createPartial_1 = __importDefault(require("./createPartial")); const baseSetData_1 = __importDefault(require("./baseSetData")); const setData_1 = __importDefault(require("./setData")); const setWrapToString_1 = __importDefault(require("./setWrapToString")); const createHybrid_1 = __importDefault(require("./createHybrid")); const mergeData_1 = __importDefault(require("./mergeData")); const createBind_1 = __importDefault(require("./createBind")); const createCurry_1 = __importDefault(require("./createCurry")); /** * Creates a function that either curries or invokes `func` with optional * `this` binding and partially applied arguments. * * @private * @param {Function|string} func The function or method name to wrap. * @param {number} bitmask The bitmask flags. * 1 - `bind` * 2 - `bindKey` * 4 - `curry` or `curryRight` of a bound function * 8 - `curry` * 16 - `curryRight` * 32 - `partial` * 64 - `partialRight` * 128 - `rearg` * 256 - `ary` * 512 - `flip` * @param {*} [thisArg] The `this` binding of `func`. * @param {Array} [partials] The arguments to be partially applied. * @param {Array} [holders] The `partials` placeholder indexes. * @param {Array} [argPos] The argument positions of the new function. * @param {number} [ary] The arity cap of `func`. * @param {number} [arity] The arity of `func`. * @returns {Function} Returns the new wrapped function. */ function createWrap(func, bitmask, thisArg, partials, holders, argPos, ary, arity) { var isBindKey = bitmask & CONSTANTS_1.WRAP_BIND_KEY_FLAG; if (!isBindKey && typeof func != 'function') { throw new TypeError(CONSTANTS_1.FUNC_ERROR_TEXT); } var length = partials ? partials.length : 0; if (!length) { bitmask &= ~(CONSTANTS_1.WRAP_PARTIAL_FLAG | CONSTANTS_1.WRAP_PARTIAL_RIGHT_FLAG); partials = holders = undefined; } ary = ary === undefined ? ary : (0, nativeMax_1.default)((0, toInteger_1.default)(ary), 0); arity = arity === undefined ? arity : (0, toInteger_1.default)(arity); length -= holders ? holders.length : 0; if (bitmask & CONSTANTS_1.WRAP_PARTIAL_RIGHT_FLAG) { var partialsRight = partials, holdersRight = holders; partials = holders = undefined; } var data = isBindKey ? undefined : (0, getData_1.default)(func); var newData = [ func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity ]; if (data) { (0, mergeData_1.default)(newData, data); } func = newData[0]; bitmask = newData[1]; thisArg = newData[2]; partials = newData[3]; holders = newData[4]; arity = newData[9] = newData[9] === undefined ? (isBindKey ? 0 : func.length) : (0, nativeMax_1.default)(newData[9] - length, 0); if (!arity && bitmask & (CONSTANTS_1.WRAP_CURRY_FLAG | CONSTANTS_1.WRAP_CURRY_RIGHT_FLAG)) { bitmask &= ~(CONSTANTS_1.WRAP_CURRY_FLAG | CONSTANTS_1.WRAP_CURRY_RIGHT_FLAG); } if (!bitmask || bitmask == CONSTANTS_1.WRAP_BIND_FLAG) { var result = (0, createBind_1.default)(func, bitmask, thisArg); } else if (bitmask == CONSTANTS_1.WRAP_CURRY_FLAG || bitmask == CONSTANTS_1.WRAP_CURRY_RIGHT_FLAG) { result = (0, createCurry_1.default)(func, bitmask, arity); } else if ((bitmask == CONSTANTS_1.WRAP_PARTIAL_FLAG || bitmask == (CONSTANTS_1.WRAP_BIND_FLAG | CONSTANTS_1.WRAP_PARTIAL_FLAG)) && !holders.length) { result = (0, createPartial_1.default)(func, bitmask, thisArg, partials); } else { result = createHybrid_1.default.apply(undefined, newData); } var setter = data ? baseSetData_1.default : setData_1.default; return (0, setWrapToString_1.default)(setter(result, newData), func, bitmask); } exports.default = createWrap;