Consistently coerce keys to strings before passing them to hasOwnProperty and init Array lengths when possible.

Former-commit-id: 5bd397eafbae888c7e6c76e62a7021b85796e65a
This commit is contained in:
John-David Dalton
2012-12-25 23:12:19 -06:00
parent e3b80a5e09
commit f4120a9c8c
5 changed files with 152 additions and 142 deletions

View File

@@ -1595,7 +1595,7 @@
callback = createCallback(callback, thisArg);
forEach(collection, function(value, key, collection) {
key = callback(value, key, collection);
key = callback(value, key, collection) + '';
(hasOwnProperty.call(result, key) ? result[key]++ : result[key] = 1);
});
return result;
@@ -1784,7 +1784,7 @@
callback = createCallback(callback, thisArg);
forEach(collection, function(value, key, collection) {
key = callback(value, key, collection);
key = callback(value, key, collection) + '';
(hasOwnProperty.call(result, key) ? result[key] : result[key] = []).push(value);
});
return result;
@@ -1814,11 +1814,13 @@
*/
function invoke(collection, methodName) {
var args = slice(arguments, 2),
index = -1,
isFunc = typeof methodName == 'function',
result = [];
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
forEach(collection, function(value) {
result.push((isFunc ? methodName : value[methodName]).apply(value, args));
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
return result;
}
@@ -2106,7 +2108,8 @@
*/
function shuffle(collection) {
var index = -1,
result = Array(collection ? collection.length : 0);
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
forEach(collection, function(value) {
var rand = floor(nativeRandom() * (++index + 1));
@@ -2208,18 +2211,20 @@
* // => ['moe', 'larry', 'brendan']
*/
function sortBy(collection, callback, thisArg) {
var result = [];
callback = createCallback(callback, thisArg);
var index = -1,
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
forEach(collection, function(value, index, collection) {
result.push({
'criteria': callback(value, index, collection),
callback = createCallback(callback, thisArg);
forEach(collection, function(value, key, collection) {
result[++index] = {
'criteria': callback(value, key, collection),
'index': index,
'value': value
});
};
});
var length = result.length;
length = result.length;
result.sort(compareAscending);
while (length--) {
result[length] = result[length].value;
@@ -3110,7 +3115,7 @@
function memoize(func, resolver) {
var cache = {};
return function() {
var key = resolver ? resolver.apply(this, arguments) : arguments[0];
var key = (resolver ? resolver.apply(this, arguments) : arguments[0]) + '';
return hasOwnProperty.call(cache, key)
? cache[key]
: (cache[key] = func.apply(this, arguments));