From 4ce1d5ddd38b212f900f17622e1f937cbf478ec6 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 16 Dec 2015 17:48:03 -0800 Subject: [PATCH] Bump to v3.4.0. --- LICENSE.txt | 2 +- README.md | 2 +- array/difference.js | 9 +- array/dropRightWhile.js | 2 +- array/findLastIndex.js | 2 +- array/flatten.js | 2 +- array/flattenDeep.js | 2 +- array/indexOf.js | 4 +- array/intersection.js | 4 +- array/pull.js | 8 +- array/union.js | 2 +- chain/lodash.js | 22 +- collection.js | 4 +- collection/max.js | 52 +- collection/min.js | 52 +- collection/partition.js | 2 +- collection/size.js | 4 +- collection/sortBy.js | 5 +- collection/sortByAll.js | 30 +- collection/sortByOrder.js | 46 ++ collection/sum.js | 3 + function/flow.js | 32 +- function/flowRight.js | 32 +- function/memoize.js | 7 +- internal/baseDifference.js | 2 +- internal/baseFlatten.js | 10 +- internal/baseIndexOf.js | 4 +- internal/baseSortBy.js | 4 +- internal/baseSortByOrder.js | 36 + internal/baseUniq.js | 2 +- ...ultipleAscending.js => compareMultiple.js} | 22 +- internal/createAssigner.js | 27 +- internal/createComposer.js | 42 + internal/indexOfNaN.js | 4 +- internal/lazyValue.js | 19 +- internal/root.js | 24 +- lang/isEmpty.js | 2 +- main.js | 738 ++++++++++-------- math.js | 8 + math/add.js | 22 + math/max.js | 53 ++ math/min.js | 53 ++ math/sum.js | 33 + number/inRange.js | 2 +- object/defaults.js | 2 +- package.json | 2 +- string/escape.js | 2 +- string/words.js | 2 +- utility/attempt.js | 12 +- utility/range.js | 6 +- 50 files changed, 879 insertions(+), 584 deletions(-) create mode 100644 collection/sortByOrder.js create mode 100644 collection/sum.js create mode 100644 internal/baseSortByOrder.js rename internal/{compareMultipleAscending.js => compareMultiple.js} (57%) create mode 100644 internal/createComposer.js create mode 100644 math.js create mode 100644 math/add.js create mode 100644 math/max.js create mode 100644 math/min.js create mode 100644 math/sum.js diff --git a/LICENSE.txt b/LICENSE.txt index 17764328c..9cd87e5dc 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,5 +1,5 @@ Copyright 2012-2015 The Dojo Foundation -Based on Underscore.js 1.7.0, copyright 2009-2015 Jeremy Ashkenas, +Based on Underscore.js, copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors Permission is hereby granted, free of charge, to any person obtaining diff --git a/README.md b/README.md index ca6ba6c36..744b2e972 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# lodash v3.3.1 +# lodash v3.4.0 The [modern build](https://github.com/lodash/lodash/wiki/Build-Differences) of [lodash](https://lodash.com/) exported as [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) modules. diff --git a/array/difference.js b/array/difference.js index ae5f3184f..218bcd5b6 100644 --- a/array/difference.js +++ b/array/difference.js @@ -21,16 +21,17 @@ define(['../internal/baseDifference', '../internal/baseFlatten', '../lang/isArgu * // => [1, 3] */ function difference() { - var index = -1, - length = arguments.length; + var args = arguments, + index = -1, + length = args.length; while (++index < length) { - var value = arguments[index]; + var value = args[index]; if (isArray(value) || isArguments(value)) { break; } } - return baseDifference(value, baseFlatten(arguments, false, true, ++index)); + return baseDifference(value, baseFlatten(args, false, true, ++index)); } return difference; diff --git a/array/dropRightWhile.js b/array/dropRightWhile.js index 9f20459b7..5d62ff0e5 100644 --- a/array/dropRightWhile.js +++ b/array/dropRightWhile.js @@ -38,7 +38,7 @@ define(['../internal/baseCallback', '../internal/baseSlice'], function(baseCallb * ]; * * // using the `_.matches` callback shorthand - * _.pluck(_.dropRightWhile(users, { 'user': pebbles, 'active': false }), 'user'); + * _.pluck(_.dropRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user'); * // => ['barney', 'fred'] * * // using the `_.matchesProperty` callback shorthand diff --git a/array/findLastIndex.js b/array/findLastIndex.js index dea9d8a95..f5eee6303 100644 --- a/array/findLastIndex.js +++ b/array/findLastIndex.js @@ -42,7 +42,7 @@ define(['../internal/baseCallback'], function(baseCallback) { * * // using the `_.matchesProperty` callback shorthand * _.findLastIndex(users, 'active', false); - * // => 1 + * // => 2 * * // using the `_.property` callback shorthand * _.findLastIndex(users, 'active'); diff --git a/array/flatten.js b/array/flatten.js index ca549bf0c..b1ad7c3af 100644 --- a/array/flatten.js +++ b/array/flatten.js @@ -25,7 +25,7 @@ define(['../internal/baseFlatten', '../internal/isIterateeCall'], function(baseF if (guard && isIterateeCall(array, isDeep, guard)) { isDeep = false; } - return length ? baseFlatten(array, isDeep) : []; + return length ? baseFlatten(array, isDeep, false, 0) : []; } return flatten; diff --git a/array/flattenDeep.js b/array/flattenDeep.js index 297f424fe..1c64fe39c 100644 --- a/array/flattenDeep.js +++ b/array/flattenDeep.js @@ -15,7 +15,7 @@ define(['../internal/baseFlatten'], function(baseFlatten) { */ function flattenDeep(array) { var length = array ? array.length : 0; - return length ? baseFlatten(array, true) : []; + return length ? baseFlatten(array, true, false, 0) : []; } return flattenDeep; diff --git a/array/indexOf.js b/array/indexOf.js index 99ec1499b..5d497f834 100644 --- a/array/indexOf.js +++ b/array/indexOf.js @@ -41,14 +41,14 @@ define(['../internal/baseIndexOf', '../internal/binaryIndex'], function(baseInde return -1; } if (typeof fromIndex == 'number') { - fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0); + fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex; } else if (fromIndex) { var index = binaryIndex(array, value), other = array[index]; return (value === value ? value === other : other !== other) ? index : -1; } - return baseIndexOf(array, value, fromIndex); + return baseIndexOf(array, value, fromIndex || 0); } return indexOf; diff --git a/array/intersection.js b/array/intersection.js index 657a45501..fae848e3c 100644 --- a/array/intersection.js +++ b/array/intersection.js @@ -43,11 +43,11 @@ define(['../internal/baseIndexOf', '../internal/cacheIndexOf', '../internal/crea outer: while (++index < length) { value = array[index]; - if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value)) < 0) { + if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) { argsIndex = argsLength; while (--argsIndex) { var cache = caches[argsIndex]; - if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) { + if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value, 0)) < 0) { continue outer; } } diff --git a/array/pull.js b/array/pull.js index 07cc4c468..2a86bc79d 100644 --- a/array/pull.js +++ b/array/pull.js @@ -31,17 +31,19 @@ define(['../internal/baseIndexOf'], function(baseIndexOf) { * // => [1, 1] */ function pull() { - var array = arguments[0]; + var args = arguments, + array = args[0]; + if (!(array && array.length)) { return array; } var index = 0, indexOf = baseIndexOf, - length = arguments.length; + length = args.length; while (++index < length) { var fromIndex = 0, - value = arguments[index]; + value = args[index]; while ((fromIndex = indexOf(array, value, fromIndex)) > -1) { splice.call(array, fromIndex, 1); diff --git a/array/union.js b/array/union.js index 56b11a823..8648246ab 100644 --- a/array/union.js +++ b/array/union.js @@ -20,7 +20,7 @@ define(['../internal/baseFlatten', '../internal/baseUniq'], function(baseFlatten * // => [1, 2, 4] */ function union() { - return baseUniq(baseFlatten(arguments, false, true)); + return baseUniq(baseFlatten(arguments, false, true, 0)); } return union; diff --git a/chain/lodash.js b/chain/lodash.js index 6e5814934..10d4238e0 100644 --- a/chain/lodash.js +++ b/chain/lodash.js @@ -44,26 +44,26 @@ define(['../internal/LazyWrapper', '../internal/LodashWrapper', '../internal/bas * `mixin`, `negate`, `noop`, `omit`, `once`, `pairs`, `partial`, `partialRight`, * `partition`, `pick`, `plant`, `pluck`, `property`, `propertyOf`, `pull`, * `pullAt`, `push`, `range`, `rearg`, `reject`, `remove`, `rest`, `reverse`, - * `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`, `splice`, `spread`, - * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, - * `thru`, `times`, `toArray`, `toPlainObject`, `transform`, `union`, `uniq`, - * `unshift`, `unzip`, `values`, `valuesIn`, `where`, `without`, `wrap`, `xor`, - * `zip`, and `zipObject` + * `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`, `sortByOrder`, `splice`, + * `spread`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, + * `throttle`, `thru`, `times`, `toArray`, `toPlainObject`, `transform`, + * `union`, `uniq`, `unshift`, `unzip`, `values`, `valuesIn`, `where`, + * `without`, `wrap`, `xor`, `zip`, and `zipObject` * * The wrapper methods that are **not** chainable by default are: - * `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`, + * `add`, `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`, * `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`, * `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`, `has`, - * `identity`, `includes`, `indexOf`, `isArguments`, `isArray`, `isBoolean`, - * `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`, `isFinite`, - * `isFunction`, `isMatch`, `isNative`, `isNaN`, `isNull`, `isNumber`, + * `identity`, `includes`, `indexOf`, `inRange`, `isArguments`, `isArray`, + * `isBoolean`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`, + * `isFinite`,`isFunction`, `isMatch`, `isNative`, `isNaN`, `isNull`, `isNumber`, * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, * `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `max`, `min`, * `noConflict`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, * `random`, `reduce`, `reduceRight`, `repeat`, `result`, `runInContext`, * `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedLastIndex`, - * `startCase`, `startsWith`, `template`, `trim`, `trimLeft`, `trimRight`, - * `trunc`, `unescape`, `uniqueId`, `value`, and `words` + * `startCase`, `startsWith`, `sum`, `template`, `trim`, `trimLeft`, + * `trimRight`, `trunc`, `unescape`, `uniqueId`, `value`, and `words` * * The wrapper method `sample` will return a wrapped value when `n` is provided, * otherwise an unwrapped value is returned. diff --git a/collection.js b/collection.js index 293411a42..13b531f27 100644 --- a/collection.js +++ b/collection.js @@ -1,4 +1,4 @@ -define(['./collection/all', './collection/any', './collection/at', './collection/collect', './collection/contains', './collection/countBy', './collection/detect', './collection/each', './collection/eachRight', './collection/every', './collection/filter', './collection/find', './collection/findLast', './collection/findWhere', './collection/foldl', './collection/foldr', './collection/forEach', './collection/forEachRight', './collection/groupBy', './collection/include', './collection/includes', './collection/indexBy', './collection/inject', './collection/invoke', './collection/map', './collection/max', './collection/min', './collection/partition', './collection/pluck', './collection/reduce', './collection/reduceRight', './collection/reject', './collection/sample', './collection/select', './collection/shuffle', './collection/size', './collection/some', './collection/sortBy', './collection/sortByAll', './collection/where'], function(all, any, at, collect, contains, countBy, detect, each, eachRight, every, filter, find, findLast, findWhere, foldl, foldr, forEach, forEachRight, groupBy, include, includes, indexBy, inject, invoke, map, max, min, partition, pluck, reduce, reduceRight, reject, sample, select, shuffle, size, some, sortBy, sortByAll, where) { +define(['./collection/all', './collection/any', './collection/at', './collection/collect', './collection/contains', './collection/countBy', './collection/detect', './collection/each', './collection/eachRight', './collection/every', './collection/filter', './collection/find', './collection/findLast', './collection/findWhere', './collection/foldl', './collection/foldr', './collection/forEach', './collection/forEachRight', './collection/groupBy', './collection/include', './collection/includes', './collection/indexBy', './collection/inject', './collection/invoke', './collection/map', './math/max', './math/min', './collection/partition', './collection/pluck', './collection/reduce', './collection/reduceRight', './collection/reject', './collection/sample', './collection/select', './collection/shuffle', './collection/size', './collection/some', './collection/sortBy', './collection/sortByAll', './collection/sortByOrder', './math/sum', './collection/where'], function(all, any, at, collect, contains, countBy, detect, each, eachRight, every, filter, find, findLast, findWhere, foldl, foldr, forEach, forEachRight, groupBy, include, includes, indexBy, inject, invoke, map, max, min, partition, pluck, reduce, reduceRight, reject, sample, select, shuffle, size, some, sortBy, sortByAll, sortByOrder, sum, where) { return { 'all': all, 'any': any, @@ -39,6 +39,8 @@ define(['./collection/all', './collection/any', './collection/at', './collection 'some': some, 'sortBy': sortBy, 'sortByAll': sortByAll, + 'sortByOrder': sortByOrder, + 'sum': sum, 'where': where }; }); diff --git a/collection/max.js b/collection/max.js index fd6254e88..b109ef0ee 100644 --- a/collection/max.js +++ b/collection/max.js @@ -1,53 +1,3 @@ -define(['../internal/arrayMax', '../internal/createExtremum'], function(arrayMax, createExtremum) { - - /** - * Gets the maximum value of `collection`. If `collection` is empty or falsey - * `-Infinity` is returned. If an iteratee function is provided it is invoked - * for each value in `collection` to generate the criterion by which the value - * is ranked. The `iteratee` is bound to `thisArg` and invoked with three - * arguments; (value, index, collection). - * - * If a property name is provided for `predicate` the created `_.property` - * style callback returns the property value of the given element. - * - * If a value is also provided for `thisArg` the created `_.matchesProperty` - * style callback returns `true` for elements that have a matching property - * value, else `false`. - * - * If an object is provided for `predicate` the created `_.matches` style - * callback returns `true` for elements that have the properties of the given - * object, else `false`. - * - * @static - * @memberOf _ - * @category Collection - * @param {Array|Object|string} collection The collection to iterate over. - * @param {Function|Object|string} [iteratee] The function invoked per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {*} Returns the maximum value. - * @example - * - * _.max([4, 2, 8, 6]); - * // => 8 - * - * _.max([]); - * // => -Infinity - * - * var users = [ - * { 'user': 'barney', 'age': 36 }, - * { 'user': 'fred', 'age': 40 } - * ]; - * - * _.max(users, function(chr) { - * return chr.age; - * }); - * // => { 'user': 'fred', 'age': 40 }; - * - * // using the `_.property` callback shorthand - * _.max(users, 'age'); - * // => { 'user': 'fred', 'age': 40 }; - */ - var max = createExtremum(arrayMax); - +define(["../math/max"], function(max) { return max; }); diff --git a/collection/min.js b/collection/min.js index 9fb229991..6c9b4faa2 100644 --- a/collection/min.js +++ b/collection/min.js @@ -1,53 +1,3 @@ -define(['../internal/arrayMin', '../internal/createExtremum'], function(arrayMin, createExtremum) { - - /** - * Gets the minimum value of `collection`. If `collection` is empty or falsey - * `Infinity` is returned. If an iteratee function is provided it is invoked - * for each value in `collection` to generate the criterion by which the value - * is ranked. The `iteratee` is bound to `thisArg` and invoked with three - * arguments; (value, index, collection). - * - * If a property name is provided for `predicate` the created `_.property` - * style callback returns the property value of the given element. - * - * If a value is also provided for `thisArg` the created `_.matchesProperty` - * style callback returns `true` for elements that have a matching property - * value, else `false`. - * - * If an object is provided for `predicate` the created `_.matches` style - * callback returns `true` for elements that have the properties of the given - * object, else `false`. - * - * @static - * @memberOf _ - * @category Collection - * @param {Array|Object|string} collection The collection to iterate over. - * @param {Function|Object|string} [iteratee] The function invoked per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {*} Returns the minimum value. - * @example - * - * _.min([4, 2, 8, 6]); - * // => 2 - * - * _.min([]); - * // => Infinity - * - * var users = [ - * { 'user': 'barney', 'age': 36 }, - * { 'user': 'fred', 'age': 40 } - * ]; - * - * _.min(users, function(chr) { - * return chr.age; - * }); - * // => { 'user': 'barney', 'age': 36 }; - * - * // using the `_.property` callback shorthand - * _.min(users, 'age'); - * // => { 'user': 'barney', 'age': 36 }; - */ - var min = createExtremum(arrayMin, true); - +define(["../math/min"], function(min) { return min; }); diff --git a/collection/partition.js b/collection/partition.js index 5cc4bd709..caa86d4f6 100644 --- a/collection/partition.js +++ b/collection/partition.js @@ -35,7 +35,7 @@ define(['../internal/createAggregator'], function(createAggregator) { * _.partition([1.2, 2.3, 3.4], function(n) { * return this.floor(n) % 2; * }, Math); - * // => [[1, 3], [2]] + * // => [[1.2, 3.4], [2.3]] * * var users = [ * { 'user': 'barney', 'age': 36, 'active': false }, diff --git a/collection/size.js b/collection/size.js index 6168ad92d..d47800195 100644 --- a/collection/size.js +++ b/collection/size.js @@ -1,8 +1,8 @@ define(['../internal/isLength', '../object/keys'], function(isLength, keys) { /** - * Gets the size of `collection` by returning `collection.length` for - * array-like values or the number of own enumerable properties for objects. + * Gets the size of `collection` by returning its length for array-like + * values or the number of own enumerable properties for objects. * * @static * @memberOf _ diff --git a/collection/sortBy.js b/collection/sortBy.js index f41ae3a76..103ead6da 100644 --- a/collection/sortBy.js +++ b/collection/sortBy.js @@ -50,8 +50,11 @@ define(['../internal/baseCallback', '../internal/baseEach', '../internal/baseSor * // => ['barney', 'fred', 'pebbles'] */ function sortBy(collection, iteratee, thisArg) { + if (collection == null) { + return []; + } var index = -1, - length = collection ? collection.length : 0, + length = collection.length, result = isLength(length) ? Array(length) : []; if (thisArg && isIterateeCall(collection, iteratee, thisArg)) { diff --git a/collection/sortByAll.js b/collection/sortByAll.js index 02a0b6fdc..76018fad9 100644 --- a/collection/sortByAll.js +++ b/collection/sortByAll.js @@ -1,7 +1,4 @@ -define(['../internal/baseEach', '../internal/baseFlatten', '../internal/baseSortBy', '../internal/compareMultipleAscending', '../internal/isIterateeCall', '../internal/isLength'], function(baseEach, baseFlatten, baseSortBy, compareMultipleAscending, isIterateeCall, isLength) { - - /** Used as a safe reference for `undefined` in pre-ES5 environments. */ - var undefined; +define(['../internal/baseFlatten', '../internal/baseSortByOrder', '../internal/isIterateeCall'], function(baseFlatten, baseSortByOrder, isIterateeCall) { /** * This method is like `_.sortBy` except that it sorts by property names @@ -27,25 +24,16 @@ define(['../internal/baseEach', '../internal/baseFlatten', '../internal/baseSort * // => [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]] */ function sortByAll(collection) { - var args = arguments; - if (args.length > 3 && isIterateeCall(args[1], args[2], args[3])) { + if (collection == null) { + return []; + } + var args = arguments, + guard = args[3]; + + if (guard && isIterateeCall(args[1], args[2], guard)) { args = [collection, args[1]]; } - var index = -1, - length = collection ? collection.length : 0, - props = baseFlatten(args, false, false, 1), - result = isLength(length) ? Array(length) : []; - - baseEach(collection, function(value) { - var length = props.length, - criteria = Array(length); - - while (length--) { - criteria[length] = value == null ? undefined : value[props[length]]; - } - result[++index] = { 'criteria': criteria, 'index': index, 'value': value }; - }); - return baseSortBy(result, compareMultipleAscending); + return baseSortByOrder(collection, baseFlatten(args, false, false, 1), []); } return sortByAll; diff --git a/collection/sortByOrder.js b/collection/sortByOrder.js new file mode 100644 index 000000000..b3e13f252 --- /dev/null +++ b/collection/sortByOrder.js @@ -0,0 +1,46 @@ +define(['../internal/baseSortByOrder', '../lang/isArray', '../internal/isIterateeCall'], function(baseSortByOrder, isArray, isIterateeCall) { + + /** + * This method is like `_.sortByAll` except that it allows specifying the + * sort orders of the property names to sort by. A truthy value in `orders` + * will sort the corresponding property name in ascending order while a + * falsey value will sort it in descending order. + * + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object|string} collection The collection to iterate over. + * @param {string[]} props The property names to sort by. + * @param {boolean[]} orders The sort orders of `props`. + * @returns {Array} Returns the new sorted array. + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 }, + * { 'user': 'barney', 'age': 26 }, + * { 'user': 'fred', 'age': 30 } + * ]; + * + * // sort by `user` in ascending order and by `age` in descending order + * _.map(_.sortByOrder(users, ['user', 'age'], [true, false]), _.values); + * // => [['barney', 36], ['barney', 26], ['fred', 40], ['fred', 30]] + */ + function sortByOrder(collection, props, orders, guard) { + if (collection == null) { + return []; + } + if (guard && isIterateeCall(props, orders, guard)) { + orders = null; + } + if (!isArray(props)) { + props = props == null ? [] : [props]; + } + if (!isArray(orders)) { + orders = orders == null ? [] : [orders]; + } + return baseSortByOrder(collection, props, orders); + } + + return sortByOrder; +}); diff --git a/collection/sum.js b/collection/sum.js new file mode 100644 index 000000000..9f45380b9 --- /dev/null +++ b/collection/sum.js @@ -0,0 +1,3 @@ +define(["../math/sum"], function(sum) { + return sum; +}); diff --git a/function/flow.js b/function/flow.js index f19e0f00e..03a3ccb18 100644 --- a/function/flow.js +++ b/function/flow.js @@ -1,7 +1,4 @@ -define(['../internal/arrayEvery', '../internal/baseIsFunction'], function(arrayEvery, baseIsFunction) { - - /** Used as the `TypeError` message for "Functions" methods. */ - var FUNC_ERROR_TEXT = 'Expected a function'; +define(['../internal/createComposer'], function(createComposer) { /** * Creates a function that returns the result of invoking the provided @@ -15,38 +12,15 @@ define(['../internal/arrayEvery', '../internal/baseIsFunction'], function(arrayE * @returns {Function} Returns the new function. * @example * - * function add(x, y) { - * return x + y; - * } - * * function square(n) { * return n * n; * } * - * var addSquare = _.flow(add, square); + * var addSquare = _.flow(_.add, square); * addSquare(1, 2); * // => 9 */ - function flow() { - var funcs = arguments, - length = funcs.length; - - if (!length) { - return function() { return arguments[0]; }; - } - if (!arrayEvery(funcs, baseIsFunction)) { - throw new TypeError(FUNC_ERROR_TEXT); - } - return function() { - var index = 0, - result = funcs[index].apply(this, arguments); - - while (++index < length) { - result = funcs[index].call(this, result); - } - return result; - }; - } + var flow = createComposer(); return flow; }); diff --git a/function/flowRight.js b/function/flowRight.js index 632b25981..12048e817 100644 --- a/function/flowRight.js +++ b/function/flowRight.js @@ -1,7 +1,4 @@ -define(['../internal/arrayEvery', '../internal/baseIsFunction'], function(arrayEvery, baseIsFunction) { - - /** Used as the `TypeError` message for "Functions" methods. */ - var FUNC_ERROR_TEXT = 'Expected a function'; +define(['../internal/createComposer'], function(createComposer) { /** * This method is like `_.flow` except that it creates a function that @@ -15,38 +12,15 @@ define(['../internal/arrayEvery', '../internal/baseIsFunction'], function(arrayE * @returns {Function} Returns the new function. * @example * - * function add(x, y) { - * return x + y; - * } - * * function square(n) { * return n * n; * } * - * var addSquare = _.flowRight(square, add); + * var addSquare = _.flowRight(square, _.add); * addSquare(1, 2); * // => 9 */ - function flowRight() { - var funcs = arguments, - fromIndex = funcs.length - 1; - - if (fromIndex < 0) { - return function() { return arguments[0]; }; - } - if (!arrayEvery(funcs, baseIsFunction)) { - throw new TypeError(FUNC_ERROR_TEXT); - } - return function() { - var index = fromIndex, - result = funcs[index].apply(this, arguments); - - while (index--) { - result = funcs[index].call(this, result); - } - return result; - }; - } + var flowRight = createComposer(true); return flowRight; }); diff --git a/function/memoize.js b/function/memoize.js index 68dafe117..1d082b89e 100644 --- a/function/memoize.js +++ b/function/memoize.js @@ -61,13 +61,14 @@ define(['../internal/MapCache'], function(MapCache) { throw new TypeError(FUNC_ERROR_TEXT); } var memoized = function() { - var cache = memoized.cache, - key = resolver ? resolver.apply(this, arguments) : arguments[0]; + var args = arguments, + cache = memoized.cache, + key = resolver ? resolver.apply(this, args) : args[0]; if (cache.has(key)) { return cache.get(key); } - var result = func.apply(this, arguments); + var result = func.apply(this, args); cache.set(key, result); return result; }; diff --git a/internal/baseDifference.js b/internal/baseDifference.js index 30aae0d17..1ed64e4ac 100644 --- a/internal/baseDifference.js +++ b/internal/baseDifference.js @@ -40,7 +40,7 @@ define(['./baseIndexOf', './cacheIndexOf', './createCache'], function(baseIndexO } result.push(value); } - else if (indexOf(values, value) < 0) { + else if (indexOf(values, value, 0) < 0) { result.push(value); } } diff --git a/internal/baseFlatten.js b/internal/baseFlatten.js index bc96e10be..7b7c49dbf 100644 --- a/internal/baseFlatten.js +++ b/internal/baseFlatten.js @@ -6,13 +6,13 @@ define(['../lang/isArguments', '../lang/isArray', './isLength', './isObjectLike' * * @private * @param {Array} array The array to flatten. - * @param {boolean} [isDeep] Specify a deep flatten. - * @param {boolean} [isStrict] Restrict flattening to arrays and `arguments` objects. - * @param {number} [fromIndex=0] The index to start from. + * @param {boolean} isDeep Specify a deep flatten. + * @param {boolean} isStrict Restrict flattening to arrays and `arguments` objects. + * @param {number} fromIndex The index to start from. * @returns {Array} Returns the new flattened array. */ function baseFlatten(array, isDeep, isStrict, fromIndex) { - var index = (fromIndex || 0) - 1, + var index = fromIndex - 1, length = array.length, resIndex = -1, result = []; @@ -23,7 +23,7 @@ define(['../lang/isArguments', '../lang/isArray', './isLength', './isObjectLike' if (isObjectLike(value) && isLength(value.length) && (isArray(value) || isArguments(value))) { if (isDeep) { // Recursively flatten arrays (susceptible to call stack limits). - value = baseFlatten(value, isDeep, isStrict); + value = baseFlatten(value, isDeep, isStrict, 0); } var valIndex = -1, valLength = value.length; diff --git a/internal/baseIndexOf.js b/internal/baseIndexOf.js index 91638d55f..917c4f47f 100644 --- a/internal/baseIndexOf.js +++ b/internal/baseIndexOf.js @@ -6,14 +6,14 @@ define(['./indexOfNaN'], function(indexOfNaN) { * @private * @param {Array} array The array to search. * @param {*} value The value to search for. - * @param {number} [fromIndex=0] The index to search from. + * @param {number} fromIndex The index to search from. * @returns {number} Returns the index of the matched value, else `-1`. */ function baseIndexOf(array, value, fromIndex) { if (value !== value) { return indexOfNaN(array, fromIndex); } - var index = (fromIndex || 0) - 1, + var index = fromIndex - 1, length = array.length; while (++index < length) { diff --git a/internal/baseSortBy.js b/internal/baseSortBy.js index 0d1161b15..1377461c8 100644 --- a/internal/baseSortBy.js +++ b/internal/baseSortBy.js @@ -1,8 +1,8 @@ define([], function() { /** - * The base implementation of `_.sortBy` and `_.sortByAll` which uses `comparer` - * to define the sort order of `array` and replaces criteria objects with their + * The base implementation of `_.sortBy` which uses `comparer` to define + * the sort order of `array` and replaces criteria objects with their * corresponding values. * * @private diff --git a/internal/baseSortByOrder.js b/internal/baseSortByOrder.js new file mode 100644 index 000000000..2b41bf142 --- /dev/null +++ b/internal/baseSortByOrder.js @@ -0,0 +1,36 @@ +define(['./baseEach', './baseSortBy', './compareMultiple', './isLength'], function(baseEach, baseSortBy, compareMultiple, isLength) { + + /** Used as a safe reference for `undefined` in pre-ES5 environments. */ + var undefined; + + /** + * The base implementation of `_.sortByOrder` without param guards. + * + * @private + * @param {Array|Object|string} collection The collection to iterate over. + * @param {string[]} props The property names to sort by. + * @param {boolean[]} orders The sort orders of `props`. + * @returns {Array} Returns the new sorted array. + */ + function baseSortByOrder(collection, props, orders) { + var index = -1, + length = collection.length, + result = isLength(length) ? Array(length) : []; + + baseEach(collection, function(value) { + var length = props.length, + criteria = Array(length); + + while (length--) { + criteria[length] = value == null ? undefined : value[props[length]]; + } + result[++index] = { 'criteria': criteria, 'index': index, 'value': value }; + }); + + return baseSortBy(result, function(object, other) { + return compareMultiple(object, other, orders); + }); + } + + return baseSortByOrder; +}); diff --git a/internal/baseUniq.js b/internal/baseUniq.js index 1307e96d3..acf8e5d3c 100644 --- a/internal/baseUniq.js +++ b/internal/baseUniq.js @@ -42,7 +42,7 @@ define(['./baseIndexOf', './cacheIndexOf', './createCache'], function(baseIndexO } result.push(value); } - else if (indexOf(seen, computed) < 0) { + else if (indexOf(seen, computed, 0) < 0) { if (iteratee || isLarge) { seen.push(computed); } diff --git a/internal/compareMultipleAscending.js b/internal/compareMultiple.js similarity index 57% rename from internal/compareMultipleAscending.js rename to internal/compareMultiple.js index 5086926f3..ad475e629 100644 --- a/internal/compareMultipleAscending.js +++ b/internal/compareMultiple.js @@ -1,24 +1,34 @@ define(['./baseCompareAscending'], function(baseCompareAscending) { /** - * Used by `_.sortByAll` to compare multiple properties of each element - * in a collection and stable sort them in ascending order. + * Used by `_.sortByOrder` to compare multiple properties of each element + * in a collection and stable sort them in the following order: + * + * If orders is unspecified, sort in ascending order for all properties. + * Otherwise, for each property, sort in ascending order if its corresponding value in + * orders is true, and descending order if false. * * @private * @param {Object} object The object to compare to `other`. * @param {Object} other The object to compare to `object`. + * @param {boolean[]} orders The order to sort by for each property. * @returns {number} Returns the sort order indicator for `object`. */ - function compareMultipleAscending(object, other) { + function compareMultiple(object, other, orders) { var index = -1, objCriteria = object.criteria, othCriteria = other.criteria, - length = objCriteria.length; + length = objCriteria.length, + ordersLength = orders.length; while (++index < length) { var result = baseCompareAscending(objCriteria[index], othCriteria[index]); if (result) { - return result; + if (index >= ordersLength) { + return result; + } else { + return orders[index] ? result : result * -1; + } } } // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications @@ -31,5 +41,5 @@ define(['./baseCompareAscending'], function(baseCompareAscending) { return object.index - other.index; } - return compareMultipleAscending; + return compareMultiple; }); diff --git a/internal/createAssigner.js b/internal/createAssigner.js index 73f5350b2..5978cb4ef 100644 --- a/internal/createAssigner.js +++ b/internal/createAssigner.js @@ -10,24 +10,31 @@ define(['./bindCallback', './isIterateeCall'], function(bindCallback, isIteratee */ function createAssigner(assigner) { return function() { - var length = arguments.length, - object = arguments[0]; + var args = arguments, + length = args.length, + object = args[0]; if (length < 2 || object == null) { return object; } - if (length > 3 && isIterateeCall(arguments[1], arguments[2], arguments[3])) { - length = 2; + var customizer = args[length - 2], + thisArg = args[length - 1], + guard = args[3]; + + if (length > 3 && typeof customizer == 'function') { + customizer = bindCallback(customizer, thisArg, 5); + length -= 2; + } else { + customizer = (length > 2 && typeof thisArg == 'function') ? thisArg : null; + length -= (customizer ? 1 : 0); } - // Juggle arguments. - if (length > 3 && typeof arguments[length - 2] == 'function') { - var customizer = bindCallback(arguments[--length - 1], arguments[length--], 5); - } else if (length > 2 && typeof arguments[length - 1] == 'function') { - customizer = arguments[--length]; + if (guard && isIterateeCall(args[1], args[2], guard)) { + customizer = length == 3 ? null : customizer; + length = 2; } var index = 0; while (++index < length) { - var source = arguments[index]; + var source = args[index]; if (source) { assigner(object, source, customizer); } diff --git a/internal/createComposer.js b/internal/createComposer.js new file mode 100644 index 000000000..be0d886af --- /dev/null +++ b/internal/createComposer.js @@ -0,0 +1,42 @@ +define([], function() { + + /** Used as the `TypeError` message for "Functions" methods. */ + var FUNC_ERROR_TEXT = 'Expected a function'; + + /** + * Creates a function to compose other functions into a single function. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new composer function. + */ + function createComposer(fromRight) { + return function() { + var length = arguments.length, + index = length, + fromIndex = fromRight ? length - 1 : 0; + + if (!length) { + return function() { return arguments[0]; }; + } + var funcs = Array(length); + while (index--) { + funcs[index] = arguments[index]; + if (typeof funcs[index] != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + } + return function() { + var index = fromIndex, + result = funcs[index].apply(this, arguments); + + while ((fromRight ? index-- : ++index < length)) { + result = funcs[index].call(this, result); + } + return result; + }; + }; + } + + return createComposer; +}); diff --git a/internal/indexOfNaN.js b/internal/indexOfNaN.js index cb2c4fe28..913d8801f 100644 --- a/internal/indexOfNaN.js +++ b/internal/indexOfNaN.js @@ -6,13 +6,13 @@ define([], function() { * * @private * @param {Array} array The array to search. - * @param {number} [fromIndex] The index to search from. + * @param {number} fromIndex The index to search from. * @param {boolean} [fromRight] Specify iterating from right to left. * @returns {number} Returns the index of the matched `NaN`, else `-1`. */ function indexOfNaN(array, fromIndex, fromRight) { var length = array.length, - index = fromRight ? (fromIndex || length) : ((fromIndex || 0) - 1); + index = fromIndex + (fromRight ? 0 : -1); while ((fromRight ? index-- : ++index < length)) { var other = array[index]; diff --git a/internal/lazyValue.js b/internal/lazyValue.js index 32b732933..0e8853a25 100644 --- a/internal/lazyValue.js +++ b/internal/lazyValue.js @@ -1,8 +1,9 @@ define(['./baseWrapperValue', './getView', '../lang/isArray'], function(baseWrapperValue, getView, isArray) { /** Used to indicate the type of lazy iteratees. */ - var LAZY_FILTER_FLAG = 0, - LAZY_MAP_FLAG = 1; + var LAZY_DROP_WHILE_FLAG = 0, + LAZY_MAP_FLAG = 2, + LAZY_TAKE_WHILE_FLAG = 3; /* Native method references for those with the same name as other `lodash` methods. */ var nativeMin = Math.min; @@ -44,16 +45,22 @@ define(['./baseWrapperValue', './getView', '../lang/isArray'], function(baseWrap while (++iterIndex < iterLength) { var data = iteratees[iterIndex], iteratee = data.iteratee, - computed = iteratee(value, index, array), type = data.type; + if (type != LAZY_DROP_WHILE_FLAG) { + var computed = iteratee(value); + } else { + data.done = data.done && (isRight ? index < data.index : index > data.index); + data.index = index; + computed = data.done || (data.done = !iteratee(value)); + } if (type == LAZY_MAP_FLAG) { value = computed; } else if (!computed) { - if (type == LAZY_FILTER_FLAG) { - continue outer; - } else { + if (type == LAZY_TAKE_WHILE_FLAG) { break outer; + } else { + continue outer; } } } diff --git a/internal/root.js b/internal/root.js index b4dc26948..ebbef2755 100644 --- a/internal/root.js +++ b/internal/root.js @@ -6,25 +6,25 @@ define([], function() { 'object': true }; - /** - * Used as a reference to the global object. - * - * The `this` value is used if it is the global object to avoid Greasemonkey's - * restricted `window` object, otherwise the `window` object is used. - */ - var root = (objectTypes[typeof window] && window !== (this && this.window)) ? window : this; - /** Detect free variable `exports`. */ var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports; /** Detect free variable `module`. */ var freeModule = objectTypes[typeof module] && module && !module.nodeType && module; - /** Detect free variable `global` from Node.js or Browserified code and use it as `root`. */ + /** Detect free variable `global` from Node.js. */ var freeGlobal = freeExports && freeModule && typeof global == 'object' && global; - if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal || freeGlobal.self === freeGlobal)) { - root = freeGlobal; - } + + /** Detect free variable `window`. */ + var freeWindow = objectTypes[typeof window] && window; + + /** + * Used as a reference to the global object. + * + * The `this` value is used if it is the global object to avoid Greasemonkey's + * restricted `window` object, otherwise the `window` object is used. + */ + var root = freeGlobal || ((freeWindow !== (this && this.window)) && freeWindow) || this; return root; }); diff --git a/lang/isEmpty.js b/lang/isEmpty.js index 0f30090e3..f7501e38b 100644 --- a/lang/isEmpty.js +++ b/lang/isEmpty.js @@ -1,7 +1,7 @@ define(['./isArguments', './isArray', './isFunction', '../internal/isLength', '../internal/isObjectLike', './isString', '../object/keys'], function(isArguments, isArray, isFunction, isLength, isObjectLike, isString, keys) { /** - * Checks if a value is empty. A value is considered empty unless it is an + * Checks if `value` is empty. A value is considered empty unless it is an * `arguments` object, array, string, or jQuery-like collection with a length * greater than `0` or an object with own enumerable properties. * diff --git a/main.js b/main.js index 6bfacf856..38c8d1dbe 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,6 @@ /** * @license - * lodash 3.3.1 (Custom Build) + * lodash 3.4.0 (Custom Build) * Build: `lodash modern exports="amd" -d -o ./main.js` * Copyright 2012-2015 The Dojo Foundation * Based on Underscore.js 1.8.2 @@ -13,7 +13,7 @@ var undefined; /** Used as the semantic version number. */ - var VERSION = '3.3.1'; + var VERSION = '3.4.0'; /** Used to compose bitmasks for wrapper metadata. */ var BIND_FLAG = 1, @@ -35,9 +35,9 @@ HOT_SPAN = 16; /** Used to indicate the type of lazy iteratees. */ - var LAZY_FILTER_FLAG = 0, - LAZY_MAP_FLAG = 1, - LAZY_WHILE_FLAG = 2; + var LAZY_DROP_WHILE_FLAG = 0, + LAZY_MAP_FLAG = 2, + LAZY_TAKE_WHILE_FLAG = 3; /** Used as the `TypeError` message for "Functions" methods. */ var FUNC_ERROR_TEXT = 'Expected a function'; @@ -131,7 +131,7 @@ var upper = '[A-Z\\xc0-\\xd6\\xd8-\\xde]', lower = '[a-z\\xdf-\\xf6\\xf8-\\xff]+'; - return RegExp(upper + '{2,}(?=' + upper + lower + ')|' + upper + '?' + lower + '|' + upper + '+|[0-9]+', 'g'); + return RegExp(upper + '+(?=' + upper + lower + ')|' + upper + '?' + lower + '|' + upper + '+|[0-9]+', 'g'); }()); /** Used to detect and test for whitespace. */ @@ -253,25 +253,25 @@ '\u2029': 'u2029' }; - /** - * Used as a reference to the global object. - * - * The `this` value is used if it is the global object to avoid Greasemonkey's - * restricted `window` object, otherwise the `window` object is used. - */ - var root = (objectTypes[typeof window] && window !== (this && this.window)) ? window : this; - /** Detect free variable `exports`. */ var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports; /** Detect free variable `module`. */ var freeModule = objectTypes[typeof module] && module && !module.nodeType && module; - /** Detect free variable `global` from Node.js or Browserified code and use it as `root`. */ + /** Detect free variable `global` from Node.js. */ var freeGlobal = freeExports && freeModule && typeof global == 'object' && global; - if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal || freeGlobal.self === freeGlobal)) { - root = freeGlobal; - } + + /** Detect free variable `window`. */ + var freeWindow = objectTypes[typeof window] && window; + + /** + * Used as a reference to the global object. + * + * The `this` value is used if it is the global object to avoid Greasemonkey's + * restricted `window` object, otherwise the `window` object is used. + */ + var root = freeGlobal || ((freeWindow !== (this && this.window)) && freeWindow) || this; /*--------------------------------------------------------------------------*/ @@ -305,14 +305,14 @@ * @private * @param {Array} array The array to search. * @param {*} value The value to search for. - * @param {number} [fromIndex=0] The index to search from. + * @param {number} fromIndex The index to search from. * @returns {number} Returns the index of the matched value, else `-1`. */ function baseIndexOf(array, value, fromIndex) { if (value !== value) { return indexOfNaN(array, fromIndex); } - var index = (fromIndex || 0) - 1, + var index = fromIndex - 1, length = array.length; while (++index < length) { @@ -337,26 +337,6 @@ return typeof value == 'function' || false; } - /** - * The base implementation of `_.sortBy` and `_.sortByAll` which uses `comparer` - * to define the sort order of `array` and replaces criteria objects with their - * corresponding values. - * - * @private - * @param {Array} array The array to sort. - * @param {Function} comparer The function to define sort order. - * @returns {Array} Returns `array`. - */ - function baseSortBy(array, comparer) { - var length = array.length; - - array.sort(comparer); - while (length--) { - array[length] = array[length].value; - } - return array; - } - /** * Converts `value` to a string if it is not one. An empty string is returned * for `null` or `undefined` values. @@ -430,24 +410,34 @@ } /** - * Used by `_.sortByAll` to compare multiple properties of each element - * in a collection and stable sort them in ascending order. + * Used by `_.sortByOrder` to compare multiple properties of each element + * in a collection and stable sort them in the following order: + * + * If orders is unspecified, sort in ascending order for all properties. + * Otherwise, for each property, sort in ascending order if its corresponding value in + * orders is true, and descending order if false. * * @private * @param {Object} object The object to compare to `other`. * @param {Object} other The object to compare to `object`. + * @param {boolean[]} orders The order to sort by for each property. * @returns {number} Returns the sort order indicator for `object`. */ - function compareMultipleAscending(object, other) { + function compareMultiple(object, other, orders) { var index = -1, objCriteria = object.criteria, othCriteria = other.criteria, - length = objCriteria.length; + length = objCriteria.length, + ordersLength = orders.length; while (++index < length) { var result = baseCompareAscending(objCriteria[index], othCriteria[index]); if (result) { - return result; + if (index >= ordersLength) { + return result; + } else { + return orders[index] ? result : result * -1; + } } } // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications @@ -500,13 +490,13 @@ * * @private * @param {Array} array The array to search. - * @param {number} [fromIndex] The index to search from. + * @param {number} fromIndex The index to search from. * @param {boolean} [fromRight] Specify iterating from right to left. * @returns {number} Returns the index of the matched `NaN`, else `-1`. */ function indexOfNaN(array, fromIndex, fromRight) { var length = array.length, - index = fromRight ? (fromIndex || length) : ((fromIndex || 0) - 1); + index = fromIndex + (fromRight ? 0 : -1); while ((fromRight ? index-- : ++index < length)) { var other = array[index]; @@ -827,26 +817,26 @@ * `mixin`, `negate`, `noop`, `omit`, `once`, `pairs`, `partial`, `partialRight`, * `partition`, `pick`, `plant`, `pluck`, `property`, `propertyOf`, `pull`, * `pullAt`, `push`, `range`, `rearg`, `reject`, `remove`, `rest`, `reverse`, - * `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`, `splice`, `spread`, - * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, - * `thru`, `times`, `toArray`, `toPlainObject`, `transform`, `union`, `uniq`, - * `unshift`, `unzip`, `values`, `valuesIn`, `where`, `without`, `wrap`, `xor`, - * `zip`, and `zipObject` + * `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`, `sortByOrder`, `splice`, + * `spread`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, + * `throttle`, `thru`, `times`, `toArray`, `toPlainObject`, `transform`, + * `union`, `uniq`, `unshift`, `unzip`, `values`, `valuesIn`, `where`, + * `without`, `wrap`, `xor`, `zip`, and `zipObject` * * The wrapper methods that are **not** chainable by default are: - * `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`, + * `add`, `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`, * `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`, * `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`, `has`, - * `identity`, `includes`, `indexOf`, `isArguments`, `isArray`, `isBoolean`, - * `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`, `isFinite`, - * `isFunction`, `isMatch`, `isNative`, `isNaN`, `isNull`, `isNumber`, + * `identity`, `includes`, `indexOf`, `inRange`, `isArguments`, `isArray`, + * `isBoolean`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`, + * `isFinite`,`isFunction`, `isMatch`, `isNative`, `isNaN`, `isNull`, `isNumber`, * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, * `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `max`, `min`, * `noConflict`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, * `random`, `reduce`, `reduceRight`, `repeat`, `result`, `runInContext`, * `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedLastIndex`, - * `startCase`, `startsWith`, `template`, `trim`, `trimLeft`, `trimRight`, - * `trunc`, `unescape`, `uniqueId`, `value`, and `words` + * `startCase`, `startsWith`, `sum`, `template`, `trim`, `trimLeft`, + * `trimRight`, `trunc`, `unescape`, `uniqueId`, `value`, and `words` * * The wrapper method `sample` will return a wrapped value when `n` is provided, * otherwise an unwrapped value is returned. @@ -1133,16 +1123,22 @@ while (++iterIndex < iterLength) { var data = iteratees[iterIndex], iteratee = data.iteratee, - computed = iteratee(value, index, array), type = data.type; + if (type != LAZY_DROP_WHILE_FLAG) { + var computed = iteratee(value); + } else { + data.done = data.done && (isRight ? index < data.index : index > data.index); + data.index = index; + computed = data.done || (data.done = !iteratee(value)); + } if (type == LAZY_MAP_FLAG) { value = computed; } else if (!computed) { - if (type == LAZY_FILTER_FLAG) { - continue outer; - } else { + if (type == LAZY_TAKE_WHILE_FLAG) { break outer; + } else { + continue outer; } } } @@ -1822,7 +1818,7 @@ } result.push(value); } - else if (indexOf(values, value) < 0) { + else if (indexOf(values, value, 0) < 0) { result.push(value); } } @@ -1975,13 +1971,13 @@ * * @private * @param {Array} array The array to flatten. - * @param {boolean} [isDeep] Specify a deep flatten. - * @param {boolean} [isStrict] Restrict flattening to arrays and `arguments` objects. - * @param {number} [fromIndex=0] The index to start from. + * @param {boolean} isDeep Specify a deep flatten. + * @param {boolean} isStrict Restrict flattening to arrays and `arguments` objects. + * @param {number} fromIndex The index to start from. * @returns {Array} Returns the new flattened array. */ function baseFlatten(array, isDeep, isStrict, fromIndex) { - var index = (fromIndex || 0) - 1, + var index = fromIndex - 1, length = array.length, resIndex = -1, result = []; @@ -1992,7 +1988,7 @@ if (isObjectLike(value) && isLength(value.length) && (isArray(value) || isArguments(value))) { if (isDeep) { // Recursively flatten arrays (susceptible to call stack limits). - value = baseFlatten(value, isDeep, isStrict); + value = baseFlatten(value, isDeep, isStrict, 0); } var valIndex = -1, valLength = value.length; @@ -2606,6 +2602,55 @@ return !!result; } + /** + * The base implementation of `_.sortBy` which uses `comparer` to define + * the sort order of `array` and replaces criteria objects with their + * corresponding values. + * + * @private + * @param {Array} array The array to sort. + * @param {Function} comparer The function to define sort order. + * @returns {Array} Returns `array`. + */ + function baseSortBy(array, comparer) { + var length = array.length; + + array.sort(comparer); + while (length--) { + array[length] = array[length].value; + } + return array; + } + + /** + * The base implementation of `_.sortByOrder` without param guards. + * + * @private + * @param {Array|Object|string} collection The collection to iterate over. + * @param {string[]} props The property names to sort by. + * @param {boolean[]} orders The sort orders of `props`. + * @returns {Array} Returns the new sorted array. + */ + function baseSortByOrder(collection, props, orders) { + var index = -1, + length = collection.length, + result = isLength(length) ? Array(length) : []; + + baseEach(collection, function(value) { + var length = props.length, + criteria = Array(length); + + while (length--) { + criteria[length] = value == null ? undefined : value[props[length]]; + } + result[++index] = { 'criteria': criteria, 'index': index, 'value': value }; + }); + + return baseSortBy(result, function(object, other) { + return compareMultiple(object, other, orders); + }); + } + /** * The base implementation of `_.uniq` without support for callback shorthands * and `this` binding. @@ -2648,7 +2693,7 @@ } result.push(value); } - else if (indexOf(seen, computed) < 0) { + else if (indexOf(seen, computed, 0) < 0) { if (iteratee || isLarge) { seen.push(computed); } @@ -2952,24 +2997,31 @@ */ function createAssigner(assigner) { return function() { - var length = arguments.length, - object = arguments[0]; + var args = arguments, + length = args.length, + object = args[0]; if (length < 2 || object == null) { return object; } - if (length > 3 && isIterateeCall(arguments[1], arguments[2], arguments[3])) { - length = 2; + var customizer = args[length - 2], + thisArg = args[length - 1], + guard = args[3]; + + if (length > 3 && typeof customizer == 'function') { + customizer = bindCallback(customizer, thisArg, 5); + length -= 2; + } else { + customizer = (length > 2 && typeof thisArg == 'function') ? thisArg : null; + length -= (customizer ? 1 : 0); } - // Juggle arguments. - if (length > 3 && typeof arguments[length - 2] == 'function') { - var customizer = bindCallback(arguments[--length - 1], arguments[length--], 5); - } else if (length > 2 && typeof arguments[length - 1] == 'function') { - customizer = arguments[--length]; + if (guard && isIterateeCall(args[1], args[2], guard)) { + customizer = length == 3 ? null : customizer; + length = 2; } var index = 0; while (++index < length) { - var source = arguments[index]; + var source = args[index]; if (source) { assigner(object, source, customizer); } @@ -3007,6 +3059,41 @@ return new SetCache(values); }; + /** + * Creates a function to compose other functions into a single function. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new composer function. + */ + function createComposer(fromRight) { + return function() { + var length = arguments.length, + index = length, + fromIndex = fromRight ? length - 1 : 0; + + if (!length) { + return function() { return arguments[0]; }; + } + var funcs = Array(length); + while (index--) { + funcs[index] = arguments[index]; + if (typeof funcs[index] != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + } + return function() { + var index = fromIndex, + result = funcs[index].apply(this, arguments); + + while ((fromRight ? index-- : ++index < length)) { + result = funcs[index].call(this, result); + } + return result; + }; + }; + } + /** * Creates a function that produces compound words out of the words in a * given string. @@ -4076,16 +4163,17 @@ * // => [1, 3] */ function difference() { - var index = -1, - length = arguments.length; + var args = arguments, + index = -1, + length = args.length; while (++index < length) { - var value = arguments[index]; + var value = args[index]; if (isArray(value) || isArguments(value)) { break; } } - return baseDifference(value, baseFlatten(arguments, false, true, ++index)); + return baseDifference(value, baseFlatten(args, false, true, ++index)); } /** @@ -4197,7 +4285,7 @@ * ]; * * // using the `_.matches` callback shorthand - * _.pluck(_.dropRightWhile(users, { 'user': pebbles, 'active': false }), 'user'); + * _.pluck(_.dropRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user'); * // => ['barney', 'fred'] * * // using the `_.matchesProperty` callback shorthand @@ -4408,7 +4496,7 @@ * * // using the `_.matchesProperty` callback shorthand * _.findLastIndex(users, 'active', false); - * // => 1 + * // => 2 * * // using the `_.property` callback shorthand * _.findLastIndex(users, 'active'); @@ -4471,7 +4559,7 @@ if (guard && isIterateeCall(array, isDeep, guard)) { isDeep = false; } - return length ? baseFlatten(array, isDeep) : []; + return length ? baseFlatten(array, isDeep, false, 0) : []; } /** @@ -4489,7 +4577,7 @@ */ function flattenDeep(array) { var length = array ? array.length : 0; - return length ? baseFlatten(array, true) : []; + return length ? baseFlatten(array, true, false, 0) : []; } /** @@ -4530,14 +4618,14 @@ return -1; } if (typeof fromIndex == 'number') { - fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0); + fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex; } else if (fromIndex) { var index = binaryIndex(array, value), other = array[index]; return (value === value ? value === other : other !== other) ? index : -1; } - return baseIndexOf(array, value, fromIndex); + return baseIndexOf(array, value, fromIndex || 0); } /** @@ -4600,11 +4688,11 @@ outer: while (++index < length) { value = array[index]; - if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value)) < 0) { + if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) { argsIndex = argsLength; while (--argsIndex) { var cache = caches[argsIndex]; - if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) { + if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value, 0)) < 0) { continue outer; } } @@ -4709,17 +4797,19 @@ * // => [1, 1] */ function pull() { - var array = arguments[0]; + var args = arguments, + array = args[0]; + if (!(array && array.length)) { return array; } var index = 0, indexOf = getIndexOf(), - length = arguments.length; + length = args.length; while (++index < length) { var fromIndex = 0, - value = arguments[index]; + value = args[index]; while ((fromIndex = indexOf(array, value, fromIndex)) > -1) { splice.call(array, fromIndex, 1); @@ -5149,7 +5239,7 @@ * // => [1, 2, 4] */ function union() { - return baseUniq(baseFlatten(arguments, false, true)); + return baseUniq(baseFlatten(arguments, false, true, 0)); } /** @@ -6220,104 +6310,6 @@ return func(collection, iteratee); } - /** - * Gets the maximum value of `collection`. If `collection` is empty or falsey - * `-Infinity` is returned. If an iteratee function is provided it is invoked - * for each value in `collection` to generate the criterion by which the value - * is ranked. The `iteratee` is bound to `thisArg` and invoked with three - * arguments; (value, index, collection). - * - * If a property name is provided for `predicate` the created `_.property` - * style callback returns the property value of the given element. - * - * If a value is also provided for `thisArg` the created `_.matchesProperty` - * style callback returns `true` for elements that have a matching property - * value, else `false`. - * - * If an object is provided for `predicate` the created `_.matches` style - * callback returns `true` for elements that have the properties of the given - * object, else `false`. - * - * @static - * @memberOf _ - * @category Collection - * @param {Array|Object|string} collection The collection to iterate over. - * @param {Function|Object|string} [iteratee] The function invoked per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {*} Returns the maximum value. - * @example - * - * _.max([4, 2, 8, 6]); - * // => 8 - * - * _.max([]); - * // => -Infinity - * - * var users = [ - * { 'user': 'barney', 'age': 36 }, - * { 'user': 'fred', 'age': 40 } - * ]; - * - * _.max(users, function(chr) { - * return chr.age; - * }); - * // => { 'user': 'fred', 'age': 40 }; - * - * // using the `_.property` callback shorthand - * _.max(users, 'age'); - * // => { 'user': 'fred', 'age': 40 }; - */ - var max = createExtremum(arrayMax); - - /** - * Gets the minimum value of `collection`. If `collection` is empty or falsey - * `Infinity` is returned. If an iteratee function is provided it is invoked - * for each value in `collection` to generate the criterion by which the value - * is ranked. The `iteratee` is bound to `thisArg` and invoked with three - * arguments; (value, index, collection). - * - * If a property name is provided for `predicate` the created `_.property` - * style callback returns the property value of the given element. - * - * If a value is also provided for `thisArg` the created `_.matchesProperty` - * style callback returns `true` for elements that have a matching property - * value, else `false`. - * - * If an object is provided for `predicate` the created `_.matches` style - * callback returns `true` for elements that have the properties of the given - * object, else `false`. - * - * @static - * @memberOf _ - * @category Collection - * @param {Array|Object|string} collection The collection to iterate over. - * @param {Function|Object|string} [iteratee] The function invoked per iteration. - * @param {*} [thisArg] The `this` binding of `iteratee`. - * @returns {*} Returns the minimum value. - * @example - * - * _.min([4, 2, 8, 6]); - * // => 2 - * - * _.min([]); - * // => Infinity - * - * var users = [ - * { 'user': 'barney', 'age': 36 }, - * { 'user': 'fred', 'age': 40 } - * ]; - * - * _.min(users, function(chr) { - * return chr.age; - * }); - * // => { 'user': 'barney', 'age': 36 }; - * - * // using the `_.property` callback shorthand - * _.min(users, 'age'); - * // => { 'user': 'barney', 'age': 36 }; - */ - var min = createExtremum(arrayMin, true); - /** * Creates an array of elements split into two groups, the first of which * contains elements `predicate` returns truthy for, while the second of which @@ -6353,7 +6345,7 @@ * _.partition([1.2, 2.3, 3.4], function(n) { * return this.floor(n) % 2; * }, Math); - * // => [[1, 3], [2]] + * // => [[1.2, 3.4], [2.3]] * * var users = [ * { 'user': 'barney', 'age': 36, 'active': false }, @@ -6593,8 +6585,8 @@ } /** - * Gets the size of `collection` by returning `collection.length` for - * array-like values or the number of own enumerable properties for objects. + * Gets the size of `collection` by returning its length for array-like + * values or the number of own enumerable properties for objects. * * @static * @memberOf _ @@ -6724,8 +6716,11 @@ * // => ['barney', 'fred', 'pebbles'] */ function sortBy(collection, iteratee, thisArg) { + if (collection == null) { + return []; + } var index = -1, - length = collection ? collection.length : 0, + length = collection.length, result = isLength(length) ? Array(length) : []; if (thisArg && isIterateeCall(collection, iteratee, thisArg)) { @@ -6762,25 +6757,58 @@ * // => [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]] */ function sortByAll(collection) { - var args = arguments; - if (args.length > 3 && isIterateeCall(args[1], args[2], args[3])) { + if (collection == null) { + return []; + } + var args = arguments, + guard = args[3]; + + if (guard && isIterateeCall(args[1], args[2], guard)) { args = [collection, args[1]]; } - var index = -1, - length = collection ? collection.length : 0, - props = baseFlatten(args, false, false, 1), - result = isLength(length) ? Array(length) : []; + return baseSortByOrder(collection, baseFlatten(args, false, false, 1), []); + } - baseEach(collection, function(value) { - var length = props.length, - criteria = Array(length); - - while (length--) { - criteria[length] = value == null ? undefined : value[props[length]]; - } - result[++index] = { 'criteria': criteria, 'index': index, 'value': value }; - }); - return baseSortBy(result, compareMultipleAscending); + /** + * This method is like `_.sortByAll` except that it allows specifying the + * sort orders of the property names to sort by. A truthy value in `orders` + * will sort the corresponding property name in ascending order while a + * falsey value will sort it in descending order. + * + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object|string} collection The collection to iterate over. + * @param {string[]} props The property names to sort by. + * @param {boolean[]} orders The sort orders of `props`. + * @returns {Array} Returns the new sorted array. + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 }, + * { 'user': 'barney', 'age': 26 }, + * { 'user': 'fred', 'age': 30 } + * ]; + * + * // sort by `user` in ascending order and by `age` in descending order + * _.map(_.sortByOrder(users, ['user', 'age'], [true, false]), _.values); + * // => [['barney', 36], ['barney', 26], ['fred', 40], ['fred', 30]] + */ + function sortByOrder(collection, props, orders, guard) { + if (collection == null) { + return []; + } + if (guard && isIterateeCall(props, orders, guard)) { + orders = null; + } + if (!isArray(props)) { + props = props == null ? [] : [props]; + } + if (!isArray(orders)) { + orders = orders == null ? [] : [orders]; + } + return baseSortByOrder(collection, props, orders); } /** @@ -7403,38 +7431,15 @@ * @returns {Function} Returns the new function. * @example * - * function add(x, y) { - * return x + y; - * } - * * function square(n) { * return n * n; * } * - * var addSquare = _.flow(add, square); + * var addSquare = _.flow(_.add, square); * addSquare(1, 2); * // => 9 */ - function flow() { - var funcs = arguments, - length = funcs.length; - - if (!length) { - return function() { return arguments[0]; }; - } - if (!arrayEvery(funcs, baseIsFunction)) { - throw new TypeError(FUNC_ERROR_TEXT); - } - return function() { - var index = 0, - result = funcs[index].apply(this, arguments); - - while (++index < length) { - result = funcs[index].call(this, result); - } - return result; - }; - } + var flow = createComposer(); /** * This method is like `_.flow` except that it creates a function that @@ -7448,38 +7453,15 @@ * @returns {Function} Returns the new function. * @example * - * function add(x, y) { - * return x + y; - * } - * * function square(n) { * return n * n; * } * - * var addSquare = _.flowRight(square, add); + * var addSquare = _.flowRight(square, _.add); * addSquare(1, 2); * // => 9 */ - function flowRight() { - var funcs = arguments, - fromIndex = funcs.length - 1; - - if (fromIndex < 0) { - return function() { return arguments[0]; }; - } - if (!arrayEvery(funcs, baseIsFunction)) { - throw new TypeError(FUNC_ERROR_TEXT); - } - return function() { - var index = fromIndex, - result = funcs[index].apply(this, arguments); - - while (index--) { - result = funcs[index].call(this, result); - } - return result; - }; - } + var flowRight = createComposer(true); /** * Creates a function that memoizes the result of `func`. If `resolver` is @@ -7539,13 +7521,14 @@ throw new TypeError(FUNC_ERROR_TEXT); } var memoized = function() { - var cache = memoized.cache, - key = resolver ? resolver.apply(this, arguments) : arguments[0]; + var args = arguments, + cache = memoized.cache, + key = resolver ? resolver.apply(this, args) : args[0]; if (cache.has(key)) { return cache.get(key); } - var result = func.apply(this, arguments); + var result = func.apply(this, args); cache.set(key, result); return result; }; @@ -8060,7 +8043,7 @@ } /** - * Checks if a value is empty. A value is considered empty unless it is an + * Checks if `value` is empty. A value is considered empty unless it is an * `arguments` object, array, string, or jQuery-like collection with a length * greater than `0` or an object with own enumerable properties. * @@ -8675,7 +8658,7 @@ /** * Assigns own enumerable properties of source object(s) to the destination * object for all destination properties that resolve to `undefined`. Once a - * property is set, additional defaults of the same property are ignored. + * property is set, additional values of the same property are ignored. * * @static * @memberOf _ @@ -9465,7 +9448,7 @@ /** * Checks if `n` is between `start` and up to but not including, `end`. If - * `end` is not specified it defaults to `start` with `start` becoming `0`. + * `end` is not specified it is set to `start` with `start` then set to `0`. * * @static * @memberOf _ @@ -9663,7 +9646,7 @@ } /** - * Converts the characters "&", "<", ">", '"', "'", and '`', in `string` to + * Converts the characters "&", "<", ">", '"', "'", and "\`", in `string` to * their corresponding HTML entities. * * **Note:** No other characters are escaped. To escape additional characters @@ -10466,14 +10449,14 @@ * } */ function attempt() { - var length = arguments.length, - func = arguments[0]; + var func = arguments[0], + length = arguments.length, + args = Array(length ? length - 1 : 0); + while (--length > 0) { + args[length - 1] = arguments[length]; + } try { - var args = Array(length ? length - 1 : 0); - while (--length > 0) { - args[length - 1] = arguments[length]; - } return func.apply(undefined, args); } catch(e) { return isError(e) ? e : new Error(e); @@ -10802,9 +10785,9 @@ /** * Creates an array of numbers (positive and/or negative) progressing from - * `start` up to, but not including, `end`. If `end` is not specified it - * defaults to `start` with `start` becoming `0`. If `start` is less than - * `end` a zero-length range is created unless a negative `step` is specified. + * `start` up to, but not including, `end`. If `end` is not specified it is + * set to `start` with `start` then set to `0`. If `start` is less than `end` + * a zero-length range is created unless a negative `step` is specified. * * @static * @memberOf _ @@ -10931,6 +10914,153 @@ /*------------------------------------------------------------------------*/ + /** + * Adds two numbers. + * + * @static + * @memberOf _ + * @category Math + * @param {number} augend The first number to add. + * @param {number} addend The second number to add. + * @returns {number} Returns the sum. + * @example + * + * _.add(6, 4); + * // => 10 + */ + function add(augend, addend) { + return augend + addend; + } + + /** + * Gets the maximum value of `collection`. If `collection` is empty or falsey + * `-Infinity` is returned. If an iteratee function is provided it is invoked + * for each value in `collection` to generate the criterion by which the value + * is ranked. The `iteratee` is bound to `thisArg` and invoked with three + * arguments; (value, index, collection). + * + * If a property name is provided for `predicate` the created `_.property` + * style callback returns the property value of the given element. + * + * If a value is also provided for `thisArg` the created `_.matchesProperty` + * style callback returns `true` for elements that have a matching property + * value, else `false`. + * + * If an object is provided for `predicate` the created `_.matches` style + * callback returns `true` for elements that have the properties of the given + * object, else `false`. + * + * @static + * @memberOf _ + * @category Math + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function|Object|string} [iteratee] The function invoked per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {*} Returns the maximum value. + * @example + * + * _.max([4, 2, 8, 6]); + * // => 8 + * + * _.max([]); + * // => -Infinity + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 } + * ]; + * + * _.max(users, function(chr) { + * return chr.age; + * }); + * // => { 'user': 'fred', 'age': 40 }; + * + * // using the `_.property` callback shorthand + * _.max(users, 'age'); + * // => { 'user': 'fred', 'age': 40 }; + */ + var max = createExtremum(arrayMax); + + /** + * Gets the minimum value of `collection`. If `collection` is empty or falsey + * `Infinity` is returned. If an iteratee function is provided it is invoked + * for each value in `collection` to generate the criterion by which the value + * is ranked. The `iteratee` is bound to `thisArg` and invoked with three + * arguments; (value, index, collection). + * + * If a property name is provided for `predicate` the created `_.property` + * style callback returns the property value of the given element. + * + * If a value is also provided for `thisArg` the created `_.matchesProperty` + * style callback returns `true` for elements that have a matching property + * value, else `false`. + * + * If an object is provided for `predicate` the created `_.matches` style + * callback returns `true` for elements that have the properties of the given + * object, else `false`. + * + * @static + * @memberOf _ + * @category Math + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function|Object|string} [iteratee] The function invoked per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {*} Returns the minimum value. + * @example + * + * _.min([4, 2, 8, 6]); + * // => 2 + * + * _.min([]); + * // => Infinity + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 } + * ]; + * + * _.min(users, function(chr) { + * return chr.age; + * }); + * // => { 'user': 'barney', 'age': 36 }; + * + * // using the `_.property` callback shorthand + * _.min(users, 'age'); + * // => { 'user': 'barney', 'age': 36 }; + */ + var min = createExtremum(arrayMin, true); + + /** + * Gets the sum of the values in `collection`. + * + * @static + * @memberOf _ + * @category Math + * @param {Array|Object|string} collection The collection to iterate over. + * @returns {number} Returns the sum. + * @example + * + * _.sum([4, 6, 2]); + * // => 12 + * + * _.sum({ 'a': 4, 'b': 6, 'c': 2 }); + * // => 12 + */ + function sum(collection) { + if (!isArray(collection)) { + collection = toIterable(collection); + } + var length = collection.length, + result = 0; + + while (length--) { + result += +collection[length] || 0; + } + return result; + } + + /*------------------------------------------------------------------------*/ + // Ensure wrappers are instances of `baseLodash`. lodash.prototype = baseLodash.prototype; @@ -11029,6 +11159,7 @@ lodash.slice = slice; lodash.sortBy = sortBy; lodash.sortByAll = sortByAll; + lodash.sortByOrder = sortByOrder; lodash.spread = spread; lodash.take = take; lodash.takeRight = takeRight; @@ -11073,6 +11204,7 @@ /*------------------------------------------------------------------------*/ // Add functions that return unwrapped values when chaining. + lodash.add = add; lodash.attempt = attempt; lodash.camelCase = camelCase; lodash.capitalize = capitalize; @@ -11142,6 +11274,7 @@ lodash.sortedLastIndex = sortedLastIndex; lodash.startCase = startCase; lodash.startsWith = startsWith; + lodash.sum = sum; lodash.template = template; lodash.trim = trim; lodash.trimLeft = trimLeft; @@ -11203,15 +11336,17 @@ }); // Add `LazyWrapper` methods that accept an `iteratee` value. - arrayEach(['filter', 'map', 'takeWhile'], function(methodName, index) { - var isFilter = index == LAZY_FILTER_FLAG || index == LAZY_WHILE_FLAG; + arrayEach(['dropWhile', 'filter', 'map', 'takeWhile'], function(methodName, type) { + var isFilter = type != LAZY_MAP_FLAG, + isDropWhile = type == LAZY_DROP_WHILE_FLAG; LazyWrapper.prototype[methodName] = function(iteratee, thisArg) { - var result = this.clone(), + var filtered = this.__filtered__, + result = (filtered && isDropWhile) ? new LazyWrapper(this) : this.clone(), iteratees = result.__iteratees__ || (result.__iteratees__ = []); - result.__filtered__ = result.__filtered__ || isFilter; - iteratees.push({ 'iteratee': getCallback(iteratee, thisArg, 3), 'type': index }); + result.__filtered__ = filtered || isFilter; + iteratees.push({ 'done': false, 'index': 0, 'iteratee': getCallback(iteratee, thisArg, 1), 'type': type }); return result; }; }); @@ -11276,23 +11411,10 @@ return this.filter(identity); }; - LazyWrapper.prototype.dropWhile = function(predicate, thisArg) { - var done, - lastIndex, - isRight = this.__dir__ < 0; - - predicate = getCallback(predicate, thisArg, 3); - return this.filter(function(value, index, array) { - done = done && (isRight ? index < lastIndex : index > lastIndex); - lastIndex = index; - return done || (done = !predicate(value, index, array)); - }); - }; - LazyWrapper.prototype.reject = function(predicate, thisArg) { - predicate = getCallback(predicate, thisArg, 3); - return this.filter(function(value, index, array) { - return !predicate(value, index, array); + predicate = getCallback(predicate, thisArg, 1); + return this.filter(function(value) { + return !predicate(value); }); }; @@ -11314,16 +11436,24 @@ // Add `LazyWrapper` methods to `lodash.prototype`. baseForOwn(LazyWrapper.prototype, function(func, methodName) { var lodashFunc = lodash[methodName], + checkIteratee = /^(?:filter|map|reject)|While$/.test(methodName), retUnwrapped = /^(?:first|last)$/.test(methodName); lodash.prototype[methodName] = function() { - var value = this.__wrapped__, - args = arguments, + var args = arguments, + length = args.length, chainAll = this.__chain__, + value = this.__wrapped__, isHybrid = !!this.__actions__.length, isLazy = value instanceof LazyWrapper, - onlyLazy = isLazy && !isHybrid; + iteratee = args[0], + useLazy = isLazy || isArray(value); + if (useLazy && checkIteratee && typeof iteratee == 'function' && iteratee.length != 1) { + // avoid lazy use if the iteratee has a `length` other than `1` + isLazy = useLazy = false; + } + var onlyLazy = isLazy && !isHybrid; if (retUnwrapped && !chainAll) { return onlyLazy ? func.call(value) @@ -11334,7 +11464,7 @@ push.apply(otherArgs, args); return lodashFunc.apply(lodash, otherArgs); }; - if (isLazy || isArray(value)) { + if (useLazy) { var wrapper = onlyLazy ? value : new LazyWrapper(this), result = func.apply(wrapper, args); diff --git a/math.js b/math.js new file mode 100644 index 000000000..a0f97cba9 --- /dev/null +++ b/math.js @@ -0,0 +1,8 @@ +define(['./math/add', './math/max', './math/min', './math/sum'], function(add, max, min, sum) { + return { + 'add': add, + 'max': max, + 'min': min, + 'sum': sum + }; +}); diff --git a/math/add.js b/math/add.js new file mode 100644 index 000000000..aeef3402a --- /dev/null +++ b/math/add.js @@ -0,0 +1,22 @@ +define([], function() { + + /** + * Adds two numbers. + * + * @static + * @memberOf _ + * @category Math + * @param {number} augend The first number to add. + * @param {number} addend The second number to add. + * @returns {number} Returns the sum. + * @example + * + * _.add(6, 4); + * // => 10 + */ + function add(augend, addend) { + return augend + addend; + } + + return add; +}); diff --git a/math/max.js b/math/max.js new file mode 100644 index 000000000..0b691c9b4 --- /dev/null +++ b/math/max.js @@ -0,0 +1,53 @@ +define(['../internal/arrayMax', '../internal/createExtremum'], function(arrayMax, createExtremum) { + + /** + * Gets the maximum value of `collection`. If `collection` is empty or falsey + * `-Infinity` is returned. If an iteratee function is provided it is invoked + * for each value in `collection` to generate the criterion by which the value + * is ranked. The `iteratee` is bound to `thisArg` and invoked with three + * arguments; (value, index, collection). + * + * If a property name is provided for `predicate` the created `_.property` + * style callback returns the property value of the given element. + * + * If a value is also provided for `thisArg` the created `_.matchesProperty` + * style callback returns `true` for elements that have a matching property + * value, else `false`. + * + * If an object is provided for `predicate` the created `_.matches` style + * callback returns `true` for elements that have the properties of the given + * object, else `false`. + * + * @static + * @memberOf _ + * @category Math + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function|Object|string} [iteratee] The function invoked per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {*} Returns the maximum value. + * @example + * + * _.max([4, 2, 8, 6]); + * // => 8 + * + * _.max([]); + * // => -Infinity + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 } + * ]; + * + * _.max(users, function(chr) { + * return chr.age; + * }); + * // => { 'user': 'fred', 'age': 40 }; + * + * // using the `_.property` callback shorthand + * _.max(users, 'age'); + * // => { 'user': 'fred', 'age': 40 }; + */ + var max = createExtremum(arrayMax); + + return max; +}); diff --git a/math/min.js b/math/min.js new file mode 100644 index 000000000..9652a8a39 --- /dev/null +++ b/math/min.js @@ -0,0 +1,53 @@ +define(['../internal/arrayMin', '../internal/createExtremum'], function(arrayMin, createExtremum) { + + /** + * Gets the minimum value of `collection`. If `collection` is empty or falsey + * `Infinity` is returned. If an iteratee function is provided it is invoked + * for each value in `collection` to generate the criterion by which the value + * is ranked. The `iteratee` is bound to `thisArg` and invoked with three + * arguments; (value, index, collection). + * + * If a property name is provided for `predicate` the created `_.property` + * style callback returns the property value of the given element. + * + * If a value is also provided for `thisArg` the created `_.matchesProperty` + * style callback returns `true` for elements that have a matching property + * value, else `false`. + * + * If an object is provided for `predicate` the created `_.matches` style + * callback returns `true` for elements that have the properties of the given + * object, else `false`. + * + * @static + * @memberOf _ + * @category Math + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function|Object|string} [iteratee] The function invoked per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {*} Returns the minimum value. + * @example + * + * _.min([4, 2, 8, 6]); + * // => 2 + * + * _.min([]); + * // => Infinity + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 } + * ]; + * + * _.min(users, function(chr) { + * return chr.age; + * }); + * // => { 'user': 'barney', 'age': 36 }; + * + * // using the `_.property` callback shorthand + * _.min(users, 'age'); + * // => { 'user': 'barney', 'age': 36 }; + */ + var min = createExtremum(arrayMin, true); + + return min; +}); diff --git a/math/sum.js b/math/sum.js new file mode 100644 index 000000000..4f60c925d --- /dev/null +++ b/math/sum.js @@ -0,0 +1,33 @@ +define(['../lang/isArray', '../internal/toIterable'], function(isArray, toIterable) { + + /** + * Gets the sum of the values in `collection`. + * + * @static + * @memberOf _ + * @category Math + * @param {Array|Object|string} collection The collection to iterate over. + * @returns {number} Returns the sum. + * @example + * + * _.sum([4, 6, 2]); + * // => 12 + * + * _.sum({ 'a': 4, 'b': 6, 'c': 2 }); + * // => 12 + */ + function sum(collection) { + if (!isArray(collection)) { + collection = toIterable(collection); + } + var length = collection.length, + result = 0; + + while (length--) { + result += +collection[length] || 0; + } + return result; + } + + return sum; +}); diff --git a/number/inRange.js b/number/inRange.js index b2c1e4a89..4854fefb2 100644 --- a/number/inRange.js +++ b/number/inRange.js @@ -2,7 +2,7 @@ define([], function() { /** * Checks if `n` is between `start` and up to but not including, `end`. If - * `end` is not specified it defaults to `start` with `start` becoming `0`. + * `end` is not specified it is set to `start` with `start` then set to `0`. * * @static * @memberOf _ diff --git a/object/defaults.js b/object/defaults.js index 1f10b78e4..c6a03a60d 100644 --- a/object/defaults.js +++ b/object/defaults.js @@ -6,7 +6,7 @@ define(['../internal/arrayCopy', './assign', '../internal/assignDefaults'], func /** * Assigns own enumerable properties of source object(s) to the destination * object for all destination properties that resolve to `undefined`. Once a - * property is set, additional defaults of the same property are ignored. + * property is set, additional values of the same property are ignored. * * @static * @memberOf _ diff --git a/package.json b/package.json index afefc135f..c35adf5e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lodash", - "version": "3.3.1", + "version": "3.4.0", "main": "main.js", "private": true, "volo": { diff --git a/string/escape.js b/string/escape.js index 3fadeaec7..8df24b198 100644 --- a/string/escape.js +++ b/string/escape.js @@ -5,7 +5,7 @@ define(['../internal/baseToString', '../internal/escapeHtmlChar'], function(base reHasUnescapedHtml = RegExp(reUnescapedHtml.source); /** - * Converts the characters "&", "<", ">", '"', "'", and '`', in `string` to + * Converts the characters "&", "<", ">", '"', "'", and "\`", in `string` to * their corresponding HTML entities. * * **Note:** No other characters are escaped. To escape additional characters diff --git a/string/words.js b/string/words.js index 06254d9a5..0f06f2fe5 100644 --- a/string/words.js +++ b/string/words.js @@ -5,7 +5,7 @@ define(['../internal/baseToString', '../internal/isIterateeCall'], function(base var upper = '[A-Z\\xc0-\\xd6\\xd8-\\xde]', lower = '[a-z\\xdf-\\xf6\\xf8-\\xff]+'; - return RegExp(upper + '{2,}(?=' + upper + lower + ')|' + upper + '?' + lower + '|' + upper + '+|[0-9]+', 'g'); + return RegExp(upper + '+(?=' + upper + lower + ')|' + upper + '?' + lower + '|' + upper + '+|[0-9]+', 'g'); }()); /** diff --git a/utility/attempt.js b/utility/attempt.js index ef66b491c..95975e14a 100644 --- a/utility/attempt.js +++ b/utility/attempt.js @@ -24,14 +24,14 @@ define(['../lang/isError'], function(isError) { * } */ function attempt() { - var length = arguments.length, - func = arguments[0]; + var func = arguments[0], + length = arguments.length, + args = Array(length ? length - 1 : 0); + while (--length > 0) { + args[length - 1] = arguments[length]; + } try { - var args = Array(length ? length - 1 : 0); - while (--length > 0) { - args[length - 1] = arguments[length]; - } return func.apply(undefined, args); } catch(e) { return isError(e) ? e : new Error(e); diff --git a/utility/range.js b/utility/range.js index 3fe0bb006..9bfeaf876 100644 --- a/utility/range.js +++ b/utility/range.js @@ -8,9 +8,9 @@ define(['../internal/isIterateeCall'], function(isIterateeCall) { /** * Creates an array of numbers (positive and/or negative) progressing from - * `start` up to, but not including, `end`. If `end` is not specified it - * defaults to `start` with `start` becoming `0`. If `start` is less than - * `end` a zero-length range is created unless a negative `step` is specified. + * `start` up to, but not including, `end`. If `end` is not specified it is + * set to `start` with `start` then set to `0`. If `start` is less than `end` + * a zero-length range is created unless a negative `step` is specified. * * @static * @memberOf _