index.js 829 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*!
  2. * array-unique <https://github.com/jonschlinkert/array-unique>
  3. *
  4. * Copyright (c) 2014-2015, Jon Schlinkert.
  5. * Licensed under the MIT License.
  6. */
  7. 'use strict';
  8. module.exports = function unique(arr) {
  9. if (!Array.isArray(arr)) {
  10. throw new TypeError('array-unique expects an array.');
  11. }
  12. var len = arr.length;
  13. var i = -1;
  14. while (i++ < len) {
  15. var j = i + 1;
  16. for (; j < arr.length; ++j) {
  17. if (arr[i] === arr[j]) {
  18. arr.splice(j--, 1);
  19. }
  20. }
  21. }
  22. return arr;
  23. };
  24. module.exports.immutable = function uniqueImmutable(arr) {
  25. if (!Array.isArray(arr)) {
  26. throw new TypeError('array-unique expects an array.');
  27. }
  28. var arrLen = arr.length;
  29. var newArr = new Array(arrLen);
  30. for (var i = 0; i < arrLen; i++) {
  31. newArr[i] = arr[i];
  32. }
  33. return module.exports(newArr);
  34. };