Rename createBound to createWrapper and break createWrapper apart.

This commit is contained in:
John-David Dalton
2013-10-29 23:58:16 -07:00
parent e019441ede
commit 852b324819
7 changed files with 430 additions and 332 deletions

View File

@@ -476,6 +476,62 @@
return bind(func, thisArg);
}
/**
* The base implementation of `createWrapper` without `func` type checking
* or support for setting meta data.
*
* @private
* @param {Function|string} func The function or method name to reference.
* @param {number} bitmask The bitmask of method flags to compose.
* @param {Array} [partialArgs] An array of arguments to prepend to those
* provided to the new function.
* @param {Array} [partialRightArgs] An array of arguments to append to those
* provided to the new function.
* @param {*} [thisArg] The `this` binding of `func`.
* @param {number} [arity] The arity of `func`.
* @returns {Function} Returns the new function.
*/
function baseCreateWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
var isBind = bitmask & 1,
isBindKey = bitmask & 2,
isCurry = bitmask & 4,
isCurryBound = bitmask & 8,
isPartial = bitmask & 16,
isPartialRight = bitmask & 32,
key = func;
function bound() {
var thisBinding = isBind ? thisArg : this;
if (isCurry || isPartial || isPartialRight) {
if (isPartial) {
var args = partialArgs.slice();
push.apply(args, arguments);
}
if (isPartialRight || isCurry) {
args || (args = slice(arguments));
if (isPartialRight) {
push.apply(args, partialRightArgs);
}
if (isCurry && args.length < arity) {
bitmask |= 16 & ~32;
return createWrapper(func, (isCurryBound ? bitmask : bitmask & ~3), args, null, thisArg, arity);
}
}
}
args || (args = arguments);
if (isBindKey) {
func = thisBinding[key];
}
if (this instanceof bound) {
thisBinding = baseCreate(func.prototype);
var result = func.apply(thisBinding, args);
return isObject(result) ? result : thisBinding;
}
return func.apply(thisBinding, args);
}
return bound;
}
/**
* The base implementation of `_.flatten` without support for callback
* shorthands or `thisArg` binding.
@@ -726,16 +782,15 @@
* provided to the new function.
* @param {*} [thisArg] The `this` binding of `func`.
* @param {number} [arity] The arity of `func`.
* @returns {Function} Returns the new bound function.
* @returns {Function} Returns the new function.
*/
function createBound(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
function createWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
var isBind = bitmask & 1,
isBindKey = bitmask & 2,
isCurry = bitmask & 4,
isCurryBound = bitmask & 8,
isPartial = bitmask & 16,
isPartialRight = bitmask & 32,
key = func;
isPartialRight = bitmask & 32;
if (!isBindKey && !isFunction(func)) {
throw new TypeError;
@@ -749,41 +804,11 @@
isPartialRight = partialRightArgs = false;
}
// fast path for `_.bind`
if (bitmask == 1 || bitmask === 17) {
var bound = baseBind(func, thisArg, partialArgs);
}
else {
bound = function() {
var thisBinding = isBind ? thisArg : this;
if (isCurry || isPartial || isPartialRight) {
if (isPartial) {
var args = partialArgs.slice();
push.apply(args, arguments);
}
if (isPartialRight || isCurry) {
args || (args = slice(arguments));
if (isPartialRight) {
push.apply(args, partialRightArgs);
}
if (isCurry && args.length < arity) {
bitmask |= 16 & ~32;
return createBound(func, (isCurryBound ? bitmask : bitmask & ~3), args, null, thisArg, arity);
}
}
}
args || (args = arguments);
if (isBindKey) {
func = thisBinding[key];
}
if (this instanceof bound) {
thisBinding = baseCreate(func.prototype);
var result = func.apply(thisBinding, args);
return isObject(result) ? result : thisBinding;
}
return func.apply(thisBinding, args);
};
}
return bound;
var result = (bitmask == 1 || bitmask === 17)
? baseBind(func, thisArg, partialArgs)
: baseCreateWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, arity);
return result;
}
/**
@@ -3630,8 +3655,8 @@
*/
function bind(func, thisArg) {
return arguments.length > 2
? createBound(func, 17, slice(arguments, 2), null, thisArg)
: createBound(func, 1, null, null, thisArg);
? createWrapper(func, 17, slice(arguments, 2), null, thisArg)
: createWrapper(func, 1, null, null, thisArg);
}
/**
@@ -3665,7 +3690,7 @@
while (++index < length) {
var key = funcs[index];
object[key] = createBound(object[key], 1, null, null, object);
object[key] = createWrapper(object[key], 1, null, null, object);
}
return object;
}
@@ -4052,7 +4077,7 @@
* // => 'hi fred'
*/
function partial(func) {
return createBound(func, 16, slice(arguments, 1));
return createWrapper(func, 16, slice(arguments, 1));
}
/**
@@ -4129,7 +4154,7 @@
* // => '<p>Fred, Wilma, &amp; Pebbles</p>'
*/
function wrap(value, wrapper) {
return createBound(wrapper, 16, [value]);
return createWrapper(wrapper, 16, [value]);
}
/*--------------------------------------------------------------------------*/