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