Ensure _.every and _.some work in Underscore builds without the "exit early" feature of _.forEach and reduce unexposed forIn and forOwn.

Former-commit-id: 35488c1c0f70cb5446c875bd4647b0ce47077d9e
This commit is contained in:
John-David Dalton
2012-10-20 02:38:59 -07:00
parent 7dfe69f6a5
commit 84eec23793
3 changed files with 56 additions and 33 deletions

View File

@@ -1185,9 +1185,17 @@
// simplify DOM node check from `_.isEqual`
source = source.replace(/(if *\(className *!= *objectClass).+?noNodeClass[\s\S]+?{/, '$1) {');
// remove "exit early" feature from `_.forEach`, `_.forIn`, and `_.forOwn`
// unexpose "exit early" feature from `_.forEach`, `_.forIn`, and `_.forOwn`
source = source.replace(/( +)var forEachIteratorOptions *=[\s\S]+?\n\1.+?;/, function(match) {
return match.replace(/if *\(callback[^']+/, 'callback(value, index, collection)');
return match.replace(/=== *false\)/, '=== objectTypes)');
});
source = source.replace(matchFunction(source, 'every'), function(match) {
return match.replace(/\(result *= *(.+?)\);/, '!(result = $1) && objectTypes;');
});
source = source.replace(matchFunction(source, 'some'), function(match) {
return match.replace(/!\(result *= *(.+?)\);/, '(result = $1) && objectTypes;');
});
// remove unused features from `createBound`
@@ -1406,6 +1414,18 @@
source = source.replace(/(?:\s*\/\/.*)*\n( +)if *\(hasObjectSpliceBug[\s\S]+?\n\1}/, '');
}
// remove `thisArg` from unexposed `forIn` and `forOwn`
_.each([
{ 'methodName': 'forIn', 'flag': exposeForIn },
{ 'methodName': 'forOwn', 'flag': exposeForOwn }
], function(data) {
if (!data.flag) {
source = source.replace(matchFunction(source, data.methodName), function(match) {
return match.replace(/(callback), *thisArg/g, '$1');
});
}
});
// remove `iteratesOwnLast` from `isPlainObject`
source = source.replace(/(?:\s*\/\/.*)*\n( +)if *\(iteratesOwnLast[\s\S]+?\n\1}/, '');