From 1708663b3218c7bcc25d2c305d3676b7fa74b067 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 9 Oct 2012 00:48:02 -0700 Subject: [PATCH] Move `_.max`, `_.min`, `_.shuffle` back to "Collections" methods for compat but still optimized for arrays. Former-commit-id: 28cd9298915ad123445400a5d061ac8e9432eb6b --- lodash.js | 237 ++++++++++++++++++++++----------------------- test/test-build.js | 6 +- test/test.js | 1 + 3 files changed, 122 insertions(+), 122 deletions(-) diff --git a/lodash.js b/lodash.js index 48c5e5d2b..a69543cae 100644 --- a/lodash.js +++ b/lodash.js @@ -2088,6 +2088,98 @@ */ var map = createIterator(baseIteratorOptions, mapIteratorOptions); + /** + * Retrieves the maximum value of an `array`. If `callback` is passed, + * it will be executed for each value in the `array` to generate the + * criterion by which the value is ranked. The `callback` is bound to + * `thisArg` and invoked with three arguments; (value, index, collection). + * + * @static + * @memberOf _ + * @category Collections + * @param {Array} collection The collection to iterate over. + * @param {Function} [callback] The function called per iteration. + * @param {Mixed} [thisArg] The `this` binding of `callback`. + * @returns {Mixed} Returns the maximum value. + * @example + * + * var stooges = [ + * { 'name': 'moe', 'age': 40 }, + * { 'name': 'larry', 'age': 50 }, + * { 'name': 'curly', 'age': 60 } + * ]; + * + * _.max(stooges, function(stooge) { return stooge.age; }); + * // => { 'name': 'curly', 'age': 60 }; + */ + function max(collection, callback, thisArg) { + var computed = -Infinity, + index = -1, + length = collection ? collection.length : 0, + result = computed; + + if (callback || length !== +length) { + callback = createCallback(callback, thisArg); + forEach(collection, function(value, index, collection) { + var current = callback(value, index, collection); + if (current > computed) { + computed = current; + result = value; + } + }); + } else { + while (++index < length) { + if (collection[index] > result) { + result = collection[index]; + } + } + } + return result; + } + + /** + * Retrieves the minimum value of an `array`. If `callback` is passed, + * it will be executed for each value in the `array` to generate the + * criterion by which the value is ranked. The `callback` is bound to `thisArg` + * and invoked with three arguments; (value, index, collection). + * + * @static + * @memberOf _ + * @category Collections + * @param {Array} collection The collection to iterate over. + * @param {Function} [callback] The function called per iteration. + * @param {Mixed} [thisArg] The `this` binding of `callback`. + * @returns {Mixed} Returns the minimum value. + * @example + * + * _.min([10, 5, 100, 2, 1000]); + * // => 2 + */ + function min(collection, callback, thisArg) { + var computed = Infinity, + index = -1, + length = collection ? collection.length : 0, + result = computed; + + if (callback || length !== +length) { + callback = createCallback(callback, thisArg); + forEach(collection, function(value, index, collection) { + var current = callback(value, index, collection); + if (current < computed) { + computed = current; + result = value; + } + }); + } else { + while (++index < length) { + if (collection[index] < result) { + result = collection[index]; + } + } + } + return result; + } + /** * Retrieves the value of a specified property from all elements in * the `collection`. @@ -2215,6 +2307,32 @@ 'inLoop': '!' + filterIteratorOptions.inLoop }); + /** + * Creates an array of shuffled `array` values, using a version of the + * Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. + * + * @static + * @memberOf _ + * @category Collections + * @param {Array} collection The collection to shuffle. + * @returns {Array} Returns a new shuffled collection. + * @example + * + * _.shuffle([1, 2, 3, 4, 5, 6]); + * // => [4, 1, 6, 3, 5, 2] + */ + function shuffle(collection) { + var index = -1, + result = Array(collection ? collection.length : 0); + + forEach(collection, function(value) { + var rand = floor(nativeRandom() * (++index + 1)); + result[index] = result[rand]; + result[rand] = value; + }); + return result; + } + /** * Gets the size of the `collection` by returning `collection.length` for arrays * and array-like objects or the number of own enumerable properties for objects. @@ -2658,98 +2776,6 @@ return -1; } - /** - * Retrieves the maximum value of an `array`. If `callback` is passed, - * it will be executed for each value in the `array` to generate the - * criterion by which the value is ranked. The `callback` is bound to - * `thisArg` and invoked with three arguments; (value, index, array). - * - * @static - * @memberOf _ - * @category Arrays - * @param {Array} array The array to iterate over. - * @param {Function} [callback] The function called per iteration. - * @param {Mixed} [thisArg] The `this` binding of `callback`. - * @returns {Mixed} Returns the maximum value. - * @example - * - * var stooges = [ - * { 'name': 'moe', 'age': 40 }, - * { 'name': 'larry', 'age': 50 }, - * { 'name': 'curly', 'age': 60 } - * ]; - * - * _.max(stooges, function(stooge) { return stooge.age; }); - * // => { 'name': 'curly', 'age': 60 }; - */ - function max(array, callback, thisArg) { - var computed = -Infinity, - index = -1, - length = array ? array.length : 0, - result = computed; - - if (callback) { - callback = createCallback(callback, thisArg); - while (++index < length) { - var current = callback(array[index], index, array); - if (current > computed) { - computed = current; - result = array[index]; - } - } - } else { - while (++index < length) { - if (array[index] > result) { - result = array[index]; - } - } - } - return result; - } - - /** - * Retrieves the minimum value of an `array`. If `callback` is passed, - * it will be executed for each value in the `array` to generate the - * criterion by which the value is ranked. The `callback` is bound to `thisArg` - * and invoked with three arguments; (value, index, array). - * - * @static - * @memberOf _ - * @category Arrays - * @param {Array} array The array to iterate over. - * @param {Function} [callback] The function called per iteration. - * @param {Mixed} [thisArg] The `this` binding of `callback`. - * @returns {Mixed} Returns the minimum value. - * @example - * - * _.min([10, 5, 100, 2, 1000]); - * // => 2 - */ - function min(array, callback, thisArg) { - var computed = Infinity, - index = -1, - length = array ? array.length : 0, - result = computed; - - if (callback) { - callback = createCallback(callback, thisArg); - while (++index < length) { - var current = callback(array[index], index, array); - if (current < computed) { - computed = current; - result = array[index]; - } - } - } else { - while (++index < length) { - if (array[index] < result) { - result = array[index]; - } - } - } - return result; - } - /** * Creates an object composed from arrays of `keys` and `values`. Pass either * a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]`, or @@ -2857,33 +2883,6 @@ : []; } - /** - * Creates an array of shuffled `array` values, using a version of the - * Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. - * - * @static - * @memberOf _ - * @category Arrays - * @param {Array} array The array to shuffle. - * @returns {Array} Returns a new shuffled array. - * @example - * - * _.shuffle([1, 2, 3, 4, 5, 6]); - * // => [4, 1, 6, 3, 5, 2] - */ - function shuffle(array) { - var index = -1, - length = array ? array.length : 0, - result = Array(length); - - while (++index < length) { - var rand = floor(nativeRandom() * (index + 1)); - result[index] = result[rand]; - result[rand] = array[index]; - } - return result; - } - /** * Uses a binary search to determine the smallest index at which the `value` * should be inserted into `array` in order to maintain the sort order of the diff --git a/test/test-build.js b/test/test-build.js index 67148f92f..5bddf0969 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -69,12 +69,9 @@ 'intersection', 'last', 'lastIndexOf', - 'max', - 'min', 'object', 'range', 'rest', - 'shuffle', 'sortedIndex', 'tail', 'take', @@ -113,11 +110,14 @@ 'inject', 'invoke', 'map', + 'max', + 'min', 'pluck', 'reduce', 'reduceRight', 'reject', 'select', + 'shuffle', 'size', 'some', 'sortBy', diff --git a/test/test.js b/test/test.js index 2d11a12a1..7a4da8255 100644 --- a/test/test.js +++ b/test/test.js @@ -1733,6 +1733,7 @@ 'map', 'pluck', 'reject', + 'shuffle', 'sortBy', 'toArray', 'where'