Ensure _.intersection works with a single array. [closes #1199]

This commit is contained in:
jdalton
2015-05-11 23:52:56 -07:00
parent 846bde35ff
commit c6ff845ecb
2 changed files with 34 additions and 34 deletions

View File

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

View File

@@ -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() {