forked from hexojs/warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
176 lines (138 loc) · 3.97 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import chai from 'chai';
const should = chai.should(); // eslint-disable-line
import * as util from '../../dist/util';
describe('util', () => {
it('shuffle()', () => {
const src = Array(100).fill(0).map((_, i) => i);
const result = util.shuffle(src);
result.should.not.eql(src);
result.should.to.have.members(src);
result.should.to.have.length(src.length);
src.should.eql(Array(100).fill(0).map((_, i) => i));
});
it('shuffle() - array must be an array', () => {
(() => util.shuffle({})).should.to.throw('array must be an Array!');
});
it('getProp()', () => {
const obj = {
a: {
b: 1
},
c: 2,
d: {
e: {
f: 'foo'
}
}
};
util.getProp(obj, 'a.b').should.eql(obj.a.b);
util.getProp(obj, 'c').should.eql(obj.c);
util.getProp(obj, 'd.e.f').should.eql(obj.d.e.f);
});
it('getProp() - obj must be an object', () => {
(() => util.getProp('', null)).should.to.throw('obj must be an object!');
});
it('getProp() - key is required', () => {
(() => util.getProp({}, null)).should.to.throw('key is required!');
});
it('setProp()', () => {
const obj = {
a: {
b: 1
},
c: 2,
d: {
e: {
f: 'foo'
}
}
};
util.setProp(obj, 'a.b', 10);
obj.a.b.should.eql(10);
util.setProp(obj, 'c', 20);
obj.c.should.eql(20);
util.setProp(obj, 'd.e.f', 'haha');
obj.d.e.f.should.eql('haha');
});
it('setProp() - obj must be an object', () => {
(() => util.setProp('', null, null)).should.to.throw('obj must be an object!');
});
it('setProp() - key is required', () => {
(() => util.setProp({}, null, null)).should.to.throw('key is required!');
});
it('delProp()', () => {
const obj = {
a: {
b: 1
},
c: 2,
d: {
e: {
f: 'foo'
}
}
};
util.delProp(obj, 'a.b');
should.not.exist(obj.a.b);
util.delProp(obj, 'c');
should.not.exist(obj.c);
util.delProp(obj, 'd.e.f');
should.not.exist(obj.d.e.f);
});
it('delProp() - obj must be an object', () => {
(() => util.delProp('', null)).should.to.throw('obj must be an object!');
});
it('delProp() - key is required', () => {
(() => util.delProp({}, null)).should.to.throw('key is required!');
});
it('setGetter()', () => {
const obj = {
a: {
b: 1
},
c: 2,
d: {
e: {
f: 'foo'
}
}
};
util.setGetter(obj, 'a.b', () => 100);
obj.a.b.should.eql(100);
util.setGetter(obj, 'c', () => 200);
obj.c.should.eql(200);
util.setGetter(obj, 'd.e.f', () => 'haha');
obj.d.e.f.should.eql('haha');
util.setGetter(obj, 'a.c.h', () => 'ach');
// @ts-ignore
obj.a.c.h.should.eql('ach');
});
it('setGetter() - obj must be an object', () => {
(() => util.setGetter('', null, null)).should.to.throw('obj must be an object!');
});
it('setGetter() - key is required', () => {
(() => util.setGetter({}, null, null)).should.to.throw('key is required!');
});
it('setGetter() - fn must be a function', () => {
(() => util.setGetter({}, 'test', {})).should.to.throw('fn must be a function!');
});
it('arr2obj()', () => {
util.arr2obj(['a', 'b'], 1).should.eql({a: 1, b: 1});
});
it('arr2obj() - arr must be an array', () => {
(() => util.arr2obj({}, null)).should.to.throw('arr must be an array!');
});
it('reverse()', () => {
const arr = [1, '2', 'w'];
util.reverse(arr).should.eql(['w', '2', 1]);
});
it('reverse() - arr must be an array', () => {
(() => util.reverse({})).should.to.throw('arr must be an array!');
});
it('parseArgs()', () => {
util.parseArgs('name').should.eql({name: 1});
util.parseArgs('name', -1).should.eql({name: -1});
util.parseArgs('name -date').should.eql({name: 1, date: -1});
util.parseArgs('name -date +priority').should.eql({name: 1, date: -1, priority: 1});
});
});