You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

4 years ago
  1. [![Build Status](https://travis-ci.org/fgnass/uniqs.svg?branch=master)](https://travis-ci.org/fgnass/uniqs)
  2. ### Tiny utility to create unions and de-duplicated lists.
  3. Example:
  4. ```js
  5. var uniqs = require('uniqs');
  6. var foo = { foo: 23 };
  7. var list = [3, 2, 2, 1, foo, foo];
  8. uniqs(list);
  9. // => [3, 2, 1, { foo: 23 }]
  10. ```
  11. You can pass multiple lists to create a union:
  12. ```js
  13. uniqs([2, 1, 1], [2, 3, 3, 4], [4, 3, 2]);
  14. // => [2, 1, 3, 4]
  15. ```
  16. Passing individual items works too:
  17. ```js
  18. uniqs(3, 2, 2, [1, 1, 2]);
  19. // => [3, 2, 1]
  20. ```
  21. ### Summary
  22. * Uniqueness is defined based on strict object equality.
  23. * The lists do not need to be sorted.
  24. * The resulting array contains the items in the order of their first appearance.
  25. ### About
  26. This package has been written to accompany utilities like
  27. [flatten](https://npmjs.org/package/flatten) as alternative to full-blown
  28. libraries like underscore or lodash.
  29. The implementation is optimized for simplicity rather than performance and
  30. looks like this:
  31. ```js
  32. module.exports = function uniqs() {
  33. var list = Array.prototype.concat.apply([], arguments);
  34. return list.filter(function(item, i) {
  35. return i == list.indexOf(item);
  36. });
  37. };
  38. ```
  39. ### License
  40. MIT