diff --git a/fp/_baseConvert.js b/fp/_baseConvert.js index 524498526..c9d324747 100644 --- a/fp/_baseConvert.js +++ b/fp/_baseConvert.js @@ -1,6 +1,9 @@ var mapping = require('./_mapping'), fallbackHolder = require('./placeholder'); +/** Built-in value reference. */ +var push = Array.prototype.push; + /** * Creates a function, with an arity of `n`, that invokes `func` with the * arguments it receives. @@ -61,6 +64,36 @@ function createCloner(func) { }; } +/** + * This function is like `_.spread` except that it includes arguments after those spread. + * + * @private + * @param {Function} func The function to spread arguments over. + * @param {number} start The start position of the spread. + * @returns {Function} Returns the new function. + */ +function spread(func, start) { + return function() { + var length = arguments.length, + args = Array(length); + + while (length--) { + args[length] = arguments[length]; + } + var array = args[start], + lastIndex = args.length - 1, + otherArgs = args.slice(0, start); + + if (array) { + push.apply(otherArgs, array); + } + if (start != lastIndex) { + push.apply(otherArgs, args.slice(start + 1)); + } + return func.apply(this, otherArgs); + }; +} + /** * Creates a function that wraps `func` and uses `cloner` to clone the first * argument it receives. @@ -141,7 +174,6 @@ function baseConvert(util, name, func, options) { 'iteratee': util.iteratee, 'keys': util.keys, 'rearg': util.rearg, - 'spread': util.spread, 'toInteger': util.toInteger, 'toPath': util.toPath }; @@ -155,7 +187,6 @@ function baseConvert(util, name, func, options) { isFunction = helpers.isFunction, keys = helpers.keys, rearg = helpers.rearg, - spread = helpers.spread, toInteger = helpers.toInteger, toPath = helpers.toPath; diff --git a/lib/fp/template/modules/_util.jst b/lib/fp/template/modules/_util.jst index f8148129e..708446302 100644 --- a/lib/fp/template/modules/_util.jst +++ b/lib/fp/template/modules/_util.jst @@ -9,7 +9,6 @@ module.exports = { 'iteratee': require('../iteratee'), 'keys': require('../_baseKeys'), 'rearg': require('../rearg'), - 'spread': require('../spread'), 'toInteger': require('../toInteger'), 'toPath': require('../toPath') }; diff --git a/lodash.js b/lodash.js index e64b0af32..05cd9198c 100644 --- a/lodash.js +++ b/lodash.js @@ -10853,9 +10853,6 @@ if (array) { arrayPush(otherArgs, array); } - if (start != lastIndex) { - arrayPush(otherArgs, castSlice(args, start + 1)); - } return apply(func, this, otherArgs); }); } diff --git a/test/test.js b/test/test.js index e7753db2d..f96a752f4 100644 --- a/test/test.js +++ b/test/test.js @@ -20870,10 +20870,11 @@ QUnit.test('should spread arguments to `func`', function(assert) { assert.expect(2); - var spread = _.spread(fn); + var spread = _.spread(fn), + expected = [1, 2]; - assert.deepEqual(spread([1, 2]), [1, 2]); - assert.deepEqual(spread([1, 2], 3), [1, 2, 3]); + assert.deepEqual(spread([1, 2]), expected); + assert.deepEqual(spread([1, 2], 3), expected); }); QUnit.test('should accept a falsey `array`', function(assert) { @@ -20894,10 +20895,11 @@ QUnit.test('should work with `start`', function(assert) { assert.expect(2); - var spread = _.spread(fn, 1); + var spread = _.spread(fn, 1), + expected = [1, 2, 3]; - assert.deepEqual(spread(1, [2, 3]), [1, 2, 3]); - assert.deepEqual(spread(1, [2, 3], 4), [1, 2, 3, 4]); + assert.deepEqual(spread(1, [2, 3]), expected); + assert.deepEqual(spread(1, [2, 3], 4), expected); }); QUnit.test('should treat `start` as `0` for negative or `NaN` values', function(assert) { @@ -20917,10 +20919,11 @@ QUnit.test('should coerce `start` to an integer', function(assert) { assert.expect(2); - var spread = _.spread(fn, 1.6); + var spread = _.spread(fn, 1.6), + expected = [1, 2, 3]; - assert.deepEqual(spread(1, [2, 3]), [1, 2, 3]); - assert.deepEqual(spread(1, [2, 3], 4), [1, 2, 3, 4]); + assert.deepEqual(spread(1, [2, 3]), expected); + assert.deepEqual(spread(1, [2, 3], 4), expected); }); }());