mirror of
https://github.com/stCarolas/setup-maven.git
synced 2024-11-27 05:36:09 +08:00
38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
define(['./isBoolean', './_cb', './_getLength', './contains'], function (isBoolean, _cb, _getLength, contains) {
|
|
|
|
// Produce a duplicate-free version of the array. If the array has already
|
|
// been sorted, you have the option of using a faster algorithm.
|
|
// The faster algorithm will not work with an iteratee if the iteratee
|
|
// is not a one-to-one function, so providing an iteratee will disable
|
|
// the faster algorithm.
|
|
function uniq(array, isSorted, iteratee, context) {
|
|
if (!isBoolean(isSorted)) {
|
|
context = iteratee;
|
|
iteratee = isSorted;
|
|
isSorted = false;
|
|
}
|
|
if (iteratee != null) iteratee = _cb(iteratee, context);
|
|
var result = [];
|
|
var seen = [];
|
|
for (var i = 0, length = _getLength(array); i < length; i++) {
|
|
var value = array[i],
|
|
computed = iteratee ? iteratee(value, i, array) : value;
|
|
if (isSorted && !iteratee) {
|
|
if (!i || seen !== computed) result.push(value);
|
|
seen = computed;
|
|
} else if (iteratee) {
|
|
if (!contains(seen, computed)) {
|
|
seen.push(computed);
|
|
result.push(value);
|
|
}
|
|
} else if (!contains(result, value)) {
|
|
result.push(value);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
return uniq;
|
|
|
|
});
|