vendor/assets/javascripts/lodash.compat.js in lodash-rails-3.3.1.1 vs vendor/assets/javascripts/lodash.compat.js in lodash-rails-3.4.0

- old
+ new

@@ -1,8 +1,8 @@ /** * @license - * lodash 3.3.1 (Custom Build) <https://lodash.com/> + * lodash 3.4.0 (Custom Build) <https://lodash.com/> * Build: `lodash compat -o ./lodash.js` * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/> * Based on Underscore.js 1.8.2 <http://underscorejs.org/LICENSE> * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors * Available under MIT license <https://lodash.com/license> @@ -11,11 +11,11 @@ /** Used as a safe reference for `undefined` in pre-ES5 environments. */ 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, BIND_KEY_FLAG = 2, CURRY_BOUND_FLAG = 4, @@ -33,13 +33,13 @@ /** Used to detect when a function becomes hot. */ var HOT_COUNT = 150, 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'; /** Used as the internal argument placeholder. */ @@ -129,11 +129,11 @@ /** Used to match words to create compound words. */ var reWords = (function() { 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. */ var whitespace = ( // Basic whitespace characters. @@ -257,33 +257,33 @@ '\r': 'r', '\u2028': 'u2028', '\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; + /** Detect the popular CommonJS extension `module.exports`. */ var moduleExports = freeModule && freeModule.exports === freeExports && freeExports; + /** + * 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; + /*--------------------------------------------------------------------------*/ /** * The base implementation of `compareAscending` which compares values and * sorts them in ascending order without guaranteeing a stable sort. @@ -312,18 +312,18 @@ * The base implementation of `_.indexOf` without support for binary searches. * * @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) { if (array[index] === value) { return index; @@ -345,30 +345,10 @@ // See https://github.com/jashkenas/underscore/issues/1621 for more details. 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. * * @private * @param {*} value The value to process. @@ -437,28 +417,38 @@ function compareAscending(object, other) { return baseCompareAscending(object.criteria, other.criteria) || (object.index - other.index); } /** - * 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 // that causes it, under certain circumstances, to provide the same value for // `object` and `other`. See https://github.com/jashkenas/underscore/pull/1247 @@ -507,17 +497,17 @@ * Gets the index at which the first occurrence of `NaN` is found in `array`. * If `fromRight` is provided elements of `array` are iterated from right to left. * * @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]; if (other !== other) { return index; @@ -884,30 +874,30 @@ * `groupBy`, `indexBy`, `initial`, `intersection`, `invert`, `invoke`, `keys`, * `keysIn`, `map`, `mapValues`, `matches`, `matchesProperty`, `memoize`, `merge`, * `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. * * @name _ @@ -1291,20 +1281,26 @@ value = array[index]; 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; } } } if (dropCount) { dropCount--; @@ -1983,11 +1979,11 @@ continue outer; } } result.push(value); } - else if (indexOf(values, value) < 0) { + else if (indexOf(values, value, 0) < 0) { result.push(value); } } return result; } @@ -2136,28 +2132,28 @@ * The base implementation of `_.flatten` with added support for restricting * flattening and specifying the start index. * * @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 = []; while (++index < length) { var value = array[index]; 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; result.length += valLength; @@ -2768,10 +2764,59 @@ }); 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. * * @private * @param {Array} array The array to inspect. @@ -2809,11 +2854,11 @@ if (iteratee) { seen.push(computed); } result.push(value); } - else if (indexOf(seen, computed) < 0) { + else if (indexOf(seen, computed, 0) < 0) { if (iteratee || isLarge) { seen.push(computed); } result.push(value); } @@ -3113,28 +3158,35 @@ * @param {Function} assigner The function to assign values. * @returns {Function} Returns the new assigner function. */ 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])) { + 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); + } + if (guard && isIterateeCall(args[1], args[2], guard)) { + customizer = length == 3 ? null : customizer; length = 2; } - // 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]; - } var index = 0; while (++index < length) { - var source = arguments[index]; + var source = args[index]; if (source) { assigner(object, source, customizer); } } return object; @@ -3169,10 +3221,45 @@ var createCache = !(nativeCreate && Set) ? constant(null) : function(values) { 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. * * @private * @param {Function} callback The function to combine each word. @@ -4263,20 +4350,21 @@ * * _.difference([1, 2, 3], [4, 2]); * // => [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)); } /** * Creates a slice of `array` with `n` elements dropped from the beginning. * @@ -4384,11 +4472,11 @@ * { 'user': 'fred', 'active': false }, * { 'user': 'pebbles', 'active': false } * ]; * * // 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 * _.pluck(_.dropRightWhile(users, 'active', false), 'user'); * // => ['barney'] @@ -4595,11 +4683,11 @@ * _.findLastIndex(users, { 'user': 'barney', 'active': true }); * // => 0 * * // using the `_.matchesProperty` callback shorthand * _.findLastIndex(users, 'active', false); - * // => 1 + * // => 2 * * // using the `_.property` callback shorthand * _.findLastIndex(users, 'active'); * // => 0 */ @@ -4658,11 +4746,11 @@ function flatten(array, isDeep, guard) { var length = array ? array.length : 0; if (guard && isIterateeCall(array, isDeep, guard)) { isDeep = false; } - return length ? baseFlatten(array, isDeep) : []; + return length ? baseFlatten(array, isDeep, false, 0) : []; } /** * Recursively flattens a nested array. * @@ -4676,11 +4764,11 @@ * _.flattenDeep([1, [2, 3, [4]]]); * // => [1, 2, 3, 4]; */ function flattenDeep(array) { var length = array ? array.length : 0; - return length ? baseFlatten(array, true) : []; + return length ? baseFlatten(array, true, false, 0) : []; } /** * Gets the index at which the first occurrence of `value` is found in `array` * using `SameValueZero` for equality comparisons. If `fromIndex` is negative, @@ -4717,18 +4805,18 @@ var length = array ? array.length : 0; if (!length) { 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); } /** * Gets all but the last element of `array`. * @@ -4787,15 +4875,15 @@ seen = caches[0]; 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; } } if (seen) { seen.push(value); @@ -4896,21 +4984,23 @@ * _.pull(array, 2, 3); * console.log(array); * // => [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); } } @@ -5336,11 +5426,11 @@ * * _.union([1, 2], [4, 2], [2, 1]); * // => [1, 2, 4] */ function union() { - return baseUniq(baseFlatten(arguments, false, true)); + return baseUniq(baseFlatten(arguments, false, true, 0)); } /** * Creates a duplicate-value-free version of an array using `SameValueZero` * for equality comparisons. Providing `true` for `isSorted` performs a faster @@ -6408,108 +6498,10 @@ iteratee = getCallback(iteratee, thisArg, 3); 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 * contains elements `predicate` returns falsey for. The predicate is bound * to `thisArg` and invoked with three arguments; (value, index|key, collection). * @@ -6540,11 +6532,11 @@ * // => [[1, 3], [2]] * * _.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 }, * { 'user': 'fred', 'age': 40, 'active': true }, * { 'user': 'pebbles', 'age': 1, 'active': false } @@ -6780,12 +6772,12 @@ } return result; } /** - * 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 _ * @category Collection * @param {Array|Object|string} collection The collection to inspect. @@ -6911,12 +6903,15 @@ * // using the `_.property` callback shorthand * _.pluck(_.sortBy(users, 'user'), 'user'); * // => ['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)) { iteratee = null; } @@ -6949,29 +6944,62 @@ * * _.map(_.sortByAll(users, ['user', 'age']), _.values); * // => [['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); } /** * Performs a deep comparison between each element in `collection` and the * source object, returning an array of all elements that have equivalent @@ -7590,43 +7618,20 @@ * @category Function * @param {...Function} [funcs] Functions to invoke. * @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; + var flow = createComposer(); - 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; - }; - } - /** * This method is like `_.flow` except that it creates a function that * invokes the provided functions from right to left. * * @static @@ -7635,43 +7640,20 @@ * @category Function * @param {...Function} [funcs] Functions to invoke. * @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; + var flowRight = createComposer(true); - 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; - }; - } - /** * Creates a function that memoizes the result of `func`. If `resolver` is * provided it determines the cache key for storing the result based on the * arguments provided to the memoized function. By default, the first argument * provided to the memoized function is coerced to a string and used as the @@ -7726,17 +7708,18 @@ function memoize(func, resolver) { if (typeof func != 'function' || (resolver && typeof resolver != 'function')) { 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; }; memoized.cache = new memoize.Cache; return memoized; @@ -8255,11 +8238,11 @@ return (value && value.nodeType === 1 && isObjectLike(value) && !isPlainObject(value)) || false; }; } /** - * 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. * * @static * @memberOf _ @@ -8873,11 +8856,11 @@ } /** * 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 _ * @category Object * @param {Object} object The destination object. @@ -9692,11 +9675,11 @@ /*------------------------------------------------------------------------*/ /** * 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 _ * @category Number * @param {number} n The number to check. @@ -9890,11 +9873,11 @@ position = (typeof position == 'undefined' ? length : nativeMin(position < 0 ? 0 : (+position || 0), length)) - target.length; return position >= 0 && string.indexOf(target, position) == position; } /** - * 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 * use a third-party library like [_he_](https://mths.be/he). * @@ -10693,18 +10676,18 @@ * if (_.isError(elements)) { * elements = []; * } */ 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); } } @@ -11029,13 +11012,13 @@ }; } /** * 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 _ * @category Utility * @param {number} [start=0] The start of the range. @@ -11158,10 +11141,157 @@ return baseToString(prefix) + id; } /*------------------------------------------------------------------------*/ + /** + * 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; LodashWrapper.prototype = baseCreate(baseLodash.prototype); LodashWrapper.prototype.constructor = LodashWrapper; @@ -11256,10 +11386,11 @@ lodash.rest = rest; lodash.shuffle = shuffle; lodash.slice = slice; lodash.sortBy = sortBy; lodash.sortByAll = sortByAll; + lodash.sortByOrder = sortByOrder; lodash.spread = spread; lodash.take = take; lodash.takeRight = takeRight; lodash.takeRightWhile = takeRightWhile; lodash.takeWhile = takeWhile; @@ -11300,10 +11431,11 @@ mixin(lodash, lodash); /*------------------------------------------------------------------------*/ // Add functions that return unwrapped values when chaining. + lodash.add = add; lodash.attempt = attempt; lodash.camelCase = camelCase; lodash.capitalize = capitalize; lodash.clone = clone; lodash.cloneDeep = cloneDeep; @@ -11369,10 +11501,11 @@ lodash.some = some; lodash.sortedIndex = sortedIndex; lodash.sortedLastIndex = sortedLastIndex; lodash.startCase = startCase; lodash.startsWith = startsWith; + lodash.sum = sum; lodash.template = template; lodash.trim = trim; lodash.trimLeft = trimLeft; lodash.trimRight = trimRight; lodash.trunc = trunc; @@ -11430,19 +11563,21 @@ arrayEach(['bind', 'bindKey', 'curry', 'curryRight', 'partial', 'partialRight'], function(methodName) { lodash[methodName].placeholder = lodash; }); // 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; }; }); // Add `LazyWrapper` methods for `_.drop` and `_.take` variants. @@ -11503,27 +11638,14 @@ LazyWrapper.prototype.compact = function() { 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); }); }; LazyWrapper.prototype.slice = function(start, end) { start = start == null ? 0 : (+start || 0); @@ -11541,30 +11663,38 @@ }; // 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) : lodashFunc.call(lodash, this.value()); } var interceptor = function(value) { var otherArgs = [value]; 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); if (!retUnwrapped && (isHybrid || result.__actions__)) { var actions = result.__actions__ || (result.__actions__ = []);