diff --git a/README.md b/README.md index b7ce5be9a..8ba235379 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# lodash-es v3.7.0 +# lodash-es v3.8.0 The [modern build](https://github.com/lodash/lodash/wiki/Build-Differences) of [lodash](https://lodash.com/) exported as [ES](https://people.mozilla.org/~jorendorff/es6-draft.html) modules. @@ -7,4 +7,4 @@ Generated using [lodash-cli](https://www.npmjs.com/package/lodash-cli): $ lodash modularize modern exports=es -o ./ ``` -See the [package source](https://github.com/lodash/lodash/tree/3.7.0-es) for more details. +See the [package source](https://github.com/lodash/lodash/tree/3.8.0-es) for more details. diff --git a/array.js b/array.js index 93303c6d2..b1f14a58d 100644 --- a/array.js +++ b/array.js @@ -34,10 +34,12 @@ import union from './array/union'; import uniq from './array/uniq'; import unique from './array/unique'; import unzip from './array/unzip'; +import unzipWith from './array/unzipWith'; import without from './array/without'; import xor from './array/xor'; import zip from './array/zip'; import zipObject from './array/zipObject'; +import zipWith from './array/zipWith'; export default { 'chunk': chunk, @@ -76,8 +78,10 @@ export default { 'uniq': uniq, 'unique': unique, 'unzip': unzip, + 'unzipWith': unzipWith, 'without': without, 'xor': xor, 'zip': zip, - 'zipObject': zipObject + 'zipObject': zipObject, + 'zipWith': zipWith }; diff --git a/array/difference.js b/array/difference.js index bec97e536..be2e45869 100644 --- a/array/difference.js +++ b/array/difference.js @@ -1,16 +1,12 @@ import baseDifference from '../internal/baseDifference'; import baseFlatten from '../internal/baseFlatten'; -import isArguments from '../lang/isArguments'; -import isArray from '../lang/isArray'; +import isArrayLike from '../internal/isArrayLike'; import restParam from '../function/restParam'; /** * Creates an array excluding all values of the provided arrays using - * `SameValueZero` for equality comparisons. - * - * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * comparisons are like strict equality comparisons, e.g. `===`, except that - * `NaN` matches `NaN`. + * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) + * for equality comparisons. * * @static * @memberOf _ @@ -24,7 +20,7 @@ import restParam from '../function/restParam'; * // => [1, 3] */ var difference = restParam(function(array, values) { - return (isArray(array) || isArguments(array)) + return isArrayLike(array) ? baseDifference(array, baseFlatten(values, false, true)) : []; }); diff --git a/array/indexOf.js b/array/indexOf.js index 4cea0c160..dd500afb5 100644 --- a/array/indexOf.js +++ b/array/indexOf.js @@ -6,13 +6,10 @@ var nativeMax = Math.max; /** * Gets the index at which the first occurrence of `value` is found in `array` - * using `SameValueZero` for equality comparisons. If `fromIndex` is negative, - * it is used as the offset from the end of `array`. If `array` is sorted - * providing `true` for `fromIndex` performs a faster binary search. - * - * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * comparisons are like strict equality comparisons, e.g. `===`, except that - * `NaN` matches `NaN`. + * using [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) + * for equality comparisons. If `fromIndex` is negative, it is used as the offset + * from the end of `array`. If `array` is sorted providing `true` for `fromIndex` + * performs a faster binary search. * * @static * @memberOf _ diff --git a/array/intersection.js b/array/intersection.js index 018f356a4..9175f6aae 100644 --- a/array/intersection.js +++ b/array/intersection.js @@ -1,17 +1,13 @@ import baseIndexOf from '../internal/baseIndexOf'; import cacheIndexOf from '../internal/cacheIndexOf'; import createCache from '../internal/createCache'; -import isArguments from '../lang/isArguments'; -import isArray from '../lang/isArray'; +import isArrayLike from '../internal/isArrayLike'; /** - * Creates an array of unique values in all provided arrays using `SameValueZero` + * Creates an array of unique values in all provided arrays using + * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) * for equality comparisons. * - * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * comparisons are like strict equality comparisons, e.g. `===`, except that - * `NaN` matches `NaN`. - * * @static * @memberOf _ * @category Array @@ -32,7 +28,7 @@ function intersection() { while (++argsIndex < argsLength) { var value = arguments[argsIndex]; - if (isArray(value) || isArguments(value)) { + if (isArrayLike(value)) { args.push(value); caches.push((isCommon && value.length >= 120) ? createCache(argsIndex && value) : null); } diff --git a/array/pull.js b/array/pull.js index 76ad1e9fa..ed0036990 100644 --- a/array/pull.js +++ b/array/pull.js @@ -7,14 +7,11 @@ var arrayProto = Array.prototype; var splice = arrayProto.splice; /** - * Removes all provided values from `array` using `SameValueZero` for equality - * comparisons. + * Removes all provided values from `array` using + * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) + * for equality comparisons. * - * **Notes:** - * - Unlike `_.without`, this method mutates `array` - * - [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * comparisons are like strict equality comparisons, e.g. `===`, except - * that `NaN` matches `NaN` + * **Note:** Unlike `_.without`, this method mutates `array`. * * @static * @memberOf _ diff --git a/array/pullAt.js b/array/pullAt.js index 3299c9869..92ee3553b 100644 --- a/array/pullAt.js +++ b/array/pullAt.js @@ -30,7 +30,6 @@ import restParam from '../function/restParam'; * // => [10, 20] */ var pullAt = restParam(function(array, indexes) { - array || (array = []); indexes = baseFlatten(indexes); var result = baseAt(array, indexes); diff --git a/array/union.js b/array/union.js index c94dd1e0c..ffe601e42 100644 --- a/array/union.js +++ b/array/union.js @@ -4,11 +4,8 @@ import restParam from '../function/restParam'; /** * Creates an array of unique values, in order, of the provided arrays using - * `SameValueZero` for equality comparisons. - * - * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * comparisons are like strict equality comparisons, e.g. `===`, except that - * `NaN` matches `NaN`. + * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) + * for equality comparisons. * * @static * @memberOf _ diff --git a/array/uniq.js b/array/uniq.js index 02c333459..06352075e 100644 --- a/array/uniq.js +++ b/array/uniq.js @@ -4,8 +4,9 @@ import isIterateeCall from '../internal/isIterateeCall'; import sortedUniq from '../internal/sortedUniq'; /** - * Creates a duplicate-free version of an array, using `SameValueZero` for - * equality comparisons, in which only the first occurence of each element + * Creates a duplicate-free version of an array, using + * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) + * for equality comparisons, in which only the first occurence of each element * is kept. Providing `true` for `isSorted` performs a faster search algorithm * for sorted arrays. If an iteratee function is provided it is invoked for * each element in the array to generate the criterion by which uniqueness @@ -23,10 +24,6 @@ import sortedUniq from '../internal/sortedUniq'; * callback returns `true` for elements that have the properties of the given * object, else `false`. * - * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * comparisons are like strict equality comparisons, e.g. `===`, except that - * `NaN` matches `NaN`. - * * @static * @memberOf _ * @alias unique diff --git a/array/unzip.js b/array/unzip.js index 658d8fe58..841b8174f 100644 --- a/array/unzip.js +++ b/array/unzip.js @@ -1,11 +1,14 @@ +import arrayFilter from '../internal/arrayFilter'; import arrayMap from '../internal/arrayMap'; -import arrayMax from '../internal/arrayMax'; import baseProperty from '../internal/baseProperty'; -import getLength from '../internal/getLength'; +import isArrayLike from '../internal/isArrayLike'; + +/* Native method references for those with the same name as other `lodash` methods. */ +var nativeMax = Math.max; /** * This method is like `_.zip` except that it accepts an array of grouped - * elements and creates an array regrouping the elements to their pre-`_.zip` + * elements and creates an array regrouping the elements to their pre-zip * configuration. * * @static @@ -22,10 +25,19 @@ import getLength from '../internal/getLength'; * // => [['fred', 'barney'], [30, 40], [true, false]] */ function unzip(array) { + if (!(array && array.length)) { + return []; + } var index = -1, - length = (array && array.length && arrayMax(arrayMap(array, getLength))) >>> 0, - result = Array(length); + length = 0; + array = arrayFilter(array, function(group) { + if (isArrayLike(group)) { + length = nativeMax(group.length, length); + return true; + } + }); + var result = Array(length); while (++index < length) { result[index] = arrayMap(array, baseProperty(index)); } diff --git a/array/unzipWith.js b/array/unzipWith.js new file mode 100644 index 000000000..3c623a764 --- /dev/null +++ b/array/unzipWith.js @@ -0,0 +1,41 @@ +import arrayMap from '../internal/arrayMap'; +import arrayReduce from '../internal/arrayReduce'; +import bindCallback from '../internal/bindCallback'; +import unzip from './unzip'; + +/** + * This method is like `_.unzip` except that it accepts an iteratee to specify + * how regrouped values should be combined. The `iteratee` is bound to `thisArg` + * and invoked with four arguments: (accumulator, value, index, group). + * + * @static + * @memberOf _ + * @category Array + * @param {Array} array The array of grouped elements to process. + * @param {Function} [iteratee] The function to combine regrouped values. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Array} Returns the new array of regrouped elements. + * @example + * + * var zipped = _.zip([1, 2], [10, 20], [100, 200]); + * // => [[1, 10, 100], [2, 20, 200]] + * + * _.unzipWith(zipped, _.add); + * // => [3, 30, 300] + */ +function unzipWith(array, iteratee, thisArg) { + var length = array ? array.length : 0; + if (!length) { + return []; + } + var result = unzip(array); + if (iteratee == null) { + return result; + } + iteratee = bindCallback(iteratee, thisArg, 4); + return arrayMap(result, function(group) { + return arrayReduce(group, iteratee, undefined, true); + }); +} + +export default unzipWith; diff --git a/array/without.js b/array/without.js index 2f019c18b..649daf516 100644 --- a/array/without.js +++ b/array/without.js @@ -1,15 +1,11 @@ import baseDifference from '../internal/baseDifference'; -import isArguments from '../lang/isArguments'; -import isArray from '../lang/isArray'; +import isArrayLike from '../internal/isArrayLike'; import restParam from '../function/restParam'; /** - * Creates an array excluding all provided values using `SameValueZero` for - * equality comparisons. - * - * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * comparisons are like strict equality comparisons, e.g. `===`, except that - * `NaN` matches `NaN`. + * Creates an array excluding all provided values using + * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) + * for equality comparisons. * * @static * @memberOf _ @@ -23,7 +19,7 @@ import restParam from '../function/restParam'; * // => [3] */ var without = restParam(function(array, values) { - return (isArray(array) || isArguments(array)) + return isArrayLike(array) ? baseDifference(array, values) : []; }); diff --git a/array/xor.js b/array/xor.js index 9abfbdad6..ddeb59341 100644 --- a/array/xor.js +++ b/array/xor.js @@ -1,7 +1,6 @@ import baseDifference from '../internal/baseDifference'; import baseUniq from '../internal/baseUniq'; -import isArguments from '../lang/isArguments'; -import isArray from '../lang/isArray'; +import isArrayLike from '../internal/isArrayLike'; /** * Creates an array that is the [symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference) @@ -23,7 +22,7 @@ function xor() { while (++index < length) { var array = arguments[index]; - if (isArray(array) || isArguments(array)) { + if (isArrayLike(array)) { var result = result ? baseDifference(result, array).concat(baseDifference(array, result)) : array; diff --git a/array/zipWith.js b/array/zipWith.js new file mode 100644 index 000000000..8c12ba2cb --- /dev/null +++ b/array/zipWith.js @@ -0,0 +1,36 @@ +import restParam from '../function/restParam'; +import unzipWith from './unzipWith'; + +/** + * This method is like `_.zip` except that it accepts an iteratee to specify + * how grouped values should be combined. The `iteratee` is bound to `thisArg` + * and invoked with four arguments: (accumulator, value, index, group). + * + * @static + * @memberOf _ + * @category Array + * @param {...Array} [arrays] The arrays to process. + * @param {Function} [iteratee] The function to combine grouped values. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Array} Returns the new array of grouped elements. + * @example + * + * _.zipWith([1, 2], [10, 20], [100, 200], _.add); + * // => [111, 222] + */ +var zipWith = restParam(function(arrays) { + var length = arrays.length, + iteratee = arrays[length - 2], + thisArg = arrays[length - 1]; + + if (length > 2 && typeof iteratee == 'function') { + length -= 2; + } else { + iteratee = (length > 1 && typeof thisArg == 'function') ? (--length, thisArg) : undefined; + thisArg = undefined; + } + arrays.length = length; + return unzipWith(arrays, iteratee, thisArg); +}); + +export default zipWith; diff --git a/collection/at.js b/collection/at.js index 2e4ef4598..a0135c59e 100644 --- a/collection/at.js +++ b/collection/at.js @@ -1,9 +1,6 @@ import baseAt from '../internal/baseAt'; import baseFlatten from '../internal/baseFlatten'; -import getLength from '../internal/getLength'; -import isLength from '../internal/isLength'; import restParam from '../function/restParam'; -import toIterable from '../internal/toIterable'; /** * Creates an array of elements corresponding to the given keys, or indexes, @@ -26,10 +23,6 @@ import toIterable from '../internal/toIterable'; * // => ['barney', 'pebbles'] */ var at = restParam(function(collection, props) { - var length = collection ? getLength(collection) : 0; - if (isLength(length)) { - collection = toIterable(collection); - } return baseAt(collection, baseFlatten(props)); }); diff --git a/collection/includes.js b/collection/includes.js index 1fdc04c17..47becc215 100644 --- a/collection/includes.js +++ b/collection/includes.js @@ -10,13 +10,10 @@ import values from '../object/values'; var nativeMax = Math.max; /** - * Checks if `value` is in `collection` using `SameValueZero` for equality - * comparisons. If `fromIndex` is negative, it is used as the offset from - * the end of `collection`. - * - * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * comparisons are like strict equality comparisons, e.g. `===`, except that - * `NaN` matches `NaN`. + * Checks if `value` is in `collection` using + * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) + * for equality comparisons. If `fromIndex` is negative, it is used as the offset + * from the end of `collection`. * * @static * @memberOf _ diff --git a/collection/invoke.js b/collection/invoke.js index 341f6706d..7e5121de8 100644 --- a/collection/invoke.js +++ b/collection/invoke.js @@ -1,8 +1,7 @@ import baseEach from '../internal/baseEach'; -import getLength from '../internal/getLength'; import invokePath from '../internal/invokePath'; +import isArrayLike from '../internal/isArrayLike'; import isKey from '../internal/isKey'; -import isLength from '../internal/isLength'; import restParam from '../function/restParam'; /** @@ -31,8 +30,7 @@ var invoke = restParam(function(collection, path, args) { var index = -1, isFunc = typeof path == 'function', isProp = isKey(path), - length = getLength(collection), - result = isLength(length) ? Array(length) : []; + result = isArrayLike(collection) ? Array(collection.length) : []; baseEach(collection, function(value) { var func = isFunc ? path : (isProp && value != null && value[path]); diff --git a/collection/map.js b/collection/map.js index 63630190e..156b20d57 100644 --- a/collection/map.js +++ b/collection/map.js @@ -23,10 +23,11 @@ import isArray from '../lang/isArray'; * `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`. * * The guarded methods are: - * `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`, `drop`, - * `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`, `parseInt`, - * `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`, `trimLeft`, - * `trimRight`, `trunc`, `random`, `range`, `sample`, `some`, `uniq`, and `words` + * `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`, + * `drop`, `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`, + * `parseInt`, `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`, + * `trimLeft`, `trimRight`, `trunc`, `random`, `range`, `sample`, `some`, + * `sum`, `uniq`, and `words` * * @static * @memberOf _ diff --git a/collection/reduceRight.js b/collection/reduceRight.js index c0a32661d..df78324f1 100644 --- a/collection/reduceRight.js +++ b/collection/reduceRight.js @@ -24,6 +24,6 @@ import createReduce from '../internal/createReduce'; * }, []); * // => [4, 5, 2, 3, 0, 1] */ -var reduceRight = createReduce(arrayReduceRight, baseEachRight); +var reduceRight = createReduce(arrayReduceRight, baseEachRight); export default reduceRight; diff --git a/collection/reject.js b/collection/reject.js index cb4ac1b58..2eba4273d 100644 --- a/collection/reject.js +++ b/collection/reject.js @@ -7,17 +7,6 @@ import isArray from '../lang/isArray'; * The opposite of `_.filter`; this method returns the elements of `collection` * that `predicate` does **not** return truthy for. * - * 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 diff --git a/internal/baseAssign.js b/internal/baseAssign.js index fb0b86696..14b9581c6 100644 --- a/internal/baseAssign.js +++ b/internal/baseAssign.js @@ -4,7 +4,7 @@ import isNative from '../lang/isNative'; import keys from '../object/keys'; /** Native method references. */ -var preventExtensions = isNative(Object.preventExtensions = Object.preventExtensions) && preventExtensions; +var preventExtensions = isNative(preventExtensions = Object.preventExtensions) && preventExtensions; /** Used as `baseAssign`. */ var nativeAssign = (function() { @@ -14,12 +14,19 @@ var nativeAssign = (function() { // // Use `Object.preventExtensions` on a plain object instead of simply using // `Object('x')` because Chrome and IE fail to throw an error when attempting - // to assign values to readonly indexes of strings in strict mode. - var object = { '1': 0 }, - func = preventExtensions && isNative(func = Object.assign) && func; - - try { func(preventExtensions(object), 'xo'); } catch(e) {} - return !object[1] && func; + // to assign values to readonly indexes of strings. + var func = preventExtensions && isNative(func = Object.assign) && func; + try { + if (func) { + var object = preventExtensions({ '1': 0 }); + object[0] = 1; + } + } catch(e) { + // Only attempt in strict mode. + try { func(object, 'xo'); } catch(e) {} + return !object[1] && func; + } + return false; }()); /** diff --git a/internal/baseAt.js b/internal/baseAt.js index fe427a367..6e8064f12 100644 --- a/internal/baseAt.js +++ b/internal/baseAt.js @@ -1,5 +1,5 @@ +import isArrayLike from './isArrayLike'; import isIndex from './isIndex'; -import isLength from './isLength'; /** * The base implementation of `_.at` without support for string collections @@ -12,8 +12,9 @@ import isLength from './isLength'; */ function baseAt(collection, props) { var index = -1, - length = collection.length, - isArr = isLength(length), + isNil = collection == null, + isArr = !isNil && isArrayLike(collection), + length = isArr && collection.length, propsLength = props.length, result = Array(propsLength); @@ -22,7 +23,7 @@ function baseAt(collection, props) { if (isArr) { result[index] = isIndex(key, length) ? collection[key] : undefined; } else { - result[index] = collection[key]; + result[index] = isNil ? undefined : collection[key]; } } return result; diff --git a/internal/baseFlatten.js b/internal/baseFlatten.js index 7b0c54ddc..88b7dcddc 100644 --- a/internal/baseFlatten.js +++ b/internal/baseFlatten.js @@ -1,6 +1,6 @@ import isArguments from '../lang/isArguments'; import isArray from '../lang/isArray'; -import isLength from './isLength'; +import isArrayLike from './isArrayLike'; import isObjectLike from './isObjectLike'; /** @@ -9,8 +9,8 @@ import isObjectLike from './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 {boolean} [isDeep] Specify a deep flatten. + * @param {boolean} [isStrict] Restrict flattening to arrays-like objects. * @returns {Array} Returns the new flattened array. */ function baseFlatten(array, isDeep, isStrict) { @@ -21,8 +21,8 @@ function baseFlatten(array, isDeep, isStrict) { while (++index < length) { var value = array[index]; - - if (isObjectLike(value) && isLength(value.length) && (isArray(value) || isArguments(value))) { + if (isObjectLike(value) && isArrayLike(value) && + (isStrict || isArray(value) || isArguments(value))) { if (isDeep) { // Recursively flatten arrays (susceptible to call stack limits). value = baseFlatten(value, isDeep, isStrict); @@ -30,7 +30,6 @@ function baseFlatten(array, isDeep, isStrict) { var valIndex = -1, valLength = value.length; - result.length += valLength; while (++valIndex < valLength) { result[++resIndex] = value[valIndex]; } diff --git a/internal/baseGet.js b/internal/baseGet.js index 17b8a2448..495e49e8b 100644 --- a/internal/baseGet.js +++ b/internal/baseGet.js @@ -21,9 +21,9 @@ function baseGet(object, path, pathKey) { length = path.length; while (object != null && ++index < length) { - var result = object = object[path[index]]; + object = object[path[index]]; } - return result; + return (index && index == length) ? object : undefined; } export default baseGet; diff --git a/internal/baseIsEqual.js b/internal/baseIsEqual.js index cbe78bcd7..ec6aaee74 100644 --- a/internal/baseIsEqual.js +++ b/internal/baseIsEqual.js @@ -16,8 +16,7 @@ import baseIsEqualDeep from './baseIsEqualDeep'; function baseIsEqual(value, other, customizer, isLoose, stackA, stackB) { // Exit early for identical values. if (value === other) { - // Treat `+0` vs. `-0` as not equal. - return value !== 0 || (1 / value == 1 / other); + return true; } var valType = typeof value, othType = typeof other; diff --git a/internal/baseMap.js b/internal/baseMap.js index f679ae13f..6f6cd346f 100644 --- a/internal/baseMap.js +++ b/internal/baseMap.js @@ -1,6 +1,5 @@ import baseEach from './baseEach'; -import getLength from './getLength'; -import isLength from './isLength'; +import isArrayLike from './isArrayLike'; /** * The base implementation of `_.map` without support for callback shorthands @@ -13,8 +12,7 @@ import isLength from './isLength'; */ function baseMap(collection, iteratee) { var index = -1, - length = getLength(collection), - result = isLength(length) ? Array(length) : []; + result = isArrayLike(collection) ? Array(collection.length) : []; baseEach(collection, function(value, key, collection) { result[++index] = iteratee(value, key, collection); diff --git a/internal/baseMerge.js b/internal/baseMerge.js index c10735a97..caf73774d 100644 --- a/internal/baseMerge.js +++ b/internal/baseMerge.js @@ -2,7 +2,7 @@ import arrayEach from './arrayEach'; import baseMergeDeep from './baseMergeDeep'; import getSymbols from './getSymbols'; import isArray from '../lang/isArray'; -import isLength from './isLength'; +import isArrayLike from './isArrayLike'; import isObject from '../lang/isObject'; import isObjectLike from './isObjectLike'; import isTypedArray from '../lang/isTypedArray'; @@ -30,7 +30,7 @@ function baseMerge(object, source, customizer, stackA, stackB) { if (!isObject(object)) { return object; } - var isSrcArr = isLength(source.length) && (isArray(source) || isTypedArray(source)); + var isSrcArr = isArrayLike(source) && (isArray(source) || isTypedArray(source)); if (!isSrcArr) { var props = keys(source); push.apply(props, getSymbols(source)); diff --git a/internal/baseMergeDeep.js b/internal/baseMergeDeep.js index 7147f12e8..3f174c8ff 100644 --- a/internal/baseMergeDeep.js +++ b/internal/baseMergeDeep.js @@ -1,8 +1,7 @@ import arrayCopy from './arrayCopy'; -import getLength from './getLength'; import isArguments from '../lang/isArguments'; import isArray from '../lang/isArray'; -import isLength from './isLength'; +import isArrayLike from './isArrayLike'; import isPlainObject from '../lang/isPlainObject'; import isTypedArray from '../lang/isTypedArray'; import toPlainObject from '../lang/toPlainObject'; @@ -38,10 +37,10 @@ function baseMergeDeep(object, source, key, mergeFunc, customizer, stackA, stack if (isCommon) { result = srcValue; - if (isLength(srcValue.length) && (isArray(srcValue) || isTypedArray(srcValue))) { + if (isArrayLike(srcValue) && (isArray(srcValue) || isTypedArray(srcValue))) { result = isArray(value) ? value - : (getLength(value) ? arrayCopy(value) : []); + : (isArrayLike(value) ? arrayCopy(value) : []); } else if (isPlainObject(srcValue) || isArguments(srcValue)) { result = isArguments(value) diff --git a/internal/basePullAt.js b/internal/basePullAt.js index fe00c5b64..a4ea22d33 100644 --- a/internal/basePullAt.js +++ b/internal/basePullAt.js @@ -16,7 +16,7 @@ var splice = arrayProto.splice; * @returns {Array} Returns `array`. */ function basePullAt(array, indexes) { - var length = indexes.length; + var length = array ? indexes.length : 0; while (length--) { var index = parseFloat(indexes[length]); if (index != previous && isIndex(index)) { diff --git a/internal/binaryIndexBy.js b/internal/binaryIndexBy.js index 191dcd892..8e0cc1bca 100644 --- a/internal/binaryIndexBy.js +++ b/internal/binaryIndexBy.js @@ -6,7 +6,7 @@ var nativeMin = Math.min; /** Used as references for the maximum length and index of an array. */ var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1, - MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1; + MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1; /** * This function is like `binaryIndex` except that it invokes `iteratee` for diff --git a/internal/composeArgsRight.js b/internal/composeArgsRight.js index f00e57c65..0886f3cbb 100644 --- a/internal/composeArgsRight.js +++ b/internal/composeArgsRight.js @@ -23,12 +23,12 @@ function composeArgsRight(args, partials, holders) { while (++argsIndex < argsLength) { result[argsIndex] = args[argsIndex]; } - var pad = argsIndex; + var offset = argsIndex; while (++rightIndex < rightLength) { - result[pad + rightIndex] = partials[rightIndex]; + result[offset + rightIndex] = partials[rightIndex]; } while (++holdersIndex < holdersLength) { - result[pad + holders[holdersIndex]] = args[argsIndex++]; + result[offset + holders[holdersIndex]] = args[argsIndex++]; } return result; } diff --git a/internal/createFind.js b/internal/createFind.js index 10ca957dd..8445c9651 100644 --- a/internal/createFind.js +++ b/internal/createFind.js @@ -19,7 +19,7 @@ function createFind(eachFunc, fromRight) { return index > -1 ? collection[index] : undefined; } return baseFind(collection, predicate, eachFunc); - } + }; } export default createFind; diff --git a/internal/createFlow.js b/internal/createFlow.js index 80e48b35f..9e3e47e7b 100644 --- a/internal/createFlow.js +++ b/internal/createFlow.js @@ -4,6 +4,12 @@ import getFuncName from './getFuncName'; import isArray from '../lang/isArray'; import isLaziable from './isLaziable'; +/** Used to compose bitmasks for wrapper metadata. */ +var CURRY_FLAG = 8, + PARTIAL_FLAG = 32, + ARY_FLAG = 128, + REARG_FLAG = 256; + /** Used as the `TypeError` message for "Functions" methods. */ var FUNC_ERROR_TEXT = 'Expected a function'; @@ -39,7 +45,7 @@ function createFlow(fromRight) { funcName = getFuncName(func); var data = funcName == 'wrapper' ? getData(func) : null; - if (data && isLaziable(data[0])) { + if (data && isLaziable(data[0]) && data[1] == (ARY_FLAG | CURRY_FLAG | PARTIAL_FLAG | REARG_FLAG) && !data[4].length && data[9] == 1) { wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]); } else { wrapper = (func.length == 1 && isLaziable(func)) ? wrapper[funcName]() : wrapper.thru(func); diff --git a/internal/createObjectMapper.js b/internal/createObjectMapper.js new file mode 100644 index 000000000..e0d1b8e30 --- /dev/null +++ b/internal/createObjectMapper.js @@ -0,0 +1,26 @@ +import baseCallback from './baseCallback'; +import baseForOwn from './baseForOwn'; + +/** + * Creates a function for `_.mapKeys` or `_.mapValues`. + * + * @private + * @param {boolean} [isMapKeys] Specify mapping keys instead of values. + * @returns {Function} Returns the new map function. + */ +function createObjectMapper(isMapKeys) { + return function(object, iteratee, thisArg) { + var result = {}; + iteratee = baseCallback(iteratee, thisArg, 3); + + baseForOwn(object, function(value, key, object) { + var mapped = iteratee(value, key, object); + key = isMapKeys ? mapped : key; + value = isMapKeys ? value : mapped; + result[key] = value; + }); + return result; + }; +} + +export default createObjectMapper; diff --git a/internal/createPadDir.js b/internal/createPadDir.js index d6059b56f..e2ee0d72b 100644 --- a/internal/createPadDir.js +++ b/internal/createPadDir.js @@ -11,7 +11,7 @@ import createPadding from './createPadding'; function createPadDir(fromRight) { return function(string, length, chars) { string = baseToString(string); - return string && ((fromRight ? string : '') + createPadding(string, length, chars) + (fromRight ? '' : string)); + return (fromRight ? string : '') + createPadding(string, length, chars) + (fromRight ? '' : string); }; } diff --git a/internal/equalByTag.js b/internal/equalByTag.js index 530ab763a..a002b6fd0 100644 --- a/internal/equalByTag.js +++ b/internal/equalByTag.js @@ -34,8 +34,7 @@ function equalByTag(object, other, tag) { // Treat `NaN` vs. `NaN` as equal. return (object != +object) ? other != +other - // But, treat `-0` vs. `+0` as not equal. - : (object == 0 ? ((1 / object) == (1 / other)) : object == +other); + : object == +other; case regexpTag: case stringTag: diff --git a/internal/getLength.js b/internal/getLength.js index 0ecc1e0df..b4fff076b 100644 --- a/internal/getLength.js +++ b/internal/getLength.js @@ -4,7 +4,7 @@ import baseProperty from './baseProperty'; * Gets the "length" property value of `object`. * * **Note:** This function is used to avoid a [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792) - * in Safari on iOS 8.1 ARM64. + * that affects Safari on at least iOS 8.1-8.3 ARM64. * * @private * @param {Object} object The object to query. diff --git a/internal/isArrayLike.js b/internal/isArrayLike.js new file mode 100644 index 000000000..df01faef9 --- /dev/null +++ b/internal/isArrayLike.js @@ -0,0 +1,15 @@ +import getLength from './getLength'; +import isLength from './isLength'; + +/** + * Checks if `value` is array-like. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is array-like, else `false`. + */ +function isArrayLike(value) { + return value != null && isLength(getLength(value)); +} + +export default isArrayLike; diff --git a/internal/isIterateeCall.js b/internal/isIterateeCall.js index 1c82f734a..4c85d77b1 100644 --- a/internal/isIterateeCall.js +++ b/internal/isIterateeCall.js @@ -1,6 +1,5 @@ -import getLength from './getLength'; +import isArrayLike from './isArrayLike'; import isIndex from './isIndex'; -import isLength from './isLength'; import isObject from '../lang/isObject'; /** @@ -17,13 +16,9 @@ function isIterateeCall(value, index, object) { return false; } var type = typeof index; - if (type == 'number') { - var length = getLength(object), - prereq = isLength(length) && isIndex(index, length); - } else { - prereq = type == 'string' && index in object; - } - if (prereq) { + if (type == 'number' + ? (isArrayLike(object) && isIndex(index, object.length)) + : (type == 'string' && index in object)) { var other = object[index]; return value === value ? (value === other) : (other !== other); } diff --git a/internal/isKey.js b/internal/isKey.js index f3b2f38d1..51345543f 100644 --- a/internal/isKey.js +++ b/internal/isKey.js @@ -2,7 +2,7 @@ import isArray from '../lang/isArray'; import toObject from './toObject'; /** Used to match property names within property paths. */ -var reIsDeepProp = /\.|\[(?:[^[\]]+|(["'])(?:(?!\1)[^\n\\]|\\.)*?)\1\]/, +var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\n\\]|\\.)*?\1)\]/, reIsPlainProp = /^\w*$/; /** diff --git a/internal/isStrictComparable.js b/internal/isStrictComparable.js index 0b3a97bff..a3d21d21c 100644 --- a/internal/isStrictComparable.js +++ b/internal/isStrictComparable.js @@ -9,7 +9,7 @@ import isObject from '../lang/isObject'; * equality comparisons, else `false`. */ function isStrictComparable(value) { - return value === value && (value === 0 ? ((1 / value) > 0) : !isObject(value)); + return value === value && !isObject(value); } export default isStrictComparable; diff --git a/internal/pickByArray.js b/internal/pickByArray.js index 6fde01d36..928932e5a 100644 --- a/internal/pickByArray.js +++ b/internal/pickByArray.js @@ -1,7 +1,7 @@ import toObject from './toObject'; /** - * A specialized version of `_.pick` that picks `object` properties specified + * A specialized version of `_.pick` which picks `object` properties specified * by `props`. * * @private diff --git a/internal/pickByCallback.js b/internal/pickByCallback.js index 63feb356c..a8af9550d 100644 --- a/internal/pickByCallback.js +++ b/internal/pickByCallback.js @@ -1,7 +1,7 @@ import baseForIn from './baseForIn'; /** - * A specialized version of `_.pick` that picks `object` properties `predicate` + * A specialized version of `_.pick` which picks `object` properties `predicate` * returns truthy for. * * @private diff --git a/internal/toIterable.js b/internal/toIterable.js index b0cc6e7a6..908662599 100644 --- a/internal/toIterable.js +++ b/internal/toIterable.js @@ -1,5 +1,4 @@ -import getLength from './getLength'; -import isLength from './isLength'; +import isArrayLike from './isArrayLike'; import isObject from '../lang/isObject'; import values from '../object/values'; @@ -14,7 +13,7 @@ function toIterable(value) { if (value == null) { return []; } - if (!isLength(getLength(value))) { + if (!isArrayLike(value)) { return values(value); } return isObject(value) ? value : Object(value); diff --git a/lang/isArguments.js b/lang/isArguments.js index 28ba8979a..45a6a1468 100644 --- a/lang/isArguments.js +++ b/lang/isArguments.js @@ -1,4 +1,4 @@ -import isLength from '../internal/isLength'; +import isArrayLike from '../internal/isArrayLike'; import isObjectLike from '../internal/isObjectLike'; /** `Object#toString` result references. */ @@ -30,8 +30,7 @@ var objToString = objectProto.toString; * // => false */ function isArguments(value) { - var length = isObjectLike(value) ? value.length : undefined; - return isLength(length) && objToString.call(value) == argsTag; + return isObjectLike(value) && isArrayLike(value) && objToString.call(value) == argsTag; } export default isArguments; diff --git a/lang/isEmpty.js b/lang/isEmpty.js index 5329041f1..6e676b7bf 100644 --- a/lang/isEmpty.js +++ b/lang/isEmpty.js @@ -1,8 +1,7 @@ -import getLength from '../internal/getLength'; import isArguments from './isArguments'; import isArray from './isArray'; +import isArrayLike from '../internal/isArrayLike'; import isFunction from './isFunction'; -import isLength from '../internal/isLength'; import isObjectLike from '../internal/isObjectLike'; import isString from './isString'; import keys from '../object/keys'; @@ -38,10 +37,9 @@ function isEmpty(value) { if (value == null) { return true; } - var length = getLength(value); - if (isLength(length) && (isArray(value) || isString(value) || isArguments(value) || + if (isArrayLike(value) && (isArray(value) || isString(value) || isArguments(value) || (isObjectLike(value) && isFunction(value.splice)))) { - return !length; + return !value.length; } return !keys(value).length; } diff --git a/lang/isRegExp.js b/lang/isRegExp.js index 8caa23a55..da278a94b 100644 --- a/lang/isRegExp.js +++ b/lang/isRegExp.js @@ -29,7 +29,7 @@ var objToString = objectProto.toString; * // => false */ function isRegExp(value) { - return (isObjectLike(value) && objToString.call(value) == regexpTag) || false; + return isObjectLike(value) && objToString.call(value) == regexpTag; } export default isRegExp; diff --git a/lodash.js b/lodash.js index fe5f6fc49..08dacbd4c 100644 --- a/lodash.js +++ b/lodash.js @@ -1,6 +1,6 @@ /** * @license - * lodash 3.7.0 (Custom Build) + * lodash 3.8.0 (Custom Build) * Build: `lodash modularize modern exports="es" -o ./` * Copyright 2012-2015 The Dojo Foundation * Based on Underscore.js 1.8.3 @@ -42,7 +42,7 @@ import support from './support'; import thru from './chain/thru'; /** Used as the semantic version number. */ -var VERSION = '3.7.0'; +var VERSION = '3.8.0'; /** Used to compose bitmasks for wrapper metadata. */ var BIND_KEY_FLAG = 2; @@ -130,6 +130,7 @@ lodash.invoke = collection.invoke; lodash.keys = keys; lodash.keysIn = object.keysIn; lodash.map = collection.map; +lodash.mapKeys = object.mapKeys; lodash.mapValues = object.mapValues; lodash.matches = utility.matches; lodash.matchesProperty = utility.matchesProperty; @@ -178,6 +179,7 @@ lodash.transform = object.transform; lodash.union = array.union; lodash.uniq = array.uniq; lodash.unzip = array.unzip; +lodash.unzipWith = array.unzipWith; lodash.values = object.values; lodash.valuesIn = object.valuesIn; lodash.where = collection.where; @@ -186,6 +188,7 @@ lodash.wrap = func.wrap; lodash.xor = array.xor; lodash.zip = array.zip; lodash.zipObject = array.zipObject; +lodash.zipWith = array.zipWith; // Add aliases. lodash.backflow = func.flowRight; @@ -430,8 +433,13 @@ LazyWrapper.prototype.reject = function(predicate, thisArg) { LazyWrapper.prototype.slice = function(start, end) { start = start == null ? 0 : (+start || 0); - var result = start < 0 ? this.takeRight(-start) : this.drop(start); + var result = this; + if (start < 0) { + result = this.takeRight(-start); + } else if (start) { + result = this.drop(start); + } if (end !== undefined) { end = (+end || 0); result = end < 0 ? result.dropRight(-end) : result.take(end - start); @@ -454,7 +462,6 @@ baseForOwn(LazyWrapper.prototype, function(func, methodName) { lodash.prototype[methodName] = function() { var args = arguments, - length = args.length, chainAll = this.__chain__, value = this.__wrapped__, isHybrid = !!this.__actions__.length, diff --git a/object.js b/object.js index 4da422d97..4ff256800 100644 --- a/object.js +++ b/object.js @@ -14,6 +14,7 @@ import has from './object/has'; import invert from './object/invert'; import keys from './object/keys'; import keysIn from './object/keysIn'; +import mapKeys from './object/mapKeys'; import mapValues from './object/mapValues'; import merge from './object/merge'; import methods from './object/methods'; @@ -43,6 +44,7 @@ export default { 'invert': invert, 'keys': keys, 'keysIn': keysIn, + 'mapKeys': mapKeys, 'mapValues': mapValues, 'merge': merge, 'methods': methods, diff --git a/object/assign.js b/object/assign.js index fd6a6b0ae..b11601740 100644 --- a/object/assign.js +++ b/object/assign.js @@ -12,7 +12,6 @@ import createAssigner from '../internal/createAssigner'; * **Note:** This method mutates `object` and is based on * [`Object.assign`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign). * - * * @static * @memberOf _ * @alias extend diff --git a/object/keys.js b/object/keys.js index db7ea27f3..7e3ccb969 100644 --- a/object/keys.js +++ b/object/keys.js @@ -1,4 +1,4 @@ -import isLength from '../internal/isLength'; +import isArrayLike from '../internal/isArrayLike'; import isNative from '../lang/isNative'; import isObject from '../lang/isObject'; import shimKeys from '../internal/shimKeys'; @@ -34,12 +34,9 @@ var nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys; * // => ['0', '1'] */ var keys = !nativeKeys ? shimKeys : function(object) { - if (object) { - var Ctor = object.constructor, - length = object.length; - } + var Ctor = object != null && object.constructor; if ((typeof Ctor == 'function' && Ctor.prototype === object) || - (typeof object != 'function' && isLength(length))) { + (typeof object != 'function' && isArrayLike(object))) { return shimKeys(object); } return isObject(object) ? nativeKeys(object) : []; diff --git a/object/mapKeys.js b/object/mapKeys.js new file mode 100644 index 000000000..ae66ea3c8 --- /dev/null +++ b/object/mapKeys.js @@ -0,0 +1,25 @@ +import createObjectMapper from '../internal/createObjectMapper'; + +/** + * The opposite of `_.mapValues`; this method creates an object with the + * same values as `object` and keys generated by running each own enumerable + * property of `object` through `iteratee`. + * + * @static + * @memberOf _ + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function|Object|string} [iteratee=_.identity] The function invoked + * per iteration. + * @param {*} [thisArg] The `this` binding of `iteratee`. + * @returns {Object} Returns the new mapped object. + * @example + * + * _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) { + * return key + value; + * }); + * // => { 'a1': 1, 'b2': 2 } + */ +var mapKeys = createObjectMapper(true); + +export default mapKeys; diff --git a/object/mapValues.js b/object/mapValues.js index fa4ab88f7..852668b99 100644 --- a/object/mapValues.js +++ b/object/mapValues.js @@ -1,5 +1,4 @@ -import baseCallback from '../internal/baseCallback'; -import baseForOwn from '../internal/baseForOwn'; +import createObjectMapper from '../internal/createObjectMapper'; /** * Creates an object with the same keys as `object` and values generated by @@ -42,14 +41,6 @@ import baseForOwn from '../internal/baseForOwn'; * _.mapValues(users, 'age'); * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed) */ -function mapValues(object, iteratee, thisArg) { - var result = {}; - iteratee = baseCallback(iteratee, thisArg, 3); - - baseForOwn(object, function(value, key, object) { - result[key] = iteratee(value, key, object); - }); - return result; -} +var mapValues = createObjectMapper(); export default mapValues; diff --git a/object/omit.js b/object/omit.js index c46924236..f89df57c3 100644 --- a/object/omit.js +++ b/object/omit.js @@ -10,11 +10,6 @@ import restParam from '../function/restParam'; /** * The opposite of `_.pick`; this method creates an object composed of the * own and inherited enumerable properties of `object` that are not omitted. - * Property names may be specified as individual arguments or as arrays of - * property names. If `predicate` is provided it is invoked for each property - * of `object` omitting the properties `predicate` returns truthy for. The - * predicate is bound to `thisArg` and invoked with three arguments: - * (value, key, object). * * @static * @memberOf _ diff --git a/package.json b/package.json index 16cf62d9f..f2bccfcbd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lodash-es", - "version": "3.7.0", + "version": "3.8.0", "description": "The modern build of lodash exported as ES modules.", "homepage": "https://lodash.com/custom-builds", "license": "MIT", diff --git a/support.js b/support.js index 8a0791918..1657bfc65 100644 --- a/support.js +++ b/support.js @@ -20,6 +20,7 @@ var support = {}; (function(x) { var Ctor = function() { this.x = x; }, + args = arguments, object = { '0': x, 'length': x }, props = []; @@ -69,7 +70,7 @@ var support = {}; * @type boolean */ try { - support.nonEnumArgs = !propertyIsEnumerable.call(arguments, 1); + support.nonEnumArgs = !propertyIsEnumerable.call(args, 1); } catch(e) { support.nonEnumArgs = true; } diff --git a/utility/callback.js b/utility/callback.js index 92d5160cd..48834cb46 100644 --- a/utility/callback.js +++ b/utility/callback.js @@ -1,5 +1,7 @@ import baseCallback from '../internal/baseCallback'; import isIterateeCall from '../internal/isIterateeCall'; +import isObjectLike from '../internal/isObjectLike'; +import matches from './matches'; /** * Creates a function that invokes `func` with the `this` binding of `thisArg` @@ -43,7 +45,9 @@ function callback(func, thisArg, guard) { if (guard && isIterateeCall(func, thisArg, guard)) { thisArg = null; } - return baseCallback(func, thisArg); + return isObjectLike(func) + ? matches(func) + : baseCallback(func, thisArg); } export default callback; diff --git a/utility/method.js b/utility/method.js index 362ebfe8c..869663901 100644 --- a/utility/method.js +++ b/utility/method.js @@ -25,7 +25,7 @@ import restParam from '../function/restParam'; var method = restParam(function(path, args) { return function(object) { return invokePath(object, path, args); - } + }; }); export default method;