mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-14 04:37:50 +00:00
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:
@@ -1178,7 +1178,7 @@
|
|||||||
|
|
||||||
// allows working with "Collections" methods without using their `callback`
|
// allows working with "Collections" methods without using their `callback`
|
||||||
// argument, `index|key`, for this method's `callback`
|
// argument, `index|key`, for this method's `callback`
|
||||||
if (typeof deep == 'function') {
|
if (typeof deep != 'boolean' && deep != null) {
|
||||||
thisArg = callback;
|
thisArg = callback;
|
||||||
callback = deep;
|
callback = deep;
|
||||||
deep = false;
|
deep = false;
|
||||||
@@ -3529,7 +3529,7 @@
|
|||||||
// juggle arguments
|
// juggle arguments
|
||||||
if (typeof isShallow != 'boolean' && isShallow != null) {
|
if (typeof isShallow != 'boolean' && isShallow != null) {
|
||||||
thisArg = callback;
|
thisArg = callback;
|
||||||
callback = isShallow;
|
callback = !(thisArg && thisArg[isShallow] === array) ? isShallow : undefined;
|
||||||
isShallow = false;
|
isShallow = false;
|
||||||
}
|
}
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
@@ -4090,7 +4090,7 @@
|
|||||||
// juggle arguments
|
// juggle arguments
|
||||||
if (typeof isSorted != 'boolean' && isSorted != null) {
|
if (typeof isSorted != 'boolean' && isSorted != null) {
|
||||||
thisArg = callback;
|
thisArg = callback;
|
||||||
callback = isSorted;
|
callback = !(thisArg && thisArg[isSorted] === array) ? isSorted : undefined;
|
||||||
isSorted = false;
|
isSorted = false;
|
||||||
}
|
}
|
||||||
// init value cache for large arrays
|
// init value cache for large arrays
|
||||||
|
|||||||
18
test/test.js
18
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] }],
|
var expected = [{ 'a': [0] }, { 'b': [1] }],
|
||||||
actual = _.map(expected, _.clone);
|
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() {
|
test('should deep clone `index` and `input` array properties', function() {
|
||||||
@@ -928,6 +928,13 @@
|
|||||||
deepEqual(_.flatten(array, 'a'), [1, 2, 3]);
|
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() {
|
test('should treat sparse arrays as dense', function() {
|
||||||
var array = [[1, 2, 3], Array(3)],
|
var array = [[1, 2, 3], Array(3)],
|
||||||
expected = [1, 2, 3],
|
expected = [1, 2, 3],
|
||||||
@@ -3105,6 +3112,13 @@
|
|||||||
deepEqual(actual, [1, 2, 3]);
|
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() {
|
test('should distinguish between numbers and numeric strings', function() {
|
||||||
var array = [],
|
var array = [],
|
||||||
expected = ['2', 2, Object('2'), Object(2)];
|
expected = ['2', 2, Object('2'), Object(2)];
|
||||||
|
|||||||
Reference in New Issue
Block a user