-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1525 from davidchambers/consistent-each
remove references to native array methods
- Loading branch information
Showing
4 changed files
with
25 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,15 +31,6 @@ | |
// All **ECMAScript 5** native function implementations that we hope to use | ||
// are declared here. | ||
var | ||
nativeForEach = ArrayProto.forEach, | ||
nativeMap = ArrayProto.map, | ||
nativeReduce = ArrayProto.reduce, | ||
nativeReduceRight = ArrayProto.reduceRight, | ||
nativeFilter = ArrayProto.filter, | ||
nativeEvery = ArrayProto.every, | ||
nativeSome = ArrayProto.some, | ||
nativeIndexOf = ArrayProto.indexOf, | ||
nativeLastIndexOf = ArrayProto.lastIndexOf, | ||
nativeIsArray = Array.isArray, | ||
nativeKeys = Object.keys, | ||
nativeBind = FuncProto.bind; | ||
|
@@ -71,13 +62,11 @@ | |
// -------------------- | ||
|
||
// The cornerstone, an `each` implementation, aka `forEach`. | ||
// Handles objects with the built-in `forEach`, arrays, and raw objects. | ||
// Delegates to **ECMAScript 5**'s native `forEach` if available. | ||
// Handles raw objects in addition to array-likes. Treats all | ||
// sparse array-likes as if they were dense. | ||
_.each = _.forEach = function(obj, iterator, context) { | ||
if (obj == null) return obj; | ||
if (nativeForEach && obj.forEach === nativeForEach) { | ||
obj.forEach(iterator, context); | ||
} else if (obj.length === +obj.length) { | ||
if (obj.length === +obj.length) { | ||
for (var i = 0, length = obj.length; i < length; i++) { | ||
if (iterator.call(context, obj[i], i, obj) === breaker) return; | ||
} | ||
|
@@ -91,11 +80,9 @@ | |
}; | ||
|
||
// Return the results of applying the iterator to each element. | ||
// Delegates to **ECMAScript 5**'s native `map` if available. | ||
_.map = _.collect = function(obj, iterator, context) { | ||
var results = []; | ||
if (obj == null) return results; | ||
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context); | ||
_.each(obj, function(value, index, list) { | ||
results.push(iterator.call(context, value, index, list)); | ||
}); | ||
|
@@ -105,14 +92,10 @@ | |
var reduceError = 'Reduce of empty array with no initial value'; | ||
|
||
// **Reduce** builds up a single result from a list of values, aka `inject`, | ||
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available. | ||
// or `foldl`. | ||
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) { | ||
var initial = arguments.length > 2; | ||
if (obj == null) obj = []; | ||
if (nativeReduce && obj.reduce === nativeReduce) { | ||
if (context) iterator = _.bind(iterator, context); | ||
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator); | ||
} | ||
_.each(obj, function(value, index, list) { | ||
if (!initial) { | ||
memo = value; | ||
|
@@ -126,14 +109,9 @@ | |
}; | ||
|
||
// The right-associative version of reduce, also known as `foldr`. | ||
// Delegates to **ECMAScript 5**'s native `reduceRight` if available. | ||
_.reduceRight = _.foldr = function(obj, iterator, memo, context) { | ||
var initial = arguments.length > 2; | ||
if (obj == null) obj = []; | ||
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) { | ||
if (context) iterator = _.bind(iterator, context); | ||
return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator); | ||
} | ||
var length = obj.length; | ||
if (length !== +length) { | ||
var keys = _.keys(obj); | ||
|
@@ -165,12 +143,10 @@ | |
}; | ||
|
||
// Return all the elements that pass a truth test. | ||
// Delegates to **ECMAScript 5**'s native `filter` if available. | ||
// Aliased as `select`. | ||
_.filter = _.select = function(obj, predicate, context) { | ||
var results = []; | ||
if (obj == null) return results; | ||
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(predicate, context); | ||
_.each(obj, function(value, index, list) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jdalton
Contributor
|
||
if (predicate.call(context, value, index, list)) results.push(value); | ||
}); | ||
|
@@ -183,27 +159,23 @@ | |
}; | ||
|
||
// Determine whether all of the elements match a truth test. | ||
// Delegates to **ECMAScript 5**'s native `every` if available. | ||
// Aliased as `all`. | ||
_.every = _.all = function(obj, predicate, context) { | ||
predicate || (predicate = _.identity); | ||
var result = true; | ||
if (obj == null) return result; | ||
if (nativeEvery && obj.every === nativeEvery) return obj.every(predicate, context); | ||
_.each(obj, function(value, index, list) { | ||
if (!(result = result && predicate.call(context, value, index, list))) return breaker; | ||
}); | ||
return !!result; | ||
}; | ||
|
||
// Determine if at least one element in the object matches a truth test. | ||
// Delegates to **ECMAScript 5**'s native `some` if available. | ||
// Aliased as `any`. | ||
_.some = _.any = function(obj, predicate, context) { | ||
predicate || (predicate = _.identity); | ||
var result = false; | ||
if (obj == null) return result; | ||
if (nativeSome && obj.some === nativeSome) return obj.some(predicate, context); | ||
_.each(obj, function(value, index, list) { | ||
if (result || (result = predicate.call(context, value, index, list))) return breaker; | ||
}); | ||
|
@@ -214,7 +186,6 @@ | |
// Aliased as `include`. | ||
_.contains = _.include = function(obj, target) { | ||
if (obj == null) return false; | ||
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1; | ||
This comment has been minimized.
Sorry, something went wrong.
jdalton
Contributor
|
||
return _.some(obj, function(value) { | ||
return value === target; | ||
}); | ||
|
@@ -568,7 +539,6 @@ | |
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**), | ||
// we need this function. Return the position of the first occurrence of an | ||
// item in an array, or -1 if the item is not included in the array. | ||
// Delegates to **ECMAScript 5**'s native `indexOf` if available. | ||
// If the array is large and already in sort order, pass `true` | ||
// for **isSorted** to use binary search. | ||
_.indexOf = function(array, item, isSorted) { | ||
|
@@ -582,19 +552,13 @@ | |
return array[i] === item ? i : -1; | ||
} | ||
} | ||
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted); | ||
for (; i < length; i++) if (array[i] === item) return i; | ||
return -1; | ||
}; | ||
|
||
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available. | ||
_.lastIndexOf = function(array, item, from) { | ||
if (array == null) return -1; | ||
var hasIndex = from != null; | ||
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) { | ||
return hasIndex ? array.lastIndexOf(item, from) : array.lastIndexOf(item); | ||
} | ||
var i = (hasIndex ? from : array.length); | ||
var i = from == null ? array.length : from; | ||
while (i--) if (array[i] === item) return i; | ||
return -1; | ||
}; | ||
|
internal
_.each
use could be swapped with abaseEach
like helper that avoids things likecontext
binding (e.g.iterator.call(...)
since internal use isn't using thecontext
.