uniq.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var baseCallback = require('../internal/baseCallback'),
  2. baseUniq = require('../internal/baseUniq'),
  3. isIterateeCall = require('../internal/isIterateeCall'),
  4. sortedUniq = require('../internal/sortedUniq');
  5. /**
  6. * Creates a duplicate-free version of an array, using
  7. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  8. * for equality comparisons, in which only the first occurence of each element
  9. * is kept. Providing `true` for `isSorted` performs a faster search algorithm
  10. * for sorted arrays. If an iteratee function is provided it's invoked for
  11. * each element in the array to generate the criterion by which uniqueness
  12. * is computed. The `iteratee` is bound to `thisArg` and invoked with three
  13. * arguments: (value, index, array).
  14. *
  15. * If a property name is provided for `iteratee` the created `_.property`
  16. * style callback returns the property value of the given element.
  17. *
  18. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  19. * style callback returns `true` for elements that have a matching property
  20. * value, else `false`.
  21. *
  22. * If an object is provided for `iteratee` the created `_.matches` style
  23. * callback returns `true` for elements that have the properties of the given
  24. * object, else `false`.
  25. *
  26. * @static
  27. * @memberOf _
  28. * @alias unique
  29. * @category Array
  30. * @param {Array} array The array to inspect.
  31. * @param {boolean} [isSorted] Specify the array is sorted.
  32. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  33. * @param {*} [thisArg] The `this` binding of `iteratee`.
  34. * @returns {Array} Returns the new duplicate-value-free array.
  35. * @example
  36. *
  37. * _.uniq([2, 1, 2]);
  38. * // => [2, 1]
  39. *
  40. * // using `isSorted`
  41. * _.uniq([1, 1, 2], true);
  42. * // => [1, 2]
  43. *
  44. * // using an iteratee function
  45. * _.uniq([1, 2.5, 1.5, 2], function(n) {
  46. * return this.floor(n);
  47. * }, Math);
  48. * // => [1, 2.5]
  49. *
  50. * // using the `_.property` callback shorthand
  51. * _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  52. * // => [{ 'x': 1 }, { 'x': 2 }]
  53. */
  54. function uniq(array, isSorted, iteratee, thisArg) {
  55. var length = array ? array.length : 0;
  56. if (!length) {
  57. return [];
  58. }
  59. if (isSorted != null && typeof isSorted != 'boolean') {
  60. thisArg = iteratee;
  61. iteratee = isIterateeCall(array, isSorted, thisArg) ? undefined : isSorted;
  62. isSorted = false;
  63. }
  64. iteratee = iteratee == null ? iteratee : baseCallback(iteratee, thisArg, 3);
  65. return (isSorted)
  66. ? sortedUniq(array, iteratee)
  67. : baseUniq(array, iteratee);
  68. }
  69. module.exports = uniq;