From 4b3009a1957ee00e07e506a444b1627541180051 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 14 May 2013 00:49:31 -0700 Subject: [PATCH] Ensure `_.clone`, `_.flatten`, and `_.uniq` can be used as a `callback` for methods like `_.map`. [closes #270] Former-commit-id: fb62b5bbdad844cb04c3259c323e27fb81932809 --- lodash.js | 6 +++--- test/test.js | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lodash.js b/lodash.js index 922174a77..d55cec4e9 100644 --- a/lodash.js +++ b/lodash.js @@ -1178,7 +1178,7 @@ // allows working with "Collections" methods without using their `callback` // argument, `index|key`, for this method's `callback` - if (typeof deep == 'function') { + if (typeof deep != 'boolean' && deep != null) { thisArg = callback; callback = deep; deep = false; @@ -3529,7 +3529,7 @@ // juggle arguments if (typeof isShallow != 'boolean' && isShallow != null) { thisArg = callback; - callback = isShallow; + callback = !(thisArg && thisArg[isShallow] === array) ? isShallow : undefined; isShallow = false; } if (callback != null) { @@ -4090,7 +4090,7 @@ // juggle arguments if (typeof isSorted != 'boolean' && isSorted != null) { thisArg = callback; - callback = isSorted; + callback = !(thisArg && thisArg[isSorted] === array) ? isSorted : undefined; isSorted = false; } // init value cache for large arrays diff --git a/test/test.js b/test/test.js index 4be611790..f5be7bc45 100644 --- a/test/test.js +++ b/test/test.js @@ -392,11 +392,11 @@ }); }); - test('should shallow clone when used as `callback` for `_.map`', function() { + test('should perform a shallow clone when used as `callback` for `_.map`', function() { var expected = [{ 'a': [0] }, { 'b': [1] }], actual = _.map(expected, _.clone); - ok(actual != expected && actual.a == expected.a && actual.b == expected.b); + ok(actual[0] != expected[0] && actual[0].a === expected[0].a && actual[1].b === expected[1].b); }); test('should deep clone `index` and `input` array properties', function() { @@ -928,6 +928,13 @@ deepEqual(_.flatten(array, 'a'), [1, 2, 3]); }); + test('should perform a deep flatten when used as `callback` for `_.map`', function() { + var array = [[[['a']]], [[['b']]]], + actual = _.map(array, _.flatten); + + deepEqual(actual, [['a'], ['b']]); + }); + test('should treat sparse arrays as dense', function() { var array = [[1, 2, 3], Array(3)], expected = [1, 2, 3], @@ -3105,6 +3112,13 @@ deepEqual(actual, [1, 2, 3]); }); + test('should perform an unsorted uniq operation when used as `callback` for `_.map`', function() { + var array = [[2, 1, 2], [1, 2, 1]], + actual = _.map(array, _.uniq); + + deepEqual(actual, [[2, 1], [1, 2]]); + }); + test('should distinguish between numbers and numeric strings', function() { var array = [], expected = ['2', 2, Object('2'), Object(2)];