mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 19:07:49 +00:00
Ensure _.intersection works with a single array. [closes #1199]
This commit is contained in:
@@ -5229,27 +5229,19 @@
|
|||||||
* _.intersection([1, 2], [4, 2], [2, 1]);
|
* _.intersection([1, 2], [4, 2], [2, 1]);
|
||||||
* // => [2]
|
* // => [2]
|
||||||
*/
|
*/
|
||||||
function intersection() {
|
var intersection = restParam(function(arrays) {
|
||||||
var args = [],
|
var othLength = arrays.length,
|
||||||
argsIndex = -1,
|
othIndex = othLength,
|
||||||
argsLength = arguments.length,
|
caches = Array(length),
|
||||||
caches = [],
|
|
||||||
indexOf = getIndexOf(),
|
indexOf = getIndexOf(),
|
||||||
isCommon = indexOf == baseIndexOf,
|
isCommon = indexOf == baseIndexOf,
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
while (++argsIndex < argsLength) {
|
while (othIndex--) {
|
||||||
var value = arguments[argsIndex];
|
var value = arrays[othIndex] = isArrayLike(value = arrays[othIndex]) ? value : [];
|
||||||
if (isArrayLike(value)) {
|
caches[othIndex] = (isCommon && value.length >= 120) ? createCache(othIndex && value) : null;
|
||||||
args.push(value);
|
|
||||||
caches.push((isCommon && value.length >= 120) ? createCache(argsIndex && value) : null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
argsLength = args.length;
|
var array = arrays[0],
|
||||||
if (argsLength < 2) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
var array = args[0],
|
|
||||||
index = -1,
|
index = -1,
|
||||||
length = array ? array.length : 0,
|
length = array ? array.length : 0,
|
||||||
seen = caches[0];
|
seen = caches[0];
|
||||||
@@ -5258,10 +5250,10 @@
|
|||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
value = array[index];
|
value = array[index];
|
||||||
if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) {
|
if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) {
|
||||||
argsIndex = argsLength;
|
var othIndex = othLength;
|
||||||
while (--argsIndex) {
|
while (--othIndex) {
|
||||||
var cache = caches[argsIndex];
|
var cache = caches[othIndex];
|
||||||
if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value, 0)) < 0) {
|
if ((cache ? cacheIndexOf(cache, value) : indexOf(arrays[othIndex], value, 0)) < 0) {
|
||||||
continue outer;
|
continue outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5272,7 +5264,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the last element of `array`.
|
* Gets the last element of `array`.
|
||||||
|
|||||||
34
test/test.js
34
test/test.js
@@ -6848,19 +6848,12 @@
|
|||||||
deepEqual(actual, [NaN]);
|
deepEqual(actual, [NaN]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with large arrays of objects', 1, function() {
|
|
||||||
var object = {},
|
|
||||||
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(object));
|
|
||||||
|
|
||||||
deepEqual(_.intersection([object], largeArray), [object]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should work with large arrays of objects', 2, function() {
|
test('should work with large arrays of objects', 2, function() {
|
||||||
var object = {},
|
var object = {},
|
||||||
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(object));
|
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(object));
|
||||||
|
|
||||||
deepEqual(_.intersection([object], largeArray), [object]);
|
deepEqual(_.intersection([object], largeArray), [object]);
|
||||||
deepEqual(_.intersection(_.range(LARGE_ARRAY_SIZE), null, [1]), [1]);
|
deepEqual(_.intersection(_.range(LARGE_ARRAY_SIZE), [1]), [1]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with large arrays of `NaN`', 1, function() {
|
test('should work with large arrays of `NaN`', 1, function() {
|
||||||
@@ -6868,11 +6861,26 @@
|
|||||||
deepEqual(_.intersection([1, NaN, 3], largeArray), [NaN]);
|
deepEqual(_.intersection([1, NaN, 3], largeArray), [NaN]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should ignore values that are not arrays or `arguments` objects', 3, function() {
|
test('should work with `arguments` objects', 2, function() {
|
||||||
var array = [0, 1, null, 3];
|
var array = [0, 1, null, 3],
|
||||||
deepEqual(_.intersection(array, 3, null, { '0': 1 }), []);
|
expected = [1, 3];
|
||||||
deepEqual(_.intersection(null, array, null, [2, 1]), [1]);
|
|
||||||
deepEqual(_.intersection(array, null, args, null), [1, 3]);
|
deepEqual(_.intersection(array, args), expected);
|
||||||
|
deepEqual(_.intersection(args, array), expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work with a single array', 1, function() {
|
||||||
|
var actual = _.intersection([1, 1, 3, 2, 2]);
|
||||||
|
deepEqual(actual, [1, 3, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should treat values that are not arrays or `arguments` objects as empty', 3, function() {
|
||||||
|
var array = [0, 1, null, 3],
|
||||||
|
values = [3, null, { '0': 1 }];
|
||||||
|
|
||||||
|
_.each(values, function(value) {
|
||||||
|
deepEqual(_.intersection(array, value), []);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return a wrapped value when chaining', 2, function() {
|
test('should return a wrapped value when chaining', 2, function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user