sortedUniq.js 742 B

1234567891011121314151617181920212223242526272829
  1. /**
  2. * An implementation of `_.uniq` optimized for sorted arrays without support
  3. * for callback shorthands and `this` binding.
  4. *
  5. * @private
  6. * @param {Array} array The array to inspect.
  7. * @param {Function} [iteratee] The function invoked per iteration.
  8. * @returns {Array} Returns the new duplicate free array.
  9. */
  10. function sortedUniq(array, iteratee) {
  11. var seen,
  12. index = -1,
  13. length = array.length,
  14. resIndex = -1,
  15. result = [];
  16. while (++index < length) {
  17. var value = array[index],
  18. computed = iteratee ? iteratee(value, index, array) : value;
  19. if (!index || seen !== computed) {
  20. seen = computed;
  21. result[++resIndex] = value;
  22. }
  23. }
  24. return result;
  25. }
  26. module.exports = sortedUniq;