From 3994c7fc2ba1d6b88426a2ae81dbe0b7f967b3e5 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 22 May 2012 13:01:14 -0400 Subject: [PATCH 01/24] Update docdown submodule and README.md. Former-commit-id: 745eccb03a7eed1f3f2ef696002c57d6fcc44d25 --- README.md | 14 +-- doc/README.md | 270 -------------------------------------------- vendor/benchmark.js | 2 +- vendor/docdown | 2 +- 4 files changed, 9 insertions(+), 279 deletions(-) diff --git a/README.md b/README.md index 0a439a9694..0cfa14612a 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,9 @@ For more information check out these screencasts over Lo-Dash: * [_.bind](http://lodash.com/docs#_bindfunc--arg1-arg2-) supports *"lazy"* binding * [_.debounce](http://lodash.com/docs#_debouncefunc-wait-immediate)’ed functions match [_.throttle](http://lodash.com/docs#_throttlefunc-wait)’ed functions’ return value behavior * [_.forEach](http://lodash.com/docs#_foreachcollection-callback--thisarg) is chainable - * [_.groupBy](http://lodash.com/docs#_groupbycollection-callback--thisarg) accepts a third `thisArg` argument + * [_.groupBy](http://lodash.com/docs#_groupbycollection-callback--thisarg) accepts a third, `thisArg`, argument * [_.partial](http://lodash.com/docs#_partialfunc--arg1-arg2-) for more functional fun - * [_.size](http://lodash.com/docs#_sizecollection) returns the `length` of string values + * [_.size](http://lodash.com/docs#_sizecollection) supports returning the `length` of string values ## Support @@ -121,9 +121,9 @@ git submodule update --init * Methods should work on pages with incorrectly shimmed native methods [#7](https://github.com/documentcloud/underscore/issues/7) * Register as AMD module, but still export to global [#431](https://github.com/documentcloud/underscore/pull/431) * `_.forEach` should be chainable [#142](https://github.com/documentcloud/underscore/issues/142) - * `_isNaN(new Number(NaN))` should return `true` - * `_.reduceRight` should pass correct callback arguments when iterating objects - * `_.size` should return the `length` of string values + * `_isNaN(new Number(NaN))` should return `true` [[test](https://github.com/bestiejs/lodash/blob/5bcd444084c92b1753feeaf66c20323e57a2dac3/test/test.js#L95-99)] + * `_.reduceRight` should pass correct callback arguments when iterating objects [[test](https://github.com/bestiejs/lodash/blob/5bcd444084c92b1753feeaf66c20323e57a2dac3/test/test.js#L106-116)] + * `_.size` should return the `length` of string values [[test](https://github.com/bestiejs/lodash/blob/5bcd444084c92b1753feeaf66c20323e57a2dac3/test/test.js#L121-127)] ## Optimized methods (50+) @@ -189,9 +189,9 @@ git submodule update --init * Added *"lazy bind"* support to `_.bind` * Added native method overwrite detection to avoid bad native shims * Added support for more AMD build optimizers and aliasing as the *"underscore"* module - * Added `thisArg` to `_.groupBy` + * Added `thisArg` argument to `_.groupBy` * Added whitespace to compiled strings - * Added `_.partial` + * Added `_.partial` method * Commented the `iterationFactory` options object * Ensured `_.max` and `_.min` support extremely large arrays * Fixed IE < 9 `[DontEnum]` bug and Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1’s prototype property iteration bug diff --git a/doc/README.md b/doc/README.md index b1f21d887d..d488f95bdc 100644 --- a/doc/README.md +++ b/doc/README.md @@ -124,9 +124,6 @@ ## `_` - - - ### `_(value)` @@ -145,9 +142,6 @@ The `lodash` function. - - - ### `_.VERSION` @@ -160,9 +154,6 @@ The `lodash` function. - - - ### `_.after(n, func)` @@ -191,9 +182,6 @@ _.forEach(notes, function(note) { - - - ### `_.bind(func [, arg1, arg2, ...])` @@ -241,9 +229,6 @@ func(); - - - ### `_.bindAll(object [, methodName1, methodName2, ...])` @@ -276,9 +261,6 @@ jQuery('#lodash_button').on('click', buttonView.onClick); - - - ### `_.chain(value)` @@ -313,9 +295,6 @@ var youngest = _.chain(stooges) - - - ### `_.clone(value)` @@ -340,9 +319,6 @@ _.clone({ 'name': 'moe' }); - - - ### `_.compact(array)` @@ -367,9 +343,6 @@ _.compact([0, 1, false, 2, '', 3]); - - - ### `_.compose([func1, func2, ...])` @@ -397,9 +370,6 @@ welcome('moe'); - - - ### `_.contains(collection, target)` @@ -425,9 +395,6 @@ _.contains([1, 2, 3], 3); - - - ### `_.debounce(func, wait, immediate)` @@ -454,9 +421,6 @@ jQuery(window).on('resize', lazyLayout); - - - ### `_.defaults(object [, defaults1, defaults2, ...])` @@ -483,9 +447,6 @@ _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'lots' }); - - - ### `_.defer(func [, arg1, arg2, ...])` @@ -511,9 +472,6 @@ _.defer(function() { alert('deferred'); }); - - - ### `_.delay(func, wait [, arg1, arg2, ...])` @@ -541,9 +499,6 @@ _.delay(log, 1000, 'logged later'); - - - ### `_.difference(array [, array1, array2, ...])` @@ -569,9 +524,6 @@ _.difference([1, 2, 3, 4, 5], [5, 2, 10]); - - - ### `_.escape(string)` @@ -596,9 +548,6 @@ _.escape('Curly, Larry & Moe'); - - - ### `_.every(collection, callback [, thisArg])` @@ -625,9 +574,6 @@ _.every([true, 1, null, 'yes'], Boolean); - - - ### `_.extend(object [, source1, source2, ...])` @@ -653,9 +599,6 @@ _.extend({ 'name': 'moe' }, { 'age': 40 }); - - - ### `_.filter(collection, callback [, thisArg])` @@ -682,9 +625,6 @@ var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }) - - - ### `_.find(collection, callback [, thisArg])` @@ -711,9 +651,6 @@ var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); - - - ### `_.first(array [, n, guard])` @@ -740,9 +677,6 @@ _.first([5, 4, 3, 2, 1]); - - - ### `_.flatten(array, shallow)` @@ -771,9 +705,6 @@ _.flatten([1, [2], [3, [[4]]]], true); - - - ### `_.forEach(collection, callback [, thisArg])` @@ -803,9 +734,6 @@ _([1, 2, 3]).forEach(function(num) { alert(num); }).join(','); - - - ### `_.functions(object)` @@ -830,9 +758,6 @@ _.functions(_); - - - ### `_.groupBy(collection, callback [, thisArg])` @@ -865,9 +790,6 @@ _.groupBy(['one', 'two', 'three'], 'length'); - - - ### `_.has(object, property)` @@ -893,9 +815,6 @@ _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b'); - - - ### `_.identity(value)` @@ -921,9 +840,6 @@ moe === _.identity(moe); - - - ### `_.indexOf(array, value [, isSorted=false])` @@ -950,9 +866,6 @@ _.indexOf([1, 2, 3], 2); - - - ### `_.initial(array [, n, guard])` @@ -979,9 +892,6 @@ _.initial([5, 4, 3, 2, 1]); - - - ### `_.intersection([array1, array2, ...])` @@ -1006,9 +916,6 @@ _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); - - - ### `_.invoke(array, methodName [, arg1, arg2, ...])` @@ -1035,9 +942,6 @@ _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); - - - ### `_.isArguments(value)` @@ -1065,9 +969,6 @@ _.isArguments([1, 2, 3]); - - - ### `_.isArray(value)` @@ -1095,9 +996,6 @@ _.isArray([1, 2, 3]); - - - ### `_.isBoolean(value)` @@ -1122,9 +1020,6 @@ _.isBoolean(null); - - - ### `_.isDate(value)` @@ -1149,9 +1044,6 @@ _.isDate(new Date); - - - ### `_.isElement(value)` @@ -1176,9 +1068,6 @@ _.isElement(document.body); - - - ### `_.isEmpty(value)` @@ -1206,9 +1095,6 @@ _.isEmpty({}); - - - ### `_.isEqual(a, b [, stack])` @@ -1241,9 +1127,6 @@ _.isEqual(moe, clone); - - - ### `_.isFinite(value)` @@ -1274,9 +1157,6 @@ _.isFinite(Infinity); - - - ### `_.isFunction(value)` @@ -1301,9 +1181,6 @@ _.isFunction(''.concat); - - - ### `_.isNaN(value)` @@ -1337,9 +1214,6 @@ _.isNaN(undefined); - - - ### `_.isNull(value)` @@ -1367,9 +1241,6 @@ _.isNull(undefined); - - - ### `_.isNumber(value)` @@ -1394,9 +1265,6 @@ _.isNumber(8.4 * 5; - - - ### `_.isObject(value)` @@ -1424,9 +1292,6 @@ _.isObject(1); - - - ### `_.isRegExp(value)` @@ -1451,9 +1316,6 @@ _.isRegExp(/moe/); - - - ### `_.isString(value)` @@ -1478,9 +1340,6 @@ _.isString('moe'); - - - ### `_.isUndefined(value)` @@ -1505,9 +1364,6 @@ _.isUndefined(void 0); - - - ### `_.keys(object)` @@ -1532,9 +1388,6 @@ _.keys({ 'one': 1, 'two': 2, 'three': 3 }); - - - ### `_.last(array [, n, guard])` @@ -1561,9 +1414,6 @@ _.last([5, 4, 3, 2, 1]); - - - ### `_.lastIndexOf(array, value)` @@ -1589,9 +1439,6 @@ _.lastIndexOf([1, 2, 3, 1, 2, 3], 2); - - - ### `_.map(collection, callback [, thisArg])` @@ -1621,9 +1468,6 @@ _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; }); - - - ### `_.max(array [, callback, thisArg])` @@ -1656,9 +1500,6 @@ _.max(stooges, function(stooge) { return stooge.age; }); - - - ### `_.memoize(func [, resolver])` @@ -1685,9 +1526,6 @@ var fibonacci = _.memoize(function(n) { - - - ### `_.min(array [, callback, thisArg])` @@ -1714,9 +1552,6 @@ _.min([10, 5, 100, 2, 1000]); - - - ### `_.mixin(object)` @@ -1747,9 +1582,6 @@ _('larry').capitalize(); - - - ### `_.noConflict()` @@ -1770,9 +1602,6 @@ var lodash = _.noConflict(); - - - ### `_.once(func)` @@ -1799,9 +1628,6 @@ initialize(); - - - ### `_.partial(func [, arg1, arg2, ...])` @@ -1829,9 +1655,6 @@ hi('moe'); - - - ### `_.pick(object [, prop1, prop2, ...])` @@ -1857,9 +1680,6 @@ _.pick({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'name', 'age'); - - - ### `_.pluck(collection, property)` @@ -1891,9 +1711,6 @@ _.pluck(stooges, 'name'); - - - ### `_.range([start=0], end [, step=1])` @@ -1932,9 +1749,6 @@ _.range(0); - - - ### `_.reduce(collection, callback [, accumulator, thisArg])` @@ -1962,9 +1776,6 @@ var sum = _.reduce([1, 2, 3], function(memo, num) { return memo + num; }); - - - ### `_.reduceRight(collection, callback [, accumulator, thisArg])` @@ -1993,9 +1804,6 @@ var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []); - - - ### `_.reject(collection, callback [, thisArg])` @@ -2022,9 +1830,6 @@ var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); - - - ### `_.rest(array [, n, guard])` @@ -2051,9 +1856,6 @@ _.rest([5, 4, 3, 2, 1]); - - - ### `_.result(object, property)` @@ -2089,9 +1891,6 @@ _.result(object, 'stuff'); - - - ### `_.shuffle(array)` @@ -2116,9 +1915,6 @@ _.shuffle([1, 2, 3, 4, 5, 6]); - - - ### `_.size(collection)` @@ -2149,9 +1945,6 @@ _.size('curly'); - - - ### `_.some(collection, callback [, thisArg])` @@ -2178,9 +1971,6 @@ _.some([null, 0, 'yes', false]); - - - ### `_.sortBy(collection, callback [, thisArg])` @@ -2210,9 +2000,6 @@ _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return this.sin(num); }, Math); - - - ### `_.sortedIndex(array, value [, callback])` @@ -2239,9 +2026,6 @@ _.sortedIndex([10, 20, 30, 40, 50], 35); - - - ### `_.tap(value, interceptor)` @@ -2272,9 +2056,6 @@ _.chain([1,2,3,200]) - - - ### `_.template(text, data, options)` @@ -2335,9 +2116,6 @@ _.template('<%= data.hasWith %>', { 'hasWith': 'no' }, { 'variable': 'data' }); - - - ### `_.throttle(func, wait)` @@ -2363,9 +2141,6 @@ jQuery(window).on('scroll', throttled); - - - ### `_.times(n, callback [, thisArg])` @@ -2388,9 +2163,6 @@ _.times(3, function() { genie.grantWish(); }); - - - ### `_.toArray(collection)` @@ -2415,9 +2187,6 @@ Converts the `collection`, into an array. Useful for converting the `arguments` - - - ### `_.union([array1, array2, ...])` @@ -2442,9 +2211,6 @@ _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]); - - - ### `_.uniq(array [, isSorted=false, callback])` @@ -2471,9 +2237,6 @@ _.uniq([1, 2, 1, 3, 1, 4]); - - - ### `_.uniqueId([prefix])` @@ -2498,9 +2261,6 @@ _.uniqueId('contact_'); - - - ### `_.values(collection)` @@ -2525,9 +2285,6 @@ _.values({ 'one': 1, 'two': 2, 'three': 3 }); - - - ### `_.without(array [, value1, value2, ...])` @@ -2553,9 +2310,6 @@ _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); - - - ### `_.wrap(func, wrapper [, arg1, arg2, ...])` @@ -2586,9 +2340,6 @@ hello(); - - - ### `_.zip([array1, array2, ...])` @@ -2620,9 +2371,6 @@ _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]); ## `_.prototype` - - - ### `_.prototype.chain()` @@ -2644,9 +2392,6 @@ _([1, 2, 3]).value(); - - - ### `_.prototype.value()` @@ -2675,9 +2420,6 @@ _([1, 2, 3]).value(); ## `_.templateSettings` - - - ### `_.templateSettings` @@ -2690,9 +2432,6 @@ _([1, 2, 3]).value(); - - - ### `_.templateSettings.escape` @@ -2705,9 +2444,6 @@ _([1, 2, 3]).value(); - - - ### `_.templateSettings.evaluate` @@ -2720,9 +2456,6 @@ _([1, 2, 3]).value(); - - - ### `_.templateSettings.interpolate` @@ -2735,9 +2468,6 @@ _([1, 2, 3]).value(); - - - ### `_.templateSettings.variable` diff --git a/vendor/benchmark.js b/vendor/benchmark.js index 5ff44811ef..f0306c0345 160000 --- a/vendor/benchmark.js +++ b/vendor/benchmark.js @@ -1 +1 @@ -Subproject commit 5ff44811efe0834a08ae2db77eb55d7efc89d145 +Subproject commit f0306c03451a84b85b313410f3881b36bc935f39 diff --git a/vendor/docdown b/vendor/docdown index 5448a5837d..87466c279a 160000 --- a/vendor/docdown +++ b/vendor/docdown @@ -1 +1 @@ -Subproject commit 5448a5837d261262091c908553354ffac78f3fc8 +Subproject commit 87466c279a5424127566555a544fc9eaf5dc8670 From 5c48abff4b2b7f6a8af3528688d5c801b10a2250 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 22 May 2012 13:10:43 -0400 Subject: [PATCH 02/24] Add links to relevant sections in the description. Former-commit-id: da9e65fadae73792cfb2ec885318bc073d4468c3 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0cfa14612a..33c56e98ac 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Lo-Dash v0.2.0 -A drop-in replacement for Underscore.js, from the devs behind [jsPerf.com](http://jsperf.com), that delivers [performance improvements](http://jsperf.com/lodash-underscore#filterby=family), bug fixes, and additional features. +A drop-in replacement for Underscore.js, from the devs behind [jsPerf.com](http://jsperf.com), that delivers [performance improvements](http://jsperf.com/lodash-underscore#filterby=family), [bug fixes](https://github.com/bestiejs/lodash#closed-underscorejs-issues), and [additional features](https://github.com/bestiejs/lodash#features). Lo-Dash’s performance is gained by avoiding slower native methods, instead opting for simplified non-ES5 compliant methods optimized for common usage, and by leveraging function compilation to reduce the number of overall function calls. From b38947146e145a38ed588cdf81a86a44dad2ab7f Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 23 May 2012 00:43:23 -0400 Subject: [PATCH 03/24] Move `_.groupBy` and `_.size` to the "Objects" category and shuffle method order to avoid extra private variables. Former-commit-id: 8dc89136a3bf2ed94f594f5c1c1f97a0dd7291c4 --- lodash.js | 505 +++++++++++++++++++++++++++--------------------------- 1 file changed, 249 insertions(+), 256 deletions(-) diff --git a/lodash.js b/lodash.js index ca8747d4ce..a1e8c82e03 100644 --- a/lodash.js +++ b/lodash.js @@ -70,15 +70,6 @@ /** Used to restore the original `_` reference in `noConflict` */ var oldDash = window._; - /** Used to match the "escape" template delimiters */ - var reEscapeDelimiter = /<%-([\s\S]+?)%>/g; - - /** Used to match the "evaluate" template delimiters */ - var reEvaluateDelimiter = /<%([\s\S]+?)%>/g; - - /** Used to match the "interpolate" template delimiters */ - var reInterpolateDelimiter = /<%=([\s\S]+?)%>/g; - /** Used to detect if a method is native */ var reNative = RegExp('^' + ({}.valueOf + '') .replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&') @@ -137,6 +128,83 @@ /*--------------------------------------------------------------------------*/ + /** + * The `lodash` function. + * + * @name _ + * @constructor + * @param {Mixed} value The value to wrap in a `LoDash` instance. + * @returns {Object} Returns a `LoDash` instance. + */ + function lodash(value) { + // allow invoking `lodash` without the `new` operator + return new LoDash(value); + } + + /** + * Creates a `LoDash` instance that wraps a value to allow chaining. + * + * @private + * @constructor + * @param {Mixed} value The value to wrap. + */ + function LoDash(value) { + // exit early if already wrapped + if (value && value._wrapped) { + return value; + } + this._wrapped = value; + } + + /** + * By default, Lo-Dash uses ERB-style template delimiters, change the + * following template settings to use alternative delimiters. + * + * @static + * @memberOf _ + * @type Object + */ + lodash.templateSettings = { + + /** + * Used to detect `data` property values to be HTML-escaped. + * + * @static + * @memberOf _.templateSettings + * @type RegExp + */ + 'escape': /<%-([\s\S]+?)%>/g, + + /** + * Used to detect code to be evaluated. + * + * @static + * @memberOf _.templateSettings + * @type RegExp + */ + 'evaluate': /<%([\s\S]+?)%>/g, + + /** + * Used to detect `data` property values to inject. + * + * @static + * @memberOf _.templateSettings + * @type RegExp + */ + 'interpolate': /<%=([\s\S]+?)%>/g, + + /** + * Used to reference the data object in the template text. + * + * @static + * @memberOf _.templateSettings + * @type String + */ + 'variable': 'object' + }; + + /*--------------------------------------------------------------------------*/ + /** * The template used to create iterator functions. * @@ -213,11 +281,7 @@ '<%= bottom %>;\n' + // finally, return the `result` 'return result' - - , null, { - 'evaluate': reEvaluateDelimiter, - 'interpolate': reInterpolateDelimiter - }); + ); /** * Reusable iterator options shared by @@ -278,86 +342,6 @@ /*--------------------------------------------------------------------------*/ - /** - * The `lodash` function. - * - * @name _ - * @constructor - * @param {Mixed} value The value to wrap in a `LoDash` instance. - * @returns {Object} Returns a `LoDash` instance. - */ - function lodash(value) { - // allow invoking `lodash` without the `new` operator - return new LoDash(value); - } - - /** - * Creates a `LoDash` instance that wraps a value to allow chaining. - * - * @private - * @constructor - * @param {Mixed} value The value to wrap. - */ - function LoDash(value) { - // exit early if already wrapped - if (value && value._wrapped) { - return value; - } - this._wrapped = value; - } - - /*--------------------------------------------------------------------------*/ - - /** - * Checks if a `value` is an array. - * - * @static - * @memberOf _ - * @category Objects - * @param {Mixed} value The value to check. - * @returns {Boolean} Returns `true` if the `value` is an array, else `false`. - * @example - * - * (function() { return _.isArray(arguments); })(); - * // => false - * - * _.isArray([1, 2, 3]); - * // => true - */ - var isArray = nativeIsArray || function(value) { - return toString.call(value) == arrayClass; - }; - - /** - * Checks if a `value` is empty. Arrays or strings with a length of `0` and - * objects with no enumerable own properties are considered "empty". - * - * @static - * @memberOf _ - * @category Objects - * @param {Mixed} value The value to check. - * @returns {Boolean} Returns `true` if the `value` is empty, else `false`. - * @example - * - * _.isEmpty([1, 2, 3]); - * // => false - * - * _.isEmpty({}); - * // => true - */ - var isEmpty = createIterator({ - 'args': 'value', - 'init': 'true', - 'top': - 'var className = toString.call(value);\n' + - 'if (className == arrayClass || className == stringClass) return !value.length', - 'inLoop': { - 'object': 'return false' - } - }); - - /*--------------------------------------------------------------------------*/ - /** * Creates compiled iteration functions. The iteration function will be created * to iterate over only objects if the first argument of `options.args` is @@ -645,43 +629,6 @@ 'top': 'if (thisArg) callback = bind(callback, thisArg)' }); - /** - * Splits a `collection` into sets, grouped by the result of running each value - * through `callback`. The `callback` is invoked with 3 arguments; for arrays - * they are (value, index, array) and for objects they are (value, key, object). - * The `callback` argument may also be the name of a property to group by. - * - * @static - * @memberOf _ - * @category Collections - * @param {Array|Object} collection The collection to iterate over. - * @param {Function|String} callback The function called per iteration or - * property name to group by. - * @param {Mixed} [thisArg] The `this` binding for the callback. - * @returns {Object} Returns an object of grouped values. - * @example - * - * _.groupBy([1.3, 2.1, 2.4], function(num) { return Math.floor(num); }); - * // => { '1': [1.3], '2': [2.1, 2.4] } - * - * _.groupBy([1.3, 2.1, 2.4], function(num) { return this.floor(num); }, Math); - * // => { '1': [1.3], '2': [2.1, 2.4] } - * - * _.groupBy(['one', 'two', 'three'], 'length'); - * // => { '3': ['one', 'two'], '5': ['three'] } - */ - var groupBy = createIterator(baseIteratorOptions, { - 'init': '{}', - 'top': - 'var prop, isFunc = toString.call(callback) == funcClass;\n' + - 'if (isFunc && thisArg) callback = bind(callback, thisArg)', - 'inLoop': - 'prop = isFunc\n' + - ' ? callback(collection[index], index, collection)\n' + - ' : collection[index][callback];\n' + - '(result[prop] || (result[prop] = [])).push(collection[index])' - }); - /** * Produces a new array of values by mapping each value in the `collection` * through a transformation `callback`. The `callback` is bound to the `thisArg` @@ -853,81 +800,6 @@ 'inLoop': '!' + filterIteratorOptions.inLoop }); - /** - * Gets the number of values in the `collection` or the `length` of a string value. - * - * @static - * @memberOf _ - * @category Collections - * @param {Array|Object} collection The collection inspect. - * @returns {Number} Returns the number of values in the collection. - * @example - * - * _.size([1, 2]); - * // => 2 - * - * _.size({ 'one': 1, 'two': 2, 'three': 3 }); - * // => 3 - * - * _.size('curly'); - * // => 5 - */ - function size(collection) { - var className = toString.call(collection); - return className == arrayClass || className == stringClass - ? collection.length - : keys(collection).length; - } - - /** - * Produces a new sorted array, ranked in ascending order by the results of - * running each value of a `collection` through `callback`. The `callback` is - * invoked with 3 arguments; for arrays they are (value, index, array) and for - * objects they are (value, key, object). The `callback` argument may also be - * the name of a property to sort by (e.g. 'length'). - * - * @static - * @memberOf _ - * @category Collections - * @param {Array|Object} collection The collection to iterate over. - * @param {Function|String} callback The function called per iteration or - * property name to sort by. - * @param {Mixed} [thisArg] The `this` binding for the callback. - * @returns {Array} Returns a new array of sorted values. - * @example - * - * _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return Math.sin(num); }); - * // => [5, 4, 6, 3, 1, 2] - * - * _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return this.sin(num); }, Math); - * // => [5, 4, 6, 3, 1, 2] - */ - function sortBy(collection, callback, thisArg) { - if (toString.call(callback) != funcClass) { - var prop = callback; - callback = function(collection) { return collection[prop]; }; - } else if (thisArg) { - callback = bind(callback, thisArg); - } - return pluck(map(collection, function(value, index) { - return { - 'criteria': callback(value, index, collection), - 'value': value - }; - }).sort(function(left, right) { - var a = left.criteria, - b = right.criteria; - - if (a === undefined) { - return 1; - } - if (b === undefined) { - return -1; - } - return a < b ? -1 : a > b ? 1 : 0; - }), 'value'); - } - /** * Checks if the `callback` returns a truthy value for **any** element of a * `collection`. The function returns as soon as it finds passing value, and @@ -1123,6 +995,99 @@ return result; } + /** + * Splits a `collection` into sets, grouped by the result of running each value + * through `callback`. The `callback` is invoked with 3 arguments; + * (value, index, array). The `callback` argument may also be the name of a + * property to group by. + * + * @static + * @memberOf _ + * @category Arrays + * @param {Array} array The array to iterate over. + * @param {Function|String} callback The function called per iteration or + * property name to group by. + * @param {Mixed} [thisArg] The `this` binding for the callback. + * @returns {Object} Returns an object of grouped values. + * @example + * + * _.groupBy([1.3, 2.1, 2.4], function(num) { return Math.floor(num); }); + * // => { '1': [1.3], '2': [2.1, 2.4] } + * + * _.groupBy([1.3, 2.1, 2.4], function(num) { return this.floor(num); }, Math); + * // => { '1': [1.3], '2': [2.1, 2.4] } + * + * _.groupBy(['one', 'two', 'three'], 'length'); + * // => { '3': ['one', 'two'], '5': ['three'] } + */ + function groupBy(array, callback, thisArg) { + var prop, + value, + index = -1, + isFunc = toString.call(callback) == funcClass, + length = array.length, + result = {}; + + if (isFunc && thisArg) { + callback = bind(callback, thisArg); + } + while (++index < length) { + value = array[index]; + prop = isFunc ? callback(value, index, array) : value[callback]; + (hasOwnProperty.call(result, prop) ? result[prop] : result[prop] = []).push(value); + } + return result + } + + /** + * Produces a new sorted array, ranked in ascending order by the results of + * running each value of a `collection` through `callback`. The `callback` is + * invoked with 3 arguments; for arrays they are (value, index, array) and for + * objects they are (value, key, object). The `callback` argument may also be + * the name of a property to sort by (e.g. 'length'). + * + * @static + * @memberOf _ + * @category Arrays + * @param {Array} array The array to iterate over. + * @param {Function|String} callback The function called per iteration or + * property name to sort by. + * @param {Mixed} [thisArg] The `this` binding for the callback. + * @returns {Array} Returns a new array of sorted values. + * @example + * + * _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return Math.sin(num); }); + * // => [5, 4, 6, 3, 1, 2] + * + * _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return this.sin(num); }, Math); + * // => [5, 4, 6, 3, 1, 2] + */ + function sortBy(array, callback, thisArg) { + if (toString.call(callback) != funcClass) { + var prop = callback; + callback = function(array) { return array[prop]; }; + } else if (thisArg) { + callback = bind(callback, thisArg); + } + return pluck(map(array, function(value, index) { + return { + 'criteria': callback(value, index, array), + 'value': value + }; + }).sort(function(left, right) { + var a = left.criteria, + b = right.criteria; + + if (a === undefined) { + return 1; + } + if (b === undefined) { + return -1; + } + return a < b ? -1 : a > b ? 1 : 0; + }), 'value'); + } + /** * Gets the index at which the first occurrence of `value` is found using * strict equality for comparisons, i.e. `===`. If the `array` is already @@ -2229,6 +2194,26 @@ }; } + /** + * Checks if a `value` is an array. + * + * @static + * @memberOf _ + * @category Objects + * @param {Mixed} value The value to check. + * @returns {Boolean} Returns `true` if the `value` is an array, else `false`. + * @example + * + * (function() { return _.isArray(arguments); })(); + * // => false + * + * _.isArray([1, 2, 3]); + * // => true + */ + var isArray = nativeIsArray || function(value) { + return toString.call(value) == arrayClass; + }; + /** * Checks if a `value` is a boolean (`true` or `false`) value. * @@ -2280,6 +2265,34 @@ return !!(value && value.nodeType == 1); } + /** + * Checks if a `value` is empty. Arrays or strings with a length of `0` and + * objects with no enumerable own properties are considered "empty". + * + * @static + * @memberOf _ + * @category Objects + * @param {Array|Object|String} value The value to inspect. + * @returns {Boolean} Returns `true` if the `value` is empty, else `false`. + * @example + * + * _.isEmpty([1, 2, 3]); + * // => false + * + * _.isEmpty({}); + * // => true + */ + var isEmpty = createIterator({ + 'args': 'value', + 'init': 'true', + 'top': + 'var className = toString.call(value);\n' + + 'if (className == arrayClass || className == stringClass) return !value.length', + 'inLoop': { + 'object': 'return false' + } + }); + /** * Performs a deep comparison between two values to determine if they are * equivalent to each other. @@ -2669,6 +2682,33 @@ return result; } + /** + * Gets the `length` of `value` when `value` is an array or string, or gets + * the number of enumerable own properties when `value` is an object. + * + * @static + * @memberOf _ + * @category Objects + * @param {Array|Object|String} value The value to inspect. + * @returns {Number} Returns the value's size. + * @example + * + * _.size([1, 2]); + * // => 2 + * + * _.size({ 'one': 1, 'two': 2, 'three': 3 }); + * // => 3 + * + * _.size('curly'); + * // => 5 + */ + function size(value) { + var className = toString.call(value); + return className == arrayClass || className == stringClass + ? value.length + : keys(value).length; + } + /** * Invokes `interceptor` with the `value` as the first argument, and then returns * `value`. The primary purpose of this method is to "tap into" a method chain, @@ -2724,7 +2764,7 @@ } /** - * This function simply returns the first argument passed to it. + * This function returns the first argument passed to it. * Note: It is used throughout Lo-Dash as a default callback. * * @static @@ -2889,7 +2929,7 @@ options || (options = {}); var result, - defaults = lodash.templateSettings || {}, + defaults = lodash.templateSettings, escapeDelimiter = options.escape, evaluateDelimiter = options.evaluate, interpolateDelimiter = options.interpolate, @@ -2926,7 +2966,7 @@ // if `options.variable` is not specified, add `data` to the top of the scope chain if (!variable) { - variable = defaults.variable || 'object'; + variable = defaults.variable; text = 'with (' + variable + ' || {}) {\n' + text + '\n}\n'; } @@ -3065,53 +3105,6 @@ */ lodash.VERSION = '0.2.0'; - /** - * By default, Lo-Dash uses ERB-style template delimiters, change the - * following template settings to use alternative delimiters. - * - * @static - * @memberOf _ - * @type Object - */ - lodash.templateSettings = { - - /** - * Used to detect `data` property values to be HTML-escaped. - * - * @static - * @memberOf _.templateSettings - * @type RegExp - */ - 'escape': reEscapeDelimiter, - - /** - * Used to detect code to be evaluated. - * - * @static - * @memberOf _.templateSettings - * @type RegExp - */ - 'evaluate': reEvaluateDelimiter, - - /** - * Used to detect `data` property values to inject. - * - * @static - * @memberOf _.templateSettings - * @type RegExp - */ - 'interpolate': reInterpolateDelimiter, - - /** - * Used to reference the data object in the template text. - * - * @static - * @memberOf _.templateSettings - * @type String - */ - 'variable': 'object' - }; - // assign static methods lodash.after = after; lodash.bind = bind; From f8af24b3839c09e46ff99904b62d3a27d5a68a77 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 23 May 2012 00:52:39 -0400 Subject: [PATCH 04/24] Remove unneeded variable references. Former-commit-id: ab2f5f4408db25dc63b5d85b4adf156f9f978711 --- build/pre-compile.js | 10 ---------- lodash.js | 14 +++++++------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/build/pre-compile.js b/build/pre-compile.js index fad4c8a3b6..1a440c5531 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -8,33 +8,24 @@ /** Used to minify variables embedded in compiled strings */ var compiledVars = [ 'accumulator', - 'args', - 'array', 'arrayClass', 'bind', 'callback', 'className', 'collection', - 'concat', 'ctor', 'false', 'funcClass', 'hasOwnProperty', 'identity', 'index', - 'indexOf', - 'isArray', - 'isEmpty', - 'isFunc', 'length', 'object', 'objectTypes', 'noaccum', - 'prop', 'property', 'result', 'skipProto', - 'slice', 'source', 'sourceIndex', 'stringClass', @@ -177,7 +168,6 @@ 'throttle', 'times', 'toArray', - 'toArray', 'union', 'uniq', 'unique', diff --git a/lodash.js b/lodash.js index a1e8c82e03..ffe815dd75 100644 --- a/lodash.js +++ b/lodash.js @@ -391,8 +391,8 @@ 'exit': '', 'init': '', 'top': '', - 'arrayBranch': { 'loopExp': '++index < length' }, - 'objectBranch': {} + 'arrayBranch': { 'beforeLoop': '', 'loopExp': '++index < length' }, + 'objectBranch': { 'beforeLoop': '' } }; while (++index < length) { @@ -427,7 +427,7 @@ data.useHas = data.useHas !== false; if (!data.exit) { - data.exit = 'if (' + firstArg + ' == null) return result'; + data.exit = 'if (!' + firstArg + ') return result'; } if (firstArg == 'object' || !arrayBranch.inLoop) { data.arrayBranch = null; @@ -437,14 +437,14 @@ } // create the function factory var factory = Function( - 'arrayClass, bind, concat, funcClass, hasOwnProperty, identity, indexOf, ' + - 'isArray, isEmpty, objectTypes, slice, stringClass, toString, undefined', + 'arrayClass, bind, funcClass, hasOwnProperty, identity, objectTypes, ' + + 'stringClass, toString, undefined', '"use strict"; return function(' + args + ') {\n' + iteratorTemplate(data) + '\n}' ); // return the compiled function return factory( - arrayClass, bind, concat, funcClass, hasOwnProperty, identity, indexOf, - isArray, isEmpty, objectTypes, slice, stringClass, toString + arrayClass, bind, funcClass, hasOwnProperty, identity, objectTypes, + stringClass, toString ); } From 52ae87812e908a1a26930c603be3b6eb11d81aa7 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 23 May 2012 01:01:35 -0400 Subject: [PATCH 05/24] Update docs and minified build. Former-commit-id: f019b90d5e866bf2a6da1c003001d0e9345bb0b1 --- doc/README.md | 208 +++++++++++++++++++++++++------------------------- lodash.min.js | 53 ++++++------- 2 files changed, 131 insertions(+), 130 deletions(-) diff --git a/doc/README.md b/doc/README.md index d488f95bdc..f1cd9810cc 100644 --- a/doc/README.md +++ b/doc/README.md @@ -30,7 +30,7 @@ * [`_.flatten`](#_flattenarray-shallow) * [`_.forEach`](#_foreachcollection-callback--thisarg) * [`_.functions`](#_functionsobject) -* [`_.groupBy`](#_groupbycollection-callback--thisarg) +* [`_.groupBy`](#_groupbyarray-callback--thisarg) * [`_.has`](#_hasobject-property) * [`_.identity`](#_identityvalue) * [`_.indexOf`](#_indexofarray-value--issortedfalse) @@ -73,9 +73,9 @@ * [`_.rest`](#_restarray--n-guard) * [`_.result`](#_resultobject-property) * [`_.shuffle`](#_shufflearray) -* [`_.size`](#_sizecollection) +* [`_.size`](#_sizevalue) * [`_.some`](#_somecollection-callback--thisarg) -* [`_.sortBy`](#_sortbycollection-callback--thisarg) +* [`_.sortBy`](#_sortbyarray-callback--thisarg) * [`_.sortedIndex`](#_sortedindexarray-value--callback) * [`_.tap`](#_tapvalue-interceptor) * [`_.template`](#_templatetext-data-options) @@ -127,7 +127,7 @@ ### `_(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L289 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L139 "View in source") [Ⓣ][1] The `lodash` function. @@ -145,7 +145,7 @@ The `lodash` function. ### `_.VERSION` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3066 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3106 "View in source") [Ⓣ][1] *(String)*: The semantic version number. @@ -157,7 +157,7 @@ The `lodash` function. ### `_.after(n, func)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1680 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1645 "View in source") [Ⓣ][1] Creates a new function that is restricted to executing only after it is called `n` times. @@ -185,7 +185,7 @@ _.forEach(notes, function(note) { ### `_.bind(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1731 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1696 "View in source") [Ⓣ][1] Creates a new function that, when called, invokes `func` with the `this` binding of `thisArg` and prepends any additional `bind` arguments to those passed to the bound function. Lazy defined methods may be bound by passing the object they are bound to as `func` and the method name as `thisArg`. @@ -232,7 +232,7 @@ func(); ### `_.bindAll(object [, methodName1, methodName2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1794 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1759 "View in source") [Ⓣ][1] Binds methods on the `object` to the object, overwriting the non-bound method. If no method names are provided, all the function properties of the `object` will be bound. @@ -264,7 +264,7 @@ jQuery('#lodash_button').on('click', buttonView.onClick); ### `_.chain(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3018 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3058 "View in source") [Ⓣ][1] Wraps the value in a `lodash` chainable object. @@ -298,7 +298,7 @@ var youngest = _.chain(stooges) ### `_.clone(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2120 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2085 "View in source") [Ⓣ][1] Create a shallow clone of the `value`. Any nested objects or arrays will be assigned by reference and not cloned. @@ -322,7 +322,7 @@ _.clone({ 'name': 'moe' }); ### `_.compact(array)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1022 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L894 "View in source") [Ⓣ][1] Produces a new array with all falsey values of `array` removed. The values `false`, `null`, `0`, `""`, `undefined` and `NaN` are all falsey. @@ -346,7 +346,7 @@ _.compact([0, 1, false, 2, '', 3]); ### `_.compose([func1, func2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1826 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1791 "View in source") [Ⓣ][1] Creates a new function that is the composition of the passed functions, where each function consumes the return value of the function that follows. In math terms, composing thefunctions `f()`, `g()`, and `h()` produces `f(g(h()))`. @@ -373,7 +373,7 @@ welcome('moe'); ### `_.contains(collection, target)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L551 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L535 "View in source") [Ⓣ][1] Checks if a given `target` value is present in a `collection` using strict equality for comparisons, i.e. `===`. @@ -398,7 +398,7 @@ _.contains([1, 2, 3], 3); ### `_.debounce(func, wait, immediate)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1859 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1824 "View in source") [Ⓣ][1] Creates a new function that will delay the execution of `func` until after `wait` milliseconds have elapsed since the last time it was invoked. Pass `true` for `immediate` to cause debounce to invoke `func` on the leading, instead of the trailing, edge of the `wait` timeout. Subsequent calls to the debounced function will return the result of the last `func` call. @@ -424,7 +424,7 @@ jQuery(window).on('resize', lazyLayout); ### `_.defaults(object [, defaults1, defaults2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2143 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2108 "View in source") [Ⓣ][1] Assigns missing properties in `object` with default values from the defaults objects. As soon as a property is set, additional defaults of the same property will be ignored. @@ -450,7 +450,7 @@ _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'lots' }); ### `_.defer(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1924 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1889 "View in source") [Ⓣ][1] Defers executing the `func` function until the current call stack has cleared. Additional arguments are passed to `func` when it is invoked. @@ -475,7 +475,7 @@ _.defer(function() { alert('deferred'); }); ### `_.delay(func, wait [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1904 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1869 "View in source") [Ⓣ][1] Executes the `func` function after `wait` milliseconds. Additional arguments are passed to `func` when it is invoked. @@ -502,7 +502,7 @@ _.delay(log, 1000, 'logged later'); ### `_.difference(array [, array1, array2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1051 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L923 "View in source") [Ⓣ][1] Produces a new array of `array` values not present in the other arrays using strict equality for comparisons, i.e. `===`. @@ -527,7 +527,7 @@ _.difference([1, 2, 3, 4, 5], [5, 2, 10]); ### `_.escape(string)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2714 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2754 "View in source") [Ⓣ][1] Escapes a string for insertion into HTML, replacing `&`, `<`, `"`, `'`, and `/` characters. @@ -551,7 +551,7 @@ _.escape('Curly, Larry & Moe'); ### `_.every(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L575 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L559 "View in source") [Ⓣ][1] Checks if the `callback` returns a truthy value for **all** elements of a `collection`. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. @@ -577,7 +577,7 @@ _.every([true, 1, null, 'yes'], Boolean); ### `_.extend(object [, source1, source2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2162 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2127 "View in source") [Ⓣ][1] Copies enumerable properties from the source objects to the `destination` object. Subsequent sources will overwrite propery assignments of previous sources. @@ -602,7 +602,7 @@ _.extend({ 'name': 'moe' }, { 'age': 40 }); ### `_.filter(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L596 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L580 "View in source") [Ⓣ][1] Examines each value in a `collection`, returning an array of all values the `callback` returns truthy for. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. @@ -628,7 +628,7 @@ var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }) ### `_.find(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L618 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L602 "View in source") [Ⓣ][1] Examines each value in a `collection`, returning the first one the `callback` returns truthy for. The function returns as soon as it finds an acceptable value, and does not iterate over the entire `collection`. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. @@ -654,7 +654,7 @@ var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); ### `_.first(array [, n, guard])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1084 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L956 "View in source") [Ⓣ][1] Gets the first value of the `array`. Pass `n` to return the first `n` values of the `array`. @@ -680,7 +680,7 @@ _.first([5, 4, 3, 2, 1]); ### `_.flatten(array, shallow)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1106 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L978 "View in source") [Ⓣ][1] Flattens a nested array *(the nesting can be to any depth)*. If `shallow` is truthy, `array` will only be flattened a single level. @@ -708,7 +708,7 @@ _.flatten([1, [2], [3, [[4]]]], true); ### `_.forEach(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L644 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L628 "View in source") [Ⓣ][1] Iterates over a `collection`, executing the `callback` for each value in the `collection`. The `callback` is bound to the `thisArg` value, if one is passed. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. @@ -737,7 +737,7 @@ _([1, 2, 3]).forEach(function(num) { alert(num); }).join(','); ### `_.functions(object)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2179 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2144 "View in source") [Ⓣ][1] Produces a sorted array of the properties, own and inherited, of `object` that have function values. @@ -760,13 +760,13 @@ _.functions(_); -### `_.groupBy(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L673 "View in source") [Ⓣ][1] +### `_.groupBy(array, callback [, thisArg])` +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1023 "View in source") [Ⓣ][1] -Splits a `collection` into sets, grouped by the result of running each value through `callback`. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. The `callback` argument may also be the name of a property to group by. +Splits a `collection` into sets, grouped by the result of running each value through `callback`. The `callback` is invoked with `3` arguments; *(value, index, array)*. The `callback` argument may also be the name of a property to group by. #### Arguments -1. `collection` *(Array|Object)*: The collection to iterate over. +1. `array` *(Array)*: The array to iterate over. 2. `callback` *(Function|String)*: The function called per iteration or property name to group by. 3. `[thisArg]` *(Mixed)*: The `this` binding for the callback. @@ -793,7 +793,7 @@ _.groupBy(['one', 'two', 'three'], 'length'); ### `_.has(object, property)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2202 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2167 "View in source") [Ⓣ][1] Checks if the specified object `property` exists and is a direct property, instead of an inherited property. @@ -818,9 +818,9 @@ _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b'); ### `_.identity(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2741 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2781 "View in source") [Ⓣ][1] -This function simply returns the first argument passed to it. Note: It is used throughout Lo-Dash as a default callback. +This function returns the first argument passed to it. Note: It is used throughout Lo-Dash as a default callback. #### Arguments 1. `value` *(Mixed)*: Any value. @@ -843,7 +843,7 @@ moe === _.identity(moe); ### `_.indexOf(array, value [, isSorted=false])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1143 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1108 "View in source") [Ⓣ][1] Gets the index at which the first occurrence of `value` is found using strict equality for comparisons, i.e. `===`. If the `array` is already sorted, passing `true` for `isSorted` will run a faster binary search. @@ -869,7 +869,7 @@ _.indexOf([1, 2, 3], 2); ### `_.initial(array [, n, guard])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1177 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1142 "View in source") [Ⓣ][1] Gets all but the last value of the `array`. Pass `n` to exclude the last `n` values from the result. @@ -895,7 +895,7 @@ _.initial([5, 4, 3, 2, 1]); ### `_.intersection([array1, array2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1196 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1161 "View in source") [Ⓣ][1] Computes the intersection of all the passed-in arrays. @@ -919,7 +919,7 @@ _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); ### `_.invoke(array, methodName [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1229 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1194 "View in source") [Ⓣ][1] Calls the method named by `methodName` for each value of the `collection`. Additional arguments will be passed to each invoked method. @@ -945,7 +945,7 @@ _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); ### `_.isArguments(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2222 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2187 "View in source") [Ⓣ][1] Checks if a `value` is an `arguments` object. @@ -972,7 +972,7 @@ _.isArguments([1, 2, 3]); ### `_.isArray(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L327 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2213 "View in source") [Ⓣ][1] Checks if a `value` is an array. @@ -999,7 +999,7 @@ _.isArray([1, 2, 3]); ### `_.isBoolean(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2245 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2230 "View in source") [Ⓣ][1] Checks if a `value` is a boolean *(`true` or `false`)* value. @@ -1023,7 +1023,7 @@ _.isBoolean(null); ### `_.isDate(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2262 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2247 "View in source") [Ⓣ][1] Checks if a `value` is a date. @@ -1047,7 +1047,7 @@ _.isDate(new Date); ### `_.isElement(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2279 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2264 "View in source") [Ⓣ][1] Checks if a `value` is a DOM element. @@ -1071,12 +1071,12 @@ _.isElement(document.body); ### `_.isEmpty(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L348 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2285 "View in source") [Ⓣ][1] Checks if a `value` is empty. Arrays or strings with a length of `0` and objects with no enumerable own properties are considered "empty". #### Arguments -1. `value` *(Mixed)*: The value to check. +1. `value` *(Array|Object|String)*: The value to inspect. #### Returns *(Boolean)*: Returns `true` if the `value` is empty, else `false`. @@ -1098,7 +1098,7 @@ _.isEmpty({}); ### `_.isEqual(a, b [, stack])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2306 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2319 "View in source") [Ⓣ][1] Performs a deep comparison between two values to determine if they are equivalent to each other. @@ -1130,7 +1130,7 @@ _.isEqual(moe, clone); ### `_.isFinite(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2458 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2471 "View in source") [Ⓣ][1] Checks if a `value` is a finite number. @@ -1160,7 +1160,7 @@ _.isFinite(Infinity); ### `_.isFunction(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2475 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2488 "View in source") [Ⓣ][1] Checks if a `value` is a function. @@ -1184,7 +1184,7 @@ _.isFunction(''.concat); ### `_.isNaN(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2526 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2539 "View in source") [Ⓣ][1] Checks if a `value` is `NaN`. Note: this is not the same as native `isNaN`, which will return true for `undefined` and other values. See http://es5.github.com/#x15.1.2.4. @@ -1217,7 +1217,7 @@ _.isNaN(undefined); ### `_.isNull(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2548 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2561 "View in source") [Ⓣ][1] Checks if a `value` is `null`. @@ -1244,7 +1244,7 @@ _.isNull(undefined); ### `_.isNumber(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2565 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2578 "View in source") [Ⓣ][1] Checks if a `value` is a number. @@ -1268,7 +1268,7 @@ _.isNumber(8.4 * 5; ### `_.isObject(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2496 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2509 "View in source") [Ⓣ][1] Checks if a `value` is the language type of Object. *(e.g. arrays, functions, objects, regexps, `new Number(0)*`, and `new String('')`) @@ -1295,7 +1295,7 @@ _.isObject(1); ### `_.isRegExp(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2582 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2595 "View in source") [Ⓣ][1] Checks if a `value` is a regular expression. @@ -1319,7 +1319,7 @@ _.isRegExp(/moe/); ### `_.isString(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2599 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2612 "View in source") [Ⓣ][1] Checks if a `value` is a string. @@ -1343,7 +1343,7 @@ _.isString('moe'); ### `_.isUndefined(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2616 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2629 "View in source") [Ⓣ][1] Checks if a `value` is `undefined`. @@ -1367,7 +1367,7 @@ _.isUndefined(void 0); ### `_.keys(object)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2633 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2646 "View in source") [Ⓣ][1] Produces an array of the `object`'s enumerable own property names. @@ -1391,7 +1391,7 @@ _.keys({ 'one': 1, 'two': 2, 'three': 3 }); ### `_.last(array [, n, guard])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1259 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1224 "View in source") [Ⓣ][1] Gets the last value of the `array`. Pass `n` to return the lasy `n` values of the `array`. @@ -1417,7 +1417,7 @@ _.last([5, 4, 3, 2, 1]); ### `_.lastIndexOf(array, value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1279 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1244 "View in source") [Ⓣ][1] Gets the index at which the last occurrence of `value` is found using strict equality for comparisons, i.e. `===`. @@ -1442,7 +1442,7 @@ _.lastIndexOf([1, 2, 3, 1, 2, 3], 2); ### `_.map(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L707 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L654 "View in source") [Ⓣ][1] Produces a new array of values by mapping each value in the `collection` through a transformation `callback`. The `callback` is bound to the `thisArg` value, if one is passed. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. @@ -1471,7 +1471,7 @@ _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; }); ### `_.max(array [, callback, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1316 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1281 "View in source") [Ⓣ][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 invoked with `3` arguments; *(value, index, array)*. @@ -1503,7 +1503,7 @@ _.max(stooges, function(stooge) { return stooge.age; }); ### `_.memoize(func [, resolver])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1947 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1912 "View in source") [Ⓣ][1] Creates a new function that memoizes the result of `func`. If `resolver` is passed, it will be used to determine the cache key for storing the result based on the arguments passed to the memoized function. By default, the first argument passed to the memoized function is used as the cache key. @@ -1529,7 +1529,7 @@ var fibonacci = _.memoize(function(n) { ### `_.min(array [, callback, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1366 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1331 "View in source") [Ⓣ][1] 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 invoked with `3` arguments; *(value, index, array)*. @@ -1555,7 +1555,7 @@ _.min([10, 5, 100, 2, 1000]); ### `_.mixin(object)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2767 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2807 "View in source") [Ⓣ][1] Adds functions properties of `object` to the `lodash` function and chainable wrapper. @@ -1585,7 +1585,7 @@ _('larry').capitalize(); ### `_.noConflict()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2798 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2838 "View in source") [Ⓣ][1] Reverts the '_' variable to its previous value and returns a reference to the `lodash` function. @@ -1605,7 +1605,7 @@ var lodash = _.noConflict(); ### `_.once(func)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1973 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1938 "View in source") [Ⓣ][1] Creates a new function that is restricted to one execution. Repeat calls to the function will return the value of the first call. @@ -1631,7 +1631,7 @@ initialize(); ### `_.partial(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2006 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1971 "View in source") [Ⓣ][1] Creates a new function that, when called, invokes `func` with any additional `partial` arguments prepended to those passed to the partially applied function. This method is similar `bind`, except it does **not** alter the `this` binding. @@ -1658,7 +1658,7 @@ hi('moe'); ### `_.pick(object [, prop1, prop2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2655 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2668 "View in source") [Ⓣ][1] Creates an object composed of the specified properties. Property names may be specified as individual arguments or as arrays of property names. @@ -1683,7 +1683,7 @@ _.pick({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'name', 'age'); ### `_.pluck(collection, property)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L729 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L676 "View in source") [Ⓣ][1] Retrieves the value of a specified property from all values in a `collection`. @@ -1714,7 +1714,7 @@ _.pluck(stooges, 'name'); ### `_.range([start=0], end [, step=1])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1424 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1389 "View in source") [Ⓣ][1] Creates an array of numbers *(positive and/or negative)* progressing from `start` up to but not including `stop`. This method is a port of Python's `range()` function. See http://docs.python.org/library/functions.html#range. @@ -1752,7 +1752,7 @@ _.range(0); ### `_.reduce(collection, callback [, accumulator, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L759 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L706 "View in source") [Ⓣ][1] Boils down a `collection` to a single value. The initial state of the reduction is `accumulator` and each successive step of it should be returned by the `callback`. The `callback` is bound to the `thisArg` value, if one is passed. The `callback` is invoked with `4` arguments; for arrays they are *(accumulator, value, index, array)* and for objects they are *(accumulator, value, key, object)*. @@ -1779,7 +1779,7 @@ var sum = _.reduce([1, 2, 3], function(memo, num) { return memo + num; }); ### `_.reduceRight(collection, callback [, accumulator, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L799 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L746 "View in source") [Ⓣ][1] The right-associative version of `_.reduce`. The `callback` is bound to the `thisArg` value, if one is passed. The `callback` is invoked with `4` arguments; for arrays they are *(accumulator, value, index, array)* and for objects they are *(accumulator, value, key, object)*. @@ -1807,7 +1807,7 @@ var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []); ### `_.reject(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L852 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L799 "View in source") [Ⓣ][1] The opposite of `_.filter`, this method returns the values of a `collection` that `callback` does **not** return truthy for. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. @@ -1833,7 +1833,7 @@ var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); ### `_.rest(array [, n, guard])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1460 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1425 "View in source") [Ⓣ][1] The opposite of `_.initial`, this method gets all but the first value of the `array`. Pass `n` to exclude the first `n` values from the result. @@ -1859,7 +1859,7 @@ _.rest([5, 4, 3, 2, 1]); ### `_.result(object, property)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2828 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2868 "View in source") [Ⓣ][1] Resolves the value of `property` on `object`. If the property is a function it will be invoked and its result returned, else the property value is returned. @@ -1894,7 +1894,7 @@ _.result(object, 'stuff'); ### `_.shuffle(array)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1478 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1443 "View in source") [Ⓣ][1] Produces a new array of shuffled `array` values, using a version of the Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. @@ -1917,16 +1917,16 @@ _.shuffle([1, 2, 3, 4, 5, 6]); -### `_.size(collection)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L875 "View in source") [Ⓣ][1] +### `_.size(value)` +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2705 "View in source") [Ⓣ][1] -Gets the number of values in the `collection` or the `length` of a string value. +Gets the `length` of `value` when `value` is an array or string, or gets the number of enumerable own properties when `value` is an object. #### Arguments -1. `collection` *(Array|Object)*: The collection inspect. +1. `value` *(Array|Object|String)*: The value to inspect. #### Returns -*(Number)*: Returns the number of values in the collection. +*(Number)*: Returns the value's size. #### Example ~~~ js @@ -1948,7 +1948,7 @@ _.size('curly'); ### `_.some(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L951 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L823 "View in source") [Ⓣ][1] Checks if the `callback` returns a truthy value for **any** element of a `collection`. The function returns as soon as it finds passing value, and does not iterate over the entire `collection`. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. @@ -1973,13 +1973,13 @@ _.some([null, 0, 'yes', false]); -### `_.sortBy(collection, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L905 "View in source") [Ⓣ][1] +### `_.sortBy(array, callback [, thisArg])` +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1065 "View in source") [Ⓣ][1] Produces a new sorted array, ranked in ascending order by the results of running each value of a `collection` through `callback`. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. The `callback` argument may also be the name of a property to sort by *(e.g. 'length')*. #### Arguments -1. `collection` *(Array|Object)*: The collection to iterate over. +1. `array` *(Array)*: The array to iterate over. 2. `callback` *(Function|String)*: The function called per iteration or property name to sort by. 3. `[thisArg]` *(Mixed)*: The `this` binding for the callback. @@ -2003,7 +2003,7 @@ _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return this.sin(num); }, Math); ### `_.sortedIndex(array, value [, callback])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1512 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1477 "View in source") [Ⓣ][1] Uses a binary search to determine the smallest index at which the `value` should be inserted into the `collection` in order to maintain the sort order of the `collection`. If `callback` is passed, it will be executed for each value in the `collection` to compute their sort ranking. The `callback` is invoked with `1` argument; *(value)*. @@ -2029,7 +2029,7 @@ _.sortedIndex([10, 20, 30, 40, 50], 35); ### `_.tap(value, interceptor)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2693 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2733 "View in source") [Ⓣ][1] Invokes `interceptor` with the `value` as the first argument, and then returns `value`. The primary purpose of this method is to "tap into" a method chain, in order to performoperations on intermediate results within the chain. @@ -2059,7 +2059,7 @@ _.chain([1,2,3,200]) ### `_.template(text, data, options)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2888 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2928 "View in source") [Ⓣ][1] A JavaScript micro-templating method, similar to John Resig's implementation. Lo-Dash templating handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code. @@ -2119,7 +2119,7 @@ _.template('<%= data.hasWith %>', { 'hasWith': 'no' }, { 'variable': 'data' }); ### `_.throttle(func, wait)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2042 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2007 "View in source") [Ⓣ][1] Creates a new function that, when executed, will only call the `func` function at most once per every `wait` milliseconds. If the throttled function is invoked more than once, `func` will also be called on the trailing edge of the `wait` timeout. Subsequent calls to the throttled function will return the result of the last `func` call. @@ -2144,7 +2144,7 @@ jQuery(window).on('scroll', throttled); ### `_.times(n, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2965 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3005 "View in source") [Ⓣ][1] Executes the `callback` function `n` times. The `callback` is invoked with `1` argument; *(index)*. @@ -2166,7 +2166,7 @@ _.times(3, function() { genie.grantWish(); }); ### `_.toArray(collection)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L970 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L842 "View in source") [Ⓣ][1] Converts the `collection`, into an array. Useful for converting the `arguments` object. @@ -2190,7 +2190,7 @@ Converts the `collection`, into an array. Useful for converting the `arguments` ### `_.union([array1, array2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1545 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1510 "View in source") [Ⓣ][1] Computes the union of the passed-in arrays. @@ -2214,7 +2214,7 @@ _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]); ### `_.uniq(array [, isSorted=false, callback])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1580 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1545 "View in source") [Ⓣ][1] Produces a duplicate-value-free version of the `array` using strict equality for comparisons, i.e. `===`. If the `array` is already sorted, passing `true` for `isSorted` will run a faster algorithm. If `callback` is passed, each value of `array` is passed through a transformation `callback` before uniqueness is computed. The `callback` is invoked with `3` arguments; *(value, index, array)*. @@ -2240,7 +2240,7 @@ _.uniq([1, 2, 1, 3, 1, 4]); ### `_.uniqueId([prefix])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2988 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3028 "View in source") [Ⓣ][1] Generates a unique id. If `prefix` is passed, the id will be appended to it. @@ -2264,7 +2264,7 @@ _.uniqueId('contact_'); ### `_.values(collection)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L998 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L870 "View in source") [Ⓣ][1] Produces an array of enumerable own property values of the `collection`. @@ -2288,7 +2288,7 @@ _.values({ 'one': 1, 'two': 2, 'three': 3 }); ### `_.without(array [, value1, value2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1618 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1583 "View in source") [Ⓣ][1] Produces a new array with all occurrences of the passed values removed using strict equality for comparisons, i.e. `===`. @@ -2313,7 +2313,7 @@ _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); ### `_.wrap(func, wrapper [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2094 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L2059 "View in source") [Ⓣ][1] Create a new function that passes the `func` function to the `wrapper` function as its first argument. Additional arguments are appended to those passed to the `wrapper` function. @@ -2343,7 +2343,7 @@ hello(); ### `_.zip([array1, array2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1648 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L1613 "View in source") [Ⓣ][1] Merges together the values of each of the arrays with the value at the corresponding position. Useful for separate data sources that are coordinated through matching array indexes. For a matrix of nested arrays, `_.zip.apply(...)` can transpose the matrix in a similar fashion. @@ -2374,7 +2374,7 @@ _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]); ### `_.prototype.chain()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3036 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3076 "View in source") [Ⓣ][1] Extracts the value from a wrapped chainable object. @@ -2395,7 +2395,7 @@ _([1, 2, 3]).value(); ### `_.prototype.value()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3053 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3093 "View in source") [Ⓣ][1] Extracts the value from a wrapped chainable object. @@ -2423,7 +2423,7 @@ _([1, 2, 3]).value(); ### `_.templateSettings` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3076 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L167 "View in source") [Ⓣ][1] *(Object)*: By default, Lo-Dash uses ERB-style template delimiters, change the following template settings to use alternative delimiters. @@ -2435,7 +2435,7 @@ _([1, 2, 3]).value(); ### `_.templateSettings.escape` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3085 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L176 "View in source") [Ⓣ][1] *(RegExp)*: Used to detect `data` property values to be HTML-escaped. @@ -2447,7 +2447,7 @@ _([1, 2, 3]).value(); ### `_.templateSettings.evaluate` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3094 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L185 "View in source") [Ⓣ][1] *(RegExp)*: Used to detect code to be evaluated. @@ -2459,7 +2459,7 @@ _([1, 2, 3]).value(); ### `_.templateSettings.interpolate` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3103 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L194 "View in source") [Ⓣ][1] *(RegExp)*: Used to detect `data` property values to inject. @@ -2471,7 +2471,7 @@ _([1, 2, 3]).value(); ### `_.templateSettings.variable` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3112 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L203 "View in source") [Ⓣ][1] *(String)*: Used to reference the data object in the template text. diff --git a/lodash.min.js b/lodash.min.js index d7fcf19f10..7605b2db7c 100644 --- a/lodash.min.js +++ b/lodash.min.js @@ -2,29 +2,30 @@ Lo-Dash 0.2.0 lodash.com/license Underscore.js 1.3.3 github.com/documentcloud/underscore/blob/master/LICENSE */ -;(function(u,m){"use strict";function R(a){return"[object Arguments]"==h.call(a)}function c(a){return new p(a)}function p(a){if(a&&a._wrapped)return a;this._wrapped=a}function j(){for(var a,b,d,k=-1,c=arguments.length,f={e:"",f:"",k:"",q:"",c:{m:"++o/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:"object"},c.after= -function(a,b){return 1>a?b():function(){if(1>--a)return b.apply(this,arguments)}},c.bind=v,c.bindAll=function(a){var b=arguments,d=1;1==b.length&&(d=0,b=O(a));for(var c=b.length;dw(e,a[b])&&c.push(a[b]);return c},c.escape=function(a){return(a+"").replace(/&/g,"&").replace(/arguments.length&&(b=a||0,a=0);for(var c=-1,e=Math.max(Math.ceil((b-a)/d),0),f=Array(e);++cc?1:0}),"b")},c.sortedIndex=ga,c.tap=function(a,b) -{return b(a),a},c.template=function(a,b,d){d||(d={});var k;k=c.templateSettings||{};var e=d.escape,f=d.evaluate,g=d.interpolate,d=d.variable;return e==o&&(e=k.escape),f==o&&(f=k.evaluate),g==o&&(g=k.interpolate),e&&(a=a.replace(e,xa)),g&&(a=a.replace(g,ya)),f&&(a=a.replace(f,za)),a="__p='"+a.replace(Da,va).replace(Ca,ua)+"';\n",x.length=0,d||(d=k.variable||"object",a="with("+d+"||{}){"+a+"}"),a="function("+d+"){var __p,__t,__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}" -,k=Function("_","return "+a)(c),b?k(b):(k.source=a,k)},c.throttle=function(a,b){function d(){i=new Date,g=m,a.apply(f,c)}var c,e,f,g,i=0;return function(){var h=new Date,j=b-(h-i);return c=arguments,f=this,0>=j?(i=h,e=a.apply(f,c)):g||(g=P(d,j)),e}},c.times=function(a,b,d){d&&(b=v(b,d));for(d=0;dw(b,d[a])&&b.push(d[a]);return b},c.uniq=ka,c.uniqueId=function(a){var b=Aa++;return a?a+b:b},c.values=ra,c.without=function(a){for(var b=l.call(arguments,1),d=-1,c=a.length,e=[];++dw(b,a[d])&&e.push(a[d]);return e},c.wrap=function(a,b){return function(){var c=[a];return arguments.length&&E.apply(c,arguments),b.apply(this,c)}},c.zip=function(){for(var a=-1,b=ia(Q(arguments,"length")),c=Array(b);++a/g,evaluate:/<%([\s\S]+?)%>/g, +interpolate:/<%=([\s\S]+?)%>/g,variable:"object"};var ta=Function("n","var __p;with(n){__p='var l,r';if(k){__p+='='+k};__p+=';'+f+';'+q+';';if(c){__p+='var m='+g+'.length;l=-1;';if(o){__p+='if(m===+m){'};__p+=''+c['d']+';while('+c['m']+'){'+c['j']+'}';if(o){__p+='}'}}if(o){if(c){__p+='else{'}if(!i){__p+='var s=typeof '+l+'==\\'function\\';'};__p+=''+o['d']+';for('+o['m']+'){';if(i){if(r){__p+='if('+h+'){'};__p+=''+o['j']+';';if(r){__p+='}'}}else{__p+='if(!(s&&l==\\'prototype\\')';if(r){__p+='&&'+h};__p+='){'+o['j']+'}'};__p+='}';if(i){__p+='var g='+l+'.constructor;';for(var k=0;k<7;k++){__p+='l=\\''+p[k]+'\\';if(';if(p[k]=='constructor'){__p+='!(g&&g.prototype==='+l+')&&'};__p+=''+h+'){'+o['j']+'}'}}if(c){__p+='}'}};__p+=''+e+';return r'}return __p" +),t={a:"f,d,x",k:"f",q:"if(!d){d=k}else if(x){d=c(d,x)}",j:"d(f[l],l,f)"},$={k:"z",j:"if(!d(f[l],l,f))return!r"},aa={a:"n",k:"n",q:"for(var t,u=1,m=arguments.length;ua?c():function(){if(1>--a)return c.apply(this,arguments)}},b.bind=u,b.bindAll=function(a){var c=arguments,d=1;1==c.length&&(d=0,c=O(a));for(var b=c.length;dy(f,a[c])&&b.push(a[c]);return b},b.escape=function(a){return(a+"").replace(/&/g,"&").replace(/arguments.length&&(c=a||0,a=0);for(var b=-1,f=Math.max(Math.ceil((c-a)/d),0),e=Array(f);++bd?1:0}),"b")},b.sortedIndex=ha,b.tap=function(a,c){return c(a),a},b.template=function(a,c,d){d||(d={});var j;j=b.templateSettings +;var f=d.escape,e=d.evaluate,g=d.interpolate,d=d.variable;return f==o&&(f=j.escape),e==o&&(e=j.evaluate),g==o&&(g=j.interpolate),f&&(a=a.replace(f,xa)),g&&(a=a.replace(g,ya)),e&&(a=a.replace(e,za)),a="__p='"+a.replace(Da,va).replace(Ca,ua)+"';\n",w.length=0,d||(d=j.variable,a="with("+d+"||{}){"+a+"}"),a="function("+d+"){var __p,__t,__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}",j=Function("_","return "+a)(b),c?j(c):(j.source=a,j)},b.throttle=function(a,c){ +function b(){i=new Date,g=l,a.apply(e,j)}var j,f,e,g,i=0;return function(){var h=new Date,k=c-(h-i);return j=arguments,e=this,0>=k?(i=h,f=a.apply(e,j)):g||(g=P(b,k)),f}},b.times=function(a,c,b){b&&(c=u(c,b));for(b=0;by(c,b[a])&&c.push(b[a]);return c},b.uniq=la,b.uniqueId=function(a +){var b=Aa++;return a?a+b:b},b.values=ra,b.without=function(a){for(var b=m.call(arguments,1),d=-1,h=a.length,f=[];++dy(b,a[d])&&f.push(a[d]);return f},b.wrap=function(a,b){return function(){var d=[a];return arguments.length&&B.apply(d,arguments),b.apply(this,d)}},b.zip=function(){for(var a=-1,b=ja(Q(arguments,"length")),d=Array(b);++a Date: Wed, 23 May 2012 01:25:34 -0400 Subject: [PATCH 06/24] Add unit test to test to ensure `_.groupBy` only adds elements to own, not inherited, properties of the `result` object. Former-commit-id: 61dcdd0f6172db66d62e97873c1bc3053e339342 --- test/test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/test.js b/test/test.js index 92f426f102..8aa8418c29 100644 --- a/test/test.js +++ b/test/test.js @@ -204,6 +204,15 @@ deepEqual(actual, { '1': [1.3], '2': [2.1, 2.4] }); }); + + test('should only add elements to own, not inherited, properties', function() { + var actual = _.groupBy([1.3, 2.1, 2.4], function(num) { + return Math.floor(num) > 1 ? 'hasOwnProperty' : 'constructor'; + }); + + deepEqual(actual.constructor, [1.3]); + deepEqual(actual.hasOwnProperty, [2.1, 2.4]); + }); }()); /*--------------------------------------------------------------------------*/ From 5315058e2d9094276738076b61a193e21eba54f5 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 23 May 2012 02:19:35 -0400 Subject: [PATCH 07/24] Simplify `_.flatten` and add benchmarks. Former-commit-id: f541328bf680a75abea68bce813820def375f4a0 --- lodash.js | 5 +---- perf/index.html | 4 ++-- perf/perf.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/lodash.js b/lodash.js index ffe815dd75..bdfdc60391 100644 --- a/lodash.js +++ b/lodash.js @@ -976,9 +976,6 @@ * // => [1, 2, 3, [[4]]]; */ function flatten(array, shallow) { - if (shallow) { - return concat.apply(ArrayProto, array); - } var value, index = -1, length = array.length, @@ -987,7 +984,7 @@ while (++index < length) { value = array[index]; if (isArray(value)) { - push.apply(result, flatten(value)); + push.apply(result, shallow ? value : flatten(value)); } else { result.push(value); } diff --git a/perf/index.html b/perf/index.html index bb731ce447..52adf326c7 100644 --- a/perf/index.html +++ b/perf/index.html @@ -12,11 +12,11 @@ - + - + - + - + - + - +