Optimize _.contains.

Former-commit-id: b50628adedd21fe2c099f4dcab1aa9f0036a22ed
This commit is contained in:
John-David Dalton
2012-11-10 23:09:23 -08:00
parent 3b9bd944fb
commit 07cbfdb424
5 changed files with 74 additions and 62 deletions

View File

@@ -70,7 +70,7 @@
'clone': ['extend', 'forEach', 'forOwn', 'isArguments', 'isObject', 'isPlainObject'],
'compact': [],
'compose': [],
'contains': ['indexOf', 'isString', 'some'],
'contains': ['forEach', 'indexOf', 'isString'],
'countBy': ['forEach'],
'debounce': [],
'defaults': ['isArguments'],
@@ -974,7 +974,7 @@
dependencyMap.reduceRight = ['forEach', 'keys'];
}
if (isUnderscore) {
dependencyMap.contains = ['indexOf', 'some'],
dependencyMap.contains = ['forEach', 'indexOf'],
dependencyMap.isEqual = ['isArray', 'isFunction'];
dependencyMap.isEmpty = ['isArray', 'isString'];
dependencyMap.max = ['forEach', 'isArray'];
@@ -1075,10 +1075,16 @@
// replace `_.contains`
source = source.replace(/^( *)function contains[\s\S]+?\n\1}/m, [
' function contains(collection, target) {',
' var length = collection ? collection.length : 0;',
" return typeof length == 'number'",
' ? indexOf(collection, target) > -1',
' : some(collection, function(value) { return value === target; });',
' var length = collection ? collection.length : 0,',
' result = false;',
" if (typeof length == 'number') {",
' result = indexOf(collection, target) > -1;',
' } else {',
' forEach(collection, function(value) {',
' return (result = value === target) && indicatorObject;',
' });',
' }',
' return result;',
' }'
].join('\n'));