@wener/utils 1.1.13 → 1.1.14
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/index.cjs +1 -1
- 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/index.js +2 -2
- 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/index.js +2 -2
- 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/server.js +3 -1
- package/lib/server.js.map +1 -1
- package/lib/servers/createFetchWithProxy.js +13 -0
- package/lib/servers/createFetchWithProxy.js.map +1 -0
- package/lib/servers/{createProxyFetch.js → createFetchWithProxyByNodeFetch.js} +6 -3
- package/lib/servers/createFetchWithProxyByNodeFetch.js.map +1 -0
- package/lib/servers/createFetchWithProxyByUndici.js +28 -0
- package/lib/servers/createFetchWithProxyByUndici.js.map +1 -0
- package/package.json +20 -28
- package/src/asyncs/createLazyPromise.test.ts +21 -21
- 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/ulid.test.ts +9 -9
- package/src/i18n/createTranslate.test.ts +76 -88
- 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/Buffer.test.ts +4 -4
- package/src/io/isBuffer.test.ts +4 -4
- package/src/io/isTransferable.test.ts +7 -6
- package/src/isomorphics/structuredClone.test.ts +4 -4
- 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/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/parseObjectPath.test.ts +3 -3
- package/src/objects/set.test.ts +98 -117
- package/src/server.ts +3 -1
- package/src/servers/createFetchWithLogger.ts +75 -0
- package/src/servers/createFetchWithProxy.ts +12 -0
- package/src/servers/{createProxyFetch.ts → createFetchWithProxyByNodeFetch.ts} +4 -1
- package/src/servers/createFetchWithProxyByUndici.ts +36 -0
- package/src/servers/polyfillBrowser.test.ts +9 -9
- package/src/strings/renderTemplate.test.ts +6 -9
- package/src/validations/parseTimestamp.test.ts +4 -4
- package/dist/cjs/index-da9513d6.js +0 -13
- package/dist/cjs/index-da9513d6.js.map +0 -1
- package/dist/esm/index-c696799a.js +0 -13
- package/dist/esm/index-c696799a.js.map +0 -1
- package/dist/system/index-2dfef0f3.js +0 -13
- package/dist/system/index-2dfef0f3.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/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
|
+
});
|
|
@@ -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
|
+
});
|
|
@@ -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
|
});
|