1234567891011121314151617181920212223242526272829303132333435363738 |
- var isBoolean = require('./isBoolean.js');
- var _cb = require('./_cb.js');
- var _getLength = require('./_getLength.js');
- var contains = require('./contains.js');
- // 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;
- }
- module.exports = uniq;
|