@newdash/newdash
Version:
javascript/typescript utility library
69 lines (68 loc) • 2.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bind = void 0;
// @ts-nocheck
const baseRest_1 = __importDefault(require("./.internal/baseRest"));
const CONSTANTS_1 = require("./.internal/CONSTANTS");
const createWrap_1 = __importDefault(require("./.internal/createWrap"));
const getHolder_1 = __importDefault(require("./.internal/getHolder"));
const replaceHolders_1 = __importDefault(require("./.internal/replaceHolders"));
/**
* @ignore
*/
const internalBind = (0, baseRest_1.default)((func, thisArg, partials) => {
let holders;
let bitmask = CONSTANTS_1.WRAP_BIND_FLAG;
if (partials.length) {
holders = (0, replaceHolders_1.default)(partials, (0, getHolder_1.default)(bind));
bitmask |= CONSTANTS_1.WRAP_PARTIAL_FLAG;
}
return (0, createWrap_1.default)(func, bitmask, thisArg, partials, holders);
});
/**
* Creates a function that invokes `func` with the `this` binding of `thisArg`
* and `partials` prepended to the arguments it receives.
*
* The `bind.placeholder` value, which defaults to `_` in monolithic builds,
* may be used as a placeholder for partially applied arguments.
*
* **Note:** Unlike native `Function#bind`, this method doesn't set the "length"
* property of bound functions.
*
* @since 5.3.0
* @category Function
* @param func The function to bind.
* @param thisArg The `this` binding of `func`.
* @param partials The arguments to be partially applied.
* @returns Returns the new bound function.
* @example
*
* ```js
* function greet(greeting, punctuation) {
* return greeting + ' ' + this.user + punctuation;
* }
*
* var object = { 'user': 'fred' };
*
* var bound = bind(greet, object, 'hi');
* bound('!');
* // => 'hi fred!'
*
* // Bound with placeholders.
* var bound = bind(greet, object, _, '!');
* bound('hi');
* // => 'hi fred!'
* ```
*/
function bind(func, thisArg, ...partials) {
return internalBind(func, thisArg, ...partials);
}
exports.bind = bind;
/**
* placeholder of bind function
*/
bind.placeholder = "__bind__placeholder__";
exports.default = bind;