Skip to content

Commit 5d1fbe5

Browse files
author
Shawn Dellysse
committed
added support for immutable processing
1 parent 2de837d commit 5d1fbe5

File tree

5 files changed

+142
-6
lines changed

5 files changed

+142
-6
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# array-unique [![NPM version](https://badge.fury.io/js/array-unique.svg)](http://badge.fury.io/js/array-unique) [![Build Status](https://travis-ci.org/jonschlinkert/array-unique.svg)](https://travis-ci.org/jonschlinkert/array-unique)
22

3-
> Return an array free of duplicate values. Fastest ES5 implementation.
3+
> Remove duplicate values from an array. Fastest ES5 implementation.
44
55
## Install with [npm](npmjs.org)
66

@@ -13,8 +13,17 @@ npm i array-unique --save
1313
```js
1414
var unique = require('array-unique');
1515

16-
unique(['a', 'b', 'c', 'c']);
17-
//=> ['a', 'b', 'c']
16+
var arr = ['a', 'b', 'c', 'c'];
17+
console.log(unique(arr)) //=> ['a', 'b', 'c']
18+
console.log(arr) //=> ['a', 'b', 'c']
19+
20+
/* The above modifies the input array. To prevent that at a slight performance cost: */
21+
var unique = require("array-unique").immutable;
22+
23+
var arr = ['a', 'b', 'c', 'c'];
24+
console.log(unique(arr)) //=> ['a', 'b', 'c']
25+
console.log(arr) //=> ['a', 'b', 'c', 'c']
26+
1827
```
1928

2029
## Related
@@ -48,4 +57,4 @@ Released under the MIT license
4857

4958
***
5059

51-
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 24, 2015._
60+
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 24, 2015._

benchmark/code/set-immutable.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('array-uniq').immutable;

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ module.exports = function unique(arr) {
2626
}
2727
return arr;
2828
};
29+
30+
module.exports.immutable = function uniqueImmutable(arr) {
31+
if (!Array.isArray(arr)) {
32+
throw new TypeError('array-unique expects an array.');
33+
}
34+
35+
var arrLen = arr.length;
36+
var newArr = new Array(arrLen);
37+
38+
for (var i = 0; i < arrLen; i++) {
39+
newArr[i] = arr[i];
40+
}
41+
42+
return module.exports(newArr);
43+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "array-unique",
3-
"description": "Return an array free of duplicate values. Fastest ES5 implementation.",
4-
"version": "0.2.1",
3+
"description": "Remove duplicate values from an array. Fastest ES5 implementation.",
4+
"version": "0.3.1",
55
"homepage": "https://github.com/jonschlinkert/array-unique",
66
"author": {
77
"name": "Jon Schlinkert",

test.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,112 @@ describe('unique', function () {
4040
});
4141
});
4242

