Skip to content

Commit 31d0b80

Browse files
string util comments & tests
1 parent d1f4ff5 commit 31d0b80

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

lib/utils/array.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function capitalize(value: string): string {
2+
3+
return value.charAt(0).toUpperCase() + value.substr(1, value.length);
4+
}

lib/utils/string.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Capitalize specified string value
3+
*/
14
export function capitalize(value: string): string {
25

36
return value.charAt(0).toUpperCase() + value.substr(1, value.length);

test/specs/utils/array.spec.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import {expect} from 'chai';
2+
import {deepAssign} from '../../../lib/utils/object';
3+
4+
/* tslint:disable:max-classes-per-file */
5+
6+
describe('utils', () => {
7+
8+
describe('object', () => {
9+
10+
describe('deepAssign', () => {
11+
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));
18+
19+
it('should not be undefined', () => {
20+
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+
;
55+
});
56+
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]);
95+
96+
} else {
97+
98+
expect(value).to.equal(source2.arr[index]);
99+
}
100+
});
101+
});
102+
103+
});
104+
105+
});
106+
});

test/specs/utils/string.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {expect} from 'chai';
2+
import {capitalize} from '../../../lib/utils/string';
3+
4+
/* tslint:disable:max-classes-per-file */
5+
6+
describe('utils', () => {
7+
8+
describe('string', () => {
9+
10+
describe('capitalize', () => {
11+
12+
it('should not throw', () => {
13+
14+
const value = 'abc';
15+
16+
expect(() => capitalize(value)).not.to.throw();
17+
});
18+
19+
it('should capitalize specified value', () => {
20+
21+
const value = 'abc';
22+
23+
expect(capitalize(value)).to.equal('Abc');
24+
});
25+
26+
});
27+
28+
});
29+
});

0 commit comments

Comments
 (0)