Allow argCount to be omitted an not default to 3, and cleanup createBound.

Former-commit-id: cbafeaa441aae1ef28268bc2614b1a6e038acfe1
This commit is contained in:
John-David Dalton
2013-07-22 19:11:44 -07:00
parent 2c772d0f4c
commit 17e32017a7
8 changed files with 272 additions and 277 deletions

View File

@@ -485,7 +485,6 @@
/** Native method shortcuts */
var ceil = Math.ceil,
clearTimeout = context.clearTimeout,
concat = arrayRef.concat,
defineProperty = reNative.test(defineProperty = Object.defineProperty) && defineProperty,
floor = Math.floor,
fnToString = Function.prototype.toString,
@@ -495,7 +494,8 @@
propertyIsEnumerable = objectProto.propertyIsEnumerable,
setImmediate = context.setImmediate,
setTimeout = context.setTimeout,
toString = objectProto.toString;
toString = objectProto.toString,
unshift = arrayRef.unshift;
/* Native method shortcuts for methods with the same name as other `lodash` methods */
var nativeBind = reNative.test(nativeBind = toString.bind) && nativeBind,
@@ -954,7 +954,7 @@
/** Reusable iterator options shared by `each`, `forIn`, and `forOwn` */
var eachIteratorOptions = {
'args': 'collection, callback, thisArg',
'top': "callback = callback && typeof thisArg == 'undefined' ? callback : lodash.createCallback(callback, thisArg)",
'top': "callback = callback && typeof thisArg == 'undefined' ? callback : lodash.createCallback(callback, thisArg, 3)",
'array': "typeof length == 'number'",
'loop': 'if (callback(iterable[index], index, collection) === false) return result'
};
@@ -1393,9 +1393,7 @@
if (!isFunc && !isBindKey) {
throw new TypeError;
}
var args = func.__bindData__,
key = thisArg;
var args = func.__bindData__;
if (args) {
push.apply(args[2], partialArgs);
push.apply(args[3], partialRightArgs);
@@ -1408,17 +1406,12 @@
}
return createBound.apply(null, args);
}
// take a snapshot of `arguments` before juggling
args = nativeSlice.call(arguments);
// juggle arguments for `_.bindKey` behavior
if (isBindKey) {
thisArg = func;
}
// use `Function#bind` if it exists and is fast
// (in V8 `Function#bind` is slower except when partially applied)
if (!isPartial && !isAlt && !partialRightArgs.length && (support.fastBind || (nativeBind && partialArgs.length))) {
var bound = nativeBind.call.apply(nativeBind, concat.call(arrayRef, func, thisArg, partialArgs));
args = [func, thisArg];
push.apply(args, partialArgs);
var bound = nativeBind.call.apply(nativeBind, args);
}
else {
bound = function() {
@@ -1431,7 +1424,7 @@
func = thisArg[key];
}
if (partialArgs.length || partialRightArgs.length) {
args = concat.apply(partialArgs, args);
unshift.apply(args, partialArgs);
push.apply(args, partialRightArgs);
}
if (this instanceof bound) {
@@ -1446,6 +1439,12 @@
return func.apply(thisBinding, args);
};
}
// take a snapshot of `arguments` before juggling
args = nativeSlice.call(arguments);
if (isBindKey) {
var key = thisArg;
thisArg = func;
}
setBindData(bound, args);
return bound;
}
@@ -1930,7 +1929,7 @@
*/
function findKey(object, callback, thisArg) {
var result;
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
forOwn(object, function(value, key, object) {
if (callback(value, key, object)) {
result = key;
@@ -2552,7 +2551,7 @@
result = {};
if (isFunc) {
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
} else {
var props = baseFlatten(arguments, true, false, 1);
}
@@ -2634,7 +2633,7 @@
}
}
} else {
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
forIn(object, function(value, key, object) {
if (callback(value, key, object)) {
result[key] = value;
@@ -2842,7 +2841,7 @@
*/
function countBy(collection, callback, thisArg) {
var result = {};
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
forEach(collection, function(value, key, collection) {
key = String(callback(value, key, collection));
@@ -2894,7 +2893,7 @@
*/
function every(collection, callback, thisArg) {
var result = true;
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
if (isArray(collection)) {
var index = -1,
@@ -2955,7 +2954,7 @@
*/
function filter(collection, callback, thisArg) {
var result = [];
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
if (isArray(collection)) {
var index = -1,
@@ -3021,7 +3020,7 @@
* // => { 'name': 'banana', 'organic': true, 'type': 'fruit' }
*/
function find(collection, callback, thisArg) {
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
if (isArray(collection)) {
var index = -1,
@@ -3119,7 +3118,7 @@
*/
function groupBy(collection, callback, thisArg) {
var result = {};
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
forEach(collection, function(value, key, collection) {
key = String(callback(value, key, collection));
@@ -3207,7 +3206,7 @@
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
if (isArray(collection)) {
while (++index < length) {
result[index] = callback(collection[index], index, collection);
@@ -3276,7 +3275,7 @@
} else {
callback = (!callback && isString(collection))
? charAtCallback
: lodash.createCallback(callback, thisArg);
: lodash.createCallback(callback, thisArg, 3);
baseEach(collection, function(value, index, collection) {
var current = callback(value, index, collection);
@@ -3345,7 +3344,7 @@
} else {
callback = (!callback && isString(collection))
? charAtCallback
: lodash.createCallback(callback, thisArg);
: lodash.createCallback(callback, thisArg, 3);
baseEach(collection, function(value, index, collection) {
var current = callback(value, index, collection);
@@ -3514,7 +3513,7 @@
* // => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]
*/
function reject(collection, callback, thisArg) {
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
return filter(collection, function(value, index, collection) {
return !callback(value, index, collection);
});
@@ -3616,7 +3615,7 @@
*/
function some(collection, callback, thisArg) {
var result;
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
if (isArray(collection)) {
var index = -1,
@@ -3675,7 +3674,7 @@
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
forEach(collection, function(value, key, collection) {
var object = result[++index] = getObject();
object.criteria = callback(value, key, collection);
@@ -3841,7 +3840,7 @@
var index = -1,
length = array ? array.length : 0;
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
while (++index < length) {
if (callback(array[index], index, array)) {
return index;
@@ -3914,7 +3913,7 @@
if (typeof callback != 'number' && callback != null) {
var index = -1;
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
while (++index < length && callback(array[index], index, array)) {
n++;
}
@@ -4082,7 +4081,7 @@
if (typeof callback != 'number' && callback != null) {
var index = length;
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
while (index-- && callback(array[index], index, array)) {
n++;
}
@@ -4215,7 +4214,7 @@
if (typeof callback != 'number' && callback != null) {
var index = length;
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
while (index-- && callback(array[index], index, array)) {
n++;
}
@@ -4375,7 +4374,7 @@
index = -1,
length = array ? array.length : 0;
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
while (++index < length && callback(array[index], index, array)) {
n++;
}
@@ -4519,7 +4518,7 @@
isSorted = false;
}
if (callback != null) {
callback = lodash.createCallback(callback, thisArg);
callback = lodash.createCallback(callback, thisArg, 3);
}
return baseUniq(array, isSorted, callback);
}
@@ -4779,7 +4778,7 @@
* @category Functions
* @param {Mixed} [func=identity] The value to convert to a callback.
* @param {Mixed} [thisArg] The `this` binding of the created callback.
* @param {Number} [argCount=3] The number of arguments the callback accepts.
* @param {Number} [argCount] The number of arguments the callback accepts.
* @returns {Function} Returns a callback function.
* @example
*
@@ -4863,23 +4862,22 @@
// created by `_.partial` or `_.partialRight`
return bindData[4] ? bind(func, thisArg) : func;
}
if (argCount === 1) {
return function(value) {
switch (argCount) {
case 1: return function(value) {
return func.call(thisArg, value);
};
}
if (argCount === 2) {
return function(a, b) {
case 2: return function(a, b) {
return func.call(thisArg, a, b);
};
}
if (argCount === 4) {
return function(accumulator, value, index, collection) {
case 3: return function(value, index, collection) {
return func.call(thisArg, value, index, collection);
};
case 4: return function(accumulator, value, index, collection) {
return func.call(thisArg, accumulator, value, index, collection);
};
}
return function(value, index, collection) {
return func.call(thisArg, value, index, collection);
return function() {
return func.apply(thisArg, arguments);
};
}