43+
describe('unique.immutable', function () {
44+
it('should throw an error if the value passed is not an array:', function () {
45+
(function () {
46+
unique.immutable('a', 'b', 'c');
47+
}).should.throw('array-unique expects an array.');
48+
});
49+
50+
it('should return an array with unique values without modifying the input:', function () {
51+
(function () {
52+
var original = ['a', 'b', 'c', 'a', 'b', 'd'];
53+
var before = ['a', 'b', 'c', 'a', 'b', 'd'];
54+
var expected = ['a', 'b', 'c', 'd'];
55+
56+
unique.immutable(before).should.eql(expected);
57+
before.should.eql(original);
58+
})();
59+
60+
(function () {
61+
var original = ['a', 'b', 'c', 'a', 'b', 'a', 'b', 'c', 'b', 'f', 'a', 'b'];
62+
var before = ['a', 'b', 'c', 'a', 'b', 'a', 'b', 'c', 'b', 'f', 'a', 'b'];
63+
var expected = ['a', 'b', 'c', 'f'];
64+
65+
unique.immutable(before).should.eql(expected);
66+
before.should.eql(original);
67+
})();
68+
69+
(function () {
70+
var original = ['a', 'b', 'c', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b']
71+
var before = ['a', 'b', 'c', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'f', 'a', 'b', 'x', 'y', 'z', 'a', 'b']
72+
var expected = ['a', 'b', 'c', 'f', 'x', 'y', 'z'];
73+
unique.immutable(before).should.eql(expected);
74+
before.should.eql(original);
75+
})();
76+
77+
(function () {
78+
var original = [
79+
'foo/bar/baz/quux/fez/test/fixtures',
80+
'foo/bar/baz/quux/fez/test/fixtures',
81+
'foo/bar/baz/quux/fez/test/fixtures/a.js',
82+
'foo/bar/baz/quux/fez/test/fixtures',
83+
'foo/bar/baz/quux/fez/test/fixtures',
84+
'foo/bar/baz/quux/fez/test/fixtures/b.js',
85+
'foo/bar/baz/quux/fez/test/fixtures/b.js',
86+
'foo/bar/baz/quux/fez/test/fixtures',
87+
'foo/bar/baz/quux/fez/test/fixtures',
88+
'foo/bar/baz/quux/fez/test/fixtures/a.js',
89+
'foo/bar/baz/quux/fez/test/fixtures/j.js',
90+
'foo/bar/baz/quux/fez/test/fixtures/z.js',
91+
'foo/bar/baz/quux/fez/test/fixtures/c.js',
92+
'foo/bar/baz/quux/fez/test/fixtures/d.js',
93+
'foo/bar/baz/quux/fez/test/fixtures',
94+
'foo/bar/baz/quux/fez/test/fixtures/a.js',
95+
'foo/bar/baz/quux/fez/test/fixtures',
96+
'foo/bar/baz/quux/fez/test/fixtures/h.js',
97+
'foo/bar/baz/quux/fez/test/fixtures/i.js',
98+
'foo/bar/baz/quux/fez/test/fixtures/j.js',
99+
'foo/bar/baz/quux/fez/test/fixtures/k.js',
100+
'foo/bar/baz/quux/fez/test/fixtures/l.js',
101+
'foo/bar/baz/quux/fez/test/fixtures/m.js',
102+
'foo/bar/baz/quux/fez/test/fixtures',
103+
'foo/bar/baz/quux/fez/test/fixtures/a.js'
104+
];
105+
var before = [
106+
'foo/bar/baz/quux/fez/test/fixtures',
107+
'foo/bar/baz/quux/fez/test/fixtures',
108+
'foo/bar/baz/quux/fez/test/fixtures/a.js',
109+
'foo/bar/baz/quux/fez/test/fixtures',
110+
'foo/bar/baz/quux/fez/test/fixtures',
111+
'foo/bar/baz/quux/fez/test/fixtures/b.js',
112+
'foo/bar/baz/quux/fez/test/fixtures/b.js',
113+
'foo/bar/baz/quux/fez/test/fixtures',
114+
'foo/bar/baz/quux/fez/test/fixtures',
115+
'foo/bar/baz/quux/fez/test/fixtures/a.js',
116+
'foo/bar/baz/quux/fez/test/fixtures/j.js',
117+
'foo/bar/baz/quux/fez/test/fixtures/z.js',
118+
'foo/bar/baz/quux/fez/test/fixtures/c.js',
119+
'foo/bar/baz/quux/fez/test/fixtures/d.js',
120+
'foo/bar/baz/quux/fez/test/fixtures',
121+
'foo/bar/baz/quux/fez/test/fixtures/a.js',
122+
'foo/bar/baz/quux/fez/test/fixtures',
123+
'foo/bar/baz/quux/fez/test/fixtures/h.js',
124+
'foo/bar/baz/quux/fez/test/fixtures/i.js',
125+
'foo/bar/baz/quux/fez/test/fixtures/j.js',
126+
'foo/bar/baz/quux/fez/test/fixtures/k.js',
127+
'foo/bar/baz/quux/fez/test/fixtures/l.js',
128+
'foo/bar/baz/quux/fez/test/fixtures/m.js',
129+
'foo/bar/baz/quux/fez/test/fixtures',
130+
'foo/bar/baz/quux/fez/test/fixtures/a.js'
131+
];
132+
var expected = [
133+
'foo/bar/baz/quux/fez/test/fixtures',
134+
'foo/bar/baz/quux/fez/test/fixtures/a.js',
135+
'foo/bar/baz/quux/fez/test/fixtures/b.js',
136+
'foo/bar/baz/quux/fez/test/fixtures/j.js',
137+
'foo/bar/baz/quux/fez/test/fixtures/z.js',
138+
'foo/bar/baz/quux/fez/test/fixtures/c.js',
139+
'foo/bar/baz/quux/fez/test/fixtures/d.js',
140+
'foo/bar/baz/quux/fez/test/fixtures/h.js',
141+
'foo/bar/baz/quux/fez/test/fixtures/i.js',
142+
'foo/bar/baz/quux/fez/test/fixtures/k.js',
143+
'foo/bar/baz/quux/fez/test/fixtures/l.js',
144+
'foo/bar/baz/quux/fez/test/fixtures/m.js'
145+
];
146+
147+
unique.immutable(before).should.eql(expected);
148+
before.should.eql(original);
149+
})();
150+
});
151+
});

0 commit comments

Comments
 (0)