Ensure _.clone, _.flatten, and _.uniq can be used as a callback for methods like _.map. [closes #270]

Former-commit-id: fb62b5bbdad844cb04c3259c323e27fb81932809
This commit is contained in:
John-David Dalton
2013-05-14 00:49:31 -07:00
parent b72b0d60cb
commit 4b3009a195
2 changed files with 19 additions and 5 deletions

View File

@@ -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

View File

@@ -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)];