-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
248 lines (226 loc) · 11 KB
/
test.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
'use strict';
require('mocha');
const assert = require('assert');
const pretty = require('.');
const { nano, regex } = require('./utils');
describe('pretty', function() {
it('should throw an error when invalid args are passed:', function() {
assert.throws(() => pretty(), /expected an array or number in nanoseconds/);
});
it('should throw an error when invalid arrays are passed:', function() {
assert.throws(() => pretty([0,1,2]), /expected an array from process\.hrtime\(\)/);
});
it('should support hrtime:', function() {
const start = process.hrtime();
const time = process.hrtime(start);
assert(typeof pretty(time) === 'string');
});
it('should support nanoseconds:', function() {
const start = process.hrtime();
const time = process.hrtime(start);
assert(typeof pretty(nano(time)) === 'string');
});
it('should support time increment as second arg:', function() {
const time = [1200708, 795428088];
assert.equal(pretty(time, 'w'), '2w');
assert.equal(pretty(time, 'd'), '1w 6d');
assert.equal(pretty(time, 'h'), '1w 6d 22h');
assert.equal(pretty(time, 'm'), '1w 6d 21h 32m');
assert.equal(pretty(time, 's'), '1w 6d 21h 31m 49s');
assert.equal(pretty(time, 'ms'), '1w 6d 21h 31m 48s 795ms');
assert.equal(pretty(time, 'μs'), '1w 6d 21h 31m 48s 795ms 428μs');
assert.equal(pretty(time, 'ns'), '1w 6d 21h 31m 48s 795ms 428μs 88ns');
assert.equal(pretty([0, 795428088], 'ms'), '795ms');
assert.equal(pretty([0, 428088], 'ms'), '0ms');
assert.equal(pretty([0, 428088], 'ns'), '428μs 88ns');
assert.equal(pretty([0, 528088], 'ms'), '1ms');
assert.equal(pretty([0, 428088], 'ms', 1), '0.4ms');
assert.equal(pretty([0, 628088], 'ms', 1), '0.6ms');
assert.equal(pretty([0, 428088], 'ms', 2), '0.43ms');
assert.equal(pretty([0, 795946488], 'ms', 2), '795.95ms');
assert.equal(pretty([0, 795946488], 'μs'), '795ms 946μs');
assert.equal(pretty([0, 795428088], 'μs'), '795ms 428μs');
assert.equal(pretty(nano(time), 'w'), '2w');
assert.equal(pretty(nano(time), 'd'), '1w 6d');
assert.equal(pretty(nano(time), 'h'), '1w 6d 22h');
assert.equal(pretty(nano(time), 'm'), '1w 6d 21h 32m');
assert.equal(pretty(nano(time), 's'), '1w 6d 21h 31m 49s');
assert.equal(pretty(nano(time), 'ms'), '1w 6d 21h 31m 48s 795ms');
assert.equal(pretty(nano(time), 'μs'), '1w 6d 21h 31m 48s 795ms 428μs');
assert.equal(pretty(nano(time), 'ns'), '1w 6d 21h 31m 48s 795ms 428μs 88ns');
assert.equal(pretty(nano([0, 795428088]), 'ms'), '795ms');
assert.equal(pretty(nano([0, 428088]), 'ms'), '0ms');
assert.equal(pretty(nano([0, 428088]), 'ns'), '428μs 88ns');
assert.equal(pretty(nano([0, 528088]), 'ms'), '1ms');
assert.equal(pretty(nano([0, 428088]), 'ms', 1), '0.4ms');
assert.equal(pretty(nano([0, 628088]), 'ms', 1), '0.6ms');
assert.equal(pretty(nano([0, 428088]), 'ms', 2), '0.43ms');
assert.equal(pretty(nano([0, 795946488]), 'ms', 2), '795.95ms');
assert.equal(pretty(nano([0, 795946488]), 'μs'), '795ms 946μs');
assert.equal(pretty(nano([0, 795428088]), 'μs'), '795ms 428μs');
});
it('should round to the closest increment', function() {
assert.equal(pretty([1200708, 795428088]), '2w');
assert.equal(pretty([800708, 795428088]), '1w');
assert.equal(pretty([400708, 795428088]), '5d');
assert.equal(pretty([70708, 795428088]), '20h');
assert.equal(pretty([12708, 795428088]), '4h');
assert.equal(pretty([3708, 795428088]), '1h');
assert.equal(pretty([208, 795428088]), '3m');
assert.equal(pretty([20, 795428088]), '21s');
assert.equal(pretty([0, 795428088]), '795ms');
assert.equal(pretty([0, 428088]), '428μs');
assert.equal(pretty([0, 88]), '88ns');
assert.equal(pretty([0, 18]), '18ns');
assert.equal(pretty(nano([1200708, 795428088])), '2w');
assert.equal(pretty(nano([800708, 795428088])), '1w');
assert.equal(pretty(nano([400708, 795428088])), '5d');
assert.equal(pretty(nano([70708, 795428088])), '20h');
assert.equal(pretty(nano([12708, 795428088])), '4h');
assert.equal(pretty(nano([3708, 795428088])), '1h');
assert.equal(pretty(nano([208, 795428088])), '3m');
assert.equal(pretty(nano([20, 795428088])), '21s');
assert.equal(pretty(nano([0, 795428088])), '795ms');
assert.equal(pretty(nano([0, 428088])), '428μs');
assert.equal(pretty(nano([0, 88])), '88ns');
assert.equal(pretty(nano([0, 18])), '18ns');
});
it('should work when numbers are strings', function() {
assert.equal(pretty(['1200708', '795428088']), '2w');
assert.equal(pretty(['800708', '795428088']), '1w');
assert.equal(pretty(['400708', '795428088']), '5d');
assert.equal(pretty(['70708', '795428088']), '20h');
assert.equal(pretty(['3708', '795428088']), '1h');
assert.equal(pretty(['208', '795428088']), '3m');
assert.equal(pretty(['20', '795428088']), '21s');
assert.equal(pretty(['0', '795428088']), '795ms');
assert.equal(pretty(['0', '000428088']), '428μs');
assert.equal(pretty(['0', '000000088']), '88ns');
assert.equal(pretty(['0', '000000018']), '18ns');
assert.equal(pretty(['0', '000000001']), '1ns');
assert.equal(pretty(''+nano(['1200708', '795428088'])), '2w');
assert.equal(pretty(''+nano(['800708', '795428088'])), '1w');
assert.equal(pretty(''+nano(['400708', '795428088'])), '5d');
assert.equal(pretty(''+nano(['70708', '795428088'])), '20h');
assert.equal(pretty(''+nano(['3708', '795428088'])), '1h');
assert.equal(pretty(''+nano(['208', '795428088'])), '3m');
assert.equal(pretty(''+nano(['20', '795428088'])), '21s');
assert.equal(pretty(''+nano(['0', '795428088'])), '795ms');
assert.equal(pretty(''+nano(['0', '000428088'])), '428μs');
assert.equal(pretty(''+nano(['0', '000000088'])), '88ns');
assert.equal(pretty(''+nano(['0', '000000018'])), '18ns');
assert.equal(pretty(''+nano(['0', '000000001'])), '1ns');
});
it('should round the given number of digits', function() {
assert.equal(pretty([1200708, 795428088], 2), '1.99w');
assert.equal(pretty([800708, 795428088], 2), '1.32w');
assert.equal(pretty([400708, 795428088], 2), '4.64d');
assert.equal(pretty([70708, 795428088], 2), '19.64h');
assert.equal(pretty([3708, 795428088], 2), '1.03h');
assert.equal(pretty([208, 795428088], 2), '3.48m');
assert.equal(pretty([20, 795428088], 2), '20.80s');
assert.equal(pretty([0, 795428088], 2), '795.43ms');
assert.equal(pretty([0, 428088], 2), '428.09μs');
assert.equal(pretty(nano([1200708, 795428088]), 2), '1.99w');
assert.equal(pretty(nano([800708, 795428088]), 2), '1.32w');
assert.equal(pretty(nano([400708, 795428088]), 2), '4.64d');
assert.equal(pretty(nano([70708, 795428088]), 2), '19.64h');
assert.equal(pretty(nano([3708, 795428088]), 2), '1.03h');
assert.equal(pretty(nano([208, 795428088]), 2), '3.48m');
assert.equal(pretty(nano([20, 795428088]), 2), '20.80s');
assert.equal(pretty(nano([0, 795428088]), 2), '795.43ms');
assert.equal(pretty(nano([0, 428088]), 2), '428.09μs');
});
it('should support rounding as the third arg:', function() {
const time = [1281708, 795428088];
assert.equal(pretty(time, 'w'), '2w');
assert.equal(pretty(time, 'w', 1), '2.1w');
assert.equal(pretty(time, 'w', 2), '2.12w');
assert.equal(pretty(time, 'd'), '2w 1d');
assert.equal(pretty(time, 'd', 1), '2w 0.8d');
assert.equal(pretty(time, 'd', 2), '2w 0.83d');
assert.equal(pretty(time, 'h'), '2w 20h');
assert.equal(pretty(time, 'h', 1), '2w 20.0h');
assert.equal(pretty(time, 'h', 2), '2w 20.03h');
assert.equal(pretty(time, 'm'), '2w 20h 2m');
assert.equal(pretty(time, 's'), '2w 20h 1m 49s');
assert.equal(pretty(time, 's', 1), '2w 20h 1m 48.8s');
assert.equal(pretty(time, 's', 2), '2w 20h 1m 48.80s');
assert.equal(pretty(time, 'ms'), '2w 20h 1m 48s 795ms');
assert.equal(pretty(time, 'ms', 2), '2w 20h 1m 48s 795.43ms');
assert.equal(pretty(time, 'μs'), '2w 20h 1m 48s 795ms 428μs');
assert.equal(pretty(time, 'μs', 1), '2w 20h 1m 48s 795ms 428.1μs');
assert.equal(pretty(time, 'μs', 2), '2w 20h 1m 48s 795ms 428.09μs');
assert.equal(pretty(time, 'ns'), '2w 20h 1m 48s 795ms 428μs 88ns');
assert.equal(pretty(time, 'ns', 2), '2w 20h 1m 48s 795ms 428μs 88.00ns');
assert.equal(pretty(nano(time), 'w'), '2w');
assert.equal(pretty(nano(time), 'w', 1), '2.1w');
assert.equal(pretty(nano(time), 'w', 2), '2.12w');
assert.equal(pretty(nano(time), 'd'), '2w 1d');
assert.equal(pretty(nano(time), 'd', 1), '2w 0.8d');
assert.equal(pretty(nano(time), 'd', 2), '2w 0.83d');
assert.equal(pretty(nano(time), 'h'), '2w 20h');
assert.equal(pretty(nano(time), 'h', 1), '2w 20.0h');
assert.equal(pretty(nano(time), 'h', 2), '2w 20.03h');
assert.equal(pretty(nano(time), 'm'), '2w 20h 2m');
assert.equal(pretty(nano(time), 's'), '2w 20h 1m 49s');
assert.equal(pretty(nano(time), 's', 1), '2w 20h 1m 48.8s');
assert.equal(pretty(nano(time), 's', 2), '2w 20h 1m 48.80s');
assert.equal(pretty(nano(time), 'ms'), '2w 20h 1m 48s 795ms');
assert.equal(pretty(nano(time), 'ms', 2), '2w 20h 1m 48s 795.43ms');
assert.equal(pretty(nano(time), 'μs'), '2w 20h 1m 48s 795ms 428μs');
assert.equal(pretty(nano(time), 'μs', 1), '2w 20h 1m 48s 795ms 428.1μs');
assert.equal(pretty(nano(time), 'μs', 2), '2w 20h 1m 48s 795ms 428.09μs');
assert.equal(pretty(nano(time), 'ns'), '2w 20h 1m 48s 795ms 428μs 88ns');
assert.equal(pretty(nano(time), 'ns', 2), '2w 20h 1m 48s 795ms 428μs 88.00ns');
});
});
describe('regex', function() {
it('should match nanoseconds:', function() {
assert(regex.ns.test('nanoseconds'));
assert(regex.ns.test('nanosecond'));
assert(regex.ns.test('nano'));
assert(regex.ns.test('ns'));
assert(regex.ns.test('n'));
});
it('should match microseconds:', function() {
assert(regex.μs.test('microseconds'));
assert(regex.μs.test('microsecond'));
assert(regex.μs.test('micro'));
assert(regex.μs.test('μs'));
});
it('should match milliseconds:', function() {
assert(regex.ms.test('milliseconds'));
assert(regex.ms.test('millisecond'));
assert(regex.ms.test('milli'));
assert(regex.ms.test('ms'));
assert(!regex.ms.test('mil'));
assert(!regex.ms.test('ml'));
});
it('should match seconds:', function() {
assert(regex.s.test('seconds'));
assert(regex.s.test('second'));
assert(regex.s.test('sec'));
assert(regex.s.test('s'));
assert(!regex.s.test('ns'));
assert(!regex.s.test('ss'));
assert(!regex.s.test('ms'));
});
it('should match hours:', function() {
assert(regex.h.test('hours'));
assert(regex.h.test('hour'));
assert(regex.h.test('hr'));
assert(regex.h.test('h'));
});
it('should match days:', function() {
assert(regex.d.test('days'));
assert(regex.d.test('day'));
assert(regex.d.test('d'));
});
it('should match weeks:', function() {
assert(regex.w.test('weeks'));
assert(regex.w.test('week'));
assert(regex.w.test('wk'));
assert(regex.w.test('w'));
});
});