Ensure _.xor works with more than two arrays. [closes #2758]

This commit is contained in:
John-David Dalton
2016-10-26 01:37:00 -07:00
parent 73e2562cd1
commit 729d1a57aa
2 changed files with 16 additions and 9 deletions

View File

@@ -4400,18 +4400,25 @@
* @returns {Array} Returns the new array of values. * @returns {Array} Returns the new array of values.
*/ */
function baseXor(arrays, iteratee, comparator) { function baseXor(arrays, iteratee, comparator) {
var length = arrays.length;
if (length < 2) {
return length ? baseUniq(arrays[0]) : [];
}
var index = -1, var index = -1,
length = arrays.length; result = Array(length);
while (++index < length) { while (++index < length) {
var result = result var array = arrays[index],
? arrayPush( othIndex = -1;
baseDifference(result, arrays[index], iteratee, comparator),
baseDifference(arrays[index], result, iteratee, comparator) while (++othIndex < length) {
) var othArray = arrays[othIndex];
: arrays[index]; if (othArray !== array) {
result[index] = baseDifference(result[index] || array, othArray, iteratee, comparator);
}
}
} }
return (result && result.length) ? baseUniq(result, iteratee, comparator) : []; return baseUniq(baseFlatten(result, 1), iteratee, comparator);
} }
/** /**

View File

@@ -25264,7 +25264,7 @@
assert.expect(2); assert.expect(2);
var actual = func([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]); var actual = func([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]);
assert.deepEqual(actual, [1, 4, 5]); assert.deepEqual(actual, [1, 4]);
actual = func([1, 1]); actual = func([1, 1]);
assert.deepEqual(actual, [1]); assert.deepEqual(actual, [1]);