diff --git a/README.md b/README.md index 546368977..f5be76457 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# lodash-es v3.5.0 +# lodash-es v3.6.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.5.0-es) for more details. +See the [package source](https://github.com/lodash/lodash/tree/3.6.0-es) for more details. diff --git a/array/difference.js b/array/difference.js index 627919db3..bec97e536 100644 --- a/array/difference.js +++ b/array/difference.js @@ -2,15 +2,15 @@ import baseDifference from '../internal/baseDifference'; import baseFlatten from '../internal/baseFlatten'; import isArguments from '../lang/isArguments'; import isArray from '../lang/isArray'; +import restParam from '../function/restParam'; /** * Creates an array excluding all values of the provided arrays using * `SameValueZero` for equality comparisons. * - * **Note:** `SameValueZero` comparisons are like strict equality comparisons, - * e.g. `===`, except that `NaN` matches `NaN`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * for more details. + * **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 _ @@ -23,18 +23,10 @@ import isArray from '../lang/isArray'; * _.difference([1, 2, 3], [4, 2]); * // => [1, 3] */ -function difference() { - var args = arguments, - index = -1, - length = args.length; - - while (++index < length) { - var value = args[index]; - if (isArray(value) || isArguments(value)) { - break; - } - } - return baseDifference(value, baseFlatten(args, false, true, ++index)); -} +var difference = restParam(function(array, values) { + return (isArray(array) || isArguments(array)) + ? baseDifference(array, baseFlatten(values, false, true)) + : []; +}); export default difference; diff --git a/array/dropRightWhile.js b/array/dropRightWhile.js index 91c94728e..a3e52158d 100644 --- a/array/dropRightWhile.js +++ b/array/dropRightWhile.js @@ -1,10 +1,10 @@ import baseCallback from '../internal/baseCallback'; -import baseSlice from '../internal/baseSlice'; +import baseWhile from '../internal/baseWhile'; /** * Creates a slice of `array` excluding elements dropped from the end. * Elements are dropped until `predicate` returns falsey. The predicate is - * bound to `thisArg` and invoked with three arguments; (value, index, array). + * bound to `thisArg` and invoked with three arguments: (value, index, array). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. @@ -51,13 +51,9 @@ import baseSlice from '../internal/baseSlice'; * // => ['barney', 'fred', 'pebbles'] */ function dropRightWhile(array, predicate, thisArg) { - var length = array ? array.length : 0; - if (!length) { - return []; - } - predicate = baseCallback(predicate, thisArg, 3); - while (length-- && predicate(array[length], length, array)) {} - return baseSlice(array, 0, length + 1); + return (array && array.length) + ? baseWhile(array, baseCallback(predicate, thisArg, 3), true, true) + : []; } export default dropRightWhile; diff --git a/array/dropWhile.js b/array/dropWhile.js index 20ad894f1..289897670 100644 --- a/array/dropWhile.js +++ b/array/dropWhile.js @@ -1,10 +1,10 @@ import baseCallback from '../internal/baseCallback'; -import baseSlice from '../internal/baseSlice'; +import baseWhile from '../internal/baseWhile'; /** * Creates a slice of `array` excluding elements dropped from the beginning. * Elements are dropped until `predicate` returns falsey. The predicate is - * bound to `thisArg` and invoked with three arguments; (value, index, array). + * bound to `thisArg` and invoked with three arguments: (value, index, array). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. @@ -51,14 +51,9 @@ import baseSlice from '../internal/baseSlice'; * // => ['barney', 'fred', 'pebbles'] */ function dropWhile(array, predicate, thisArg) { - var length = array ? array.length : 0; - if (!length) { - return []; - } - var index = -1; - predicate = baseCallback(predicate, thisArg, 3); - while (++index < length && predicate(array[index], index, array)) {} - return baseSlice(array, index); + return (array && array.length) + ? baseWhile(array, baseCallback(predicate, thisArg, 3), true) + : []; } export default dropWhile; diff --git a/array/fill.js b/array/fill.js index 3a3e3772f..dc8d4fa90 100644 --- a/array/fill.js +++ b/array/fill.js @@ -15,6 +15,19 @@ import isIterateeCall from '../internal/isIterateeCall'; * @param {number} [start=0] The start position. * @param {number} [end=array.length] The end position. * @returns {Array} Returns `array`. + * @example + * + * var array = [1, 2, 3]; + * + * _.fill(array, 'a'); + * console.log(array); + * // => ['a', 'a', 'a'] + * + * _.fill(Array(3), 2); + * // => [2, 2, 2] + * + * _.fill([4, 6, 8], '*', 1, 2); + * // => [4, '*', 8] */ function fill(array, value, start, end) { var length = array ? array.length : 0; diff --git a/array/findIndex.js b/array/findIndex.js index 052fdb57c..1c42d41f5 100644 --- a/array/findIndex.js +++ b/array/findIndex.js @@ -1,8 +1,8 @@ -import baseCallback from '../internal/baseCallback'; +import createFindIndex from '../internal/createFindIndex'; /** * This method is like `_.find` except that it returns the index of the first - * element `predicate` returns truthy for, instead of the element itself. + * element `predicate` returns truthy for instead of the element itself. * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. @@ -48,17 +48,6 @@ import baseCallback from '../internal/baseCallback'; * _.findIndex(users, 'active'); * // => 2 */ -function findIndex(array, predicate, thisArg) { - var index = -1, - length = array ? array.length : 0; - - predicate = baseCallback(predicate, thisArg, 3); - while (++index < length) { - if (predicate(array[index], index, array)) { - return index; - } - } - return -1; -} +var findIndex = createFindIndex(); export default findIndex; diff --git a/array/findLastIndex.js b/array/findLastIndex.js index ce91f93b9..a34e60821 100644 --- a/array/findLastIndex.js +++ b/array/findLastIndex.js @@ -1,4 +1,4 @@ -import baseCallback from '../internal/baseCallback'; +import createFindIndex from '../internal/createFindIndex'; /** * This method is like `_.findIndex` except that it iterates over elements @@ -48,15 +48,6 @@ import baseCallback from '../internal/baseCallback'; * _.findLastIndex(users, 'active'); * // => 0 */ -function findLastIndex(array, predicate, thisArg) { - var length = array ? array.length : 0; - predicate = baseCallback(predicate, thisArg, 3); - while (length--) { - if (predicate(array[length], length, array)) { - return length; - } - } - return -1; -} +var findLastIndex = createFindIndex(true); export default findLastIndex; diff --git a/array/flatten.js b/array/flatten.js index b22b17ee5..459471486 100644 --- a/array/flatten.js +++ b/array/flatten.js @@ -15,18 +15,18 @@ import isIterateeCall from '../internal/isIterateeCall'; * @example * * _.flatten([1, [2, 3, [4]]]); - * // => [1, 2, 3, [4]]; + * // => [1, 2, 3, [4]] * * // using `isDeep` * _.flatten([1, [2, 3, [4]]], true); - * // => [1, 2, 3, 4]; + * // => [1, 2, 3, 4] */ function flatten(array, isDeep, guard) { var length = array ? array.length : 0; if (guard && isIterateeCall(array, isDeep, guard)) { isDeep = false; } - return length ? baseFlatten(array, isDeep, false, 0) : []; + return length ? baseFlatten(array, isDeep) : []; } export default flatten; diff --git a/array/flattenDeep.js b/array/flattenDeep.js index 0bbd95cdb..c9e3a9172 100644 --- a/array/flattenDeep.js +++ b/array/flattenDeep.js @@ -11,11 +11,11 @@ import baseFlatten from '../internal/baseFlatten'; * @example * * _.flattenDeep([1, [2, 3, [4]]]); - * // => [1, 2, 3, 4]; + * // => [1, 2, 3, 4] */ function flattenDeep(array) { var length = array ? array.length : 0; - return length ? baseFlatten(array, true, false, 0) : []; + return length ? baseFlatten(array, true) : []; } export default flattenDeep; diff --git a/array/indexOf.js b/array/indexOf.js index a3c4edf7f..4cea0c160 100644 --- a/array/indexOf.js +++ b/array/indexOf.js @@ -10,10 +10,9 @@ var nativeMax = Math.max; * 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` comparisons are like strict equality comparisons, - * e.g. `===`, except that `NaN` matches `NaN`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * for more details. + * **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 _ diff --git a/array/intersection.js b/array/intersection.js index e204f130a..d94ea5dc0 100644 --- a/array/intersection.js +++ b/array/intersection.js @@ -8,10 +8,9 @@ import isArray from '../lang/isArray'; * Creates an array of unique values in all provided arrays using `SameValueZero` * for equality comparisons. * - * **Note:** `SameValueZero` comparisons are like strict equality comparisons, - * e.g. `===`, except that `NaN` matches `NaN`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * for more details. + * **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 _ diff --git a/array/pull.js b/array/pull.js index d911ba079..76ad1e9fa 100644 --- a/array/pull.js +++ b/array/pull.js @@ -11,10 +11,10 @@ var splice = arrayProto.splice; * comparisons. * * **Notes:** - * - Unlike `_.without`, this method mutates `array`. - * - `SameValueZero` comparisons are like strict equality comparisons, e.g. `===`, - * except that `NaN` matches `NaN`. See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * for more details. + * - 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` * * @static * @memberOf _ diff --git a/array/pullAt.js b/array/pullAt.js index 5c6595b2e..5af9d7046 100644 --- a/array/pullAt.js +++ b/array/pullAt.js @@ -1,5 +1,14 @@ +import baseAt from '../internal/baseAt'; +import baseCompareAscending from '../internal/baseCompareAscending'; import baseFlatten from '../internal/baseFlatten'; -import basePullAt from '../internal/basePullAt'; +import isIndex from '../internal/isIndex'; +import restParam from '../function/restParam'; + +/** Used for native method references. */ +var arrayProto = Array.prototype; + +/** Native method references. */ +var splice = arrayProto.splice; /** * Removes elements from `array` corresponding to the given indexes and returns @@ -26,8 +35,22 @@ import basePullAt from '../internal/basePullAt'; * console.log(evens); * // => [10, 20] */ -function pullAt(array) { - return basePullAt(array || [], baseFlatten(arguments, false, false, 1)); -} +var pullAt = restParam(function(array, indexes) { + array || (array = []); + indexes = baseFlatten(indexes); + + var length = indexes.length, + result = baseAt(array, indexes); + + indexes.sort(baseCompareAscending); + while (length--) { + var index = parseFloat(indexes[length]); + if (index != previous && isIndex(index)) { + var previous = index; + splice.call(array, index, 1); + } + } + return result; +}); export default pullAt; diff --git a/array/remove.js b/array/remove.js index b8e84bae0..5109d69f8 100644 --- a/array/remove.js +++ b/array/remove.js @@ -9,7 +9,7 @@ var splice = arrayProto.splice; /** * Removes all elements from `array` that `predicate` returns truthy for * and returns an array of the removed elements. The predicate is bound to - * `thisArg` and invoked with three arguments; (value, index, array). + * `thisArg` and invoked with three arguments: (value, index, array). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. diff --git a/array/sortedIndex.js b/array/sortedIndex.js index 86b0262ab..520976e1f 100644 --- a/array/sortedIndex.js +++ b/array/sortedIndex.js @@ -1,6 +1,4 @@ -import baseCallback from '../internal/baseCallback'; -import binaryIndex from '../internal/binaryIndex'; -import binaryIndexBy from '../internal/binaryIndexBy'; +import createSortedIndex from '../internal/createSortedIndex'; /** * Uses a binary search to determine the lowest index at which `value` should @@ -9,14 +7,14 @@ import binaryIndexBy from '../internal/binaryIndexBy'; * to compute their sort ranking. The iteratee is bound to `thisArg` and * invoked with one argument; (value). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * @@ -50,10 +48,6 @@ import binaryIndexBy from '../internal/binaryIndexBy'; * _.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x'); * // => 1 */ -function sortedIndex(array, value, iteratee, thisArg) { - return iteratee == null - ? binaryIndex(array, value) - : binaryIndexBy(array, value, baseCallback(iteratee, thisArg, 1)); -} +var sortedIndex = createSortedIndex(); export default sortedIndex; diff --git a/array/sortedLastIndex.js b/array/sortedLastIndex.js index 2ed59f48d..f817aa5e1 100644 --- a/array/sortedLastIndex.js +++ b/array/sortedLastIndex.js @@ -1,6 +1,4 @@ -import baseCallback from '../internal/baseCallback'; -import binaryIndex from '../internal/binaryIndex'; -import binaryIndexBy from '../internal/binaryIndexBy'; +import createSortedIndex from '../internal/createSortedIndex'; /** * This method is like `_.sortedIndex` except that it returns the highest @@ -22,10 +20,6 @@ import binaryIndexBy from '../internal/binaryIndexBy'; * _.sortedLastIndex([4, 4, 5, 5], 5); * // => 4 */ -function sortedLastIndex(array, value, iteratee, thisArg) { - return iteratee == null - ? binaryIndex(array, value, true) - : binaryIndexBy(array, value, baseCallback(iteratee, thisArg, 1), true); -} +var sortedLastIndex = createSortedIndex(true); export default sortedLastIndex; diff --git a/array/takeRightWhile.js b/array/takeRightWhile.js index 7a95a0ed1..1c29ef73e 100644 --- a/array/takeRightWhile.js +++ b/array/takeRightWhile.js @@ -1,10 +1,10 @@ import baseCallback from '../internal/baseCallback'; -import baseSlice from '../internal/baseSlice'; +import baseWhile from '../internal/baseWhile'; /** * Creates a slice of `array` with elements taken from the end. Elements are * taken until `predicate` returns falsey. The predicate is bound to `thisArg` - * and invoked with three arguments; (value, index, array). + * and invoked with three arguments: (value, index, array). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. @@ -51,13 +51,9 @@ import baseSlice from '../internal/baseSlice'; * // => [] */ function takeRightWhile(array, predicate, thisArg) { - var length = array ? array.length : 0; - if (!length) { - return []; - } - predicate = baseCallback(predicate, thisArg, 3); - while (length-- && predicate(array[length], length, array)) {} - return baseSlice(array, length + 1); + return (array && array.length) + ? baseWhile(array, baseCallback(predicate, thisArg, 3), false, true) + : []; } export default takeRightWhile; diff --git a/array/takeWhile.js b/array/takeWhile.js index 67b34760d..f87169190 100644 --- a/array/takeWhile.js +++ b/array/takeWhile.js @@ -1,10 +1,10 @@ import baseCallback from '../internal/baseCallback'; -import baseSlice from '../internal/baseSlice'; +import baseWhile from '../internal/baseWhile'; /** * Creates a slice of `array` with elements taken from the beginning. Elements * are taken until `predicate` returns falsey. The predicate is bound to - * `thisArg` and invoked with three arguments; (value, index, array). + * `thisArg` and invoked with three arguments: (value, index, array). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. @@ -51,14 +51,9 @@ import baseSlice from '../internal/baseSlice'; * // => [] */ function takeWhile(array, predicate, thisArg) { - var length = array ? array.length : 0; - if (!length) { - return []; - } - var index = -1; - predicate = baseCallback(predicate, thisArg, 3); - while (++index < length && predicate(array[index], index, array)) {} - return baseSlice(array, 0, index); + return (array && array.length) + ? baseWhile(array, baseCallback(predicate, thisArg, 3)) + : []; } export default takeWhile; diff --git a/array/union.js b/array/union.js index 4eeeff37e..c94dd1e0c 100644 --- a/array/union.js +++ b/array/union.js @@ -1,14 +1,14 @@ import baseFlatten from '../internal/baseFlatten'; import baseUniq from '../internal/baseUniq'; +import restParam from '../function/restParam'; /** * Creates an array of unique values, in order, of the provided arrays using * `SameValueZero` for equality comparisons. * - * **Note:** `SameValueZero` comparisons are like strict equality comparisons, - * e.g. `===`, except that `NaN` matches `NaN`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * for more details. + * **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 _ @@ -20,8 +20,8 @@ import baseUniq from '../internal/baseUniq'; * _.union([1, 2], [4, 2], [2, 1]); * // => [1, 2, 4] */ -function union() { - return baseUniq(baseFlatten(arguments, false, true, 0)); -} +var union = restParam(function(arrays) { + return baseUniq(baseFlatten(arrays, false, true)); +}); export default union; diff --git a/array/uniq.js b/array/uniq.js index 3bf23b109..e1972b5d7 100644 --- a/array/uniq.js +++ b/array/uniq.js @@ -9,23 +9,22 @@ import sortedUniq from '../internal/sortedUniq'; * search algorithm for sorted arrays. If an iteratee function is provided it * is invoked for each value in the array to generate the criterion by which * uniqueness is computed. The `iteratee` is bound to `thisArg` and invoked - * with three arguments; (value, index, array). + * with three arguments: (value, index, array). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * - * **Note:** `SameValueZero` comparisons are like strict equality comparisons, - * e.g. `===`, except that `NaN` matches `NaN`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * for more details. + * **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 _ diff --git a/array/without.js b/array/without.js index 6ec21dae1..2f019c18b 100644 --- a/array/without.js +++ b/array/without.js @@ -1,14 +1,15 @@ import baseDifference from '../internal/baseDifference'; -import baseSlice from '../internal/baseSlice'; +import isArguments from '../lang/isArguments'; +import isArray from '../lang/isArray'; +import restParam from '../function/restParam'; /** * Creates an array excluding all provided values using `SameValueZero` for * equality comparisons. * - * **Note:** `SameValueZero` comparisons are like strict equality comparisons, - * e.g. `===`, except that `NaN` matches `NaN`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * for more details. + * **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 _ @@ -21,8 +22,10 @@ import baseSlice from '../internal/baseSlice'; * _.without([1, 2, 1, 3], 1, 2); * // => [3] */ -function without(array) { - return baseDifference(array, baseSlice(arguments, 1)); -} +var without = restParam(function(array, values) { + return (isArray(array) || isArguments(array)) + ? baseDifference(array, values) + : []; +}); export default without; diff --git a/array/xor.js b/array/xor.js index 48d991d6d..9abfbdad6 100644 --- a/array/xor.js +++ b/array/xor.js @@ -4,9 +4,8 @@ import isArguments from '../lang/isArguments'; import isArray from '../lang/isArray'; /** - * Creates an array that is the symmetric difference of the provided arrays. - * See [Wikipedia](https://en.wikipedia.org/wiki/Symmetric_difference) for - * more details. + * Creates an array that is the [symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference) + * of the provided arrays. * * @static * @memberOf _ diff --git a/array/zip.js b/array/zip.js index 168a06614..6a54d801d 100644 --- a/array/zip.js +++ b/array/zip.js @@ -1,3 +1,4 @@ +import restParam from '../function/restParam'; import unzip from './unzip'; /** @@ -15,14 +16,6 @@ import unzip from './unzip'; * _.zip(['fred', 'barney'], [30, 40], [true, false]); * // => [['fred', 30, true], ['barney', 40, false]] */ -function zip() { - var length = arguments.length, - array = Array(length); - - while (length--) { - array[length] = arguments[length]; - } - return unzip(array); -} +var zip = restParam(unzip); export default zip; diff --git a/array/zipObject.js b/array/zipObject.js index f7218e840..619d4984a 100644 --- a/array/zipObject.js +++ b/array/zipObject.js @@ -1,9 +1,10 @@ import isArray from '../lang/isArray'; /** - * Creates an object composed from arrays of property names and values. Provide - * either a single two dimensional array, e.g. `[[key1, value1], [key2, value2]]` - * or two arrays, one of property names and one of corresponding values. + * The inverse of `_.pairs`; this method returns an object composed from arrays + * of property names and values. Provide either a single two dimensional array, + * e.g. `[[key1, value1], [key2, value2]]` or two arrays, one of property names + * and one of corresponding values. * * @static * @memberOf _ @@ -14,6 +15,9 @@ import isArray from '../lang/isArray'; * @returns {Object} Returns the new object. * @example * + * _.zipObject([['fred', 30], ['barney', 40]]); + * // => { 'fred': 30, 'barney': 40 } + * * _.zipObject(['fred', 'barney'], [30, 40]); * // => { 'fred': 30, 'barney': 40 } */ diff --git a/chain/thru.js b/chain/thru.js index ef3393584..e5e75637c 100644 --- a/chain/thru.js +++ b/chain/thru.js @@ -10,13 +10,14 @@ * @returns {*} Returns the result of `interceptor`. * @example * - * _([1, 2, 3]) - * .last() + * _(' abc ') + * .chain() + * .trim() * .thru(function(value) { * return [value]; * }) * .value(); - * // => [3] + * // => ['abc'] */ function thru(value, interceptor, thisArg) { return interceptor.call(thisArg, value); diff --git a/collection/at.js b/collection/at.js index 800ee13cd..e63052e1d 100644 --- a/collection/at.js +++ b/collection/at.js @@ -1,6 +1,7 @@ import baseAt from '../internal/baseAt'; import baseFlatten from '../internal/baseFlatten'; import isLength from '../internal/isLength'; +import restParam from '../function/restParam'; import toIterable from '../internal/toIterable'; /** @@ -20,15 +21,15 @@ import toIterable from '../internal/toIterable'; * _.at(['a', 'b', 'c'], [0, 2]); * // => ['a', 'c'] * - * _.at(['fred', 'barney', 'pebbles'], 0, 2); - * // => ['fred', 'pebbles'] + * _.at(['barney', 'fred', 'pebbles'], 0, 2); + * // => ['barney', 'pebbles'] */ -function at(collection) { +var at = restParam(function(collection, props) { var length = collection ? collection.length : 0; if (isLength(length)) { collection = toIterable(collection); } - return baseAt(collection, baseFlatten(arguments, false, false, 1)); -} + return baseAt(collection, baseFlatten(props)); +}); export default at; diff --git a/collection/countBy.js b/collection/countBy.js index 118431330..3f5253a5c 100644 --- a/collection/countBy.js +++ b/collection/countBy.js @@ -10,17 +10,17 @@ var hasOwnProperty = objectProto.hasOwnProperty; * Creates an object composed of keys generated from the results of running * each element of `collection` through `iteratee`. The corresponding value * of each key is the number of times the key was returned by `iteratee`. - * The `iteratee` is bound to `thisArg` and invoked with three arguments; + * The `iteratee` is bound to `thisArg` and invoked with three arguments: * (value, index|key, collection). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * diff --git a/collection/every.js b/collection/every.js index 0ee1d9a7a..1ebe85943 100644 --- a/collection/every.js +++ b/collection/every.js @@ -2,10 +2,11 @@ import arrayEvery from '../internal/arrayEvery'; import baseCallback from '../internal/baseCallback'; import baseEvery from '../internal/baseEvery'; import isArray from '../lang/isArray'; +import isIterateeCall from '../internal/isIterateeCall'; /** * Checks if `predicate` returns truthy for **all** elements of `collection`. - * The predicate is bound to `thisArg` and invoked with three arguments; + * The predicate is bound to `thisArg` and invoked with three arguments: * (value, index|key, collection). * * If a property name is provided for `predicate` the created `_.property` @@ -53,6 +54,9 @@ import isArray from '../lang/isArray'; */ function every(collection, predicate, thisArg) { var func = isArray(collection) ? arrayEvery : baseEvery; + if (thisArg && isIterateeCall(collection, predicate, thisArg)) { + predicate = null; + } if (typeof predicate != 'function' || typeof thisArg != 'undefined') { predicate = baseCallback(predicate, thisArg, 3); } diff --git a/collection/filter.js b/collection/filter.js index 993fb3db5..2d90dbb1c 100644 --- a/collection/filter.js +++ b/collection/filter.js @@ -6,7 +6,7 @@ import isArray from '../lang/isArray'; /** * Iterates over elements of `collection`, returning an array of all elements * `predicate` returns truthy for. The predicate is bound to `thisArg` and - * invoked with three arguments; (value, index|key, collection). + * invoked with three arguments: (value, index|key, collection). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. diff --git a/collection/find.js b/collection/find.js index b799959d0..66df162e7 100644 --- a/collection/find.js +++ b/collection/find.js @@ -1,13 +1,10 @@ -import baseCallback from '../internal/baseCallback'; import baseEach from '../internal/baseEach'; -import baseFind from '../internal/baseFind'; -import findIndex from '../array/findIndex'; -import isArray from '../lang/isArray'; +import createFind from '../internal/createFind'; /** * Iterates over elements of `collection`, returning the first element * `predicate` returns truthy for. The predicate is bound to `thisArg` and - * invoked with three arguments; (value, index|key, collection). + * invoked with three arguments: (value, index|key, collection). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. @@ -54,13 +51,6 @@ import isArray from '../lang/isArray'; * _.result(_.find(users, 'active'), 'user'); * // => 'barney' */ -function find(collection, predicate, thisArg) { - if (isArray(collection)) { - var index = findIndex(collection, predicate, thisArg); - return index > -1 ? collection[index] : undefined; - } - predicate = baseCallback(predicate, thisArg, 3); - return baseFind(collection, predicate, baseEach); -} +var find = createFind(baseEach); export default find; diff --git a/collection/findLast.js b/collection/findLast.js index bc9c53077..1fc980e71 100644 --- a/collection/findLast.js +++ b/collection/findLast.js @@ -1,6 +1,5 @@ -import baseCallback from '../internal/baseCallback'; import baseEachRight from '../internal/baseEachRight'; -import baseFind from '../internal/baseFind'; +import createFind from '../internal/createFind'; /** * This method is like `_.find` except that it iterates over elements of @@ -21,9 +20,6 @@ import baseFind from '../internal/baseFind'; * }); * // => 3 */ -function findLast(collection, predicate, thisArg) { - predicate = baseCallback(predicate, thisArg, 3); - return baseFind(collection, predicate, baseEachRight); -} +var findLast = createFind(baseEachRight, true); export default findLast; diff --git a/collection/forEach.js b/collection/forEach.js index 441b1a75a..acee0c8d0 100644 --- a/collection/forEach.js +++ b/collection/forEach.js @@ -1,11 +1,10 @@ import arrayEach from '../internal/arrayEach'; import baseEach from '../internal/baseEach'; -import bindCallback from '../internal/bindCallback'; -import isArray from '../lang/isArray'; +import createForEach from '../internal/createForEach'; /** * Iterates over elements of `collection` invoking `iteratee` for each element. - * The `iteratee` is bound to `thisArg` and invoked with three arguments; + * The `iteratee` is bound to `thisArg` and invoked with three arguments: * (value, index|key, collection). Iterator functions may exit iteration early * by explicitly returning `false`. * @@ -33,10 +32,6 @@ import isArray from '../lang/isArray'; * }); * // => logs each value-key pair and returns the object (iteration order is not guaranteed) */ -function forEach(collection, iteratee, thisArg) { - return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection)) - ? arrayEach(collection, iteratee) - : baseEach(collection, bindCallback(iteratee, thisArg, 3)); -} +var forEach = createForEach(arrayEach, baseEach); export default forEach; diff --git a/collection/forEachRight.js b/collection/forEachRight.js index b98078314..896b00a1b 100644 --- a/collection/forEachRight.js +++ b/collection/forEachRight.js @@ -1,7 +1,6 @@ import arrayEachRight from '../internal/arrayEachRight'; import baseEachRight from '../internal/baseEachRight'; -import bindCallback from '../internal/bindCallback'; -import isArray from '../lang/isArray'; +import createForEach from '../internal/createForEach'; /** * This method is like `_.forEach` except that it iterates over elements of @@ -19,13 +18,9 @@ import isArray from '../lang/isArray'; * * _([1, 2]).forEachRight(function(n) { * console.log(n); - * }).join(','); + * }).value(); * // => logs each value from right to left and returns the array */ -function forEachRight(collection, iteratee, thisArg) { - return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection)) - ? arrayEachRight(collection, iteratee) - : baseEachRight(collection, bindCallback(iteratee, thisArg, 3)); -} +var forEachRight = createForEach(arrayEachRight, baseEachRight); export default forEachRight; diff --git a/collection/groupBy.js b/collection/groupBy.js index fc7c82425..9cc51a745 100644 --- a/collection/groupBy.js +++ b/collection/groupBy.js @@ -10,17 +10,17 @@ var hasOwnProperty = objectProto.hasOwnProperty; * Creates an object composed of keys generated from the results of running * each element of `collection` through `iteratee`. The corresponding value * of each key is an array of the elements responsible for generating the key. - * The `iteratee` is bound to `thisArg` and invoked with three arguments; + * The `iteratee` is bound to `thisArg` and invoked with three arguments: * (value, index|key, collection). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * diff --git a/collection/includes.js b/collection/includes.js index 8eea3a656..2221ebf4e 100644 --- a/collection/includes.js +++ b/collection/includes.js @@ -1,5 +1,6 @@ import baseIndexOf from '../internal/baseIndexOf'; import isArray from '../lang/isArray'; +import isIterateeCall from '../internal/isIterateeCall'; import isLength from '../internal/isLength'; import isString from '../lang/isString'; import values from '../object/values'; @@ -12,10 +13,9 @@ var nativeMax = Math.max; * comparisons. If `fromIndex` is negative, it is used as the offset from * the end of `collection`. * - * **Note:** `SameValueZero` comparisons are like strict equality comparisons, - * e.g. `===`, except that `NaN` matches `NaN`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) - * for more details. + * **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 _ @@ -24,6 +24,7 @@ var nativeMax = Math.max; * @param {Array|Object|string} collection The collection to search. * @param {*} target The value to search for. * @param {number} [fromIndex=0] The index to search from. + * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`. * @returns {boolean} Returns `true` if a matching element is found, else `false`. * @example * @@ -39,7 +40,7 @@ var nativeMax = Math.max; * _.includes('pebbles', 'eb'); * // => true */ -function includes(collection, target, fromIndex) { +function includes(collection, target, fromIndex, guard) { var length = collection ? collection.length : 0; if (!isLength(length)) { collection = values(collection); @@ -48,10 +49,10 @@ function includes(collection, target, fromIndex) { if (!length) { return false; } - if (typeof fromIndex == 'number') { - fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0); - } else { + if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) { fromIndex = 0; + } else { + fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0); } return (typeof collection == 'string' || !isArray(collection) && isString(collection)) ? (fromIndex < length && collection.indexOf(target, fromIndex) > -1) diff --git a/collection/indexBy.js b/collection/indexBy.js index 25792b0e9..c848586b5 100644 --- a/collection/indexBy.js +++ b/collection/indexBy.js @@ -4,17 +4,17 @@ import createAggregator from '../internal/createAggregator'; * Creates an object composed of keys generated from the results of running * each element of `collection` through `iteratee`. The corresponding value * of each key is the last element responsible for generating the key. The - * iteratee function is bound to `thisArg` and invoked with three arguments; + * iteratee function is bound to `thisArg` and invoked with three arguments: * (value, index|key, collection). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * diff --git a/collection/invoke.js b/collection/invoke.js index ebe0c4159..5cdc5a21c 100644 --- a/collection/invoke.js +++ b/collection/invoke.js @@ -1,5 +1,6 @@ -import baseInvoke from '../internal/baseInvoke'; -import baseSlice from '../internal/baseSlice'; +import baseEach from '../internal/baseEach'; +import isLength from '../internal/isLength'; +import restParam from '../function/restParam'; /** * Invokes the method named by `methodName` on each element in `collection`, @@ -23,8 +24,17 @@ import baseSlice from '../internal/baseSlice'; * _.invoke([123, 456], String.prototype.split, ''); * // => [['1', '2', '3'], ['4', '5', '6']] */ -function invoke(collection, methodName) { - return baseInvoke(collection, methodName, baseSlice(arguments, 2)); -} +var invoke = restParam(function(collection, methodName, args) { + var index = -1, + isFunc = typeof methodName == 'function', + length = collection ? collection.length : 0, + result = isLength(length) ? Array(length) : []; + + baseEach(collection, function(value) { + var func = isFunc ? methodName : (value != null && value[methodName]); + result[++index] = func ? func.apply(value, args) : undefined; + }); + return result; +}); export default invoke; diff --git a/collection/map.js b/collection/map.js index 5ff98029a..fb8504c7a 100644 --- a/collection/map.js +++ b/collection/map.js @@ -6,16 +6,16 @@ import isArray from '../lang/isArray'; /** * Creates an array of values by running each element in `collection` through * `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three - * arguments; (value, index|key, collection). + * arguments: (value, index|key, collection). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * @@ -24,9 +24,9 @@ import isArray from '../lang/isArray'; * * The guarded methods are: * `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`, `drop`, - * `dropRight`, `fill`, `flatten`, `invert`, `max`, `min`, `parseInt`, `slice`, - * `sortBy`, `take`, `takeRight`, `template`, `trim`, `trimLeft`, `trimRight`, - * `trunc`, `random`, `range`, `sample`, `uniq`, and `words` + * `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`, `parseInt`, + * `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`, `trimLeft`, + * `trimRight`, `trunc`, `random`, `range`, `sample`, `some`, `uniq`, and `words` * * @static * @memberOf _ diff --git a/collection/partition.js b/collection/partition.js index 5394bcab8..df7438246 100644 --- a/collection/partition.js +++ b/collection/partition.js @@ -4,7 +4,7 @@ import createAggregator from '../internal/createAggregator'; * 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). + * to `thisArg` and invoked with three arguments: (value, index|key, collection). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. diff --git a/collection/reduce.js b/collection/reduce.js index 3e9b77780..55721e311 100644 --- a/collection/reduce.js +++ b/collection/reduce.js @@ -1,22 +1,20 @@ import arrayReduce from '../internal/arrayReduce'; -import baseCallback from '../internal/baseCallback'; import baseEach from '../internal/baseEach'; -import baseReduce from '../internal/baseReduce'; -import isArray from '../lang/isArray'; +import createReduce from '../internal/createReduce'; /** * Reduces `collection` to a value which is the accumulated result of running * each element in `collection` through `iteratee`, where each successive * invocation is supplied the return value of the previous. If `accumulator` * is not provided the first element of `collection` is used as the initial - * value. The `iteratee` is bound to `thisArg`and invoked with four arguments; + * value. The `iteratee` is bound to `thisArg` and invoked with four arguments: * (accumulator, value, index|key, collection). * * Many lodash methods are guarded to work as interatees for methods like * `_.reduce`, `_.reduceRight`, and `_.transform`. * * The guarded methods are: - * `assign`, `defaults`, `merge`, and `sortAllBy` + * `assign`, `defaults`, `includes`, `merge`, `sortByAll`, and `sortByOrder` * * @static * @memberOf _ @@ -40,9 +38,6 @@ import isArray from '../lang/isArray'; * }, {}); * // => { 'a': 3, 'b': 6 } (iteration order is not guaranteed) */ -function reduce(collection, iteratee, accumulator, thisArg) { - var func = isArray(collection) ? arrayReduce : baseReduce; - return func(collection, baseCallback(iteratee, thisArg, 4), accumulator, arguments.length < 3, baseEach); -} +var reduce = createReduce(arrayReduce, baseEach); export default reduce; diff --git a/collection/reduceRight.js b/collection/reduceRight.js index 1936cb708..c0a32661d 100644 --- a/collection/reduceRight.js +++ b/collection/reduceRight.js @@ -1,8 +1,6 @@ import arrayReduceRight from '../internal/arrayReduceRight'; -import baseCallback from '../internal/baseCallback'; import baseEachRight from '../internal/baseEachRight'; -import baseReduce from '../internal/baseReduce'; -import isArray from '../lang/isArray'; +import createReduce from '../internal/createReduce'; /** * This method is like `_.reduce` except that it iterates over elements of @@ -26,9 +24,6 @@ import isArray from '../lang/isArray'; * }, []); * // => [4, 5, 2, 3, 0, 1] */ -function reduceRight(collection, iteratee, accumulator, thisArg) { - var func = isArray(collection) ? arrayReduceRight : baseReduce; - return func(collection, baseCallback(iteratee, thisArg, 4), accumulator, arguments.length < 3, baseEachRight); -} +var reduceRight = createReduce(arrayReduceRight, baseEachRight); export default reduceRight; diff --git a/collection/shuffle.js b/collection/shuffle.js index 5739872e5..d2bd30f88 100644 --- a/collection/shuffle.js +++ b/collection/shuffle.js @@ -2,9 +2,8 @@ import baseRandom from '../internal/baseRandom'; import toIterable from '../internal/toIterable'; /** - * Creates an array of shuffled values, using a version of the Fisher-Yates - * shuffle. See [Wikipedia](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle) - * for more details. + * Creates an array of shuffled values, using a version of the + * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle). * * @static * @memberOf _ diff --git a/collection/some.js b/collection/some.js index 9626b94bf..8e848c42e 100644 --- a/collection/some.js +++ b/collection/some.js @@ -2,12 +2,13 @@ import arraySome from '../internal/arraySome'; import baseCallback from '../internal/baseCallback'; import baseSome from '../internal/baseSome'; import isArray from '../lang/isArray'; +import isIterateeCall from '../internal/isIterateeCall'; /** * Checks if `predicate` returns truthy for **any** element of `collection`. * The function returns as soon as it finds a passing value and does not iterate * over the entire collection. The predicate is bound to `thisArg` and invoked - * with three arguments; (value, index|key, collection). + * with three arguments: (value, index|key, collection). * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. @@ -54,6 +55,9 @@ import isArray from '../lang/isArray'; */ function some(collection, predicate, thisArg) { var func = isArray(collection) ? arraySome : baseSome; + if (thisArg && isIterateeCall(collection, predicate, thisArg)) { + predicate = null; + } if (typeof predicate != 'function' || typeof thisArg != 'undefined') { predicate = baseCallback(predicate, thisArg, 3); } diff --git a/collection/sortBy.js b/collection/sortBy.js index 28744a43e..0a114a6f7 100644 --- a/collection/sortBy.js +++ b/collection/sortBy.js @@ -9,17 +9,17 @@ import isLength from '../internal/isLength'; * Creates an array of elements, sorted in ascending order by the results of * running each element in a collection through `iteratee`. This method performs * a stable sort, that is, it preserves the original sort order of equal elements. - * The `iteratee` is bound to `thisArg` and invoked with three arguments; + * The `iteratee` is bound to `thisArg` and invoked with three arguments: * (value, index|key, collection). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * diff --git a/collection/sortByAll.js b/collection/sortByAll.js index 075a165cb..7f5a12715 100644 --- a/collection/sortByAll.js +++ b/collection/sortByAll.js @@ -25,17 +25,24 @@ import isIterateeCall from '../internal/isIterateeCall'; * _.map(_.sortByAll(users, ['user', 'age']), _.values); * // => [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]] */ -function sortByAll(collection) { +function sortByAll() { + var args = arguments, + collection = args[0], + guard = args[3], + index = 0, + length = args.length - 1; + if (collection == null) { return []; } - var args = arguments, - guard = args[3]; - - if (guard && isIterateeCall(args[1], args[2], guard)) { - args = [collection, args[1]]; + var props = Array(length); + while (index < length) { + props[index] = args[++index]; } - return baseSortByOrder(collection, baseFlatten(args, false, false, 1), []); + if (guard && isIterateeCall(args[1], args[2], guard)) { + props = args[1]; + } + return baseSortByOrder(collection, baseFlatten(props), []); } export default sortByAll; diff --git a/function.js b/function.js index 389ed5fd5..4ad274260 100644 --- a/function.js +++ b/function.js @@ -19,6 +19,7 @@ import once from './function/once'; import partial from './function/partial'; import partialRight from './function/partialRight'; import rearg from './function/rearg'; +import restParam from './function/restParam'; import spread from './function/spread'; import throttle from './function/throttle'; import wrap from './function/wrap'; @@ -45,6 +46,7 @@ export default { 'partial': partial, 'partialRight': partialRight, 'rearg': rearg, + 'restParam': restParam, 'spread': spread, 'throttle': throttle, 'wrap': wrap diff --git a/function/ary.js b/function/ary.js index 730d9b232..27323a33b 100644 --- a/function/ary.js +++ b/function/ary.js @@ -2,7 +2,7 @@ import createWrapper from '../internal/createWrapper'; import isIterateeCall from '../internal/isIterateeCall'; /** Used to compose bitmasks for wrapper metadata. */ -var ARY_FLAG = 256; +var ARY_FLAG = 128; /* Native method references for those with the same name as other `lodash` methods. */ var nativeMax = Math.max; diff --git a/function/bind.js b/function/bind.js index 88d135910..88c896da3 100644 --- a/function/bind.js +++ b/function/bind.js @@ -1,6 +1,6 @@ -import baseSlice from '../internal/baseSlice'; import createWrapper from '../internal/createWrapper'; import replaceHolders from '../internal/replaceHolders'; +import restParam from './restParam'; /** Used to compose bitmasks for wrapper metadata. */ var BIND_FLAG = 1, @@ -22,7 +22,7 @@ var BIND_FLAG = 1, * @category Function * @param {Function} func The function to bind. * @param {*} thisArg The `this` binding of `func`. - * @param {...*} [args] The arguments to be partially applied. + * @param {...*} [partials] The arguments to be partially applied. * @returns {Function} Returns the new bound function. * @example * @@ -41,16 +41,14 @@ var BIND_FLAG = 1, * bound('hi'); * // => 'hi fred!' */ -function bind(func, thisArg) { +var bind = restParam(function(func, thisArg, partials) { var bitmask = BIND_FLAG; - if (arguments.length > 2) { - var partials = baseSlice(arguments, 2), - holders = replaceHolders(partials, bind.placeholder); - + if (partials.length) { + var holders = replaceHolders(partials, bind.placeholder); bitmask |= PARTIAL_FLAG; } return createWrapper(func, bitmask, thisArg, partials, holders); -} +}); // Assign default placeholders. bind.placeholder = {}; diff --git a/function/bindAll.js b/function/bindAll.js index 76f9de992..d446ae48e 100644 --- a/function/bindAll.js +++ b/function/bindAll.js @@ -1,6 +1,10 @@ -import baseBindAll from '../internal/baseBindAll'; import baseFlatten from '../internal/baseFlatten'; +import createWrapper from '../internal/createWrapper'; import functions from '../object/functions'; +import restParam from './restParam'; + +/** Used to compose bitmasks for wrapper metadata. */ +var BIND_FLAG = 1; /** * Binds methods of an object to the object itself, overwriting the existing @@ -30,12 +34,17 @@ import functions from '../object/functions'; * jQuery('#docs').on('click', view.onClick); * // => logs 'clicked docs' when the element is clicked */ -function bindAll(object) { - return baseBindAll(object, - arguments.length > 1 - ? baseFlatten(arguments, false, false, 1) - : functions(object) - ); -} +var bindAll = restParam(function(object, methodNames) { + methodNames = methodNames.length ? baseFlatten(methodNames) : functions(object); + + var index = -1, + length = methodNames.length; + + while (++index < length) { + var key = methodNames[index]; + object[key] = createWrapper(object[key], BIND_FLAG, object); + } + return object; +}); export default bindAll; diff --git a/function/bindKey.js b/function/bindKey.js index 12e07ac72..c641f90af 100644 --- a/function/bindKey.js +++ b/function/bindKey.js @@ -1,6 +1,6 @@ -import baseSlice from '../internal/baseSlice'; import createWrapper from '../internal/createWrapper'; import replaceHolders from '../internal/replaceHolders'; +import restParam from './restParam'; /** Used to compose bitmasks for wrapper metadata. */ var BIND_FLAG = 1, @@ -24,7 +24,7 @@ var BIND_FLAG = 1, * @category Function * @param {Object} object The object the method belongs to. * @param {string} key The key of the method. - * @param {...*} [args] The arguments to be partially applied. + * @param {...*} [partials] The arguments to be partially applied. * @returns {Function} Returns the new bound function. * @example * @@ -51,16 +51,14 @@ var BIND_FLAG = 1, * bound('hi'); * // => 'hiya fred!' */ -function bindKey(object, key) { +var bindKey = restParam(function(object, key, partials) { var bitmask = BIND_FLAG | BIND_KEY_FLAG; - if (arguments.length > 2) { - var partials = baseSlice(arguments, 2), - holders = replaceHolders(partials, bindKey.placeholder); - + if (partials.length) { + var holders = replaceHolders(partials, bindKey.placeholder); bitmask |= PARTIAL_FLAG; } return createWrapper(key, bitmask, object, partials, holders); -} +}); // Assign default placeholders. bindKey.placeholder = {}; diff --git a/function/curry.js b/function/curry.js index e63b281b3..6936ca34a 100644 --- a/function/curry.js +++ b/function/curry.js @@ -1,5 +1,4 @@ -import createWrapper from '../internal/createWrapper'; -import isIterateeCall from '../internal/isIterateeCall'; +import createCurry from '../internal/createCurry'; /** Used to compose bitmasks for wrapper metadata. */ var CURRY_FLAG = 8; @@ -44,14 +43,7 @@ var CURRY_FLAG = 8; * curried(1)(_, 3)(2); * // => [1, 2, 3] */ -function curry(func, arity, guard) { - if (guard && isIterateeCall(func, arity, guard)) { - arity = null; - } - var result = createWrapper(func, CURRY_FLAG, null, null, null, null, null, arity); - result.placeholder = curry.placeholder; - return result; -} +var curry = createCurry(CURRY_FLAG); // Assign default placeholders. curry.placeholder = {}; diff --git a/function/curryRight.js b/function/curryRight.js index 4f9ff35bf..527d06b9a 100644 --- a/function/curryRight.js +++ b/function/curryRight.js @@ -1,5 +1,4 @@ -import createWrapper from '../internal/createWrapper'; -import isIterateeCall from '../internal/isIterateeCall'; +import createCurry from '../internal/createCurry'; /** Used to compose bitmasks for wrapper metadata. */ var CURRY_RIGHT_FLAG = 16; @@ -41,14 +40,7 @@ var CURRY_RIGHT_FLAG = 16; * curried(3)(1, _)(2); * // => [1, 2, 3] */ -function curryRight(func, arity, guard) { - if (guard && isIterateeCall(func, arity, guard)) { - arity = null; - } - var result = createWrapper(func, CURRY_RIGHT_FLAG, null, null, null, null, null, arity); - result.placeholder = curryRight.placeholder; - return result; -} +var curryRight = createCurry(CURRY_RIGHT_FLAG); // Assign default placeholders. curryRight.placeholder = {}; diff --git a/function/defer.js b/function/defer.js index c027a61e0..175dafca7 100644 --- a/function/defer.js +++ b/function/defer.js @@ -1,4 +1,5 @@ import baseDelay from '../internal/baseDelay'; +import restParam from './restParam'; /** * Defers invoking the `func` until the current call stack has cleared. Any @@ -17,8 +18,8 @@ import baseDelay from '../internal/baseDelay'; * }, 'deferred'); * // logs 'deferred' after one or more milliseconds */ -function defer(func) { - return baseDelay(func, 1, arguments, 1); -} +var defer = restParam(function(func, args) { + return baseDelay(func, 1, args); +}); export default defer; diff --git a/function/delay.js b/function/delay.js index 0cd1e5859..a8533881a 100644 --- a/function/delay.js +++ b/function/delay.js @@ -1,4 +1,5 @@ import baseDelay from '../internal/baseDelay'; +import restParam from './restParam'; /** * Invokes `func` after `wait` milliseconds. Any additional arguments are @@ -18,8 +19,8 @@ import baseDelay from '../internal/baseDelay'; * }, 1000, 'later'); * // => logs 'later' after one second */ -function delay(func, wait) { - return baseDelay(func, wait, arguments, 2); -} +var delay = restParam(function(func, wait, args) { + return baseDelay(func, wait, args); +}); export default delay; diff --git a/function/flow.js b/function/flow.js index a6da82918..e5edf51f2 100644 --- a/function/flow.js +++ b/function/flow.js @@ -1,4 +1,4 @@ -import createComposer from '../internal/createComposer'; +import createFlow from '../internal/createFlow'; /** * Creates a function that returns the result of invoking the provided @@ -20,6 +20,6 @@ import createComposer from '../internal/createComposer'; * addSquare(1, 2); * // => 9 */ -var flow = createComposer(); +var flow = createFlow(); export default flow; diff --git a/function/flowRight.js b/function/flowRight.js index a1a8fc128..861995144 100644 --- a/function/flowRight.js +++ b/function/flowRight.js @@ -1,4 +1,4 @@ -import createComposer from '../internal/createComposer'; +import createFlow from '../internal/createFlow'; /** * This method is like `_.flow` except that it creates a function that @@ -20,6 +20,6 @@ import createComposer from '../internal/createComposer'; * addSquare(1, 2); * // => 9 */ -var flowRight = createComposer(true); +var flowRight = createFlow(true); export default flowRight; diff --git a/function/memoize.js b/function/memoize.js index 5b0e89fe8..5d1ff7b6d 100644 --- a/function/memoize.js +++ b/function/memoize.js @@ -13,10 +13,8 @@ var FUNC_ERROR_TEXT = 'Expected a function'; * * **Note:** The cache is exposed as the `cache` property on the memoized * function. Its creation may be customized by replacing the `_.memoize.Cache` - * constructor with one whose instances implement the ES `Map` method interface - * of `get`, `has`, and `set`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-the-map-prototype-object) - * for more details. + * constructor with one whose instances implement the [`Map`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-the-map-prototype-object) + * method interface of `get`, `has`, and `set`. * * @static * @memberOf _ diff --git a/function/once.js b/function/once.js index ecfc4affe..464b00a34 100644 --- a/function/once.js +++ b/function/once.js @@ -3,7 +3,7 @@ import before from './before'; /** * Creates a function that is restricted to invoking `func` once. Repeat calls * to the function return the value of the first call. The `func` is invoked - * with the `this` binding of the created function. + * with the `this` binding and arguments of the created function. * * @static * @memberOf _ diff --git a/function/partial.js b/function/partial.js index 9b8291dbb..26c7755ab 100644 --- a/function/partial.js +++ b/function/partial.js @@ -1,6 +1,4 @@ -import baseSlice from '../internal/baseSlice'; -import createWrapper from '../internal/createWrapper'; -import replaceHolders from '../internal/replaceHolders'; +import createPartial from '../internal/createPartial'; /** Used to compose bitmasks for wrapper metadata. */ var PARTIAL_FLAG = 32; @@ -20,7 +18,7 @@ var PARTIAL_FLAG = 32; * @memberOf _ * @category Function * @param {Function} func The function to partially apply arguments to. - * @param {...*} [args] The arguments to be partially applied. + * @param {...*} [partials] The arguments to be partially applied. * @returns {Function} Returns the new partially applied function. * @example * @@ -37,12 +35,7 @@ var PARTIAL_FLAG = 32; * greetFred('hi'); * // => 'hi fred' */ -function partial(func) { - var partials = baseSlice(arguments, 1), - holders = replaceHolders(partials, partial.placeholder); - - return createWrapper(func, PARTIAL_FLAG, null, partials, holders); -} +var partial = createPartial(PARTIAL_FLAG); // Assign default placeholders. partial.placeholder = {}; diff --git a/function/partialRight.js b/function/partialRight.js index 3bb858a43..319ccee73 100644 --- a/function/partialRight.js +++ b/function/partialRight.js @@ -1,6 +1,4 @@ -import baseSlice from '../internal/baseSlice'; -import createWrapper from '../internal/createWrapper'; -import replaceHolders from '../internal/replaceHolders'; +import createPartial from '../internal/createPartial'; /** Used to compose bitmasks for wrapper metadata. */ var PARTIAL_RIGHT_FLAG = 64; @@ -19,7 +17,7 @@ var PARTIAL_RIGHT_FLAG = 64; * @memberOf _ * @category Function * @param {Function} func The function to partially apply arguments to. - * @param {...*} [args] The arguments to be partially applied. + * @param {...*} [partials] The arguments to be partially applied. * @returns {Function} Returns the new partially applied function. * @example * @@ -36,12 +34,7 @@ var PARTIAL_RIGHT_FLAG = 64; * sayHelloTo('fred'); * // => 'hello fred' */ -function partialRight(func) { - var partials = baseSlice(arguments, 1), - holders = replaceHolders(partials, partialRight.placeholder); - - return createWrapper(func, PARTIAL_RIGHT_FLAG, null, partials, holders); -} +var partialRight = createPartial(PARTIAL_RIGHT_FLAG); // Assign default placeholders. partialRight.placeholder = {}; diff --git a/function/rearg.js b/function/rearg.js index 4a9ebd2c4..5962b3471 100644 --- a/function/rearg.js +++ b/function/rearg.js @@ -1,8 +1,9 @@ import baseFlatten from '../internal/baseFlatten'; import createWrapper from '../internal/createWrapper'; +import restParam from './restParam'; /** Used to compose bitmasks for wrapper metadata. */ -var REARG_FLAG = 128; +var REARG_FLAG = 256; /** * Creates a function that invokes `func` with arguments arranged according @@ -32,9 +33,8 @@ var REARG_FLAG = 128; * }, [1, 2, 3]); * // => [3, 6, 9] */ -function rearg(func) { - var indexes = baseFlatten(arguments, false, false, 1); - return createWrapper(func, REARG_FLAG, null, null, null, indexes); -} +var rearg = restParam(function(func, indexes) { + return createWrapper(func, REARG_FLAG, null, null, null, baseFlatten(indexes)); +}); export default rearg; diff --git a/function/restParam.js b/function/restParam.js new file mode 100644 index 000000000..fecb65a31 --- /dev/null +++ b/function/restParam.js @@ -0,0 +1,58 @@ +/** Used as the `TypeError` message for "Functions" methods. */ +var FUNC_ERROR_TEXT = 'Expected a function'; + +/* Native method references for those with the same name as other `lodash` methods. */ +var nativeMax = Math.max; + +/** + * Creates a function that invokes `func` with the `this` binding of the + * created function and arguments from `start` and beyond provided as an array. + * + * **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters). + * + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to apply a rest parameter to. + * @param {number} [start=func.length-1] The start position of the rest parameter. + * @returns {Function} Returns the new function. + * @example + * + * var say = _.restParam(function(what, names) { + * return what + ' ' + _.initial(names).join(', ') + + * (_.size(names) > 1 ? ', & ' : '') + _.last(names); + * }); + * + * say('hello', 'fred', 'barney', 'pebbles'); + * // => 'hello fred, barney, & pebbles' + */ +function restParam(func, start) { + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + start = nativeMax(typeof start == 'undefined' ? (func.length - 1) : (+start || 0), 0); + return function() { + var args = arguments, + index = -1, + length = nativeMax(args.length - start, 0), + rest = Array(length); + + while (++index < length) { + rest[index] = args[start + index]; + } + switch (start) { + case 0: return func.call(this, rest); + case 1: return func.call(this, args[0], rest); + case 2: return func.call(this, args[0], args[1], rest); + } + var otherArgs = Array(start + 1); + index = -1; + while (++index < start) { + otherArgs[index] = args[index]; + } + otherArgs[start] = rest; + return func.apply(this, otherArgs); + }; +} + +export default restParam; diff --git a/function/spread.js b/function/spread.js index e0c9dfa79..3aafd9c68 100644 --- a/function/spread.js +++ b/function/spread.js @@ -2,23 +2,24 @@ var FUNC_ERROR_TEXT = 'Expected a function'; /** - * Creates a function that invokes `func` with the `this` binding of the - * created function and the array of arguments provided to the created - * function much like [Function#apply](http://es5.github.io/#x15.3.4.3). + * Creates a function that invokes `func` with the `this` binding of the created + * function and an array of arguments much like [`Function#apply`](https://es5.github.io/#x15.3.4.3). + * + * **Note:** This method is based on the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator). * * @static * @memberOf _ * @category Function * @param {Function} func The function to spread arguments over. - * @returns {*} Returns the new function. + * @returns {Function} Returns the new function. * @example * - * var spread = _.spread(function(who, what) { + * var say = _.spread(function(who, what) { * return who + ' says ' + what; * }); * - * spread(['Fred', 'hello']); - * // => 'Fred says hello' + * say(['fred', 'hello']); + * // => 'fred says hello' * * // with a Promise * var numbers = Promise.all([ diff --git a/internal/arrayEach.js b/internal/arrayEach.js index bb463580a..7bf245dca 100644 --- a/internal/arrayEach.js +++ b/internal/arrayEach.js @@ -1,6 +1,6 @@ /** * A specialized version of `_.forEach` for arrays without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Array} array The array to iterate over. diff --git a/internal/arrayEachRight.js b/internal/arrayEachRight.js index 61c51d537..8d39f733c 100644 --- a/internal/arrayEachRight.js +++ b/internal/arrayEachRight.js @@ -1,6 +1,6 @@ /** * A specialized version of `_.forEachRight` for arrays without support for - * callback shorthands or `this` binding. + * callback shorthands and `this` binding. * * @private * @param {Array} array The array to iterate over. diff --git a/internal/arrayEvery.js b/internal/arrayEvery.js index 1eaf11275..92da7c0d6 100644 --- a/internal/arrayEvery.js +++ b/internal/arrayEvery.js @@ -1,6 +1,6 @@ /** * A specialized version of `_.every` for arrays without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Array} array The array to iterate over. diff --git a/internal/arrayFilter.js b/internal/arrayFilter.js index e3d86f0b2..d9ae85782 100644 --- a/internal/arrayFilter.js +++ b/internal/arrayFilter.js @@ -1,6 +1,6 @@ /** * A specialized version of `_.filter` for arrays without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Array} array The array to iterate over. diff --git a/internal/arrayMap.js b/internal/arrayMap.js index 95a9bd3e6..59c3920b9 100644 --- a/internal/arrayMap.js +++ b/internal/arrayMap.js @@ -1,6 +1,6 @@ /** * A specialized version of `_.map` for arrays without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Array} array The array to iterate over. diff --git a/internal/arrayReduce.js b/internal/arrayReduce.js index 71f33b912..5e2cc9ac0 100644 --- a/internal/arrayReduce.js +++ b/internal/arrayReduce.js @@ -1,6 +1,6 @@ /** * A specialized version of `_.reduce` for arrays without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Array} array The array to iterate over. diff --git a/internal/arrayReduceRight.js b/internal/arrayReduceRight.js index cbf35e71f..98be5537a 100644 --- a/internal/arrayReduceRight.js +++ b/internal/arrayReduceRight.js @@ -1,6 +1,6 @@ /** * A specialized version of `_.reduceRight` for arrays without support for - * callback shorthands or `this` binding. + * callback shorthands and `this` binding. * * @private * @param {Array} array The array to iterate over. diff --git a/internal/arraySome.js b/internal/arraySome.js index a6a7daaae..48c767d9d 100644 --- a/internal/arraySome.js +++ b/internal/arraySome.js @@ -1,6 +1,6 @@ /** * A specialized version of `_.some` for arrays without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Array} array The array to iterate over. diff --git a/internal/arraySum.js b/internal/arraySum.js new file mode 100644 index 000000000..bc0c22f3c --- /dev/null +++ b/internal/arraySum.js @@ -0,0 +1,18 @@ +/** + * A specialized version of `_.sum` for arrays without support for iteratees. + * + * @private + * @param {Array} array The array to iterate over. + * @returns {number} Returns the sum. + */ +function arraySum(array) { + var length = array.length, + result = 0; + + while (length--) { + result += +array[length] || 0; + } + return result; +} + +export default arraySum; diff --git a/internal/baseBindAll.js b/internal/baseBindAll.js deleted file mode 100644 index 70ffff845..000000000 --- a/internal/baseBindAll.js +++ /dev/null @@ -1,26 +0,0 @@ -import createWrapper from './createWrapper'; - -/** Used to compose bitmasks for wrapper metadata. */ -var BIND_FLAG = 1; - -/** - * The base implementation of `_.bindAll` without support for individual - * method name arguments. - * - * @private - * @param {Object} object The object to bind and assign the bound methods to. - * @param {string[]} methodNames The object method names to bind. - * @returns {Object} Returns `object`. - */ -function baseBindAll(object, methodNames) { - var index = -1, - length = methodNames.length; - - while (++index < length) { - var key = methodNames[index]; - object[key] = createWrapper(object[key], BIND_FLAG, object); - } - return object; -} - -export default baseBindAll; diff --git a/internal/baseCallback.js b/internal/baseCallback.js index c25a72354..2f74b7947 100644 --- a/internal/baseCallback.js +++ b/internal/baseCallback.js @@ -3,7 +3,6 @@ import baseMatchesProperty from './baseMatchesProperty'; import baseProperty from './baseProperty'; import bindCallback from './bindCallback'; import identity from '../utility/identity'; -import isBindable from './isBindable'; /** * The base implementation of `_.callback` which supports specifying the @@ -18,9 +17,9 @@ import isBindable from './isBindable'; function baseCallback(func, thisArg, argCount) { var type = typeof func; if (type == 'function') { - return (typeof thisArg != 'undefined' && isBindable(func)) - ? bindCallback(func, thisArg, argCount) - : func; + return typeof thisArg == 'undefined' + ? func + : bindCallback(func, thisArg, argCount); } if (func == null) { return identity; diff --git a/internal/baseClone.js b/internal/baseClone.js index e01816f68..ded78350a 100644 --- a/internal/baseClone.js +++ b/internal/baseClone.js @@ -54,9 +54,8 @@ cloneableTags[weakMapTag] = false; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; diff --git a/internal/baseDelay.js b/internal/baseDelay.js index 6cdc35111..19cde60f9 100644 --- a/internal/baseDelay.js +++ b/internal/baseDelay.js @@ -1,5 +1,3 @@ -import baseSlice from './baseSlice'; - /** Used as the `TypeError` message for "Functions" methods. */ var FUNC_ERROR_TEXT = 'Expected a function'; @@ -10,14 +8,14 @@ var FUNC_ERROR_TEXT = 'Expected a function'; * @private * @param {Function} func The function to delay. * @param {number} wait The number of milliseconds to delay invocation. - * @param {Object} args The `arguments` object to slice and provide to `func`. + * @param {Object} args The arguments provide to `func`. * @returns {number} Returns the timer id. */ -function baseDelay(func, wait, args, fromIndex) { +function baseDelay(func, wait, args) { if (typeof func != 'function') { throw new TypeError(FUNC_ERROR_TEXT); } - return setTimeout(function() { func.apply(undefined, baseSlice(args, fromIndex)); }, wait); + return setTimeout(function() { func.apply(undefined, args); }, wait); } export default baseDelay; diff --git a/internal/baseEach.js b/internal/baseEach.js index 6ac1c58b9..d81a68762 100644 --- a/internal/baseEach.js +++ b/internal/baseEach.js @@ -1,6 +1,5 @@ import baseForOwn from './baseForOwn'; -import isLength from './isLength'; -import toObject from './toObject'; +import createBaseEach from './createBaseEach'; /** * The base implementation of `_.forEach` without support for callback @@ -11,20 +10,6 @@ import toObject from './toObject'; * @param {Function} iteratee The function invoked per iteration. * @returns {Array|Object|string} Returns `collection`. */ -function baseEach(collection, iteratee) { - var length = collection ? collection.length : 0; - if (!isLength(length)) { - return baseForOwn(collection, iteratee); - } - var index = -1, - iterable = toObject(collection); - - while (++index < length) { - if (iteratee(iterable[index], index, iterable) === false) { - break; - } - } - return collection; -} +var baseEach = createBaseEach(baseForOwn); export default baseEach; diff --git a/internal/baseEachRight.js b/internal/baseEachRight.js index abf09aab8..f3af975ff 100644 --- a/internal/baseEachRight.js +++ b/internal/baseEachRight.js @@ -1,6 +1,5 @@ import baseForOwnRight from './baseForOwnRight'; -import isLength from './isLength'; -import toObject from './toObject'; +import createBaseEach from './createBaseEach'; /** * The base implementation of `_.forEachRight` without support for callback @@ -11,18 +10,6 @@ import toObject from './toObject'; * @param {Function} iteratee The function invoked per iteration. * @returns {Array|Object|string} Returns `collection`. */ -function baseEachRight(collection, iteratee) { - var length = collection ? collection.length : 0; - if (!isLength(length)) { - return baseForOwnRight(collection, iteratee); - } - var iterable = toObject(collection); - while (length--) { - if (iteratee(iterable[length], length, iterable) === false) { - break; - } - } - return collection; -} +var baseEachRight = createBaseEach(baseForOwnRight, true); export default baseEachRight; diff --git a/internal/baseEvery.js b/internal/baseEvery.js index 979a9792a..6abb845da 100644 --- a/internal/baseEvery.js +++ b/internal/baseEvery.js @@ -2,7 +2,7 @@ import baseEach from './baseEach'; /** * The base implementation of `_.every` without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Array|Object|string} collection The collection to iterate over. diff --git a/internal/baseFilter.js b/internal/baseFilter.js index 15cc00534..278293591 100644 --- a/internal/baseFilter.js +++ b/internal/baseFilter.js @@ -2,7 +2,7 @@ import baseEach from './baseEach'; /** * The base implementation of `_.filter` without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Array|Object|string} collection The collection to iterate over. diff --git a/internal/baseFindIndex.js b/internal/baseFindIndex.js new file mode 100644 index 000000000..7beb32bc6 --- /dev/null +++ b/internal/baseFindIndex.js @@ -0,0 +1,23 @@ +/** + * The base implementation of `_.findIndex` and `_.findLastIndex` without + * support for callback shorthands and `this` binding. + * + * @private + * @param {Array} array The array to search. + * @param {Function} predicate The function invoked per iteration. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {number} Returns the index of the matched value, else `-1`. + */ +function baseFindIndex(array, predicate, fromRight) { + var length = array.length, + index = fromRight ? length : -1; + + while ((fromRight ? index-- : ++index < length)) { + if (predicate(array[index], index, array)) { + return index; + } + } + return -1; +} + +export default baseFindIndex; diff --git a/internal/baseFlatten.js b/internal/baseFlatten.js index ff0a993e1..7b0c54ddc 100644 --- a/internal/baseFlatten.js +++ b/internal/baseFlatten.js @@ -11,11 +11,10 @@ import isObjectLike from './isObjectLike'; * @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 The index to start from. * @returns {Array} Returns the new flattened array. */ -function baseFlatten(array, isDeep, isStrict, fromIndex) { - var index = fromIndex - 1, +function baseFlatten(array, isDeep, isStrict) { + var index = -1, length = array.length, resIndex = -1, result = []; @@ -26,7 +25,7 @@ function baseFlatten(array, isDeep, isStrict, fromIndex) { 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, 0); + value = baseFlatten(value, isDeep, isStrict); } var valIndex = -1, valLength = value.length; diff --git a/internal/baseFor.js b/internal/baseFor.js index e53819df9..c336da0fd 100644 --- a/internal/baseFor.js +++ b/internal/baseFor.js @@ -1,4 +1,4 @@ -import toObject from './toObject'; +import createBaseFor from './createBaseFor'; /** * The base implementation of `baseForIn` and `baseForOwn` which iterates @@ -12,19 +12,6 @@ import toObject from './toObject'; * @param {Function} keysFunc The function to get the keys of `object`. * @returns {Object} Returns `object`. */ -function baseFor(object, iteratee, keysFunc) { - var index = -1, - iterable = toObject(object), - props = keysFunc(object), - length = props.length; - - while (++index < length) { - var key = props[index]; - if (iteratee(iterable[key], key, iterable) === false) { - break; - } - } - return object; -} +var baseFor = createBaseFor(); export default baseFor; diff --git a/internal/baseForRight.js b/internal/baseForRight.js index 84444331d..2525791dd 100644 --- a/internal/baseForRight.js +++ b/internal/baseForRight.js @@ -1,4 +1,4 @@ -import toObject from './toObject'; +import createBaseFor from './createBaseFor'; /** * This function is like `baseFor` except that it iterates over properties @@ -10,18 +10,6 @@ import toObject from './toObject'; * @param {Function} keysFunc The function to get the keys of `object`. * @returns {Object} Returns `object`. */ -function baseForRight(object, iteratee, keysFunc) { - var iterable = toObject(object), - props = keysFunc(object), - length = props.length; - - while (length--) { - var key = props[length]; - if (iteratee(iterable[key], key, iterable) === false) { - break; - } - } - return object; -} +var baseForRight = createBaseFor(true); export default baseForRight; diff --git a/internal/baseInvoke.js b/internal/baseInvoke.js deleted file mode 100644 index 9aa6fcb97..000000000 --- a/internal/baseInvoke.js +++ /dev/null @@ -1,28 +0,0 @@ -import baseEach from './baseEach'; -import isLength from './isLength'; - -/** - * The base implementation of `_.invoke` which requires additional arguments - * to be provided as an array of arguments rather than individually. - * - * @private - * @param {Array|Object|string} collection The collection to iterate over. - * @param {Function|string} methodName The name of the method to invoke or - * the function invoked per iteration. - * @param {Array} [args] The arguments to invoke the method with. - * @returns {Array} Returns the array of results. - */ -function baseInvoke(collection, methodName, args) { - var index = -1, - isFunc = typeof methodName == 'function', - length = collection ? collection.length : 0, - result = isLength(length) ? Array(length) : []; - - baseEach(collection, function(value) { - var func = isFunc ? methodName : (value != null && value[methodName]); - result[++index] = func ? func.apply(value, args) : undefined; - }); - return result; -} - -export default baseInvoke; diff --git a/internal/baseIsEqual.js b/internal/baseIsEqual.js index 5e8e35abf..cbe78bcd7 100644 --- a/internal/baseIsEqual.js +++ b/internal/baseIsEqual.js @@ -8,12 +8,12 @@ import baseIsEqualDeep from './baseIsEqualDeep'; * @param {*} value The value to compare. * @param {*} other The other value to compare. * @param {Function} [customizer] The function to customize comparing values. - * @param {boolean} [isWhere] Specify performing partial comparisons. + * @param {boolean} [isLoose] Specify performing partial comparisons. * @param {Array} [stackA] Tracks traversed `value` objects. * @param {Array} [stackB] Tracks traversed `other` objects. * @returns {boolean} Returns `true` if the values are equivalent, else `false`. */ -function baseIsEqual(value, other, customizer, isWhere, stackA, stackB) { +function baseIsEqual(value, other, customizer, isLoose, stackA, stackB) { // Exit early for identical values. if (value === other) { // Treat `+0` vs. `-0` as not equal. @@ -28,7 +28,7 @@ function baseIsEqual(value, other, customizer, isWhere, stackA, stackB) { // Return `false` unless both values are `NaN`. return value !== value && other !== other; } - return baseIsEqualDeep(value, other, baseIsEqual, customizer, isWhere, stackA, stackB); + return baseIsEqualDeep(value, other, baseIsEqual, customizer, isLoose, stackA, stackB); } export default baseIsEqual; diff --git a/internal/baseIsEqualDeep.js b/internal/baseIsEqualDeep.js index e7ac95141..daf31f91a 100644 --- a/internal/baseIsEqualDeep.js +++ b/internal/baseIsEqualDeep.js @@ -7,6 +7,7 @@ import isTypedArray from '../lang/isTypedArray'; /** `Object#toString` result references. */ var argsTag = '[object Arguments]', arrayTag = '[object Array]', + funcTag = '[object Function]', objectTag = '[object Object]'; /** Used for native method references. */ @@ -16,9 +17,8 @@ var objectProto = Object.prototype; var hasOwnProperty = objectProto.hasOwnProperty; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -32,12 +32,12 @@ var objToString = objectProto.toString; * @param {Object} other The other object to compare. * @param {Function} equalFunc The function to determine equivalents of values. * @param {Function} [customizer] The function to customize comparing objects. - * @param {boolean} [isWhere] Specify performing partial comparisons. + * @param {boolean} [isLoose] Specify performing partial comparisons. * @param {Array} [stackA=[]] Tracks traversed `value` objects. * @param {Array} [stackB=[]] Tracks traversed `other` objects. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`. */ -function baseIsEqualDeep(object, other, equalFunc, customizer, isWhere, stackA, stackB) { +function baseIsEqualDeep(object, other, equalFunc, customizer, isLoose, stackA, stackB) { var objIsArr = isArray(object), othIsArr = isArray(other), objTag = arrayTag, @@ -59,21 +59,27 @@ function baseIsEqualDeep(object, other, equalFunc, customizer, isWhere, stackA, othIsArr = isTypedArray(other); } } - var objIsObj = objTag == objectTag, - othIsObj = othTag == objectTag, + var objIsObj = (objTag == objectTag || (isLoose && objTag == funcTag)), + othIsObj = (othTag == objectTag || (isLoose && othTag == funcTag)), isSameTag = objTag == othTag; if (isSameTag && !(objIsArr || objIsObj)) { return equalByTag(object, other, objTag); } - var valWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'), - othWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__'); + if (isLoose) { + if (!isSameTag && !(objIsObj && othIsObj)) { + return false; + } + } else { + var valWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'), + othWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__'); - if (valWrapped || othWrapped) { - return equalFunc(valWrapped ? object.value() : object, othWrapped ? other.value() : other, customizer, isWhere, stackA, stackB); - } - if (!isSameTag) { - return false; + if (valWrapped || othWrapped) { + return equalFunc(valWrapped ? object.value() : object, othWrapped ? other.value() : other, customizer, isLoose, stackA, stackB); + } + if (!isSameTag) { + return false; + } } // Assume cyclic values are equal. // For more information on detecting circular references see https://es5.github.io/#JO. @@ -90,7 +96,7 @@ function baseIsEqualDeep(object, other, equalFunc, customizer, isWhere, stackA, stackA.push(object); stackB.push(other); - var result = (objIsArr ? equalArrays : equalObjects)(object, other, equalFunc, customizer, isWhere, stackA, stackB); + var result = (objIsArr ? equalArrays : equalObjects)(object, other, equalFunc, customizer, isLoose, stackA, stackB); stackA.pop(); stackB.pop(); diff --git a/internal/baseIsMatch.js b/internal/baseIsMatch.js index 09ced8b11..4815a15e4 100644 --- a/internal/baseIsMatch.js +++ b/internal/baseIsMatch.js @@ -1,14 +1,8 @@ import baseIsEqual from './baseIsEqual'; -/** Used for native method references. */ -var objectProto = Object.prototype; - -/** Used to check objects for own properties. */ -var hasOwnProperty = objectProto.hasOwnProperty; - /** * The base implementation of `_.isMatch` without support for callback - * shorthands or `this` binding. + * shorthands and `this` binding. * * @private * @param {Object} object The object to inspect. @@ -19,30 +13,27 @@ var hasOwnProperty = objectProto.hasOwnProperty; * @returns {boolean} Returns `true` if `object` is a match, else `false`. */ function baseIsMatch(object, props, values, strictCompareFlags, customizer) { - var length = props.length; - if (object == null) { - return !length; - } var index = -1, + length = props.length, noCustomizer = !customizer; while (++index < length) { if ((noCustomizer && strictCompareFlags[index]) ? values[index] !== object[props[index]] - : !hasOwnProperty.call(object, props[index]) + : !(props[index] in object) ) { return false; } } index = -1; while (++index < length) { - var key = props[index]; - if (noCustomizer && strictCompareFlags[index]) { - var result = hasOwnProperty.call(object, key); - } else { - var objValue = object[key], - srcValue = values[index]; + var key = props[index], + objValue = object[key], + srcValue = values[index]; + if (noCustomizer && strictCompareFlags[index]) { + var result = typeof objValue != 'undefined' || (key in object); + } else { result = customizer ? customizer(objValue, srcValue, key) : undefined; if (typeof result == 'undefined') { result = baseIsEqual(srcValue, objValue, customizer, true); diff --git a/internal/baseMap.js b/internal/baseMap.js index 11242f02e..53df2f4d4 100644 --- a/internal/baseMap.js +++ b/internal/baseMap.js @@ -2,7 +2,7 @@ import baseEach from './baseEach'; /** * The base implementation of `_.map` without support for callback shorthands - * or `this` binding. + * and `this` binding. * * @private * @param {Array|Object|string} collection The collection to iterate over. diff --git a/internal/baseMatches.js b/internal/baseMatches.js index 6d0bd1a3f..cefb123b4 100644 --- a/internal/baseMatches.js +++ b/internal/baseMatches.js @@ -1,12 +1,8 @@ import baseIsMatch from './baseIsMatch'; +import constant from '../utility/constant'; import isStrictComparable from './isStrictComparable'; import keys from '../object/keys'; - -/** Used for native method references. */ -var objectProto = Object.prototype; - -/** Used to check objects for own properties. */ -var hasOwnProperty = objectProto.hasOwnProperty; +import toObject from './toObject'; /** * The base implementation of `_.matches` which does not clone `source`. @@ -19,13 +15,17 @@ function baseMatches(source) { var props = keys(source), length = props.length; + if (!length) { + return constant(true); + } if (length == 1) { var key = props[0], value = source[key]; if (isStrictComparable(value)) { return function(object) { - return object != null && object[key] === value && hasOwnProperty.call(object, key); + return object != null && object[key] === value && + (typeof value != 'undefined' || (key in toObject(object))); }; } } @@ -38,7 +38,7 @@ function baseMatches(source) { strictCompareFlags[length] = isStrictComparable(value); } return function(object) { - return baseIsMatch(object, props, values, strictCompareFlags); + return object != null && baseIsMatch(toObject(object), props, values, strictCompareFlags); }; } diff --git a/internal/baseMatchesProperty.js b/internal/baseMatchesProperty.js index 241c4c9d3..242b690c2 100644 --- a/internal/baseMatchesProperty.js +++ b/internal/baseMatchesProperty.js @@ -1,5 +1,6 @@ import baseIsEqual from './baseIsEqual'; import isStrictComparable from './isStrictComparable'; +import toObject from './toObject'; /** * The base implementation of `_.matchesProperty` which does not coerce `key` @@ -13,7 +14,8 @@ import isStrictComparable from './isStrictComparable'; function baseMatchesProperty(key, value) { if (isStrictComparable(value)) { return function(object) { - return object != null && object[key] === value; + return object != null && object[key] === value && + (typeof value != 'undefined' || (key in toObject(object))); }; } return function(object) { diff --git a/internal/baseMergeDeep.js b/internal/baseMergeDeep.js index 5bce59b04..d3e67e7b9 100644 --- a/internal/baseMergeDeep.js +++ b/internal/baseMergeDeep.js @@ -40,7 +40,7 @@ function baseMergeDeep(object, source, key, mergeFunc, customizer, stackA, stack if (isLength(srcValue.length) && (isArray(srcValue) || isTypedArray(srcValue))) { result = isArray(value) ? value - : (value ? arrayCopy(value) : []); + : ((value && value.length) ? arrayCopy(value) : []); } else if (isPlainObject(srcValue) || isArguments(srcValue)) { result = isArguments(value) diff --git a/internal/basePullAt.js b/internal/basePullAt.js deleted file mode 100644 index 54302200b..000000000 --- a/internal/basePullAt.js +++ /dev/null @@ -1,35 +0,0 @@ -import baseAt from './baseAt'; -import baseCompareAscending from './baseCompareAscending'; -import isIndex from './isIndex'; - -/** Used for native method references. */ -var arrayProto = Array.prototype; - -/** Native method references. */ -var splice = arrayProto.splice; - -/** - * The base implementation of `_.pullAt` without support for individual - * index arguments. - * - * @private - * @param {Array} array The array to modify. - * @param {number[]} indexes The indexes of elements to remove. - * @returns {Array} Returns the new array of removed elements. - */ -function basePullAt(array, indexes) { - var length = indexes.length, - result = baseAt(array, indexes); - - indexes.sort(baseCompareAscending); - while (length--) { - var index = parseFloat(indexes[length]); - if (index != previous && isIndex(index)) { - var previous = index; - splice.call(array, index, 1); - } - } - return result; -} - -export default basePullAt; diff --git a/internal/baseReduce.js b/internal/baseReduce.js index ad240c357..2a7e51afb 100644 --- a/internal/baseReduce.js +++ b/internal/baseReduce.js @@ -1,6 +1,6 @@ /** * The base implementation of `_.reduce` and `_.reduceRight` without support - * for callback shorthands or `this` binding, which iterates over `collection` + * for callback shorthands and `this` binding, which iterates over `collection` * using the provided `eachFunc`. * * @private diff --git a/internal/baseSome.js b/internal/baseSome.js index b6f0a3c71..014bff510 100644 --- a/internal/baseSome.js +++ b/internal/baseSome.js @@ -2,7 +2,7 @@ import baseEach from './baseEach'; /** * The base implementation of `_.some` without support for callback shorthands - * or `this` binding. + * and `this` binding. * * @private * @param {Array|Object|string} collection The collection to iterate over. diff --git a/internal/baseSum.js b/internal/baseSum.js new file mode 100644 index 000000000..53953c10e --- /dev/null +++ b/internal/baseSum.js @@ -0,0 +1,20 @@ +import baseEach from './baseEach'; + +/** + * The base implementation of `_.sum` without support for callback shorthands + * and `this` binding. + * + * @private + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {number} Returns the sum. + */ +function baseSum(collection, iteratee) { + var result = 0; + baseEach(collection, function(value, index, collection) { + result += +iteratee(value, index, collection) || 0; + }); + return result; +} + +export default baseSum; diff --git a/internal/baseWhile.js b/internal/baseWhile.js new file mode 100644 index 000000000..997f111fa --- /dev/null +++ b/internal/baseWhile.js @@ -0,0 +1,24 @@ +import baseSlice from './baseSlice'; + +/** + * The base implementation of `_.dropRightWhile`, `_.dropWhile`, `_.takeRightWhile`, + * and `_.takeWhile` without support for callback shorthands and `this` binding. + * + * @private + * @param {Array} array The array to query. + * @param {Function} predicate The function invoked per iteration. + * @param {boolean} [isDrop] Specify dropping elements instead of taking them. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Array} Returns the slice of `array`. + */ +function baseWhile(array, predicate, isDrop, fromRight) { + var length = array.length, + index = fromRight ? length : -1; + + while ((fromRight ? index-- : ++index < length) && predicate(array[index], index, array)) {} + return isDrop + ? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length)) + : baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index)); +} + +export default baseWhile; diff --git a/internal/baseWrapperValue.js b/internal/baseWrapperValue.js index b6a9f8179..2b168f950 100644 --- a/internal/baseWrapperValue.js +++ b/internal/baseWrapperValue.js @@ -14,7 +14,7 @@ var push = arrayProto.push; * @private * @param {*} value The unwrapped value. * @param {Array} actions Actions to peform to resolve the unwrapped value. - * @returns {*} Returns the resolved unwrapped value. + * @returns {*} Returns the resolved value. */ function baseWrapperValue(value, actions) { var result = value; diff --git a/internal/binaryIndex.js b/internal/binaryIndex.js index 984162bf5..d67ec136c 100644 --- a/internal/binaryIndex.js +++ b/internal/binaryIndex.js @@ -12,8 +12,7 @@ var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1, * @private * @param {Array} array The sorted array to inspect. * @param {*} value The value to evaluate. - * @param {boolean} [retHighest] Specify returning the highest, instead - * of the lowest, index at which a value should be inserted into `array`. + * @param {boolean} [retHighest] Specify returning the highest qualified index. * @returns {number} Returns the index at which `value` should be inserted * into `array`. */ diff --git a/internal/binaryIndexBy.js b/internal/binaryIndexBy.js index 3e07e2246..022be3cde 100644 --- a/internal/binaryIndexBy.js +++ b/internal/binaryIndexBy.js @@ -17,8 +17,7 @@ var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1, * @param {Array} array The sorted array to inspect. * @param {*} value The value to evaluate. * @param {Function} iteratee The function invoked per iteration. - * @param {boolean} [retHighest] Specify returning the highest, instead - * of the lowest, index at which a value should be inserted into `array`. + * @param {boolean} [retHighest] Specify returning the highest qualified index. * @returns {number} Returns the index at which `value` should be inserted * into `array`. */ diff --git a/internal/createAggregator.js b/internal/createAggregator.js index 32dc868ac..7fde08919 100644 --- a/internal/createAggregator.js +++ b/internal/createAggregator.js @@ -7,6 +7,9 @@ import isArray from '../lang/isArray'; * object composed from the results of running each element in the collection * through an iteratee. * + * **Note:** This function is used to create `_.countBy`, `_.groupBy`, `_.indexBy`, + * and `_.partition`. + * * @private * @param {Function} setter The function to set keys and values of the accumulator object. * @param {Function} [initializer] The function to initialize the accumulator object. diff --git a/internal/createAssigner.js b/internal/createAssigner.js index 480907b1b..eb48c0b1c 100644 --- a/internal/createAssigner.js +++ b/internal/createAssigner.js @@ -5,6 +5,8 @@ import isIterateeCall from './isIterateeCall'; * Creates a function that assigns properties of source object(s) to a given * destination object. * + * **Note:** This function is used to create `_.assign`, `_.defaults`, and `_.merge`. + * * @private * @param {Function} assigner The function to assign values. * @returns {Function} Returns the new assigner function. diff --git a/internal/createBaseEach.js b/internal/createBaseEach.js new file mode 100644 index 000000000..9d332e505 --- /dev/null +++ b/internal/createBaseEach.js @@ -0,0 +1,30 @@ +import isLength from './isLength'; +import toObject from './toObject'; + +/** + * Creates a `baseEach` or `baseEachRight` function. + * + * @private + * @param {Function} eachFunc The function to iterate over a collection. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new base function. + */ +function createBaseEach(eachFunc, fromRight) { + return function(collection, iteratee) { + var length = collection ? collection.length : 0; + if (!isLength(length)) { + return eachFunc(collection, iteratee); + } + var index = fromRight ? length : -1, + iterable = toObject(collection); + + while ((fromRight ? index-- : ++index < length)) { + if (iteratee(iterable[index], index, iterable) === false) { + break; + } + } + return collection; + }; +} + +export default createBaseEach; diff --git a/internal/createBaseFor.js b/internal/createBaseFor.js new file mode 100644 index 000000000..0bc9540b2 --- /dev/null +++ b/internal/createBaseFor.js @@ -0,0 +1,27 @@ +import toObject from './toObject'; + +/** + * Creates a base function for `_.forIn` or `_.forInRight`. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new base function. + */ +function createBaseFor(fromRight) { + return function(object, iteratee, keysFunc) { + var iterable = toObject(object), + props = keysFunc(object), + length = props.length, + index = fromRight ? length : -1; + + while ((fromRight ? index-- : ++index < length)) { + var key = props[index]; + if (iteratee(iterable[key], key, iterable) === false) { + break; + } + } + return object; + }; +} + +export default createBaseFor; diff --git a/internal/createComposer.js b/internal/createComposer.js deleted file mode 100644 index 2727f3603..000000000 --- a/internal/createComposer.js +++ /dev/null @@ -1,39 +0,0 @@ -/** 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; - }; - }; -} - -export default createComposer; diff --git a/internal/createCurry.js b/internal/createCurry.js new file mode 100644 index 000000000..19015dbb8 --- /dev/null +++ b/internal/createCurry.js @@ -0,0 +1,23 @@ +import createWrapper from './createWrapper'; +import isIterateeCall from './isIterateeCall'; + +/** + * Creates a `_.curry` or `_.curryRight` function. + * + * @private + * @param {boolean} flag The curry bit flag. + * @returns {Function} Returns the new curry function. + */ +function createCurry(flag) { + function curryFunc(func, arity, guard) { + if (guard && isIterateeCall(func, arity, guard)) { + arity = null; + } + var result = createWrapper(func, flag, null, null, null, null, null, arity); + result.placeholder = curryFunc.placeholder; + return result; + } + return curryFunc; +} + +export default createCurry; diff --git a/internal/createExtremum.js b/internal/createExtremum.js index a2b6393b3..41d1456ec 100644 --- a/internal/createExtremum.js +++ b/internal/createExtremum.js @@ -7,7 +7,7 @@ import isString from '../lang/isString'; import toIterable from './toIterable'; /** - * Creates a function that gets the extremum value of a collection. + * Creates a `_.max` or `_.min` function. * * @private * @param {Function} arrayFunc The function to get the extremum value from an array. diff --git a/internal/createFind.js b/internal/createFind.js new file mode 100644 index 000000000..10ca957dd --- /dev/null +++ b/internal/createFind.js @@ -0,0 +1,25 @@ +import baseCallback from './baseCallback'; +import baseFind from './baseFind'; +import baseFindIndex from './baseFindIndex'; +import isArray from '../lang/isArray'; + +/** + * Creates a `_.find` or `_.findLast` function. + * + * @private + * @param {Function} eachFunc The function to iterate over a collection. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new find function. + */ +function createFind(eachFunc, fromRight) { + return function(collection, predicate, thisArg) { + predicate = baseCallback(predicate, thisArg, 3); + if (isArray(collection)) { + var index = baseFindIndex(collection, predicate, fromRight); + return index > -1 ? collection[index] : undefined; + } + return baseFind(collection, predicate, eachFunc); + } +} + +export default createFind; diff --git a/internal/createFindIndex.js b/internal/createFindIndex.js new file mode 100644 index 000000000..a9b8cf7cc --- /dev/null +++ b/internal/createFindIndex.js @@ -0,0 +1,21 @@ +import baseCallback from './baseCallback'; +import baseFindIndex from './baseFindIndex'; + +/** + * Creates a `_.findIndex` or `_.findLastIndex` function. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new find function. + */ +function createFindIndex(fromRight) { + return function(array, predicate, thisArg) { + if (!(array && array.length)) { + return -1; + } + predicate = baseCallback(predicate, thisArg, 3); + return baseFindIndex(array, predicate, fromRight); + }; +} + +export default createFindIndex; diff --git a/internal/createFindKey.js b/internal/createFindKey.js new file mode 100644 index 000000000..f51ac2b31 --- /dev/null +++ b/internal/createFindKey.js @@ -0,0 +1,18 @@ +import baseCallback from './baseCallback'; +import baseFind from './baseFind'; + +/** + * Creates a `_.findKey` or `_.findLastKey` function. + * + * @private + * @param {Function} objectFunc The function to iterate over an object. + * @returns {Function} Returns the new find function. + */ +function createFindKey(objectFunc) { + return function(object, predicate, thisArg) { + predicate = baseCallback(predicate, thisArg, 3); + return baseFind(object, predicate, objectFunc, true); + }; +} + +export default createFindKey; diff --git a/internal/createFlow.js b/internal/createFlow.js new file mode 100644 index 000000000..80e48b35f --- /dev/null +++ b/internal/createFlow.js @@ -0,0 +1,64 @@ +import LodashWrapper from './LodashWrapper'; +import getData from './getData'; +import getFuncName from './getFuncName'; +import isArray from '../lang/isArray'; +import isLaziable from './isLaziable'; + +/** Used as the `TypeError` message for "Functions" methods. */ +var FUNC_ERROR_TEXT = 'Expected a function'; + +/** + * Creates a `_.flow` or `_.flowRight` function. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new flow function. + */ +function createFlow(fromRight) { + return function() { + var length = arguments.length; + if (!length) { + return function() { return arguments[0]; }; + } + var wrapper, + index = fromRight ? length : -1, + leftIndex = 0, + funcs = Array(length); + + while ((fromRight ? index-- : ++index < length)) { + var func = funcs[leftIndex++] = arguments[index]; + if (typeof func != 'function') { + throw new TypeError(FUNC_ERROR_TEXT); + } + var funcName = wrapper ? '' : getFuncName(func); + wrapper = funcName == 'wrapper' ? new LodashWrapper([]) : wrapper; + } + index = wrapper ? -1 : length; + while (++index < length) { + func = funcs[index]; + funcName = getFuncName(func); + + var data = funcName == 'wrapper' ? getData(func) : null; + if (data && isLaziable(data[0])) { + wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]); + } else { + wrapper = (func.length == 1 && isLaziable(func)) ? wrapper[funcName]() : wrapper.thru(func); + } + } + return function() { + var args = arguments; + if (wrapper && args.length == 1 && isArray(args[0])) { + return wrapper.plant(args[0]).value(); + } + var index = 0, + result = funcs[index].apply(this, args); + + while (++index < length) { + result = funcs[index].call(this, result); + } + return result; + }; + }; +} + +export default createFlow; diff --git a/internal/createForEach.js b/internal/createForEach.js new file mode 100644 index 000000000..da49217e8 --- /dev/null +++ b/internal/createForEach.js @@ -0,0 +1,20 @@ +import bindCallback from './bindCallback'; +import isArray from '../lang/isArray'; + +/** + * Creates a function for `_.forEach` or `_.forEachRight`. + * + * @private + * @param {Function} arrayFunc The function to iterate over an array. + * @param {Function} eachFunc The function to iterate over a collection. + * @returns {Function} Returns the new each function. + */ +function createForEach(arrayFunc, eachFunc) { + return function(collection, iteratee, thisArg) { + return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection)) + ? arrayFunc(collection, iteratee) + : eachFunc(collection, bindCallback(iteratee, thisArg, 3)); + }; +} + +export default createForEach; diff --git a/internal/createForIn.js b/internal/createForIn.js new file mode 100644 index 000000000..d6849fffa --- /dev/null +++ b/internal/createForIn.js @@ -0,0 +1,20 @@ +import bindCallback from './bindCallback'; +import keysIn from '../object/keysIn'; + +/** + * Creates a function for `_.forIn` or `_.forInRight`. + * + * @private + * @param {Function} objectFunc The function to iterate over an object. + * @returns {Function} Returns the new each function. + */ +function createForIn(objectFunc) { + return function(object, iteratee, thisArg) { + if (typeof iteratee != 'function' || typeof thisArg != 'undefined') { + iteratee = bindCallback(iteratee, thisArg, 3); + } + return objectFunc(object, iteratee, keysIn); + }; +} + +export default createForIn; diff --git a/internal/createForOwn.js b/internal/createForOwn.js new file mode 100644 index 000000000..3b5dc796a --- /dev/null +++ b/internal/createForOwn.js @@ -0,0 +1,19 @@ +import bindCallback from './bindCallback'; + +/** + * Creates a function for `_.forOwn` or `_.forOwnRight`. + * + * @private + * @param {Function} objectFunc The function to iterate over an object. + * @returns {Function} Returns the new each function. + */ +function createForOwn(objectFunc) { + return function(object, iteratee, thisArg) { + if (typeof iteratee != 'function' || typeof thisArg != 'undefined') { + iteratee = bindCallback(iteratee, thisArg, 3); + } + return objectFunc(object, iteratee); + }; +} + +export default createForOwn; diff --git a/internal/createHybridWrapper.js b/internal/createHybridWrapper.js index 4c2ca6869..6eeeae026 100644 --- a/internal/createHybridWrapper.js +++ b/internal/createHybridWrapper.js @@ -2,9 +2,11 @@ import arrayCopy from './arrayCopy'; import composeArgs from './composeArgs'; import composeArgsRight from './composeArgsRight'; import createCtorWrapper from './createCtorWrapper'; +import isLaziable from './isLaziable'; import reorder from './reorder'; import replaceHolders from './replaceHolders'; import root from './root'; +import setData from './setData'; /** Used to compose bitmasks for wrapper metadata. */ var BIND_FLAG = 1, @@ -14,7 +16,7 @@ var BIND_FLAG = 1, CURRY_RIGHT_FLAG = 16, PARTIAL_FLAG = 32, PARTIAL_RIGHT_FLAG = 64, - ARY_FLAG = 256; + ARY_FLAG = 128; /* Native method references for those with the same name as other `lodash` methods. */ var nativeMax = Math.max; @@ -82,7 +84,12 @@ function createHybridWrapper(func, bitmask, thisArg, partials, holders, partials if (!isCurryBound) { bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG); } - var result = createHybridWrapper(func, bitmask, thisArg, newPartials, newsHolders, newPartialsRight, newHoldersRight, newArgPos, ary, newArity); + var newData = [func, bitmask, thisArg, newPartials, newsHolders, newPartialsRight, newHoldersRight, newArgPos, ary, newArity], + result = createHybridWrapper.apply(undefined, newData); + + if (isLaziable(func)) { + setData(result, newData); + } result.placeholder = placeholder; return result; } diff --git a/internal/createPadDir.js b/internal/createPadDir.js new file mode 100644 index 000000000..d6059b56f --- /dev/null +++ b/internal/createPadDir.js @@ -0,0 +1,18 @@ +import baseToString from './baseToString'; +import createPadding from './createPadding'; + +/** + * Creates a function for `_.padLeft` or `_.padRight`. + * + * @private + * @param {boolean} [fromRight] Specify padding from the right. + * @returns {Function} Returns the new pad function. + */ +function createPadDir(fromRight) { + return function(string, length, chars) { + string = baseToString(string); + return string && ((fromRight ? string : '') + createPadding(string, length, chars) + (fromRight ? '' : string)); + }; +} + +export default createPadDir; diff --git a/internal/createPad.js b/internal/createPadding.js similarity index 75% rename from internal/createPad.js rename to internal/createPadding.js index e87091640..b3f52a523 100644 --- a/internal/createPad.js +++ b/internal/createPadding.js @@ -8,9 +8,8 @@ var ceil = Math.ceil; var nativeIsFinite = root.isFinite; /** - * Creates the pad required for `string` based on the given padding length. - * The `chars` string may be truncated if the number of padding characters - * exceeds the padding length. + * Creates the padding required for `string` based on the given `length`. + * The `chars` string is truncated if the number of characters exceeds `length`. * * @private * @param {string} string The string to create padding for. @@ -18,7 +17,7 @@ var nativeIsFinite = root.isFinite; * @param {string} [chars=' '] The string used as padding. * @returns {string} Returns the pad for `string`. */ -function createPad(string, length, chars) { +function createPadding(string, length, chars) { var strLength = string.length; length = +length; @@ -30,4 +29,4 @@ function createPad(string, length, chars) { return repeat(chars, ceil(padLength / chars.length)).slice(0, padLength); } -export default createPad; +export default createPadding; diff --git a/internal/createPartial.js b/internal/createPartial.js new file mode 100644 index 000000000..5d0a598b1 --- /dev/null +++ b/internal/createPartial.js @@ -0,0 +1,20 @@ +import createWrapper from './createWrapper'; +import replaceHolders from './replaceHolders'; +import restParam from '../function/restParam'; + +/** + * Creates a `_.partial` or `_.partialRight` function. + * + * @private + * @param {boolean} flag The partial bit flag. + * @returns {Function} Returns the new partial function. + */ +function createPartial(flag) { + var partialFunc = restParam(function(func, partials) { + var holders = replaceHolders(partials, partialFunc.placeholder); + return createWrapper(func, flag, null, partials, holders); + }); + return partialFunc; +} + +export default createPartial; diff --git a/internal/createReduce.js b/internal/createReduce.js new file mode 100644 index 000000000..21ed9e086 --- /dev/null +++ b/internal/createReduce.js @@ -0,0 +1,22 @@ +import baseCallback from './baseCallback'; +import baseReduce from './baseReduce'; +import isArray from '../lang/isArray'; + +/** + * Creates a function for `_.reduce` or `_.reduceRight`. + * + * @private + * @param {Function} arrayFunc The function to iterate over an array. + * @param {Function} eachFunc The function to iterate over a collection. + * @returns {Function} Returns the new each function. + */ +function createReduce(arrayFunc, eachFunc) { + return function(collection, iteratee, accumulator, thisArg) { + var initFromArray = arguments.length < 3; + return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection)) + ? arrayFunc(collection, iteratee, accumulator, initFromArray) + : baseReduce(collection, baseCallback(iteratee, thisArg, 4), accumulator, initFromArray, eachFunc); + }; +} + +export default createReduce; diff --git a/internal/createSortedIndex.js b/internal/createSortedIndex.js new file mode 100644 index 000000000..b7807bba0 --- /dev/null +++ b/internal/createSortedIndex.js @@ -0,0 +1,20 @@ +import baseCallback from './baseCallback'; +import binaryIndex from './binaryIndex'; +import binaryIndexBy from './binaryIndexBy'; + +/** + * Creates a `_.sortedIndex` or `_.sortedLastIndex` function. + * + * @private + * @param {boolean} [retHighest] Specify returning the highest qualified index. + * @returns {Function} Returns the new index function. + */ +function createSortedIndex(retHighest) { + return function(array, value, iteratee, thisArg) { + return iteratee == null + ? binaryIndex(array, value, retHighest) + : binaryIndexBy(array, value, baseCallback(iteratee, thisArg, 1), retHighest); + }; +} + +export default createSortedIndex; diff --git a/internal/createWrapper.js b/internal/createWrapper.js index 5d4a4eaa1..7f5f72eeb 100644 --- a/internal/createWrapper.js +++ b/internal/createWrapper.js @@ -60,10 +60,10 @@ function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, a partials = holders = null; } - var data = !isBindKey && getData(func), + var data = isBindKey ? null : getData(func), newData = [func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity]; - if (data && data !== true) { + if (data) { mergeData(newData, data); bitmask = newData[1]; arity = newData[9]; diff --git a/internal/equalArrays.js b/internal/equalArrays.js index 2766c64ad..5a687a6c8 100644 --- a/internal/equalArrays.js +++ b/internal/equalArrays.js @@ -7,18 +7,18 @@ * @param {Array} other The other array to compare. * @param {Function} equalFunc The function to determine equivalents of values. * @param {Function} [customizer] The function to customize comparing arrays. - * @param {boolean} [isWhere] Specify performing partial comparisons. + * @param {boolean} [isLoose] Specify performing partial comparisons. * @param {Array} [stackA] Tracks traversed `value` objects. * @param {Array} [stackB] Tracks traversed `other` objects. * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`. */ -function equalArrays(array, other, equalFunc, customizer, isWhere, stackA, stackB) { +function equalArrays(array, other, equalFunc, customizer, isLoose, stackA, stackB) { var index = -1, arrLength = array.length, othLength = other.length, result = true; - if (arrLength != othLength && !(isWhere && othLength > arrLength)) { + if (arrLength != othLength && !(isLoose && othLength > arrLength)) { return false; } // Deep compare the contents, ignoring non-numeric properties. @@ -28,23 +28,23 @@ function equalArrays(array, other, equalFunc, customizer, isWhere, stackA, stack result = undefined; if (customizer) { - result = isWhere + result = isLoose ? customizer(othValue, arrValue, index) : customizer(arrValue, othValue, index); } if (typeof result == 'undefined') { // Recursively compare arrays (susceptible to call stack limits). - if (isWhere) { + if (isLoose) { var othIndex = othLength; while (othIndex--) { othValue = other[othIndex]; - result = (arrValue && arrValue === othValue) || equalFunc(arrValue, othValue, customizer, isWhere, stackA, stackB); + result = (arrValue && arrValue === othValue) || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB); if (result) { break; } } } else { - result = (arrValue && arrValue === othValue) || equalFunc(arrValue, othValue, customizer, isWhere, stackA, stackB); + result = (arrValue && arrValue === othValue) || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB); } } } diff --git a/internal/equalObjects.js b/internal/equalObjects.js index 9d8da188c..f51bf12fb 100644 --- a/internal/equalObjects.js +++ b/internal/equalObjects.js @@ -15,26 +15,26 @@ var hasOwnProperty = objectProto.hasOwnProperty; * @param {Object} other The other object to compare. * @param {Function} equalFunc The function to determine equivalents of values. * @param {Function} [customizer] The function to customize comparing values. - * @param {boolean} [isWhere] Specify performing partial comparisons. + * @param {boolean} [isLoose] Specify performing partial comparisons. * @param {Array} [stackA] Tracks traversed `value` objects. * @param {Array} [stackB] Tracks traversed `other` objects. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`. */ -function equalObjects(object, other, equalFunc, customizer, isWhere, stackA, stackB) { +function equalObjects(object, other, equalFunc, customizer, isLoose, stackA, stackB) { var objProps = keys(object), objLength = objProps.length, othProps = keys(other), othLength = othProps.length; - if (objLength != othLength && !isWhere) { + if (objLength != othLength && !isLoose) { return false; } - var hasCtor, + var skipCtor = isLoose, index = -1; while (++index < objLength) { var key = objProps[index], - result = hasOwnProperty.call(other, key); + result = isLoose ? key in other : hasOwnProperty.call(other, key); if (result) { var objValue = object[key], @@ -42,21 +42,21 @@ function equalObjects(object, other, equalFunc, customizer, isWhere, stackA, sta result = undefined; if (customizer) { - result = isWhere + result = isLoose ? customizer(othValue, objValue, key) : customizer(objValue, othValue, key); } if (typeof result == 'undefined') { // Recursively compare objects (susceptible to call stack limits). - result = (objValue && objValue === othValue) || equalFunc(objValue, othValue, customizer, isWhere, stackA, stackB); + result = (objValue && objValue === othValue) || equalFunc(objValue, othValue, customizer, isLoose, stackA, stackB); } } if (!result) { return false; } - hasCtor || (hasCtor = key == 'constructor'); + skipCtor || (skipCtor = key == 'constructor'); } - if (!hasCtor) { + if (!skipCtor) { var objCtor = object.constructor, othCtor = other.constructor; diff --git a/internal/extremumBy.js b/internal/extremumBy.js index e4388273d..30b9cf408 100644 --- a/internal/extremumBy.js +++ b/internal/extremumBy.js @@ -7,7 +7,7 @@ var NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY, /** * Gets the extremum value of `collection` invoking `iteratee` for each value * in `collection` to generate the criterion by which the value is ranked. - * The `iteratee` is invoked with three arguments; (value, index, collection). + * The `iteratee` is invoked with three arguments: (value, index, collection). * * @private * @param {Array|Object|string} collection The collection to iterate over. diff --git a/internal/getFuncName.js b/internal/getFuncName.js new file mode 100644 index 000000000..2c933cdb8 --- /dev/null +++ b/internal/getFuncName.js @@ -0,0 +1,37 @@ +import baseProperty from './baseProperty'; +import constant from '../utility/constant'; +import realNames from './realNames'; +import support from '../support'; + +/** + * Gets the name of `func`. + * + * @private + * @param {Function} func The function to query. + * @returns {string} Returns the function name. + */ +var getFuncName = (function() { + if (!support.funcNames) { + return constant(''); + } + if (constant.name == 'constant') { + return baseProperty('name'); + } + return function(func) { + var result = func.name, + array = realNames[result], + length = array ? array.length : 0; + + while (length--) { + var data = array[length], + otherFunc = data.func; + + if (otherFunc == null || otherFunc == func) { + return data.name; + } + } + return result; + }; +}()); + +export default getFuncName; diff --git a/internal/indexOfNaN.js b/internal/indexOfNaN.js index 18c2af405..5c089661c 100644 --- a/internal/indexOfNaN.js +++ b/internal/indexOfNaN.js @@ -1,6 +1,5 @@ /** * 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. diff --git a/internal/isBindable.js b/internal/isBindable.js deleted file mode 100644 index 78fd050e5..000000000 --- a/internal/isBindable.js +++ /dev/null @@ -1,38 +0,0 @@ -import baseSetData from './baseSetData'; -import isNative from '../lang/isNative'; -import support from '../support'; - -/** Used to detect named functions. */ -var reFuncName = /^\s*function[ \n\r\t]+\w/; - -/** Used to detect functions containing a `this` reference. */ -var reThis = /\bthis\b/; - -/** Used to resolve the decompiled source of functions. */ -var fnToString = Function.prototype.toString; - -/** - * Checks if `func` is eligible for `this` binding. - * - * @private - * @param {Function} func The function to check. - * @returns {boolean} Returns `true` if `func` is eligible, else `false`. - */ -function isBindable(func) { - var result = !(support.funcNames ? func.name : support.funcDecomp); - - if (!result) { - var source = fnToString.call(func); - if (!support.funcNames) { - result = !reFuncName.test(source); - } - if (!result) { - // Check if `func` references the `this` keyword and store the result. - result = reThis.test(source) || isNative(func); - baseSetData(func, result); - } - } - return result; -} - -export default isBindable; diff --git a/internal/isIndex.js b/internal/isIndex.js index 8a62990db..7005f834e 100644 --- a/internal/isIndex.js +++ b/internal/isIndex.js @@ -1,7 +1,6 @@ /** - * Used as the maximum length of an array-like value. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.max_safe_integer) - * for more details. + * Used as the [maximum length](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.max_safe_integer) + * of an array-like value. */ var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; diff --git a/internal/isLaziable.js b/internal/isLaziable.js new file mode 100644 index 000000000..6a14e5f6c --- /dev/null +++ b/internal/isLaziable.js @@ -0,0 +1,17 @@ +import LazyWrapper from './LazyWrapper'; +import getFuncName from './getFuncName'; +import lodash from '../chain/lodash'; + +/** + * Checks if `func` has a lazy counterpart. + * + * @private + * @param {Function} func The function to check. + * @returns {boolean} Returns `true` if `func` has a lazy counterpart, else `false`. + */ +function isLaziable(func) { + var funcName = getFuncName(func); + return !!funcName && func === lodash[funcName] && funcName in LazyWrapper.prototype; +} + +export default isLaziable; diff --git a/internal/isLength.js b/internal/isLength.js index 137f33a77..df747f68c 100644 --- a/internal/isLength.js +++ b/internal/isLength.js @@ -1,16 +1,13 @@ /** - * Used as the maximum length of an array-like value. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.max_safe_integer) - * for more details. + * Used as the [maximum length](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.max_safe_integer) + * of an array-like value. */ var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; /** * Checks if `value` is a valid array-like length. * - * **Note:** This function is based on ES `ToLength`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength) - * for more details. + * **Note:** This function is based on [`ToLength`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength). * * @private * @param {*} value The value to check. diff --git a/internal/isObjectLike.js b/internal/isObjectLike.js index 79e839485..3aab34860 100644 --- a/internal/isObjectLike.js +++ b/internal/isObjectLike.js @@ -6,7 +6,7 @@ * @returns {boolean} Returns `true` if `value` is object-like, else `false`. */ function isObjectLike(value) { - return (value && typeof value == 'object') || false; + return !!value && typeof value == 'object'; } export default isObjectLike; diff --git a/internal/mergeData.js b/internal/mergeData.js index 909cdff22..156398383 100644 --- a/internal/mergeData.js +++ b/internal/mergeData.js @@ -5,11 +5,10 @@ import replaceHolders from './replaceHolders'; /** Used to compose bitmasks for wrapper metadata. */ var BIND_FLAG = 1, - BIND_KEY_FLAG = 2, CURRY_BOUND_FLAG = 4, - CURRY_RIGHT_FLAG = 16, - REARG_FLAG = 128, - ARY_FLAG = 256; + CURRY_FLAG = 8, + ARY_FLAG = 128, + REARG_FLAG = 256; /** Used as the internal argument placeholder. */ var PLACEHOLDER = '__lodash_placeholder__'; @@ -35,22 +34,13 @@ var nativeMin = Math.min; function mergeData(data, source) { var bitmask = data[1], srcBitmask = source[1], - newBitmask = bitmask | srcBitmask; + newBitmask = bitmask | srcBitmask, + isCommon = newBitmask < ARY_FLAG; - var arityFlags = ARY_FLAG | REARG_FLAG, - bindFlags = BIND_FLAG | BIND_KEY_FLAG, - comboFlags = arityFlags | bindFlags | CURRY_BOUND_FLAG | CURRY_RIGHT_FLAG; - - var isAry = bitmask & ARY_FLAG && !(srcBitmask & ARY_FLAG), - isRearg = bitmask & REARG_FLAG && !(srcBitmask & REARG_FLAG), - argPos = (isRearg ? data : source)[7], - ary = (isAry ? data : source)[8]; - - var isCommon = !(bitmask >= REARG_FLAG && srcBitmask > bindFlags) && - !(bitmask > bindFlags && srcBitmask >= REARG_FLAG); - - var isCombo = (newBitmask >= arityFlags && newBitmask <= comboFlags) && - (bitmask < REARG_FLAG || ((isRearg || isAry) && argPos.length <= ary)); + var isCombo = + (srcBitmask == ARY_FLAG && bitmask == CURRY_FLAG) || + (srcBitmask == ARY_FLAG && bitmask == REARG_FLAG && data[7].length <= source[8]) || + (srcBitmask == (ARY_FLAG | REARG_FLAG) && bitmask == CURRY_FLAG); // Exit early if metadata can't be merged. if (!(isCommon || isCombo)) { diff --git a/internal/realNames.js b/internal/realNames.js new file mode 100644 index 000000000..df195edda --- /dev/null +++ b/internal/realNames.js @@ -0,0 +1,4 @@ +/** Used to lookup unminified function names. */ +var realNames = {}; + +export default realNames; diff --git a/internal/root.js b/internal/root.js index 2809c72e3..f217bc963 100644 --- a/internal/root.js +++ b/internal/root.js @@ -13,8 +13,11 @@ var freeModule = objectTypes[typeof module] && module && !module.nodeType && mod /** Detect free variable `global` from Node.js. */ var freeGlobal = freeExports && freeModule && typeof global == 'object' && global; +/** Detect free variable `self`. */ +var freeSelf = objectTypes[typeof self] && self && self.Object && self; + /** Detect free variable `window`. */ -var freeWindow = objectTypes[typeof window] && window; +var freeWindow = objectTypes[typeof window] && window && window.Object && window; /** * Used as a reference to the global object. @@ -22,6 +25,6 @@ var freeWindow = objectTypes[typeof window] && window; * 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; +var root = freeGlobal || ((freeWindow !== (this && this.window)) && freeWindow) || freeSelf || this; export default root; diff --git a/internal/shimIsPlainObject.js b/internal/shimIsPlainObject.js index 7299aaa7c..ab02f3632 100644 --- a/internal/shimIsPlainObject.js +++ b/internal/shimIsPlainObject.js @@ -11,9 +11,8 @@ var objectProto = Object.prototype; var hasOwnProperty = objectProto.hasOwnProperty; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; diff --git a/lang/clone.js b/lang/clone.js index 8f335c025..ac51d7f40 100644 --- a/lang/clone.js +++ b/lang/clone.js @@ -9,12 +9,12 @@ import isIterateeCall from '../internal/isIterateeCall'; * cloning is handled by the method instead. The `customizer` is bound to * `thisArg` and invoked with two argument; (value [, index|key, object]). * - * **Note:** This method is loosely based on the structured clone algorithm. + * **Note:** This method is loosely based on the + * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm). * The enumerable properties of `arguments` objects and objects created by * constructors other than `Object` are cloned to plain `Object` objects. An * empty object is returned for uncloneable values such as functions, DOM nodes, - * Maps, Sets, and WeakMaps. See the [HTML5 specification](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm) - * for more details. + * Maps, Sets, and WeakMaps. * * @static * @memberOf _ diff --git a/lang/cloneDeep.js b/lang/cloneDeep.js index 5d3fd149f..f1a355f5d 100644 --- a/lang/cloneDeep.js +++ b/lang/cloneDeep.js @@ -7,12 +7,12 @@ import bindCallback from '../internal/bindCallback'; * is handled by the method instead. The `customizer` is bound to `thisArg` * and invoked with two argument; (value [, index|key, object]). * - * **Note:** This method is loosely based on the structured clone algorithm. + * **Note:** This method is loosely based on the + * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm). * The enumerable properties of `arguments` objects and objects created by * constructors other than `Object` are cloned to plain `Object` objects. An * empty object is returned for uncloneable values such as functions, DOM nodes, - * Maps, Sets, and WeakMaps. See the [HTML5 specification](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm) - * for more details. + * Maps, Sets, and WeakMaps. * * @static * @memberOf _ diff --git a/lang/isArguments.js b/lang/isArguments.js index bb62aade2..28ba8979a 100644 --- a/lang/isArguments.js +++ b/lang/isArguments.js @@ -8,9 +8,8 @@ var argsTag = '[object Arguments]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -32,7 +31,7 @@ var objToString = objectProto.toString; */ function isArguments(value) { var length = isObjectLike(value) ? value.length : undefined; - return (isLength(length) && objToString.call(value) == argsTag) || false; + return isLength(length) && objToString.call(value) == argsTag; } export default isArguments; diff --git a/lang/isArray.js b/lang/isArray.js index 645b0c11d..4c916f47d 100644 --- a/lang/isArray.js +++ b/lang/isArray.js @@ -9,9 +9,8 @@ var arrayTag = '[object Array]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -35,7 +34,7 @@ var nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray; * // => false */ var isArray = nativeIsArray || function(value) { - return (isObjectLike(value) && isLength(value.length) && objToString.call(value) == arrayTag) || false; + return isObjectLike(value) && isLength(value.length) && objToString.call(value) == arrayTag; }; export default isArray; diff --git a/lang/isBoolean.js b/lang/isBoolean.js index c1ae0c1ec..85b23cb32 100644 --- a/lang/isBoolean.js +++ b/lang/isBoolean.js @@ -7,9 +7,8 @@ var boolTag = '[object Boolean]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -30,7 +29,7 @@ var objToString = objectProto.toString; * // => false */ function isBoolean(value) { - return (value === true || value === false || isObjectLike(value) && objToString.call(value) == boolTag) || false; + return value === true || value === false || (isObjectLike(value) && objToString.call(value) == boolTag); } export default isBoolean; diff --git a/lang/isDate.js b/lang/isDate.js index a5bbc84ca..69b34ec70 100644 --- a/lang/isDate.js +++ b/lang/isDate.js @@ -7,9 +7,8 @@ var dateTag = '[object Date]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -30,7 +29,7 @@ var objToString = objectProto.toString; * // => false */ function isDate(value) { - return (isObjectLike(value) && objToString.call(value) == dateTag) || false; + return isObjectLike(value) && objToString.call(value) == dateTag; } export default isDate; diff --git a/lang/isElement.js b/lang/isElement.js index d853b62bc..8b263bab8 100644 --- a/lang/isElement.js +++ b/lang/isElement.js @@ -6,9 +6,8 @@ import support from '../support'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -29,13 +28,13 @@ var objToString = objectProto.toString; * // => false */ function isElement(value) { - return (value && value.nodeType === 1 && isObjectLike(value) && - (objToString.call(value).indexOf('Element') > -1)) || false; + return !!value && value.nodeType === 1 && isObjectLike(value) && + (objToString.call(value).indexOf('Element') > -1); } // Fallback for environments without DOM support. if (!support.dom) { isElement = function(value) { - return (value && value.nodeType === 1 && isObjectLike(value) && !isPlainObject(value)) || false; + return !!value && value.nodeType === 1 && isObjectLike(value) && !isPlainObject(value); }; } diff --git a/lang/isEqual.js b/lang/isEqual.js index 85348bae0..11b487fc1 100644 --- a/lang/isEqual.js +++ b/lang/isEqual.js @@ -7,7 +7,7 @@ import isStrictComparable from '../internal/isStrictComparable'; * equivalent. If `customizer` is provided it is invoked to compare values. * If `customizer` returns `undefined` comparisons are handled by the method * instead. The `customizer` is bound to `thisArg` and invoked with three - * arguments; (value, other [, index|key]). + * arguments: (value, other [, index|key]). * * **Note:** This method supports comparing arrays, booleans, `Date` objects, * numbers, `Object` objects, regexes, and strings. Objects are compared by diff --git a/lang/isError.js b/lang/isError.js index 79d55bb86..01ac6d275 100644 --- a/lang/isError.js +++ b/lang/isError.js @@ -7,9 +7,8 @@ var errorTag = '[object Error]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -31,7 +30,7 @@ var objToString = objectProto.toString; * // => false */ function isError(value) { - return (isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag) || false; + return isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag; } export default isError; diff --git a/lang/isFinite.js b/lang/isFinite.js index faf3e9c48..5bd7f679e 100644 --- a/lang/isFinite.js +++ b/lang/isFinite.js @@ -8,9 +8,7 @@ var nativeIsFinite = root.isFinite, /** * Checks if `value` is a finite primitive number. * - * **Note:** This method is based on ES `Number.isFinite`. See the - * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isfinite) - * for more details. + * **Note:** This method is based on [`Number.isFinite`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isfinite). * * @static * @memberOf _ diff --git a/lang/isFunction.js b/lang/isFunction.js index ab20457ea..5af802afb 100644 --- a/lang/isFunction.js +++ b/lang/isFunction.js @@ -9,9 +9,8 @@ var funcTag = '[object Function]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; diff --git a/lang/isMatch.js b/lang/isMatch.js index e4406ce6b..80c22aecf 100644 --- a/lang/isMatch.js +++ b/lang/isMatch.js @@ -2,19 +2,14 @@ import baseIsMatch from '../internal/baseIsMatch'; import bindCallback from '../internal/bindCallback'; import isStrictComparable from '../internal/isStrictComparable'; import keys from '../object/keys'; - -/** Used for native method references. */ -var objectProto = Object.prototype; - -/** Used to check objects for own properties. */ -var hasOwnProperty = objectProto.hasOwnProperty; +import toObject from '../internal/toObject'; /** * Performs a deep comparison between `object` and `source` to determine if * `object` contains equivalent property values. If `customizer` is provided * it is invoked to compare values. If `customizer` returns `undefined` * comparisons are handled by the method instead. The `customizer` is bound - * to `thisArg` and invoked with three arguments; (value, other, index|key). + * to `thisArg` and invoked with three arguments: (value, other, index|key). * * **Note:** This method supports comparing properties of arrays, booleans, * `Date` objects, numbers, `Object` objects, regexes, and strings. Functions @@ -52,13 +47,19 @@ function isMatch(object, source, customizer, thisArg) { var props = keys(source), length = props.length; + if (!length) { + return true; + } + if (object == null) { + return false; + } customizer = typeof customizer == 'function' && bindCallback(customizer, thisArg, 3); if (!customizer && length == 1) { var key = props[0], value = source[key]; if (isStrictComparable(value)) { - return object != null && value === object[key] && hasOwnProperty.call(object, key); + return value === object[key] && (typeof value != 'undefined' || (key in toObject(object))); } } var values = Array(length), @@ -68,7 +69,7 @@ function isMatch(object, source, customizer, thisArg) { value = values[length] = source[props[length]]; strictCompareFlags[length] = isStrictComparable(value); } - return baseIsMatch(object, props, values, strictCompareFlags, customizer); + return baseIsMatch(toObject(object), props, values, strictCompareFlags, customizer); } export default isMatch; diff --git a/lang/isNaN.js b/lang/isNaN.js index 6c3bec80a..d2b2d0f5a 100644 --- a/lang/isNaN.js +++ b/lang/isNaN.js @@ -3,9 +3,8 @@ import isNumber from './isNumber'; /** * Checks if `value` is `NaN`. * - * **Note:** This method is not the same as native `isNaN` which returns `true` - * for `undefined` and other non-numeric values. See the [ES5 spec](https://es5.github.io/#x15.1.2.4) - * for more details. + * **Note:** This method is not the same as [`isNaN`](https://es5.github.io/#x15.1.2.4) + * which returns `true` for `undefined` and other non-numeric values. * * @static * @memberOf _ diff --git a/lang/isNative.js b/lang/isNative.js index 0a45cc022..6c69a8a3c 100644 --- a/lang/isNative.js +++ b/lang/isNative.js @@ -14,9 +14,8 @@ var objectProto = Object.prototype; var fnToString = Function.prototype.toString; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -49,7 +48,7 @@ function isNative(value) { if (objToString.call(value) == funcTag) { return reNative.test(fnToString.call(value)); } - return (isObjectLike(value) && reHostCtor.test(value)) || false; + return isObjectLike(value) && reHostCtor.test(value); } export default isNative; diff --git a/lang/isNumber.js b/lang/isNumber.js index 818b027ac..d3aac7221 100644 --- a/lang/isNumber.js +++ b/lang/isNumber.js @@ -7,9 +7,8 @@ var numberTag = '[object Number]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -36,7 +35,7 @@ var objToString = objectProto.toString; * // => false */ function isNumber(value) { - return typeof value == 'number' || (isObjectLike(value) && objToString.call(value) == numberTag) || false; + return typeof value == 'number' || (isObjectLike(value) && objToString.call(value) == numberTag); } export default isNumber; diff --git a/lang/isObject.js b/lang/isObject.js index c66e88d98..d1187afee 100644 --- a/lang/isObject.js +++ b/lang/isObject.js @@ -1,9 +1,7 @@ /** - * Checks if `value` is the language type of `Object`. + * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) * - * **Note:** See the [ES5 spec](https://es5.github.io/#x8) for more details. - * * @static * @memberOf _ * @category Lang @@ -24,7 +22,7 @@ function isObject(value) { // Avoid a V8 JIT bug in Chrome 19-20. // See https://code.google.com/p/v8/issues/detail?id=2291 for more details. var type = typeof value; - return type == 'function' || (value && type == 'object') || false; + return type == 'function' || (!!value && type == 'object'); } export default isObject; diff --git a/lang/isPlainObject.js b/lang/isPlainObject.js index 9e8803c64..43225beda 100644 --- a/lang/isPlainObject.js +++ b/lang/isPlainObject.js @@ -8,9 +8,8 @@ var objectTag = '[object Object]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; diff --git a/lang/isRegExp.js b/lang/isRegExp.js index a0418eb8e..8caa23a55 100644 --- a/lang/isRegExp.js +++ b/lang/isRegExp.js @@ -7,9 +7,8 @@ var regexpTag = '[object RegExp]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; diff --git a/lang/isString.js b/lang/isString.js index d8dc62641..1b7c43b74 100644 --- a/lang/isString.js +++ b/lang/isString.js @@ -7,9 +7,8 @@ var stringTag = '[object String]'; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -30,7 +29,7 @@ var objToString = objectProto.toString; * // => false */ function isString(value) { - return typeof value == 'string' || (isObjectLike(value) && objToString.call(value) == stringTag) || false; + return typeof value == 'string' || (isObjectLike(value) && objToString.call(value) == stringTag); } export default isString; diff --git a/lang/isTypedArray.js b/lang/isTypedArray.js index 9921d37ae..78ecd9b00 100644 --- a/lang/isTypedArray.js +++ b/lang/isTypedArray.js @@ -46,9 +46,8 @@ typedArrayTags[stringTag] = typedArrayTags[weakMapTag] = false; var objectProto = Object.prototype; /** - * Used to resolve the `toStringTag` of values. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) - * for more details. + * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) + * of values. */ var objToString = objectProto.toString; @@ -69,7 +68,7 @@ var objToString = objectProto.toString; * // => false */ function isTypedArray(value) { - return (isObjectLike(value) && isLength(value.length) && typedArrayTags[objToString.call(value)]) || false; + return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[objToString.call(value)]; } export default isTypedArray; diff --git a/lodash.js b/lodash.js index 1ca5bcda2..da01ec008 100644 --- a/lodash.js +++ b/lodash.js @@ -1,6 +1,6 @@ /** * @license - * lodash 3.5.0 (Custom Build) + * lodash 3.6.0 (Custom Build) * Build: `lodash modularize modern exports="es" -o ./` * Copyright 2012-2015 The Dojo Foundation * Based on Underscore.js 1.8.2 @@ -26,6 +26,7 @@ import baseForOwn from './internal/baseForOwn'; import baseFunctions from './internal/baseFunctions'; import baseMatches from './internal/baseMatches'; import baseProperty from './internal/baseProperty'; +import createHybridWrapper from './internal/createHybridWrapper'; import identity from './utility/identity'; import isArray from './lang/isArray'; import isObject from './lang/isObject'; @@ -36,11 +37,15 @@ import lazyReverse from './internal/lazyReverse'; import lazyValue from './internal/lazyValue'; import lodash from './chain/lodash'; import _mixin from './utility/mixin'; +import realNames from './internal/realNames'; import support from './support'; import thru from './chain/thru'; /** Used as the semantic version number. */ -var VERSION = '3.5.0'; +var VERSION = '3.6.0'; + +/** Used to compose bitmasks for wrapper metadata. */ +var BIND_KEY_FLAG = 2; /** Used to indicate the type of lazy iteratees. */ var LAZY_DROP_WHILE_FLAG = 0, @@ -149,6 +154,7 @@ lodash.rearg = func.rearg; lodash.reject = collection.reject; lodash.remove = array.remove; lodash.rest = array.rest; +lodash.restParam = func.restParam; lodash.shuffle = collection.shuffle; lodash.slice = array.slice; lodash.sortBy = collection.sortBy; @@ -435,8 +441,11 @@ LazyWrapper.prototype.toArray = function() { // Add `LazyWrapper` methods to `lodash.prototype`. baseForOwn(LazyWrapper.prototype, function(func, methodName) { - var lodashFunc = lodash[methodName], - checkIteratee = /^(?:filter|map|reject)|While$/.test(methodName), + var lodashFunc = lodash[methodName]; + if (!lodashFunc) { + return; + } + var checkIteratee = /^(?:filter|map|reject)|While$/.test(methodName), retUnwrapped = /^(?:first|last)$/.test(methodName); lodash.prototype[methodName] = function() { @@ -495,6 +504,19 @@ arrayEach(['concat', 'join', 'pop', 'push', 'replace', 'shift', 'sort', 'splice' }; }); +// Map minified function names to their real names. +baseForOwn(LazyWrapper.prototype, function(func, methodName) { + var lodashFunc = lodash[methodName]; + if (lodashFunc) { + var key = lodashFunc.name, + names = realNames[key] || (realNames[key] = []); + + names.push({ 'name': methodName, 'func': lodashFunc }); + } +}); + +realNames[createHybridWrapper(null, BIND_KEY_FLAG).name] = [{ 'name': 'wrapper', 'func': null }]; + // Add functions to the lazy wrapper. LazyWrapper.prototype.clone = lazyClone; LazyWrapper.prototype.reverse = lazyReverse; diff --git a/math/max.js b/math/max.js index 10dd13464..50551746c 100644 --- a/math/max.js +++ b/math/max.js @@ -6,16 +6,16 @@ import createExtremum from '../internal/createExtremum'; * `-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). + * arguments: (value, index, collection). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * @@ -42,11 +42,11 @@ import createExtremum from '../internal/createExtremum'; * _.max(users, function(chr) { * return chr.age; * }); - * // => { 'user': 'fred', 'age': 40 }; + * // => { 'user': 'fred', 'age': 40 } * * // using the `_.property` callback shorthand * _.max(users, 'age'); - * // => { 'user': 'fred', 'age': 40 }; + * // => { 'user': 'fred', 'age': 40 } */ var max = createExtremum(arrayMax); diff --git a/math/min.js b/math/min.js index dd3a2d5a3..f402c5c22 100644 --- a/math/min.js +++ b/math/min.js @@ -6,16 +6,16 @@ import createExtremum from '../internal/createExtremum'; * `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). + * arguments: (value, index, collection). * - * If a property name is provided for `predicate` the created `_.property` + * If a property name is provided for `iteratee` 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 + * If an object is provided for `iteratee` the created `_.matches` style * callback returns `true` for elements that have the properties of the given * object, else `false`. * @@ -42,11 +42,11 @@ import createExtremum from '../internal/createExtremum'; * _.min(users, function(chr) { * return chr.age; * }); - * // => { 'user': 'barney', 'age': 36 }; + * // => { 'user': 'barney', 'age': 36 } * * // using the `_.property` callback shorthand * _.min(users, 'age'); - * // => { 'user': 'barney', 'age': 36 }; + * // => { 'user': 'barney', 'age': 36 } */ var min = createExtremum(arrayMin, true); diff --git a/math/sum.js b/math/sum.js index b0ef03ece..a614d9b5b 100644 --- a/math/sum.js +++ b/math/sum.js @@ -1,4 +1,8 @@ +import arraySum from '../internal/arraySum'; +import baseCallback from '../internal/baseCallback'; +import baseSum from '../internal/baseSum'; import isArray from '../lang/isArray'; +import isIterateeCall from '../internal/isIterateeCall'; import toIterable from '../internal/toIterable'; /** @@ -8,26 +12,41 @@ import toIterable from '../internal/toIterable'; * @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 {number} Returns the sum. * @example * - * _.sum([4, 6, 2]); - * // => 12 + * _.sum([4, 6]); + * // => 10 * - * _.sum({ 'a': 4, 'b': 6, 'c': 2 }); - * // => 12 + * _.sum({ 'a': 4, 'b': 6 }); + * // => 10 + * + * var objects = [ + * { 'n': 4 }, + * { 'n': 6 } + * ]; + * + * _.sum(objects, function(object) { + * return object.n; + * }); + * // => 10 + * + * // using the `_.property` callback shorthand + * _.sum(objects, 'n'); + * // => 10 */ -function sum(collection) { - if (!isArray(collection)) { - collection = toIterable(collection); +function sum(collection, iteratee, thisArg) { + if (thisArg && isIterateeCall(collection, iteratee, thisArg)) { + iteratee = null; } - var length = collection.length, - result = 0; + var noIteratee = iteratee == null; - while (length--) { - result += +collection[length] || 0; - } - return result; + iteratee = noIteratee ? iteratee : baseCallback(iteratee, thisArg, 3); + return noIteratee + ? arraySum(isArray(collection) ? collection : toIterable(collection)) + : baseSum(collection, iteratee); } export default sum; diff --git a/object/assign.js b/object/assign.js index 92d41edd6..94d9a26bc 100644 --- a/object/assign.js +++ b/object/assign.js @@ -5,7 +5,7 @@ import createAssigner from '../internal/createAssigner'; * Assigns own enumerable properties of source object(s) to the destination * object. Subsequent sources overwrite property assignments of previous sources. * If `customizer` is provided it is invoked to produce the assigned values. - * The `customizer` is bound to `thisArg` and invoked with five arguments; + * The `customizer` is bound to `thisArg` and invoked with five arguments: * (objectValue, sourceValue, key, object, source). * * @static diff --git a/object/defaults.js b/object/defaults.js index 759c6d89b..c8c7fcb14 100644 --- a/object/defaults.js +++ b/object/defaults.js @@ -1,6 +1,6 @@ -import arrayCopy from '../internal/arrayCopy'; import assign from './assign'; import assignDefaults from '../internal/assignDefaults'; +import restParam from '../function/restParam'; /** * Assigns own enumerable properties of source object(s) to the destination @@ -18,13 +18,13 @@ import assignDefaults from '../internal/assignDefaults'; * _.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' }); * // => { 'user': 'barney', 'age': 36 } */ -function defaults(object) { +var defaults = restParam(function(args) { + var object = args[0]; if (object == null) { return object; } - var args = arrayCopy(arguments); args.push(assignDefaults); return assign.apply(undefined, args); -} +}); export default defaults; diff --git a/object/findKey.js b/object/findKey.js index 5b56e0645..2132e3314 100644 --- a/object/findKey.js +++ b/object/findKey.js @@ -1,10 +1,9 @@ -import baseCallback from '../internal/baseCallback'; -import baseFind from '../internal/baseFind'; import baseForOwn from '../internal/baseForOwn'; +import createFindKey from '../internal/createFindKey'; /** - * This method is like `_.findIndex` except that it returns the key of the - * first element `predicate` returns truthy for, instead of the element itself. + * This method is like `_.find` except that it returns the key of the first + * element `predicate` returns truthy for instead of the element itself. * * If a property name is provided for `predicate` the created `_.property` * style callback returns the property value of the given element. @@ -50,9 +49,6 @@ import baseForOwn from '../internal/baseForOwn'; * _.findKey(users, 'active'); * // => 'barney' */ -function findKey(object, predicate, thisArg) { - predicate = baseCallback(predicate, thisArg, 3); - return baseFind(object, predicate, baseForOwn, true); -} +var findKey = createFindKey(baseForOwn); export default findKey; diff --git a/object/findLastKey.js b/object/findLastKey.js index c6a9a9864..d12b2f5ee 100644 --- a/object/findLastKey.js +++ b/object/findLastKey.js @@ -1,6 +1,5 @@ -import baseCallback from '../internal/baseCallback'; -import baseFind from '../internal/baseFind'; import baseForOwnRight from '../internal/baseForOwnRight'; +import createFindKey from '../internal/createFindKey'; /** * This method is like `_.findKey` except that it iterates over elements of @@ -50,9 +49,6 @@ import baseForOwnRight from '../internal/baseForOwnRight'; * _.findLastKey(users, 'active'); * // => 'pebbles' */ -function findLastKey(object, predicate, thisArg) { - predicate = baseCallback(predicate, thisArg, 3); - return baseFind(object, predicate, baseForOwnRight, true); -} +var findLastKey = createFindKey(baseForOwnRight); export default findLastKey; diff --git a/object/forIn.js b/object/forIn.js index 2052d09b6..4f38bc242 100644 --- a/object/forIn.js +++ b/object/forIn.js @@ -1,11 +1,10 @@ import baseFor from '../internal/baseFor'; -import bindCallback from '../internal/bindCallback'; -import keysIn from './keysIn'; +import createForIn from '../internal/createForIn'; /** * Iterates over own and inherited enumerable properties of an object invoking * `iteratee` for each property. The `iteratee` is bound to `thisArg` and invoked - * with three arguments; (value, key, object). Iterator functions may exit + * with three arguments: (value, key, object). Iterator functions may exit * iteration early by explicitly returning `false`. * * @static @@ -29,11 +28,6 @@ import keysIn from './keysIn'; * }); * // => logs 'a', 'b', and 'c' (iteration order is not guaranteed) */ -function forIn(object, iteratee, thisArg) { - if (typeof iteratee != 'function' || typeof thisArg != 'undefined') { - iteratee = bindCallback(iteratee, thisArg, 3); - } - return baseFor(object, iteratee, keysIn); -} +var forIn = createForIn(baseFor); export default forIn; diff --git a/object/forInRight.js b/object/forInRight.js index 422b2cbea..f9d368342 100644 --- a/object/forInRight.js +++ b/object/forInRight.js @@ -1,6 +1,5 @@ import baseForRight from '../internal/baseForRight'; -import bindCallback from '../internal/bindCallback'; -import keysIn from './keysIn'; +import createForIn from '../internal/createForIn'; /** * This method is like `_.forIn` except that it iterates over properties of @@ -27,9 +26,6 @@ import keysIn from './keysIn'; * }); * // => logs 'c', 'b', and 'a' assuming `_.forIn ` logs 'a', 'b', and 'c' */ -function forInRight(object, iteratee, thisArg) { - iteratee = bindCallback(iteratee, thisArg, 3); - return baseForRight(object, iteratee, keysIn); -} +var forInRight = createForIn(baseForRight); export default forInRight; diff --git a/object/forOwn.js b/object/forOwn.js index 1995fbe2e..eec2a9227 100644 --- a/object/forOwn.js +++ b/object/forOwn.js @@ -1,10 +1,10 @@ import baseForOwn from '../internal/baseForOwn'; -import bindCallback from '../internal/bindCallback'; +import createForOwn from '../internal/createForOwn'; /** * Iterates over own enumerable properties of an object invoking `iteratee` * for each property. The `iteratee` is bound to `thisArg` and invoked with - * three arguments; (value, key, object). Iterator functions may exit iteration + * three arguments: (value, key, object). Iterator functions may exit iteration * early by explicitly returning `false`. * * @static @@ -28,11 +28,6 @@ import bindCallback from '../internal/bindCallback'; * }); * // => logs 'a' and 'b' (iteration order is not guaranteed) */ -function forOwn(object, iteratee, thisArg) { - if (typeof iteratee != 'function' || typeof thisArg != 'undefined') { - iteratee = bindCallback(iteratee, thisArg, 3); - } - return baseForOwn(object, iteratee); -} +var forOwn = createForOwn(baseForOwn); export default forOwn; diff --git a/object/forOwnRight.js b/object/forOwnRight.js index bb8d0183c..8d05a6948 100644 --- a/object/forOwnRight.js +++ b/object/forOwnRight.js @@ -1,6 +1,5 @@ -import baseForRight from '../internal/baseForRight'; -import bindCallback from '../internal/bindCallback'; -import keys from './keys'; +import baseForOwnRight from '../internal/baseForOwnRight'; +import createForOwn from '../internal/createForOwn'; /** * This method is like `_.forOwn` except that it iterates over properties of @@ -27,9 +26,6 @@ import keys from './keys'; * }); * // => logs 'b' and 'a' assuming `_.forOwn` logs 'a' and 'b' */ -function forOwnRight(object, iteratee, thisArg) { - iteratee = bindCallback(iteratee, thisArg, 3); - return baseForRight(object, iteratee, keys); -} +var forOwnRight = createForOwn(baseForOwnRight); export default forOwnRight; diff --git a/object/mapValues.js b/object/mapValues.js index 2493e5e14..fa4ab88f7 100644 --- a/object/mapValues.js +++ b/object/mapValues.js @@ -4,7 +4,7 @@ import baseForOwn from '../internal/baseForOwn'; /** * Creates an object with the same keys as `object` and values generated by * running each own enumerable property of `object` through `iteratee`. The - * iteratee function is bound to `thisArg` and invoked with three arguments; + * iteratee function is bound to `thisArg` and invoked with three arguments: * (value, key, object). * * If a property name is provided for `iteratee` the created `_.property` diff --git a/object/merge.js b/object/merge.js index e126022be..054bfc9b4 100644 --- a/object/merge.js +++ b/object/merge.js @@ -8,7 +8,7 @@ import createAssigner from '../internal/createAssigner'; * provided it is invoked to produce the merged values of the destination and * source properties. If `customizer` returns `undefined` merging is handled * by the method instead. The `customizer` is bound to `thisArg` and invoked - * with five arguments; (objectValue, sourceValue, key, object, source). + * with five arguments: (objectValue, sourceValue, key, object, source). * * @static * @memberOf _ diff --git a/object/omit.js b/object/omit.js index a6d0fa554..c46924236 100644 --- a/object/omit.js +++ b/object/omit.js @@ -5,6 +5,7 @@ import bindCallback from '../internal/bindCallback'; import keysIn from './keysIn'; import pickByArray from '../internal/pickByArray'; import pickByCallback from '../internal/pickByCallback'; +import restParam from '../function/restParam'; /** * The opposite of `_.pick`; this method creates an object composed of the @@ -12,7 +13,7 @@ import pickByCallback from '../internal/pickByCallback'; * 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; + * predicate is bound to `thisArg` and invoked with three arguments: * (value, key, object). * * @static @@ -34,18 +35,18 @@ import pickByCallback from '../internal/pickByCallback'; * _.omit(object, _.isNumber); * // => { 'user': 'fred' } */ -function omit(object, predicate, thisArg) { +var omit = restParam(function(object, props) { if (object == null) { return {}; } - if (typeof predicate != 'function') { - var props = arrayMap(baseFlatten(arguments, false, false, 1), String); + if (typeof props[0] != 'function') { + var props = arrayMap(baseFlatten(props), String); return pickByArray(object, baseDifference(keysIn(object), props)); } - predicate = bindCallback(predicate, thisArg, 3); + var predicate = bindCallback(props[0], props[1], 3); return pickByCallback(object, function(value, key, object) { return !predicate(value, key, object); }); -} +}); export default omit; diff --git a/object/pick.js b/object/pick.js index b49109e60..51f9eb3f7 100644 --- a/object/pick.js +++ b/object/pick.js @@ -2,13 +2,14 @@ import baseFlatten from '../internal/baseFlatten'; import bindCallback from '../internal/bindCallback'; import pickByArray from '../internal/pickByArray'; import pickByCallback from '../internal/pickByCallback'; +import restParam from '../function/restParam'; /** * Creates an object composed of the picked `object` properties. 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` * picking the properties `predicate` returns truthy for. The predicate is - * bound to `thisArg` and invoked with three arguments; (value, key, object). + * bound to `thisArg` and invoked with three arguments: (value, key, object). * * @static * @memberOf _ @@ -29,13 +30,13 @@ import pickByCallback from '../internal/pickByCallback'; * _.pick(object, _.isString); * // => { 'user': 'fred' } */ -function pick(object, predicate, thisArg) { +var pick = restParam(function(object, props) { if (object == null) { return {}; } - return typeof predicate == 'function' - ? pickByCallback(object, bindCallback(predicate, thisArg, 3)) - : pickByArray(object, baseFlatten(arguments, false, false, 1)); -} + return typeof props[0] == 'function' + ? pickByCallback(object, bindCallback(props[0], props[1], 3)) + : pickByArray(object, baseFlatten(props)); +}); export default pick; diff --git a/object/transform.js b/object/transform.js index 6bb659f35..5bcce8119 100644 --- a/object/transform.js +++ b/object/transform.js @@ -12,7 +12,7 @@ import isTypedArray from '../lang/isTypedArray'; * `accumulator` object which is the result of running each of its own enumerable * properties through `iteratee`, with each invocation potentially mutating * the `accumulator` object. The `iteratee` is bound to `thisArg` and invoked - * with four arguments; (accumulator, value, key, object). Iterator functions + * with four arguments: (accumulator, value, key, object). Iterator functions * may exit iteration early by explicitly returning `false`. * * @static diff --git a/package.json b/package.json index cb2d24f6a..a8b653ca0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lodash-es", - "version": "3.5.0", + "version": "3.6.0", "description": "The modern build of lodash exported as ES modules.", "homepage": "https://lodash.com/custom-builds", "license": "MIT", diff --git a/string/camelCase.js b/string/camelCase.js index 7d4c7dbf6..e1ff211dc 100644 --- a/string/camelCase.js +++ b/string/camelCase.js @@ -1,8 +1,7 @@ import createCompounder from '../internal/createCompounder'; /** - * Converts `string` to camel case. - * See [Wikipedia](https://en.wikipedia.org/wiki/CamelCase) for more details. + * Converts `string` to [camel case](https://en.wikipedia.org/wiki/CamelCase). * * @static * @memberOf _ diff --git a/string/deburr.js b/string/deburr.js index 53d0b9562..3976f6a87 100644 --- a/string/deburr.js +++ b/string/deburr.js @@ -1,13 +1,17 @@ import baseToString from '../internal/baseToString'; import deburrLetter from '../internal/deburrLetter'; +/** + * Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). + */ +var reComboMarks = /[\u0300-\u036f\ufe20-\ufe23]/g; + /** Used to match latin-1 supplementary letters (excluding mathematical operators). */ var reLatin1 = /[\xc0-\xd6\xd8-\xde\xdf-\xf6\xf8-\xff]/g; /** - * Deburrs `string` by converting latin-1 supplementary letters to basic latin letters. - * See [Wikipedia](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table) - * for more details. + * Deburrs `string` by converting [latin-1 supplementary letters](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table) + * to basic latin letters and removing [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). * * @static * @memberOf _ @@ -21,7 +25,7 @@ var reLatin1 = /[\xc0-\xd6\xd8-\xde\xdf-\xf6\xf8-\xff]/g; */ function deburr(string) { string = baseToString(string); - return string && string.replace(reLatin1, deburrLetter); + return string && string.replace(reLatin1, deburrLetter).replace(reComboMarks, ''); } export default deburr; diff --git a/string/escape.js b/string/escape.js index 341b07429..ddf588967 100644 --- a/string/escape.js +++ b/string/escape.js @@ -23,9 +23,8 @@ var reUnescapedHtml = /[&<>"'`]/g, * [#108](https://html5sec.org/#108), and [#133](https://html5sec.org/#133) of * the [HTML5 Security Cheatsheet](https://html5sec.org/) for more details. * - * When working with HTML you should always quote attribute values to reduce - * XSS vectors. See [Ryan Grove's article](http://wonko.com/post/html-escaping) - * for more details. + * When working with HTML you should always [quote attribute values](http://wonko.com/post/html-escaping) + * to reduce XSS vectors. * * @static * @memberOf _ diff --git a/string/escapeRegExp.js b/string/escapeRegExp.js index 6c98e6554..6008c257b 100644 --- a/string/escapeRegExp.js +++ b/string/escapeRegExp.js @@ -1,16 +1,16 @@ import baseToString from '../internal/baseToString'; /** - * Used to match `RegExp` special characters. - * See this [article on `RegExp` characters](http://www.regular-expressions.info/characters.html#special) - * for more details. + * Used to match `RegExp` [special characters](http://www.regular-expressions.info/characters.html#special). + * In addition to special characters the forward slash is escaped to allow for + * easier `eval` use and `Function` compilation. */ var reRegExpChars = /[.*+?^${}()|[\]\/\\]/g, reHasRegExpChars = RegExp(reRegExpChars.source); /** - * Escapes the `RegExp` special characters "\", "^", "$", ".", "|", "?", "*", - * "+", "(", ")", "[", "]", "{" and "}" in `string`. + * Escapes the `RegExp` special characters "\", "/", "^", "$", ".", "|", "?", + * "*", "+", "(", ")", "[", "]", "{" and "}" in `string`. * * @static * @memberOf _ @@ -20,7 +20,7 @@ var reRegExpChars = /[.*+?^${}()|[\]\/\\]/g, * @example * * _.escapeRegExp('[lodash](https://lodash.com/)'); - * // => '\[lodash\]\(https://lodash\.com/\)' + * // => '\[lodash\]\(https:\/\/lodash\.com\/\)' */ function escapeRegExp(string) { string = baseToString(string); diff --git a/string/kebabCase.js b/string/kebabCase.js index b8a0a88d5..51ee6b29e 100644 --- a/string/kebabCase.js +++ b/string/kebabCase.js @@ -1,9 +1,7 @@ import createCompounder from '../internal/createCompounder'; /** - * Converts `string` to kebab case. - * See [Wikipedia](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles) for - * more details. + * Converts `string` to [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles). * * @static * @memberOf _ diff --git a/string/pad.js b/string/pad.js index 685107f7a..c5a001a81 100644 --- a/string/pad.js +++ b/string/pad.js @@ -1,5 +1,5 @@ import baseToString from '../internal/baseToString'; -import createPad from '../internal/createPad'; +import createPadding from '../internal/createPadding'; import root from '../internal/root'; /** Native method references. */ @@ -10,9 +10,8 @@ var ceil = Math.ceil, var nativeIsFinite = root.isFinite; /** - * Pads `string` on the left and right sides if it is shorter then the given - * padding length. The `chars` string may be truncated if the number of padding - * characters can't be evenly divided by the padding length. + * Pads `string` on the left and right sides if it is shorter than `length`. + * Padding characters are truncated if they can't be evenly divided by `length`. * * @static * @memberOf _ @@ -44,7 +43,7 @@ function pad(string, length, chars) { leftLength = floor(mid), rightLength = ceil(mid); - chars = createPad('', rightLength, chars); + chars = createPadding('', rightLength, chars); return chars.slice(0, leftLength) + string + chars; } diff --git a/string/padLeft.js b/string/padLeft.js index afae0aad8..1ee0355b7 100644 --- a/string/padLeft.js +++ b/string/padLeft.js @@ -1,10 +1,8 @@ -import baseToString from '../internal/baseToString'; -import createPad from '../internal/createPad'; +import createPadDir from '../internal/createPadDir'; /** - * Pads `string` on the left side if it is shorter then the given padding - * length. The `chars` string may be truncated if the number of padding - * characters exceeds the padding length. + * Pads `string` on the left side if it is shorter than `length`. Padding + * characters are truncated if they exceed `length`. * * @static * @memberOf _ @@ -24,9 +22,6 @@ import createPad from '../internal/createPad'; * _.padLeft('abc', 3); * // => 'abc' */ -function padLeft(string, length, chars) { - string = baseToString(string); - return string && (createPad(string, length, chars) + string); -} +var padLeft = createPadDir(); export default padLeft; diff --git a/string/padRight.js b/string/padRight.js index e7bc4efb2..31103b69b 100644 --- a/string/padRight.js +++ b/string/padRight.js @@ -1,10 +1,8 @@ -import baseToString from '../internal/baseToString'; -import createPad from '../internal/createPad'; +import createPadDir from '../internal/createPadDir'; /** - * Pads `string` on the right side if it is shorter then the given padding - * length. The `chars` string may be truncated if the number of padding - * characters exceeds the padding length. + * Pads `string` on the right side if it is shorter than `length`. Padding + * characters are truncated if they exceed `length`. * * @static * @memberOf _ @@ -24,9 +22,6 @@ import createPad from '../internal/createPad'; * _.padRight('abc', 3); * // => 'abc' */ -function padRight(string, length, chars) { - string = baseToString(string); - return string && (string + createPad(string, length, chars)); -} +var padRight = createPadDir(true); export default padRight; diff --git a/string/parseInt.js b/string/parseInt.js index 7a547c851..a9a9b0ab2 100644 --- a/string/parseInt.js +++ b/string/parseInt.js @@ -25,8 +25,8 @@ var nativeParseInt = root.parseInt; * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal, * in which case a `radix` of `16` is used. * - * **Note:** This method aligns with the ES5 implementation of `parseInt`. - * See the [ES5 spec](https://es5.github.io/#E) for more details. + * **Note:** This method aligns with the [ES5 implementation](https://es5.github.io/#E) + * of `parseInt`. * * @static * @memberOf _ diff --git a/string/snakeCase.js b/string/snakeCase.js index 1a52ee846..3dc9a0b3e 100644 --- a/string/snakeCase.js +++ b/string/snakeCase.js @@ -1,8 +1,7 @@ import createCompounder from '../internal/createCompounder'; /** - * Converts `string` to snake case. - * See [Wikipedia](https://en.wikipedia.org/wiki/Snake_case) for more details. + * Converts `string` to [snake case](https://en.wikipedia.org/wiki/Snake_case). * * @static * @memberOf _ diff --git a/string/startCase.js b/string/startCase.js index 039606b30..854a91f8b 100644 --- a/string/startCase.js +++ b/string/startCase.js @@ -1,9 +1,7 @@ import createCompounder from '../internal/createCompounder'; /** - * Converts `string` to start case. - * See [Wikipedia](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage) - * for more details. + * Converts `string` to [start case](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage). * * @static * @memberOf _ diff --git a/string/template.js b/string/template.js index 003015def..e987c78d2 100644 --- a/string/template.js +++ b/string/template.js @@ -16,9 +16,7 @@ var reEmptyStringLeading = /\b__p \+= '';/g, reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g; /** - * Used to match ES template delimiters. - * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literal-lexical-components) - * for more details. + * Used to match [ES template delimiters](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literal-lexical-components). */ var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g; @@ -35,9 +33,9 @@ var reUnescapedString = /['\n\r\u2028\u2029\\]/g; * properties may be accessed as free variables in the template. If a setting * object is provided it takes precedence over `_.templateSettings` values. * - * **Note:** In the development build `_.template` utilizes sourceURLs for easier debugging. - * See the [HTML5 Rocks article on sourcemaps](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl) - * for more details. + * **Note:** In the development build `_.template` utilizes + * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl) + * for easier debugging. * * For more information on precompiling templates see * [lodash's custom builds documentation](https://lodash.com/custom-builds). diff --git a/string/trim.js b/string/trim.js index f8a4987bb..2aeaf77b9 100644 --- a/string/trim.js +++ b/string/trim.js @@ -24,7 +24,7 @@ import trimmedRightIndex from '../internal/trimmedRightIndex'; * // => 'abc' * * _.map([' foo ', ' bar '], _.trim); - * // => ['foo', 'bar] + * // => ['foo', 'bar'] */ function trim(string, chars, guard) { var value = string; diff --git a/string/trunc.js b/string/trunc.js index 0762523fd..1b516c53b 100644 --- a/string/trunc.js +++ b/string/trunc.js @@ -43,7 +43,7 @@ var reFlags = /\w*$/; * 'length': 24, * 'separator': /,? +/ * }); - * //=> 'hi-diddly-ho there...' + * // => 'hi-diddly-ho there...' * * _.trunc('hi-diddly-ho there, neighborino', { * 'omission': ' [...]' diff --git a/support.js b/support.js index 99523d1e6..85f645c60 100644 --- a/support.js +++ b/support.js @@ -1,9 +1,5 @@ -import isNative from './lang/isNative'; import root from './internal/root'; -/** Used to detect functions containing a `this` reference. */ -var reThis = /\bthis\b/; - /** Used for native method references. */ var objectProto = Object.prototype; @@ -32,7 +28,7 @@ var support = {}; * @memberOf _.support * @type boolean */ - support.funcDecomp = !isNative(root.WinRTError) && reThis.test(function() { return this; }); + support.funcDecomp = /\bthis\b/.test(function() { return this; }); /** * Detect if `Function#name` is supported (all but IE). diff --git a/utility/attempt.js b/utility/attempt.js index 0cb903790..341c68f42 100644 --- a/utility/attempt.js +++ b/utility/attempt.js @@ -1,4 +1,5 @@ import isError from '../lang/isError'; +import restParam from '../function/restParam'; /** * Attempts to invoke `func`, returning either the result or the caught error @@ -7,7 +8,7 @@ import isError from '../lang/isError'; * @static * @memberOf _ * @category Utility - * @param {*} func The function to attempt. + * @param {Function} func The function to attempt. * @returns {*} Returns the `func` result or error object. * @example * @@ -20,19 +21,12 @@ import isError from '../lang/isError'; * elements = []; * } */ -function attempt() { - var func = arguments[0], - length = arguments.length, - args = Array(length ? (length - 1) : 0); - - while (--length > 0) { - args[length - 1] = arguments[length]; - } +var attempt = restParam(function(func, args) { try { return func.apply(undefined, args); } catch(e) { return isError(e) ? e : new Error(e); } -} +}); export default attempt; diff --git a/utility/matchesProperty.js b/utility/matchesProperty.js index c241b4ca7..30594c59f 100644 --- a/utility/matchesProperty.js +++ b/utility/matchesProperty.js @@ -19,12 +19,11 @@ import baseMatchesProperty from '../internal/baseMatchesProperty'; * * var users = [ * { 'user': 'barney' }, - * { 'user': 'fred' }, - * { 'user': 'pebbles' } + * { 'user': 'fred' } * ]; * * _.find(users, _.matchesProperty('user', 'fred')); - * // => { 'user': 'fred', 'age': 40 } + * // => { 'user': 'fred' } */ function matchesProperty(key, value) { return baseMatchesProperty(key + '', baseClone(value, true)); diff --git a/utility/mixin.js b/utility/mixin.js index d17e38204..45f05b3a3 100644 --- a/utility/mixin.js +++ b/utility/mixin.js @@ -15,6 +15,9 @@ var push = arrayProto.push; * destination object. If `object` is a function then methods are added to * its prototype as well. * + * **Note:** Use `_.runInContext` to create a pristine `lodash` function + * for mixins to avoid conflicts caused by modifying the original. + * * @static * @memberOf _ * @category Utility @@ -32,7 +35,7 @@ var push = arrayProto.push; * }); * } * - * // use `_.runInContext` to avoid potential conflicts (esp. in Node.js) + * // use `_.runInContext` to avoid conflicts (esp. in Node.js) * var _ = require('lodash').runInContext(); * * _.mixin({ 'vowels': vowels }); @@ -69,12 +72,10 @@ function mixin(object, source, options) { return function() { var chainAll = this.__chain__; if (chain || chainAll) { - var result = object(this.__wrapped__); - (result.__actions__ = arrayCopy(this.__actions__)).push({ - 'func': func, - 'args': arguments, - 'thisArg': object - }); + var result = object(this.__wrapped__), + actions = result.__actions__ = arrayCopy(this.__actions__); + + actions.push({ 'func': func, 'args': arguments, 'thisArg': object }); result.__chain__ = chainAll; return result; } diff --git a/utility/property.js b/utility/property.js index 0575b9aa7..30745eee4 100644 --- a/utility/property.js +++ b/utility/property.js @@ -18,7 +18,7 @@ import baseProperty from '../internal/baseProperty'; * var getName = _.property('user'); * * _.map(users, getName); - * // => ['fred', barney'] + * // => ['fred', 'barney'] * * _.pluck(_.sortBy(users, getName), 'user'); * // => ['barney', 'fred'] diff --git a/utility/propertyOf.js b/utility/propertyOf.js index b0f9cd87f..6fa91f2ea 100644 --- a/utility/propertyOf.js +++ b/utility/propertyOf.js @@ -1,5 +1,5 @@ /** - * The inverse of `_.property`; this method creates a function which returns + * The opposite of `_.property`; this method creates a function which returns * the property value of a given key on `object`. * * @static