mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 23:57: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]);
|
||||
* // => [2]
|
||||
*/
|
||||
function intersection() {
|
||||
var args = [],
|
||||
argsIndex = -1,
|
||||
argsLength = arguments.length,
|
||||
caches = [],
|
||||
var intersection = restParam(function(arrays) {
|
||||
var othLength = arrays.length,
|
||||
othIndex = othLength,
|
||||
caches = Array(length),
|
||||
indexOf = getIndexOf(),
|
||||
isCommon = indexOf == baseIndexOf,
|
||||
result = [];
|
||||
|
||||
while (++argsIndex < argsLength) {
|
||||
var value = arguments[argsIndex];
|
||||
if (isArrayLike(value)) {
|
||||
args.push(value);
|
||||
caches.push((isCommon && value.length >= 120) ? createCache(argsIndex && value) : null);
|
||||
}
|
||||
while (othIndex--) {
|
||||
var value = arrays[othIndex] = isArrayLike(value = arrays[othIndex]) ? value : [];
|
||||
caches[othIndex] = (isCommon && value.length >= 120) ? createCache(othIndex && value) : null;
|
||||
}
|
||||
argsLength = args.length;
|
||||
if (argsLength < 2) {
|
||||
return result;
|
||||
}
|
||||
var array = args[0],
|
||||
var array = arrays[0],
|
||||
index = -1,
|
||||
length = array ? array.length : 0,
|
||||
seen = caches[0];
|
||||
@@ -5258,10 +5250,10 @@
|
||||
while (++index < length) {
|
||||
value = array[index];
|
||||
if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) {
|
||||
argsIndex = argsLength;
|
||||
while (--argsIndex) {
|
||||
var cache = caches[argsIndex];
|
||||
if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value, 0)) < 0) {
|
||||
var othIndex = othLength;
|
||||
while (--othIndex) {
|
||||
var cache = caches[othIndex];
|
||||
if ((cache ? cacheIndexOf(cache, value) : indexOf(arrays[othIndex], value, 0)) < 0) {
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
@@ -5272,7 +5264,7 @@
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets the last element of `array`.
|
||||
|
||||
34
test/test.js
34
test/test.js
@@ -6848,19 +6848,12 @@
|
||||
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() {
|
||||
var object = {},
|
||||
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(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() {
|
||||
@@ -6868,11 +6861,26 @@
|
||||
deepEqual(_.intersection([1, NaN, 3], largeArray), [NaN]);
|
||||
});
|
||||
|
||||
test('should ignore values that are not arrays or `arguments` objects', 3, function() {
|
||||
var array = [0, 1, null, 3];
|
||||
deepEqual(_.intersection(array, 3, null, { '0': 1 }), []);
|
||||
deepEqual(_.intersection(null, array, null, [2, 1]), [1]);
|
||||
deepEqual(_.intersection(array, null, args, null), [1, 3]);
|
||||
test('should work with `arguments` objects', 2, function() {
|
||||
var array = [0, 1, null, 3],
|
||||
expected = [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() {
|
||||
|
||||
Reference in New Issue
Block a user