Skip to content

Commit 93bc3d5

Browse files
array util added
1 parent ed16b6e commit 93bc3d5

File tree

2 files changed

+19
-88
lines changed

2 files changed

+19
-88
lines changed

lib/utils/array.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
export function capitalize(value: string): string {
1+
/**
2+
* Removes duplicates from specified array
3+
*/
4+
export function unique<T>(arr: T[]): T[] {
25

3-
return value.charAt(0).toUpperCase() + value.substr(1, value.length);
6+
return arr.filter(uniqueFilter);
47
}
8+
9+
/**
10+
* Returns true for items, that only exists once on an array
11+
*/
12+
export const uniqueFilter = (item, index, arr) => arr.indexOf(item) === index;

test/specs/utils/array.spec.ts

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,26 @@
11
import {expect} from 'chai';
2-
import {deepAssign} from '../../../lib/utils/object';
2+
import {unique} from '../../../lib/utils/array';
33

44
/* tslint:disable:max-classes-per-file */
55

66
describe('utils', () => {
77

8-
describe('object', () => {
8+
describe('array', () => {
99

10-
describe('deepAssign', () => {
10+
describe('unique', () => {
1111

12-
const childSourceF = {};
13-
const childSourceA = {f: childSourceF};
14-
const childSourceB = {};
15-
const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim};
16-
const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden'};
17-
const sourceKeys = [].concat(Object.keys(source1), Object.keys(source2));
12+
const duplicates = [1, 'a', 'b', 1, 'a', 'c', 2, 'd', 'b', 2, 3, 'd', 'b'];
1813

19-
it('should not be undefined', () => {
14+
it('should not throw', () => {
2015

21-
const copy = deepAssign({}, source1, source2);
22-
23-
expect(copy).not.to.be.undefined;
24-
});
25-
26-
it('should have all keys of sources', () => {
27-
28-
const copy = deepAssign({}, source1, source2);
29-
30-
sourceKeys
31-
.forEach(key => expect(copy).to.have.property(key))
32-
;
33-
});
34-
35-
it('should override previous properties', () => {
36-
37-
const copy = deepAssign({}, source1, source2);
38-
39-
expect(copy).to.have.property('over', 'ridden');
40-
});
41-
42-
it('should have all primitive & function values of sources', () => {
43-
44-
const copy = deepAssign({}, source1, source2);
45-
46-
sourceKeys
47-
.forEach(key => {
48-
49-
if (typeof copy[key] !== 'object') {
50-
51-
expect(copy[key]).to.equal(source2[key] || source1[key]);
52-
}
53-
})
54-
;
16+
expect(() => unique(duplicates)).not.to.throw();
5517
});
5618

57-
it('should have copies of all non-primitive values of sources', () => {
58-
59-
const copy = deepAssign({}, source1, source2);
60-
61-
sourceKeys
62-
.forEach(key => {
63-
64-
if (typeof copy[key] === 'object') {
65-
66-
expect(copy[key]).not.to.equal(source1[key] || source2[key]);
67-
expect(copy[key]).to.eql(source1[key] || source2[key]);
68-
}
69-
})
70-
;
71-
});
72-
73-
it('should have copies of child source', () => {
74-
75-
const copy = deepAssign({}, source1, source2);
76-
77-
expect(copy.a).to.have.property('f');
78-
expect(copy.a.f).to.not.equal(source1.a.f);
79-
expect(copy.a.f).to.eql(source1.a.f);
80-
});
81-
82-
it('should have copy of array items', () => {
83-
84-
const copy = deepAssign({}, source1, source2);
85-
86-
expect(copy.arr).to.be.an('array');
87-
88-
copy.arr.forEach((value, index) => {
89-
90-
const isObject = typeof value === 'object';
91-
92-
if (isObject) {
93-
expect(value).not.to.equal(source2.arr[index]);
94-
expect(value).to.eql(source2.arr[index]);
19+
it('should remove duplicates from array', () => {
9520

96-
} else {
21+
const unified = unique(duplicates);
9722

98-
expect(value).to.equal(source2.arr[index]);
99-
}
100-
});
23+
expect(unified).to.have.property('length', 7);
10124
});
10225

10326
});

0 commit comments

Comments
 (0)