From 958ac72805a8753555e90e82f1f4b9124ff9a3c0 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 7 Sep 2012 21:09:21 -0700 Subject: [PATCH] Ensure the internal `stack` argument of `_.merge` doesn't pave the 4th argument passed to it. [closes #69] Former-commit-id: b33e1cb7795294b9481e2c9c6888d0f37419208d --- lodash.js | 9 ++++----- test/test.js | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lodash.js b/lodash.js index c67cd23b1..5e25bbfde 100644 --- a/lodash.js +++ b/lodash.js @@ -1817,22 +1817,21 @@ * // => [{ 'name': 'moe', 'age': 40 }, { 'name': 'larry', 'age': 50 }] */ var merge = createIterator(extendIteratorOptions, { - 'args': 'object, source, indicator, stack', + 'args': 'object, source, indicator', 'top': - 'var destValue, found, isArr, stackLength, recursive = indicator == isPlainObject;\n' + - 'if (!recursive) stack = [];\n' + + 'var isArr, recursive = indicator == isPlainObject, stack = recursive ? arguments[3] : [];\n' + 'for (var argsIndex = 1, argsLength = recursive ? 2 : arguments.length; argsIndex < argsLength; argsIndex++) {\n' + ' if (iteratee = arguments[argsIndex]) {', 'inLoop': 'if (value && ((isArr = isArray(value)) || isPlainObject(value))) {\n' + - ' found = false; stackLength = stack.length;\n' + + ' var found = false, stackLength = stack.length;\n' + ' while (stackLength--) {\n' + ' if (found = stack[stackLength].source == value) break\n' + ' }\n' + ' if (found) {\n' + ' result[index] = stack[stackLength].value\n' + ' } else {\n' + - ' destValue = (destValue = result[index]) && isArr\n' + + ' var destValue = (destValue = result[index]) && isArr\n' + ' ? (isArray(destValue) ? destValue : [])\n' + ' : (isPlainObject(destValue) ? destValue : {});\n' + ' stack.push({ value: destValue, source: value });\n' + diff --git a/test/test.js b/test/test.js index 99f2622a0..dceb709da 100644 --- a/test/test.js +++ b/test/test.js @@ -1002,6 +1002,11 @@ var actual = _.merge(object, source); equal(_.isArguments(actual.args), false); }); + + test('should work with four arguments', function() { + var expected = { 'a': 4 }; + deepEqual(_.merge({ 'a': 1 }, { 'a': 2 }, { 'a': 3 }, expected), expected); + }); }(1, 2, 3)); /*--------------------------------------------------------------------------*/