mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 19:07:49 +00:00
Remove hasThis and add comments to createBound and createCallback.
Former-commit-id: cdc9a69dc60913d6c2383053a881453dd2b7b0d5
This commit is contained in:
24
build.js
24
build.js
@@ -102,7 +102,7 @@
|
|||||||
'compose': [],
|
'compose': [],
|
||||||
'contains': ['baseEach', 'getIndexOf', 'isString'],
|
'contains': ['baseEach', 'getIndexOf', 'isString'],
|
||||||
'countBy': ['createCallback', 'forEach'],
|
'countBy': ['createCallback', 'forEach'],
|
||||||
'createCallback': ['hasThis', 'identity', 'isEqual', 'isObject', 'keys'],
|
'createCallback': ['bind', 'identity', 'isEqual', 'isObject', 'keys', 'setBindData'],
|
||||||
'debounce': ['isObject'],
|
'debounce': ['isObject'],
|
||||||
'defaults': ['createCallback', 'createIterator'],
|
'defaults': ['createCallback', 'createIterator'],
|
||||||
'defer': ['bind'],
|
'defer': ['bind'],
|
||||||
@@ -215,7 +215,6 @@
|
|||||||
'getArray': [],
|
'getArray': [],
|
||||||
'getIndexOf': ['baseIndexOf', 'indexOf'],
|
'getIndexOf': ['baseIndexOf', 'indexOf'],
|
||||||
'getObject': [],
|
'getObject': [],
|
||||||
'hasThis': ['setBindData'],
|
|
||||||
'isNode': [],
|
'isNode': [],
|
||||||
'iteratorTemplate': [],
|
'iteratorTemplate': [],
|
||||||
'lodash': ['lodashWrapper'],
|
'lodash': ['lodashWrapper'],
|
||||||
@@ -539,7 +538,6 @@
|
|||||||
'escapeStringChar',
|
'escapeStringChar',
|
||||||
'getArray',
|
'getArray',
|
||||||
'getObject',
|
'getObject',
|
||||||
'hasThis',
|
|
||||||
'isNode',
|
'isNode',
|
||||||
'iteratorTemplate',
|
'iteratorTemplate',
|
||||||
'lodash',
|
'lodash',
|
||||||
@@ -1638,17 +1636,22 @@
|
|||||||
* @returns {String} Returns the modified source.
|
* @returns {String} Returns the modified source.
|
||||||
*/
|
*/
|
||||||
function removeEsOptimization(source) {
|
function removeEsOptimization(source) {
|
||||||
// remove `__bindData` logic and `setBindData` function calls from `createBound`
|
// remove `__bindData__` logic and `setBindData` function calls from `createBound`
|
||||||
source = source.replace(matchFunction(source, 'createBound'), function(match) {
|
source = source.replace(matchFunction(source, 'createBound'), function(match) {
|
||||||
return match
|
return match
|
||||||
.replace(/\bargs *=.+?__bindData__.+\s+/, '')
|
.replace(/\bargs *=.+?__bindData__.+\s+/, '')
|
||||||
.replace(/^( *)if *\(args\)[\s\S]+?\n\1}/m, '')
|
.replace(/(?:\s*\/\/.*)*\n( *)if *\(args\)[\s\S]+?\n\1}/m, '')
|
||||||
.replace(/^.+?setBindData.+\n/m, '')
|
.replace(/(?:\s*\/\/.*)*\n.+args *= *nativeSlice.+/, '')
|
||||||
|
.replace(/(?:\s*\/\/.*)*\n.+?setBindData.+/m, '')
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// remove `hasThis` use `_.createCallback`
|
// remove `__bindData__` logic and `setBindData` function calls from `_.createCallback`
|
||||||
source = source.replace(matchFunction(source, 'createCallback'), function(match) {
|
source = source.replace(matchFunction(source, 'createCallback'), function(match) {
|
||||||
return match.replace(/\s*\|\|\s*!hasThis\(.+?\)/, '');
|
return match
|
||||||
|
.replace(/^( *)var bindData *=[\s\S]+?\n\1}\n/m, '')
|
||||||
|
.replace(/\s*\|\|\s*!bindData\b/, '')
|
||||||
|
.replace(/^( *)else if \(bindData[\s\S]+?\n\1}\n/m, '')
|
||||||
});
|
});
|
||||||
|
|
||||||
return source;
|
return source;
|
||||||
@@ -2732,8 +2735,9 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (isLegacy || isMobile || isUnderscore) {
|
if (isLegacy || isMobile || isUnderscore) {
|
||||||
funcDependencyMap.createBound = _.without(funcDependencyMap.createBound, 'setBindData');
|
_.each(['createBound', 'createCallback'], function(funcName) {
|
||||||
funcDependencyMap.createCallback = _.without(funcDependencyMap.createCallback, 'hasThis');
|
funcDependencyMap[funcName] = _.without(funcDependencyMap[funcName], 'bind', 'setBindData');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (_.contains(plusFuncs, 'chain') == !isUnderscore) {
|
if (_.contains(plusFuncs, 'chain') == !isUnderscore) {
|
||||||
funcDependencyMap.mixin = _.without(funcDependencyMap.mixin, 'isFunction');
|
funcDependencyMap.mixin = _.without(funcDependencyMap.mixin, 'isFunction');
|
||||||
|
|||||||
57
lodash.js
57
lodash.js
@@ -1386,10 +1386,11 @@
|
|||||||
* @returns {Function} Returns the new bound function.
|
* @returns {Function} Returns the new bound function.
|
||||||
*/
|
*/
|
||||||
function createBound(func, thisArg, partialArgs, partialRightArgs, isPartial, isAlt) {
|
function createBound(func, thisArg, partialArgs, partialRightArgs, isPartial, isAlt) {
|
||||||
var isFunc = isFunction(func);
|
var isBindKey = isAlt && !isPartial,
|
||||||
|
isFunc = isFunction(func);
|
||||||
|
|
||||||
// except for `_.bindKey`, throw when `func` is not a function
|
// throw if `func` is not a function when not behaving as `_.bindKey`
|
||||||
if (!isFunc && (isPartial || !isAlt)) {
|
if (!isFunc && !isBindKey) {
|
||||||
throw new TypeError;
|
throw new TypeError;
|
||||||
}
|
}
|
||||||
var args = func.__bindData__,
|
var args = func.__bindData__,
|
||||||
@@ -1398,19 +1399,25 @@
|
|||||||
if (args) {
|
if (args) {
|
||||||
push.apply(args[2], partialArgs);
|
push.apply(args[2], partialArgs);
|
||||||
push.apply(args[3], partialRightArgs);
|
push.apply(args[3], partialRightArgs);
|
||||||
|
|
||||||
|
// add `thisArg` to previous `_.partial` and `_.partialRight` arguments
|
||||||
if (!isPartial && args[4]) {
|
if (!isPartial && args[4]) {
|
||||||
args[1] = thisArg;
|
args[1] = thisArg;
|
||||||
args[4] = false;
|
args[4] = false;
|
||||||
|
args[5] = isAlt;
|
||||||
}
|
}
|
||||||
return createBound.apply(null, args);
|
return createBound.apply(null, args);
|
||||||
}
|
}
|
||||||
// juggle arguments for `_.bindKey`
|
// take a snapshot of `arguments` before juggling
|
||||||
if (!isPartial && isAlt) {
|
args = nativeSlice.call(arguments);
|
||||||
|
|
||||||
|
// juggle arguments for `_.bindKey` behavior
|
||||||
|
if (isBindKey) {
|
||||||
thisArg = func;
|
thisArg = func;
|
||||||
}
|
}
|
||||||
// use `Function#bind` if it exists and is fast
|
// use `Function#bind` if it exists and is fast
|
||||||
// (in V8 `Function#bind` is slower except when partially applied)
|
// (in V8 `Function#bind` is slower except when partially applied)
|
||||||
if (!isPartial && !isAlt && (support.fastBind || (nativeBind && partialArgs.length))) {
|
if (!isPartial && !isAlt && !partialRightArgs.length && (support.fastBind || (nativeBind && partialArgs.length))) {
|
||||||
var bound = nativeBind.call.apply(nativeBind, concat.call(arrayRef, func, thisArg, partialArgs));
|
var bound = nativeBind.call.apply(nativeBind, concat.call(arrayRef, func, thisArg, partialArgs));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -1420,7 +1427,7 @@
|
|||||||
var args = arguments,
|
var args = arguments,
|
||||||
thisBinding = isPartial ? this : thisArg;
|
thisBinding = isPartial ? this : thisArg;
|
||||||
|
|
||||||
if (!isFunc) {
|
if (isBindKey) {
|
||||||
func = thisArg[key];
|
func = thisArg[key];
|
||||||
}
|
}
|
||||||
if (partialArgs.length || partialRightArgs.length) {
|
if (partialArgs.length || partialRightArgs.length) {
|
||||||
@@ -1439,7 +1446,7 @@
|
|||||||
return func.apply(thisBinding, args);
|
return func.apply(thisBinding, args);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
setBindData(bound, nativeSlice.call(arguments));
|
setBindData(bound, args);
|
||||||
return bound;
|
return bound;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1543,23 +1550,6 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if `func` references the `this` keyword.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {Function} func The function to inspect.
|
|
||||||
* @returns {Boolean} Returns `true` if `this` is referenced, else `false`.
|
|
||||||
*/
|
|
||||||
function hasThis(func) {
|
|
||||||
var result = func.__bindData__;
|
|
||||||
if (typeof result != 'undefined') {
|
|
||||||
return result === true || (result && result[4]);
|
|
||||||
}
|
|
||||||
result = !reThis || reThis.test(fnToString.call(func));
|
|
||||||
setBindData(func, result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets `this` binding data on a given function.
|
* Sets `this` binding data on a given function.
|
||||||
*
|
*
|
||||||
@@ -4828,6 +4818,7 @@
|
|||||||
}
|
}
|
||||||
var type = typeof func;
|
var type = typeof func;
|
||||||
if (type != 'function') {
|
if (type != 'function') {
|
||||||
|
// handle "_.pluck" style callback shorthands
|
||||||
if (type != 'object') {
|
if (type != 'object') {
|
||||||
return function(object) {
|
return function(object) {
|
||||||
return object[func];
|
return object[func];
|
||||||
@@ -4837,7 +4828,10 @@
|
|||||||
key = props[0],
|
key = props[0],
|
||||||
a = func[key];
|
a = func[key];
|
||||||
|
|
||||||
|
// handle "_.where" style callback shorthands
|
||||||
if (props.length == 1 && a === a && !isObject(a)) {
|
if (props.length == 1 && a === a && !isObject(a)) {
|
||||||
|
// fast path the common case of passing an object with a single
|
||||||
|
// property containing a primitive value
|
||||||
return function(object) {
|
return function(object) {
|
||||||
var b = object[key];
|
var b = object[key];
|
||||||
return a === b && (a !== 0 || (1 / a == 1 / b));
|
return a === b && (a !== 0 || (1 / a == 1 / b));
|
||||||
@@ -4855,9 +4849,20 @@
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (typeof thisArg == 'undefined' || !hasThis(func)) {
|
var bindData = func.__bindData__;
|
||||||
|
if (typeof bindData == 'undefined') {
|
||||||
|
// checks if `func` references the `this` keyword and stores the result
|
||||||
|
bindData = !reThis || reThis.test(fnToString.call(func));
|
||||||
|
setBindData(func, bindData);
|
||||||
|
}
|
||||||
|
if (typeof thisArg == 'undefined' || !bindData) {
|
||||||
return func;
|
return func;
|
||||||
}
|
}
|
||||||
|
else if (bindData !== true) {
|
||||||
|
// exit early if already bound or leverage bind optimizations if
|
||||||
|
// created by `_.partial` or `_.partialRight`
|
||||||
|
return bindData[4] ? bind(func, thisArg) : func;
|
||||||
|
}
|
||||||
if (argCount === 1) {
|
if (argCount === 1) {
|
||||||
return function(value) {
|
return function(value) {
|
||||||
return func.call(thisArg, value);
|
return func.call(thisArg, value);
|
||||||
|
|||||||
Reference in New Issue
Block a user