No Description

uniq.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637
  1. define(['./isBoolean', './_cb', './_getLength', './contains'], function (isBoolean, _cb, _getLength, contains) {
  2. // Produce a duplicate-free version of the array. If the array has already
  3. // been sorted, you have the option of using a faster algorithm.
  4. // The faster algorithm will not work with an iteratee if the iteratee
  5. // is not a one-to-one function, so providing an iteratee will disable
  6. // the faster algorithm.
  7. function uniq(array, isSorted, iteratee, context) {
  8. if (!isBoolean(isSorted)) {
  9. context = iteratee;
  10. iteratee = isSorted;
  11. isSorted = false;
  12. }
  13. if (iteratee != null) iteratee = _cb(iteratee, context);
  14. var result = [];
  15. var seen = [];
  16. for (var i = 0, length = _getLength(array); i < length; i++) {
  17. var value = array[i],
  18. computed = iteratee ? iteratee(value, i, array) : value;
  19. if (isSorted && !iteratee) {
  20. if (!i || seen !== computed) result.push(value);
  21. seen = computed;
  22. } else if (iteratee) {
  23. if (!contains(seen, computed)) {
  24. seen.push(computed);
  25. result.push(value);
  26. }
  27. } else if (!contains(result, value)) {
  28. result.push(value);
  29. }
  30. }
  31. return result;
  32. }
  33. return uniq;
  34. });