7fife-backend/node_modules/underscore/amd/uniq.js

38 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-03-07 13:01:44 +00:00
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;
});