mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 01:57:50 +00:00
Merge pull request #486 from kitcambridge/uniq
`_.uniq()` should work with sparse arrays.
This commit is contained in:
@@ -89,6 +89,28 @@ $(document).ready(function() {
|
|||||||
|
|
||||||
var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);
|
var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);
|
||||||
equal(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
|
equal(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
|
||||||
|
|
||||||
|
var list = [];
|
||||||
|
list[2] = list[3] = null;
|
||||||
|
list[8] = 2;
|
||||||
|
list[10] = 2;
|
||||||
|
list[11] = 5;
|
||||||
|
list[14] = 5;
|
||||||
|
list[16] = 8;
|
||||||
|
list[19] = 8;
|
||||||
|
list[26] = list[29] = undefined;
|
||||||
|
list[33] = "hi";
|
||||||
|
|
||||||
|
var result = _.uniq(list, true);
|
||||||
|
if (0 in [undefined]) {
|
||||||
|
// According to the JScript ES 3 spec, section 2.1.26, JScript 5.x (IE <=
|
||||||
|
// 8) treats `undefined` elements in arrays as elisions.
|
||||||
|
deepEqual(result, [null, 2, 5, 8, undefined, "hi"], "Works with sorted sparse arrays");
|
||||||
|
equal(result.length, 6, "The resulting array should not be sparse");
|
||||||
|
} else {
|
||||||
|
deepEqual(result, [null, 2, 5, 8, "hi"], "Works with sorted sparse arrays where `undefined` elements are elided");
|
||||||
|
equal(result.length, 5, "The resulting array should not be sparse");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test("arrays: intersection", function() {
|
test("arrays: intersection", function() {
|
||||||
|
|||||||
@@ -372,15 +372,17 @@
|
|||||||
// Aliased as `unique`.
|
// Aliased as `unique`.
|
||||||
_.uniq = _.unique = function(array, isSorted, iterator) {
|
_.uniq = _.unique = function(array, isSorted, iterator) {
|
||||||
var initial = iterator ? _.map(array, iterator) : array;
|
var initial = iterator ? _.map(array, iterator) : array;
|
||||||
var result = [];
|
var results = [];
|
||||||
_.reduce(initial, function(memo, el, i) {
|
// The `isSorted` flag is irrelevant if the array only contains two elements.
|
||||||
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) {
|
if (array.length < 3) isSorted = true;
|
||||||
memo[memo.length] = el;
|
_.reduce(initial, function (memo, value, index) {
|
||||||
result[result.length] = array[i];
|
if (isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) {
|
||||||
|
memo.push(value);
|
||||||
|
results.push(array[index]);
|
||||||
}
|
}
|
||||||
return memo;
|
return memo;
|
||||||
}, []);
|
}, []);
|
||||||
return result;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Produce an array that contains the union: each distinct element from all of
|
// Produce an array that contains the union: each distinct element from all of
|
||||||
|
|||||||
Reference in New Issue
Block a user