@wener/utils 1.1.13 → 1.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LICENSE.txt +1 -135
- package/dist/cjs/{globalThis-ee7c1669.js → getGlobalThis-304f74e0.js} +2 -2
- package/dist/cjs/getGlobalThis-304f74e0.js.map +1 -0
- package/dist/cjs/index.cjs +16 -5
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/server.cjs +1 -1
- package/dist/cjs/server.cjs.map +1 -1
- package/dist/esm/{globalThis-000611c3.js → getGlobalThis-b7ad0cf9.js} +2 -2
- package/dist/esm/getGlobalThis-b7ad0cf9.js.map +1 -0
- package/dist/esm/index.js +15 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/server.js +1 -1
- package/dist/esm/server.js.map +1 -1
- package/dist/system/getGlobalThis-8951eb0e.js +2 -0
- package/dist/system/getGlobalThis-8951eb0e.js.map +1 -0
- package/dist/system/index.js +16 -5
- package/dist/system/index.js.map +1 -1
- package/dist/system/server.js +1 -1
- package/dist/system/server.js.map +1 -1
- package/lib/asyncs/timeout.js.map +1 -1
- package/lib/browsers/download.js.map +1 -1
- package/lib/browsers/loaders.js.map +1 -1
- package/lib/crypto/getRandomValues.js +2 -1
- package/lib/crypto/getRandomValues.js.map +1 -1
- package/lib/crypto/randomUUID.js +2 -1
- package/lib/crypto/randomUUID.js.map +1 -1
- package/lib/fetch/createFetchWith.js +25 -0
- package/lib/fetch/createFetchWith.js.map +1 -0
- package/lib/fetch/createFetchWithLogging.js +21 -0
- package/lib/fetch/createFetchWithLogging.js.map +1 -0
- package/lib/fetch/dumpRequest.js +46 -0
- package/lib/fetch/dumpRequest.js.map +1 -0
- package/lib/fetch/dumpResponse.js +23 -0
- package/lib/fetch/dumpResponse.js.map +1 -0
- package/lib/index.js +4 -1
- package/lib/index.js.map +1 -1
- package/lib/io/ArrayBuffers.js +2 -2
- package/lib/io/ArrayBuffers.js.map +1 -1
- package/lib/io/isTransferable.js +2 -1
- package/lib/io/isTransferable.js.map +1 -1
- package/lib/isomorphics/structuredClone.js +2 -1
- package/lib/isomorphics/structuredClone.js.map +1 -1
- package/lib/logging/createLogger.js.map +1 -1
- package/lib/objects/get.js.map +1 -1
- package/lib/objects/set.js.map +1 -1
- package/lib/server.js +3 -1
- package/lib/server.js.map +1 -1
- package/lib/servers/createFetchWithProxy.js +12 -0
- package/lib/servers/createFetchWithProxy.js.map +1 -0
- package/lib/servers/{createProxyFetch.js → createFetchWithProxyByNodeFetch.js} +8 -4
- package/lib/servers/createFetchWithProxyByNodeFetch.js.map +1 -0
- package/lib/servers/createFetchWithProxyByUndici.js +28 -0
- package/lib/servers/createFetchWithProxyByUndici.js.map +1 -0
- package/lib/servers/polyfillCrypto.js +2 -1
- package/lib/servers/polyfillCrypto.js.map +1 -1
- package/lib/servers/polyfillJsDom.js +2 -1
- package/lib/servers/polyfillJsDom.js.map +1 -1
- package/package.json +39 -45
- package/src/asyncs/createLazyPromise.test.ts +21 -21
- package/src/asyncs/timeout.ts +3 -1
- package/src/browsers/download.ts +3 -1
- package/src/browsers/loaders.ts +6 -2
- package/src/crypto/getRandomValues.ts +2 -1
- package/src/crypto/hashing.test.ts +10 -12
- package/src/crypto/pem/__snapshots__/pem.test.ts.snap +18 -0
- package/src/crypto/pem/pem.test.ts +14 -17
- package/src/crypto/randomUUID.ts +3 -1
- package/src/crypto/ulid.test.ts +9 -9
- package/src/fetch/createFetchWith.ts +32 -0
- package/src/fetch/createFetchWithLogging.ts +20 -0
- package/src/fetch/dumpRequest.ts +51 -0
- package/src/fetch/dumpResponse.ts +28 -0
- package/src/fetch/index.ts +4 -0
- package/src/i18n/createTranslate.test.ts +76 -88
- package/src/index.ts +4 -2
- package/src/io/ArrayBuffer.test-d.ts +4 -2
- package/src/io/ArrayBuffers.base64.test.ts +17 -17
- package/src/io/ArrayBuffers.test.ts +8 -8
- package/src/io/ArrayBuffers.ts +2 -2
- package/src/io/Buffer.test.ts +4 -4
- package/src/io/isBuffer.test.ts +4 -4
- package/src/io/isTransferable.test.ts +7 -6
- package/src/io/isTransferable.ts +3 -1
- package/src/isomorphics/structuredClone.test.ts +4 -4
- package/src/isomorphics/structuredClone.ts +3 -1
- package/src/langs/deepEqual.test.ts +4 -4
- package/src/langs/langs.test.ts +3 -3
- package/src/libs/ms.test.ts +311 -0
- package/src/logging/createLogger.ts +15 -5
- package/src/logging/logger.test.ts +8 -8
- package/src/modules/parseModuleId.test.ts +3 -3
- package/src/objects/get.test-d.ts +30 -22
- package/src/objects/get.ts +1 -1
- package/src/objects/parseObjectPath.test.ts +3 -3
- package/src/objects/set.test.ts +98 -117
- package/src/objects/set.ts +1 -1
- package/src/server.ts +3 -1
- package/src/servers/createFetchWithProxy.ts +12 -0
- package/src/servers/{createProxyFetch.ts → createFetchWithProxyByNodeFetch.ts} +7 -3
- package/src/servers/createFetchWithProxyByUndici.ts +36 -0
- package/src/servers/polyfillBrowser.test.ts +9 -9
- package/src/servers/polyfillCrypto.ts +2 -1
- package/src/servers/polyfillJsDom.ts +3 -1
- package/src/servers/polyfillWebSocket.ts +2 -1
- package/src/strings/renderTemplate.test.ts +6 -9
- package/src/validations/parseTimestamp.test.ts +4 -4
- package/dist/cjs/globalThis-ee7c1669.js.map +0 -1
- package/dist/cjs/index-da9513d6.js +0 -13
- package/dist/cjs/index-da9513d6.js.map +0 -1
- package/dist/esm/globalThis-000611c3.js.map +0 -1
- package/dist/esm/index-c696799a.js +0 -13
- package/dist/esm/index-c696799a.js.map +0 -1
- package/dist/system/globalThis-7bba6592.js +0 -2
- package/dist/system/globalThis-7bba6592.js.map +0 -1
- package/dist/system/index-2dfef0f3.js +0 -13
- package/dist/system/index-2dfef0f3.js.map +0 -1
- package/lib/isomorphics/globalThis.js +0 -6
- package/lib/isomorphics/globalThis.js.map +0 -1
- package/lib/servers/createProxyFetch.js.map +0 -1
- package/src/crypto/pem/pem.test.ts.md +0 -24
- package/src/crypto/pem/pem.test.ts.snap +0 -0
- package/src/isomorphics/globalThis.ts +0 -3
- package/src/objects/get.test.ts +0 -63
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import ms from './ms';
|
|
3
|
+
|
|
4
|
+
describe('ms(string)', () => {
|
|
5
|
+
it('should not throw an error', () => {
|
|
6
|
+
expect(() => {
|
|
7
|
+
ms('1m');
|
|
8
|
+
}).not.toThrowError();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should preserve ms', () => {
|
|
12
|
+
expect(ms('100')).toBe(100);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should convert from m to ms', () => {
|
|
16
|
+
expect(ms('1m')).toBe(60000);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should convert from h to ms', () => {
|
|
20
|
+
expect(ms('1h')).toBe(3600000);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should convert d to ms', () => {
|
|
24
|
+
expect(ms('2d')).toBe(172800000);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should convert w to ms', () => {
|
|
28
|
+
expect(ms('3w')).toBe(1814400000);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should convert s to ms', () => {
|
|
32
|
+
expect(ms('1s')).toBe(1000);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should convert ms to ms', () => {
|
|
36
|
+
expect(ms('100ms')).toBe(100);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should convert y to ms', () => {
|
|
40
|
+
expect(ms('1y')).toBe(31557600000);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should work with decimals', () => {
|
|
44
|
+
expect(ms('1.5h')).toBe(5400000);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should work with multiple spaces', () => {
|
|
48
|
+
expect(ms('1 s')).toBe(1000);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should return NaN if invalid', () => {
|
|
52
|
+
// @ts-expect-error - We expect this to fail.
|
|
53
|
+
expect(isNaN(ms('☃'))).toBe(true);
|
|
54
|
+
// @ts-expect-error - We expect this to fail.
|
|
55
|
+
expect(isNaN(ms('10-.5'))).toBe(true);
|
|
56
|
+
// @ts-expect-error - We expect this to fail.
|
|
57
|
+
expect(isNaN(ms('ms'))).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should be case-insensitive', () => {
|
|
61
|
+
expect(ms('1.5H')).toBe(5400000);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should work with numbers starting with .', () => {
|
|
65
|
+
expect(ms('.5ms')).toBe(0.5);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should work with negative integers', () => {
|
|
69
|
+
expect(ms('-100ms')).toBe(-100);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should work with negative decimals', () => {
|
|
73
|
+
expect(ms('-1.5h')).toBe(-5400000);
|
|
74
|
+
expect(ms('-10.5h')).toBe(-37800000);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should work with negative decimals starting with "."', () => {
|
|
78
|
+
expect(ms('-.5h')).toBe(-1800000);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// long strings
|
|
83
|
+
|
|
84
|
+
describe('ms(long string)', () => {
|
|
85
|
+
it('should not throw an error', () => {
|
|
86
|
+
expect(() => {
|
|
87
|
+
ms('53 milliseconds');
|
|
88
|
+
}).not.toThrowError();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should convert milliseconds to ms', () => {
|
|
92
|
+
expect(ms('53 milliseconds')).toBe(53);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should convert msecs to ms', () => {
|
|
96
|
+
expect(ms('17 msecs')).toBe(17);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should convert sec to ms', () => {
|
|
100
|
+
expect(ms('1 sec')).toBe(1000);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should convert from min to ms', () => {
|
|
104
|
+
expect(ms('1 min')).toBe(60000);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should convert from hr to ms', () => {
|
|
108
|
+
expect(ms('1 hr')).toBe(3600000);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('should convert days to ms', () => {
|
|
112
|
+
expect(ms('2 days')).toBe(172800000);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should convert weeks to ms', () => {
|
|
116
|
+
expect(ms('1 week')).toBe(604800000);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should convert years to ms', () => {
|
|
120
|
+
expect(ms('1 year')).toBe(31557600000);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('should work with decimals', () => {
|
|
124
|
+
expect(ms('1.5 hours')).toBe(5400000);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should work with negative integers', () => {
|
|
128
|
+
expect(ms('-100 milliseconds')).toBe(-100);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should work with negative decimals', () => {
|
|
132
|
+
expect(ms('-1.5 hours')).toBe(-5400000);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should work with negative decimals starting with "."', () => {
|
|
136
|
+
expect(ms('-.5 hr')).toBe(-1800000);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// numbers
|
|
141
|
+
|
|
142
|
+
describe('ms(number, { long: true })', () => {
|
|
143
|
+
it('should not throw an error', () => {
|
|
144
|
+
expect(() => {
|
|
145
|
+
ms(500, { long: true });
|
|
146
|
+
}).not.toThrowError();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('should support milliseconds', () => {
|
|
150
|
+
expect(ms(500, { long: true })).toBe('500 ms');
|
|
151
|
+
|
|
152
|
+
expect(ms(-500, { long: true })).toBe('-500 ms');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should support seconds', () => {
|
|
156
|
+
expect(ms(1000, { long: true })).toBe('1 second');
|
|
157
|
+
expect(ms(1200, { long: true })).toBe('1 second');
|
|
158
|
+
expect(ms(10000, { long: true })).toBe('10 seconds');
|
|
159
|
+
|
|
160
|
+
expect(ms(-1000, { long: true })).toBe('-1 second');
|
|
161
|
+
expect(ms(-1200, { long: true })).toBe('-1 second');
|
|
162
|
+
expect(ms(-10000, { long: true })).toBe('-10 seconds');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('should support minutes', () => {
|
|
166
|
+
expect(ms(60 * 1000, { long: true })).toBe('1 minute');
|
|
167
|
+
expect(ms(60 * 1200, { long: true })).toBe('1 minute');
|
|
168
|
+
expect(ms(60 * 10000, { long: true })).toBe('10 minutes');
|
|
169
|
+
|
|
170
|
+
expect(ms(-1 * 60 * 1000, { long: true })).toBe('-1 minute');
|
|
171
|
+
expect(ms(-1 * 60 * 1200, { long: true })).toBe('-1 minute');
|
|
172
|
+
expect(ms(-1 * 60 * 10000, { long: true })).toBe('-10 minutes');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('should support hours', () => {
|
|
176
|
+
expect(ms(60 * 60 * 1000, { long: true })).toBe('1 hour');
|
|
177
|
+
expect(ms(60 * 60 * 1200, { long: true })).toBe('1 hour');
|
|
178
|
+
expect(ms(60 * 60 * 10000, { long: true })).toBe('10 hours');
|
|
179
|
+
|
|
180
|
+
expect(ms(-1 * 60 * 60 * 1000, { long: true })).toBe('-1 hour');
|
|
181
|
+
expect(ms(-1 * 60 * 60 * 1200, { long: true })).toBe('-1 hour');
|
|
182
|
+
expect(ms(-1 * 60 * 60 * 10000, { long: true })).toBe('-10 hours');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should support days', () => {
|
|
186
|
+
expect(ms(24 * 60 * 60 * 1000, { long: true })).toBe('1 day');
|
|
187
|
+
expect(ms(24 * 60 * 60 * 1200, { long: true })).toBe('1 day');
|
|
188
|
+
expect(ms(24 * 60 * 60 * 10000, { long: true })).toBe('10 days');
|
|
189
|
+
|
|
190
|
+
expect(ms(-1 * 24 * 60 * 60 * 1000, { long: true })).toBe('-1 day');
|
|
191
|
+
expect(ms(-1 * 24 * 60 * 60 * 1200, { long: true })).toBe('-1 day');
|
|
192
|
+
expect(ms(-1 * 24 * 60 * 60 * 10000, { long: true })).toBe('-10 days');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should round', () => {
|
|
196
|
+
expect(ms(234234234, { long: true })).toBe('3 days');
|
|
197
|
+
|
|
198
|
+
expect(ms(-234234234, { long: true })).toBe('-3 days');
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// numbers
|
|
203
|
+
|
|
204
|
+
describe('ms(number)', () => {
|
|
205
|
+
it('should not throw an error', () => {
|
|
206
|
+
expect(() => {
|
|
207
|
+
ms(500);
|
|
208
|
+
}).not.toThrowError();
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('should support milliseconds', () => {
|
|
212
|
+
expect(ms(500)).toBe('500ms');
|
|
213
|
+
|
|
214
|
+
expect(ms(-500)).toBe('-500ms');
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should support seconds', () => {
|
|
218
|
+
expect(ms(1000)).toBe('1s');
|
|
219
|
+
expect(ms(10000)).toBe('10s');
|
|
220
|
+
|
|
221
|
+
expect(ms(-1000)).toBe('-1s');
|
|
222
|
+
expect(ms(-10000)).toBe('-10s');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('should support minutes', () => {
|
|
226
|
+
expect(ms(60 * 1000)).toBe('1m');
|
|
227
|
+
expect(ms(60 * 10000)).toBe('10m');
|
|
228
|
+
|
|
229
|
+
expect(ms(-1 * 60 * 1000)).toBe('-1m');
|
|
230
|
+
expect(ms(-1 * 60 * 10000)).toBe('-10m');
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('should support hours', () => {
|
|
234
|
+
expect(ms(60 * 60 * 1000)).toBe('1h');
|
|
235
|
+
expect(ms(60 * 60 * 10000)).toBe('10h');
|
|
236
|
+
|
|
237
|
+
expect(ms(-1 * 60 * 60 * 1000)).toBe('-1h');
|
|
238
|
+
expect(ms(-1 * 60 * 60 * 10000)).toBe('-10h');
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('should support days', () => {
|
|
242
|
+
expect(ms(24 * 60 * 60 * 1000)).toBe('1d');
|
|
243
|
+
expect(ms(24 * 60 * 60 * 10000)).toBe('10d');
|
|
244
|
+
|
|
245
|
+
expect(ms(-1 * 24 * 60 * 60 * 1000)).toBe('-1d');
|
|
246
|
+
expect(ms(-1 * 24 * 60 * 60 * 10000)).toBe('-10d');
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('should round', () => {
|
|
250
|
+
expect(ms(234234234)).toBe('3d');
|
|
251
|
+
|
|
252
|
+
expect(ms(-234234234)).toBe('-3d');
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// invalid inputs
|
|
257
|
+
|
|
258
|
+
describe('ms(invalid inputs)', () => {
|
|
259
|
+
it('should throw an error, when ms("")', () => {
|
|
260
|
+
expect(() => {
|
|
261
|
+
// @ts-expect-error - We expect this to throw.
|
|
262
|
+
ms('');
|
|
263
|
+
}).toThrowError();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('should throw an error, when ms(undefined)', () => {
|
|
267
|
+
expect(() => {
|
|
268
|
+
// @ts-expect-error - We expect this to throw.
|
|
269
|
+
ms(undefined);
|
|
270
|
+
}).toThrowError();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('should throw an error, when ms(null)', () => {
|
|
274
|
+
expect(() => {
|
|
275
|
+
// @ts-expect-error - We expect this to throw.
|
|
276
|
+
ms(null);
|
|
277
|
+
}).toThrowError();
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('should throw an error, when ms([])', () => {
|
|
281
|
+
expect(() => {
|
|
282
|
+
// @ts-expect-error - We expect this to throw.
|
|
283
|
+
ms([]);
|
|
284
|
+
}).toThrowError();
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('should throw an error, when ms({})', () => {
|
|
288
|
+
expect(() => {
|
|
289
|
+
// @ts-expect-error - We expect this to throw.
|
|
290
|
+
ms({});
|
|
291
|
+
}).toThrowError();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should throw an error, when ms(NaN)', () => {
|
|
295
|
+
expect(() => {
|
|
296
|
+
ms(NaN);
|
|
297
|
+
}).toThrowError();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('should throw an error, when ms(Infinity)', () => {
|
|
301
|
+
expect(() => {
|
|
302
|
+
ms(Infinity);
|
|
303
|
+
}).toThrowError();
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('should throw an error, when ms(-Infinity)', () => {
|
|
307
|
+
expect(() => {
|
|
308
|
+
ms(-Infinity);
|
|
309
|
+
}).toThrowError();
|
|
310
|
+
});
|
|
311
|
+
});
|
|
@@ -12,11 +12,21 @@ export function createLogger(
|
|
|
12
12
|
context: object = {},
|
|
13
13
|
): LoggerWithChild {
|
|
14
14
|
return {
|
|
15
|
-
trace: (...values) => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
trace: (...values) => {
|
|
16
|
+
write({ ...context, level: 'trace', values });
|
|
17
|
+
},
|
|
18
|
+
debug: (...values) => {
|
|
19
|
+
write({ ...context, level: 'debug', values });
|
|
20
|
+
},
|
|
21
|
+
info: (...values) => {
|
|
22
|
+
write({ ...context, level: 'info', values });
|
|
23
|
+
},
|
|
24
|
+
warn: (...values) => {
|
|
25
|
+
write({ ...context, level: 'warn', values });
|
|
26
|
+
},
|
|
27
|
+
error: (...values) => {
|
|
28
|
+
write({ ...context, level: 'error', values });
|
|
29
|
+
},
|
|
20
30
|
child: (ctx) => createLogger(write, { ...context, ...ctx }),
|
|
21
31
|
};
|
|
22
32
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { test, expect } from 'vitest';
|
|
2
2
|
import { createChildLogger } from './createChildLogger';
|
|
3
3
|
import { createLogger } from './createLogger';
|
|
4
4
|
|
|
5
|
-
test('logger', (
|
|
5
|
+
test('logger', () => {
|
|
6
6
|
{
|
|
7
7
|
const logs: any[] = [];
|
|
8
8
|
const base = createLogger((o) => logs.push(o));
|
|
9
9
|
const l = createChildLogger(base, { c: 'test' });
|
|
10
10
|
l.info('hello');
|
|
11
|
-
|
|
11
|
+
expect(logs.shift()).toEqual({ level: 'info', values: ['hello'], c: 'test' });
|
|
12
12
|
l.child({ m: 1 }).trace('trace');
|
|
13
|
-
|
|
13
|
+
expect(logs.shift()).toEqual({ level: 'trace', values: ['trace'], c: 'test', m: 1 });
|
|
14
14
|
}
|
|
15
15
|
createChildLogger(console, { c: 'test' }).info('hello');
|
|
16
16
|
{
|
|
@@ -18,18 +18,18 @@ test('logger', (t) => {
|
|
|
18
18
|
const l = createLogger(
|
|
19
19
|
(o) => {
|
|
20
20
|
pass++;
|
|
21
|
-
|
|
21
|
+
console.log(`${o.level}: [${[o.m, o.c].filter(Boolean).join('.') || 'default'}]`, ...o.values);
|
|
22
22
|
},
|
|
23
23
|
{
|
|
24
24
|
m: 'Root',
|
|
25
25
|
},
|
|
26
26
|
);
|
|
27
27
|
l.info('nice');
|
|
28
|
-
|
|
28
|
+
expect(pass).toBe(1);
|
|
29
29
|
l.child({}).info('nice 2');
|
|
30
|
-
|
|
30
|
+
expect(pass).toBe(2);
|
|
31
31
|
createChildLogger(l, { m: 'Child' }).info('nice 3');
|
|
32
|
-
|
|
32
|
+
expect(pass).toBe(3);
|
|
33
33
|
|
|
34
34
|
createLogger().child({ name: 'wener' }).info({ x: 1 }, 'Nice');
|
|
35
35
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { test, expect } from 'vitest';
|
|
2
2
|
import { parseModuleId } from './parseModuleId';
|
|
3
3
|
|
|
4
|
-
test('parseModuleId', (
|
|
4
|
+
test('parseModuleId', () => {
|
|
5
5
|
const tests = {
|
|
6
6
|
'@wener/reaction': {
|
|
7
7
|
id: `@wener/reaction@latest`,
|
|
@@ -68,6 +68,6 @@ test('parseModuleId', (t) => {
|
|
|
68
68
|
},
|
|
69
69
|
};
|
|
70
70
|
for (const [k, v] of Object.entries(tests)) {
|
|
71
|
-
|
|
71
|
+
expect(parseModuleId(k)).toStrictEqual(v);
|
|
72
72
|
}
|
|
73
73
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { expectTypeOf, test } from 'vitest';
|
|
2
2
|
import { get } from './get';
|
|
3
3
|
|
|
4
4
|
interface TestClass {
|
|
@@ -27,25 +27,33 @@ interface TestClass {
|
|
|
27
27
|
};
|
|
28
28
|
}[];
|
|
29
29
|
}
|
|
30
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
31
|
-
const obj = {} as TestClass;
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
31
|
+
test('get typing', () => {
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
33
|
+
const obj = {} as TestClass;
|
|
34
|
+
expectTypeOf(get(obj, 'nested.a', null)).toMatchTypeOf<number>();
|
|
35
|
+
expectTypeOf(get(obj, 'normal', null)).toMatchTypeOf<string>();
|
|
36
|
+
expectTypeOf(get(obj, 'nested.a')).toMatchTypeOf<number>();
|
|
37
|
+
expectTypeOf(get(obj, 'nested.b.c')).toMatchTypeOf<boolean>();
|
|
38
|
+
expectTypeOf(get(obj, 'arr')).toMatchTypeOf<number[]>();
|
|
39
|
+
expectTypeOf(get(obj, 'arr[13]')).toMatchTypeOf<number>();
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
expectTypeOf(get(obj, 'arr.13')).toMatchTypeOf<number>();
|
|
42
|
+
expectTypeOf(get(obj, 'nestedArr[3].other')).toMatchTypeOf<null>();
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
expectTypeOf(get(obj, 'deep.deep')).toMatchTypeOf<string[]>();
|
|
45
|
+
expectTypeOf(get(obj, 'deep.arr[333]')).toMatchTypeOf<string>();
|
|
46
|
+
expectTypeOf(get(obj, 'deep.arr[333].length')).toMatchTypeOf<number>();
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
expectTypeOf(get(obj, 'nested["b"]["c"]')).toMatchTypeOf<boolean>();
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
expectTypeOf(get(obj, '')).toMatchTypeOf<never>();
|
|
51
|
+
expectTypeOf(get(obj, '', 3)).toMatchTypeOf<number>();
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
expectTypeOf(get(obj, 'nested.asdfasdf')).toMatchTypeOf<never>();
|
|
54
|
+
expectTypeOf(get(obj, 'deeplvl1[1].deeplvl2.deeplvl3[88].deeplvl4.value')).toMatchTypeOf<RegExp>();
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
expectTypeOf(get(obj, 'deeplvl1[1].deeplvl2.deeplvl1[88].deeplvl4.value')).toMatchTypeOf<never>();
|
|
57
|
+
expectTypeOf(get(obj, 'deeplvl1[1].deeplvl2.deeplvl1[88].deeplvl4.value', obj)).toMatchTypeOf<TestClass>();
|
|
58
|
+
expectTypeOf(get(obj, 'nested["dd"]', '')).toMatchTypeOf<string>();
|
|
59
|
+
});
|
package/src/objects/get.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { test, expect } from 'vitest';
|
|
2
2
|
import { parseObjectPath } from './parseObjectPath';
|
|
3
3
|
|
|
4
|
-
test('parseObjectPath', (
|
|
4
|
+
test('parseObjectPath', () => {
|
|
5
5
|
for (const [k, v] of [
|
|
6
6
|
['a.b.c', ['a', 'b', 'c']],
|
|
7
7
|
['a[1].c', ['a', '1', 'c']],
|
|
@@ -13,6 +13,6 @@ test('parseObjectPath', (t) => {
|
|
|
13
13
|
[Symbol.toStringTag, [Symbol.toStringTag]],
|
|
14
14
|
[[Symbol.toStringTag], [Symbol.toStringTag]],
|
|
15
15
|
]) {
|
|
16
|
-
|
|
16
|
+
expect(parseObjectPath(k)).toEqual(v);
|
|
17
17
|
}
|
|
18
18
|
});
|