Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

_baseUniq.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var SetCache = require('./_SetCache'),
  2. arrayIncludes = require('./_arrayIncludes'),
  3. arrayIncludesWith = require('./_arrayIncludesWith'),
  4. cacheHas = require('./_cacheHas'),
  5. createSet = require('./_createSet'),
  6. setToArray = require('./_setToArray');
  7. /** Used as the size to enable large array optimizations. */
  8. var LARGE_ARRAY_SIZE = 200;
  9. /**
  10. * The base implementation of `_.uniqBy` without support for iteratee shorthands.
  11. *
  12. * @private
  13. * @param {Array} array The array to inspect.
  14. * @param {Function} [iteratee] The iteratee invoked per element.
  15. * @param {Function} [comparator] The comparator invoked per element.
  16. * @returns {Array} Returns the new duplicate free array.
  17. */
  18. function baseUniq(array, iteratee, comparator) {
  19. var index = -1,
  20. includes = arrayIncludes,
  21. length = array.length,
  22. isCommon = true,
  23. result = [],
  24. seen = result;
  25. if (comparator) {
  26. isCommon = false;
  27. includes = arrayIncludesWith;
  28. }
  29. else if (length >= LARGE_ARRAY_SIZE) {
  30. var set = iteratee ? null : createSet(array);
  31. if (set) {
  32. return setToArray(set);
  33. }
  34. isCommon = false;
  35. includes = cacheHas;
  36. seen = new SetCache;
  37. }
  38. else {
  39. seen = iteratee ? [] : result;
  40. }
  41. outer:
  42. while (++index < length) {
  43. var value = array[index],
  44. computed = iteratee ? iteratee(value) : value;
  45. value = (comparator || value !== 0) ? value : 0;
  46. if (isCommon && computed === computed) {
  47. var seenIndex = seen.length;
  48. while (seenIndex--) {
  49. if (seen[seenIndex] === computed) {
  50. continue outer;
  51. }
  52. }
  53. if (iteratee) {
  54. seen.push(computed);
  55. }
  56. result.push(value);
  57. }
  58. else if (!includes(seen, computed, comparator)) {
  59. if (seen !== result) {
  60. seen.push(computed);
  61. }
  62. result.push(value);
  63. }
  64. }
  65. return result;
  66. }
  67. module.exports = baseUniq;