Ensure remove of the "exit early" feature is handled properly when mixing lodash methods into the underscore build.

Former-commit-id: 973c3e188076ca4403f59684b82bee3049370d5a
This commit is contained in:
John-David Dalton
2013-04-30 09:12:57 -07:00
parent c938017baa
commit d24d83d52a

View File

@@ -2074,7 +2074,7 @@
' result = indexOf(collection, target) > -1;', ' result = indexOf(collection, target) > -1;',
' } else {', ' } else {',
' each(collection, function(value) {', ' each(collection, function(value) {',
' return (result = value === target) && indicatorObject;', ' return !(result = value === target);',
' });', ' });',
' }', ' }',
' return result;', ' return result;',
@@ -2298,14 +2298,14 @@
' forIn(b, function(value, key, b) {', ' forIn(b, function(value, key, b) {',
' if (hasOwnProperty.call(b, key)) {', ' if (hasOwnProperty.call(b, key)) {',
' size++;', ' size++;',
' return !(result = hasOwnProperty.call(a, key) && isEqual(a[key], value, stackA, stackB)) && indicatorObject;', ' return (result = hasOwnProperty.call(a, key) && isEqual(a[key], value, stackA, stackB));',
' }', ' }',
' });', ' });',
'', '',
' if (result) {', ' if (result) {',
' forIn(a, function(value, key, a) {', ' forIn(a, function(value, key, a) {',
' if (hasOwnProperty.call(a, key)) {', ' if (hasOwnProperty.call(a, key)) {',
' return !(result = --size > -1) && indicatorObject;', ' return (result = --size > -1);',
' }', ' }',
' });', ' });',
' }', ' }',
@@ -2545,48 +2545,34 @@
source = removeFunction(source, 'slice'); source = removeFunction(source, 'slice');
source = source.replace(/([^.])\bslice\(/g, '$1nativeSlice.call('); source = source.replace(/([^.])\bslice\(/g, '$1nativeSlice.call(');
// remove `_.isEqual` use from `createCallback`
source = source.replace(matchFunction(source, 'createCallback'), function(match) {
return match.replace(/\bisEqual\(([^,]+), *([^,]+)[^)]+\)/, '$1 === $2');
});
// remove conditional `charCodeCallback` use from `_.max` and `_.min` // remove conditional `charCodeCallback` use from `_.max` and `_.min`
_.each(['max', 'min'], function(methodName) { _.each(['max', 'min'], function(methodName) {
source = source.replace(matchFunction(source, methodName), function(match) { if (!useLodashMethod(methodName)) {
return match.replace(/=.+?callback *&& *isString[^:]+:\s*/g, '= '); source = source.replace(matchFunction(source, methodName), function(match) {
}); return match.replace(/=.+?callback *&& *isString[^:]+:\s*/g, '= ');
});
}
}); });
// modify `_.every`, `_.find`, `_.isEqual`, and `_.some` to use the private `indicatorObject` // modify `_.isEqual` and `shimIsPlainObject` to use the private `indicatorObject`
_.each(['every', 'isEqual'], function(methodName) { if (!useLodashMethod('forIn')) {
source = source.replace(matchFunction(source, methodName), function(match) { source = source.replace(matchFunction(source, 'isEqual'), function(match) {
return match.replace(/\(result *= *(.+?)\);/g, '!(result = $1) && indicatorObject;'); return match.replace(/\(result *= *(.+?)\);/g, '!(result = $1) && indicatorObject;');
}); });
});
source = source.replace(matchFunction(source, 'find'), function(match) {
return match.replace(/return false/, 'return indicatorObject');
});
source = source.replace(matchFunction(source, 'some'), function(match) {
return match.replace(/!\(result *= *(.+?)\);/, '(result = $1) && indicatorObject;');
});
source = source.replace(matchFunction(source, 'shimIsPlainObject'), function(match) {
return match.replace(/return false/, 'return indicatorObject');
});
}
// remove unneeded variables // remove unneeded variables
if (!useLodashMethod('clone') && !useLodashMethod('cloneDeep')) { if (!useLodashMethod('clone') && !useLodashMethod('cloneDeep')) {
source = removeVar(source, 'cloneableClasses'); source = removeVar(source, 'cloneableClasses');
source = removeVar(source, 'ctorByClass'); source = removeVar(source, 'ctorByClass');
} }
// remove chainability from `each` and `_.forEach` // remove `_.isEqual` use from `createCallback`
if (!useLodashMethod('forEach')) { if (!useLodashMethod('where')) {
_.each(['each', 'forEach'], function(methodName) { source = source.replace(matchFunction(source, 'createCallback'), function(match) {
source = source.replace(matchFunction(source, methodName), function(match) { return match.replace(/\bisEqual\(([^,]+), *([^,]+)[^)]+\)/, '$1 === $2');
return match
.replace(/\n *return .+?([};\s]+)$/, '$1')
.replace(/\b(return) +result\b/, '$1')
});
}); });
} }
// remove unused features from `createBound` // remove unused features from `createBound`
@@ -2667,6 +2653,22 @@
.replace(/\beach(?=\(collection)/g, 'forOwn') .replace(/\beach(?=\(collection)/g, 'forOwn')
.replace(/\beach(?=\(\[)/g, 'forEach'); .replace(/\beach(?=\(\[)/g, 'forEach');
} }
// modify `_.contains`, `_.every`, `_.find`, and `_.some` to use the private `indicatorObject`
if (isUnderscore && (/\beach\(/.test(source) || !useLodashMethod('forOwn'))) {
source = source.replace(matchFunction(source, 'every'), function(match) {
return match.replace(/\(result *= *(.+?)\);/g, '!(result = $1) && indicatorObject;');
});
source = source.replace(matchFunction(source, 'find'), function(match) {
return match.replace(/return false/, 'return indicatorObject');
});
_.each(['contains', 'some'], function(methodName) {
source = source.replace(matchFunction(source, methodName), function(match) {
return match.replace(/!\(result *= *(.+?)\);/, '(result = $1) && indicatorObject;');
});
});
}
var context = vm.createContext({ var context = vm.createContext({
'clearTimeout': clearTimeout, 'clearTimeout': clearTimeout,
@@ -2739,6 +2741,16 @@
if (!useLodashMethod('createCallback')) { if (!useLodashMethod('createCallback')) {
source = source.replace(/\blodash\.(createCallback\()\b/g, '$1'); source = source.replace(/\blodash\.(createCallback\()\b/g, '$1');
} }
// remove chainability from `each` and `_.forEach`
if (!useLodashMethod('forEach')) {
_.each(['each', 'forEach'], function(methodName) {
source = source.replace(matchFunction(source, methodName), function(match) {
return match
.replace(/\n *return .+?([};\s]+)$/, '$1')
.replace(/\b(return) +result\b/, '$1')
});
});
}
// remove `_.assign`, `_.forIn`, `_.forOwn`, `_.isPlainObject`, and `_.zipObject` assignments // remove `_.assign`, `_.forIn`, `_.forOwn`, `_.isPlainObject`, and `_.zipObject` assignments
(function() { (function() {
var snippet = getMethodAssignments(source), var snippet = getMethodAssignments(source),