@gjsify/util 0.0.3 → 0.1.0
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/README.md +29 -3
- package/lib/esm/errors.js +261 -0
- package/lib/esm/index.js +578 -4
- package/lib/esm/types/index.js +297 -3
- package/lib/esm/types.js +6 -0
- package/lib/types/errors.d.ts +2 -0
- package/lib/types/index.d.ts +98 -3
- package/lib/types/types/index.d.ts +84 -2
- package/lib/types/types.d.ts +3 -0
- package/package.json +17 -28
- package/src/errors.ts +101 -0
- package/src/extended.spec.ts +469 -0
- package/src/index.spec.ts +368 -184
- package/src/index.ts +674 -3
- package/src/test.ts +2 -1
- package/src/types/index.ts +356 -3
- package/src/types.ts +4 -0
- package/tsconfig.json +22 -10
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/index.js +0 -6
- package/lib/cjs/types/index.js +0 -6
- package/test.gjs.mjs +0 -35212
- package/test.node.mjs +0 -507
- package/tsconfig.types.json +0 -8
- package/tsconfig.types.tsbuildinfo +0 -1
package/src/index.spec.ts
CHANGED
|
@@ -1,256 +1,440 @@
|
|
|
1
1
|
import { describe, it, expect } from '@gjsify/unit';
|
|
2
|
-
import * as util from 'util';
|
|
2
|
+
import * as util from 'node:util';
|
|
3
3
|
|
|
4
|
-
//
|
|
4
|
+
// Ported from refs/node/test/parallel/test-util-format.js,
|
|
5
|
+
// test-util-promisify.js, test-util-inherits.js, test-util-types.js
|
|
6
|
+
// Original: MIT license, Node.js contributors
|
|
7
|
+
|
|
8
|
+
// Helper to strip ANSI colors
|
|
5
9
|
const ANSI_PATTERN = new RegExp(
|
|
6
10
|
[
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
|
|
12
|
+
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
|
|
9
13
|
].join("|"),
|
|
10
14
|
"g",
|
|
11
15
|
);
|
|
12
|
-
|
|
13
|
-
// TODO build deno_std/fmt for this method
|
|
16
|
+
|
|
14
17
|
function stripColor(string: string): string {
|
|
15
18
|
return string.replace(ANSI_PATTERN, "");
|
|
16
19
|
}
|
|
17
|
-
|
|
18
20
|
|
|
19
21
|
export default async () => {
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
// ==================== format — no args / single values ====================
|
|
23
|
+
|
|
24
|
+
await describe('util.format: basic', async () => {
|
|
25
|
+
await it('should return empty string with no args', async () => {
|
|
26
|
+
expect(util.format()).toBe('');
|
|
24
27
|
});
|
|
25
|
-
});
|
|
26
28
|
|
|
29
|
+
await it('should format empty string', async () => {
|
|
30
|
+
expect(util.format('')).toBe('');
|
|
31
|
+
});
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
expect(util.inspect.custom.description).toEqual("nodejs.util.inspect.custom");
|
|
33
|
+
await it('should format object', async () => {
|
|
34
|
+
expect(util.format({})).toBe('{}');
|
|
31
35
|
});
|
|
32
|
-
});
|
|
33
36
|
|
|
34
|
-
|
|
37
|
+
await it('should format null', async () => {
|
|
38
|
+
expect(util.format(null)).toBe('null');
|
|
39
|
+
});
|
|
35
40
|
|
|
36
|
-
await it('should
|
|
37
|
-
expect(
|
|
38
|
-
expect(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
);
|
|
41
|
+
await it('should format true/false', async () => {
|
|
42
|
+
expect(util.format(true)).toBe('true');
|
|
43
|
+
expect(util.format(false)).toBe('false');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
await it('should format plain string', async () => {
|
|
47
|
+
expect(util.format('test')).toBe('test');
|
|
44
48
|
});
|
|
45
|
-
});
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
await it('should join multiple args with space', async () => {
|
|
51
|
+
expect(util.format('foo', 'bar', 'baz')).toBe('foo bar baz');
|
|
52
|
+
});
|
|
48
53
|
|
|
49
|
-
await it('should
|
|
50
|
-
expect(util.
|
|
51
|
-
// TODO: this is true in Gjs / Deno but false in Node.js
|
|
52
|
-
// expect(util.isBoolean(new Boolean())).toBeTruthy();
|
|
53
|
-
// expect(util.isBoolean(new Boolean(true))).toBeTruthy();
|
|
54
|
-
expect(util.isBoolean(false)).toBeTruthy();
|
|
55
|
-
expect(util.isBoolean("deno")).toBeFalsy();
|
|
56
|
-
expect(util.isBoolean("true")).toBeFalsy();
|
|
54
|
+
await it('should format Symbol', async () => {
|
|
55
|
+
expect(util.format(Symbol('foo'))).toBe('Symbol(foo)');
|
|
57
56
|
});
|
|
58
57
|
});
|
|
59
58
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
expect(util.
|
|
65
|
-
expect(util.
|
|
66
|
-
expect(util.isNull({})).toBeFalsy();
|
|
59
|
+
// ==================== format — %% (literal percent) ====================
|
|
60
|
+
|
|
61
|
+
await describe('util.format: %%', async () => {
|
|
62
|
+
await it('should handle %% with args', async () => {
|
|
63
|
+
expect(util.format('%% %s', 'foo')).toBe('% foo');
|
|
64
|
+
expect(util.format('%%', 'x')).toBe('% x');
|
|
67
65
|
});
|
|
68
66
|
});
|
|
69
67
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
expect(util.
|
|
75
|
-
expect(util.
|
|
76
|
-
expect(util.isNullOrUndefined("undefined")).toBeFalsy();
|
|
68
|
+
// ==================== format — %d (number) ====================
|
|
69
|
+
|
|
70
|
+
await describe('util.format: %d', async () => {
|
|
71
|
+
await it('should format integers', async () => {
|
|
72
|
+
expect(util.format('%d', 42.0)).toBe('42');
|
|
73
|
+
expect(util.format('%d', 42)).toBe('42');
|
|
77
74
|
});
|
|
78
|
-
});
|
|
79
75
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
expect(util.
|
|
83
|
-
// TODO: this is true in Gjs / Deno but false in Node.js
|
|
84
|
-
// expect(util.isNumber(new Number(666))).toBeTruthy();
|
|
85
|
-
expect(util.isNumber("999")).toBeFalsy();
|
|
86
|
-
expect(util.isNumber(null)).toBeFalsy();
|
|
76
|
+
await it('should format floats', async () => {
|
|
77
|
+
expect(util.format('%d', 1.5)).toBe('1.5');
|
|
78
|
+
expect(util.format('%d', -0.5)).toBe('-0.5');
|
|
87
79
|
});
|
|
88
|
-
});
|
|
89
80
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
expect(util.isString("deno")).toBeTruthy();
|
|
93
|
-
// TODO: this is true in Gjs / Deno but false in Node.js
|
|
94
|
-
// expect(util.isString(new String("DIO"))).toBeTruthy();
|
|
95
|
-
expect(util.isString(1337)).toBeFalsy();
|
|
81
|
+
await it('should format -0', async () => {
|
|
82
|
+
expect(util.format('%d', -0.0)).toBe('-0');
|
|
96
83
|
});
|
|
97
|
-
});
|
|
98
84
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
expect(util.isSymbol(Symbol())).toBeTruthy();
|
|
102
|
-
expect(util.isSymbol(123)).toBeFalsy();
|
|
103
|
-
expect(util.isSymbol("string")).toBeFalsy();
|
|
85
|
+
await it('should format Symbol as NaN', async () => {
|
|
86
|
+
expect(util.format('%d', Symbol())).toBe('NaN');
|
|
104
87
|
});
|
|
105
|
-
});
|
|
106
88
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
expect(util.
|
|
89
|
+
await it('should format BigInt', async () => {
|
|
90
|
+
expect(util.format('%d', 1180591620717411303424n)).toBe('1180591620717411303424n');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
await it('should format Infinity', async () => {
|
|
94
|
+
expect(util.format('%d', Infinity)).toBe('Infinity');
|
|
95
|
+
expect(util.format('%d', -Infinity)).toBe('-Infinity');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
await it('should handle multiple %d', async () => {
|
|
99
|
+
expect(util.format('%d %d', 42, 43)).toBe('42 43');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
await it('should return %d with no args', async () => {
|
|
103
|
+
expect(util.format('%d')).toBe('%d');
|
|
113
104
|
});
|
|
114
105
|
});
|
|
115
106
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
expect(util.
|
|
121
|
-
expect(util.
|
|
107
|
+
// ==================== format — %i (integer) ====================
|
|
108
|
+
|
|
109
|
+
await describe('util.format: %i', async () => {
|
|
110
|
+
await it('should truncate to integer', async () => {
|
|
111
|
+
expect(util.format('%i', 42)).toBe('42');
|
|
112
|
+
expect(util.format('%i', 1.5)).toBe('1');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
await it('should format -0.5 as -0', async () => {
|
|
116
|
+
expect(util.format('%i', -0.5)).toBe('-0');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
await it('should format Infinity as NaN', async () => {
|
|
120
|
+
expect(util.format('%i', Infinity)).toBe('NaN');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
await it('should format BigInt', async () => {
|
|
124
|
+
expect(util.format('%i', 1180591620717411303424n)).toBe('1180591620717411303424n');
|
|
122
125
|
});
|
|
123
126
|
});
|
|
124
127
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
expect(util.
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
// ==================== format — %f (float) ====================
|
|
129
|
+
|
|
130
|
+
await describe('util.format: %f', async () => {
|
|
131
|
+
await it('should format numbers', async () => {
|
|
132
|
+
expect(util.format('%f', 42)).toBe('42');
|
|
133
|
+
expect(util.format('%f', 1.5)).toBe('1.5');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
await it('should format Symbol as NaN', async () => {
|
|
137
|
+
expect(util.format('%f', Symbol('foo'))).toBe('NaN');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await it('should format BigInt', async () => {
|
|
141
|
+
expect(util.format('%f', 5n)).toBe('5');
|
|
133
142
|
});
|
|
134
143
|
});
|
|
135
144
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
expect(util.
|
|
141
|
-
expect(util.
|
|
145
|
+
// ==================== format — %s (string) ====================
|
|
146
|
+
|
|
147
|
+
await describe('util.format: %s', async () => {
|
|
148
|
+
await it('should format primitives', async () => {
|
|
149
|
+
expect(util.format('%s', undefined)).toBe('undefined');
|
|
150
|
+
expect(util.format('%s', null)).toBe('null');
|
|
151
|
+
expect(util.format('%s', 'foo')).toBe('foo');
|
|
152
|
+
expect(util.format('%s', 42)).toBe('42');
|
|
153
|
+
expect(util.format('%s', true)).toBe('true');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
await it('should format -0', async () => {
|
|
157
|
+
expect(util.format('%s', -0)).toBe('-0');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
await it('should format BigInt with n suffix', async () => {
|
|
161
|
+
expect(util.format('%s', 42n)).toBe('42n');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
await it('should format Symbol', async () => {
|
|
165
|
+
expect(util.format('%s', Symbol('foo'))).toBe('Symbol(foo)');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
await it('should format Infinity', async () => {
|
|
169
|
+
expect(util.format('%s', Infinity)).toBe('Infinity');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
await it('should handle multiple %s', async () => {
|
|
173
|
+
expect(util.format('%s %s', 42, 43)).toBe('42 43');
|
|
142
174
|
});
|
|
143
175
|
});
|
|
144
176
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
expect(util.
|
|
150
|
-
|
|
177
|
+
// ==================== format — %j (JSON) ====================
|
|
178
|
+
|
|
179
|
+
await describe('util.format: %j', async () => {
|
|
180
|
+
await it('should format objects as JSON', async () => {
|
|
181
|
+
expect(util.format('%j', { foo: 'bar' })).toBe('{"foo":"bar"}');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
await it('should handle circular references', async () => {
|
|
185
|
+
const obj: Record<string, unknown> = {};
|
|
186
|
+
obj.self = obj;
|
|
187
|
+
expect(util.format('%j', obj)).toBe('[Circular]');
|
|
151
188
|
});
|
|
152
189
|
});
|
|
153
190
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
expect(util.
|
|
191
|
+
// ==================== inspect ====================
|
|
192
|
+
|
|
193
|
+
await describe('util.inspect', async () => {
|
|
194
|
+
await it('should inspect objects', async () => {
|
|
195
|
+
expect(stripColor(util.inspect({ foo: 123 }))).toBe('{ foo: 123 }');
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
await it('should inspect strings with quotes', async () => {
|
|
199
|
+
expect(stripColor(util.inspect("Deno's logo is so cute.")))
|
|
200
|
+
.toBe(`"Deno's logo is so cute."`);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
await it('should have custom symbol', async () => {
|
|
204
|
+
expect(util.inspect.custom.description).toBe('nodejs.util.inspect.custom');
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
await it('should handle custom inspect', async () => {
|
|
208
|
+
const obj = {
|
|
209
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
210
|
+
return 'custom output';
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
expect(util.inspect(obj)).toBe('custom output');
|
|
159
214
|
});
|
|
160
215
|
});
|
|
161
216
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
217
|
+
// ==================== promisify ====================
|
|
218
|
+
|
|
219
|
+
await describe('util.promisify', async () => {
|
|
220
|
+
await it('should promisify a callback function', async () => {
|
|
221
|
+
function callbackFn(val: string, cb: (err: Error | null, result?: string) => void) {
|
|
222
|
+
cb(null, val + ' done');
|
|
223
|
+
}
|
|
224
|
+
const promisified = util.promisify(callbackFn);
|
|
225
|
+
const result = await promisified('test');
|
|
226
|
+
expect(result).toBe('test done');
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
await it('should reject on error', async () => {
|
|
230
|
+
function callbackFn(cb: (err: Error | null) => void) {
|
|
231
|
+
cb(new Error('test error'));
|
|
232
|
+
}
|
|
233
|
+
const promisified = util.promisify(callbackFn);
|
|
234
|
+
try {
|
|
235
|
+
await promisified();
|
|
236
|
+
expect(false).toBeTruthy(); // should not reach
|
|
237
|
+
} catch (err: unknown) {
|
|
238
|
+
expect((err as Error).message).toBe('test error');
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
await it('should use custom promisify symbol', async () => {
|
|
243
|
+
function customFn() { /* noop */ }
|
|
244
|
+
(customFn as any)[util.promisify.custom] = () => Promise.resolve('custom');
|
|
245
|
+
const promisified = util.promisify(customFn);
|
|
246
|
+
const result = await promisified();
|
|
247
|
+
expect(result).toBe('custom');
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
await it('should throw on non-function', async () => {
|
|
251
|
+
expect(() => util.promisify('not a function' as any)).toThrow();
|
|
182
252
|
});
|
|
183
253
|
});
|
|
184
254
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
expect(
|
|
255
|
+
// ==================== callbackify ====================
|
|
256
|
+
|
|
257
|
+
await describe('util.callbackify', async () => {
|
|
258
|
+
await it('should throw on non-function', async () => {
|
|
259
|
+
expect(() => util.callbackify('not a function' as any)).toThrow();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
await it('should return a function', async () => {
|
|
263
|
+
async function asyncFn() { return 'resolved'; }
|
|
264
|
+
const callbacked = util.callbackify(asyncFn);
|
|
265
|
+
expect(typeof callbacked).toBe('function');
|
|
190
266
|
});
|
|
191
267
|
});
|
|
192
268
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
269
|
+
// ==================== inherits ====================
|
|
270
|
+
|
|
271
|
+
await describe('util.inherits', async () => {
|
|
272
|
+
await it('should set up prototype chain', async () => {
|
|
273
|
+
function Parent() { /* noop */ }
|
|
274
|
+
Parent.prototype.hello = function () { return 'hello'; };
|
|
275
|
+
function Child() { /* noop */ }
|
|
276
|
+
util.inherits(Child, Parent);
|
|
277
|
+
expect(Object.getPrototypeOf(Child.prototype)).toBe(Parent.prototype);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
await it('should set super_ property', async () => {
|
|
281
|
+
function Parent() { /* noop */ }
|
|
282
|
+
function Child() { /* noop */ }
|
|
283
|
+
util.inherits(Child, Parent);
|
|
284
|
+
expect((Child as any).super_).toBe(Parent);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
await it('should throw on null constructor', async () => {
|
|
288
|
+
expect(() => util.inherits(null as any, function () { /* noop */ })).toThrow();
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
await it('should throw on null super constructor', async () => {
|
|
292
|
+
expect(() => util.inherits(function () { /* noop */ }, null as any)).toThrow();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
await it('should throw on super without prototype', async () => {
|
|
296
|
+
const fn = function () { /* noop */ };
|
|
297
|
+
(fn as any).prototype = undefined;
|
|
298
|
+
expect(() => util.inherits(function () { /* noop */ }, fn)).toThrow();
|
|
198
299
|
});
|
|
199
300
|
});
|
|
200
301
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
302
|
+
// ==================== types ====================
|
|
303
|
+
|
|
304
|
+
await describe('util.types', async () => {
|
|
305
|
+
await it('isDate', async () => {
|
|
204
306
|
expect(util.types.isDate(new Date())).toBeTruthy();
|
|
307
|
+
expect(util.types.isDate(Date.now())).toBeFalsy();
|
|
308
|
+
expect(util.types.isDate('2024-01-01')).toBeFalsy();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
await it('isRegExp', async () => {
|
|
312
|
+
expect(util.types.isRegExp(/abc/)).toBeTruthy();
|
|
313
|
+
expect(util.types.isRegExp(new RegExp('abc'))).toBeTruthy();
|
|
314
|
+
expect(util.types.isRegExp('abc')).toBeFalsy();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
await it('isMap', async () => {
|
|
318
|
+
expect(util.types.isMap(new Map())).toBeTruthy();
|
|
319
|
+
expect(util.types.isMap({})).toBeFalsy();
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
await it('isSet', async () => {
|
|
323
|
+
expect(util.types.isSet(new Set())).toBeTruthy();
|
|
324
|
+
expect(util.types.isSet([])).toBeFalsy();
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
await it('isWeakMap', async () => {
|
|
328
|
+
expect(util.types.isWeakMap(new WeakMap())).toBeTruthy();
|
|
329
|
+
expect(util.types.isWeakMap(new Map())).toBeFalsy();
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
await it('isWeakSet', async () => {
|
|
333
|
+
expect(util.types.isWeakSet(new WeakSet())).toBeTruthy();
|
|
334
|
+
expect(util.types.isWeakSet(new Set())).toBeFalsy();
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
await it('isPromise', async () => {
|
|
338
|
+
expect(util.types.isPromise(Promise.resolve())).toBeTruthy();
|
|
339
|
+
expect(util.types.isPromise({})).toBeFalsy();
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
await it('isArrayBuffer', async () => {
|
|
343
|
+
expect(util.types.isArrayBuffer(new ArrayBuffer(8))).toBeTruthy();
|
|
344
|
+
expect(util.types.isArrayBuffer(new Uint8Array(8))).toBeFalsy();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
await it('isTypedArray', async () => {
|
|
348
|
+
expect(util.types.isTypedArray(new Uint8Array())).toBeTruthy();
|
|
349
|
+
expect(util.types.isTypedArray(new Float64Array())).toBeTruthy();
|
|
350
|
+
expect(util.types.isTypedArray(new ArrayBuffer(8))).toBeFalsy();
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
await it('isUint8Array', async () => {
|
|
354
|
+
expect(util.types.isUint8Array(new Uint8Array())).toBeTruthy();
|
|
355
|
+
expect(util.types.isUint8Array(new Uint16Array())).toBeFalsy();
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
await it('isDataView', async () => {
|
|
359
|
+
const buf = new ArrayBuffer(8);
|
|
360
|
+
expect(util.types.isDataView(new DataView(buf))).toBeTruthy();
|
|
361
|
+
expect(util.types.isDataView(new Uint8Array(buf))).toBeFalsy();
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
await it('isAsyncFunction', async () => {
|
|
365
|
+
expect(util.types.isAsyncFunction(async function () { /* noop */ })).toBeTruthy();
|
|
366
|
+
expect(util.types.isAsyncFunction(function () { /* noop */ })).toBeFalsy();
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
await it('isGeneratorFunction', async () => {
|
|
370
|
+
expect(util.types.isGeneratorFunction(function* () { /* noop */ })).toBeTruthy();
|
|
371
|
+
expect(util.types.isGeneratorFunction(function () { /* noop */ })).toBeFalsy();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
await it('isNativeError', async () => {
|
|
375
|
+
expect(util.types.isNativeError(new Error())).toBeTruthy();
|
|
376
|
+
expect(util.types.isNativeError(new TypeError())).toBeTruthy();
|
|
377
|
+
expect(util.types.isNativeError({ message: 'error' })).toBeFalsy();
|
|
205
378
|
});
|
|
206
379
|
});
|
|
207
380
|
|
|
208
|
-
|
|
209
|
-
await it('should return the right results', async () => {
|
|
210
|
-
type FnTestInvalidArg = (code?: unknown) => void;
|
|
381
|
+
// ==================== isDeepStrictEqual ====================
|
|
211
382
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
).
|
|
383
|
+
await describe('util.isDeepStrictEqual', async () => {
|
|
384
|
+
await it('should compare primitives', async () => {
|
|
385
|
+
expect(util.isDeepStrictEqual(1, 1)).toBeTruthy();
|
|
386
|
+
expect(util.isDeepStrictEqual('a', 'a')).toBeTruthy();
|
|
387
|
+
expect(util.isDeepStrictEqual(1, 2)).toBeFalsy();
|
|
388
|
+
expect(util.isDeepStrictEqual(1, '1')).toBeFalsy();
|
|
389
|
+
});
|
|
215
390
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
391
|
+
await it('should compare objects', async () => {
|
|
392
|
+
expect(util.isDeepStrictEqual({ a: 1 }, { a: 1 })).toBeTruthy();
|
|
393
|
+
expect(util.isDeepStrictEqual({ a: 1 }, { a: 2 })).toBeFalsy();
|
|
394
|
+
expect(util.isDeepStrictEqual({ a: 1 }, { b: 1 })).toBeFalsy();
|
|
395
|
+
});
|
|
222
396
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
).
|
|
397
|
+
await it('should compare arrays', async () => {
|
|
398
|
+
expect(util.isDeepStrictEqual([1, 2, 3], [1, 2, 3])).toBeTruthy();
|
|
399
|
+
expect(util.isDeepStrictEqual([1, 2], [1, 2, 3])).toBeFalsy();
|
|
400
|
+
});
|
|
226
401
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
expect(util.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
case "win32":
|
|
242
|
-
case "windows" as any:
|
|
243
|
-
expect(util.getSystemErrorName(-4091)).toBe("EADDRINUSE");
|
|
244
|
-
break;
|
|
245
|
-
|
|
246
|
-
case "darwin":
|
|
247
|
-
expect(util.getSystemErrorName(-48)).toBe("EADDRINUSE");
|
|
248
|
-
break;
|
|
249
|
-
|
|
250
|
-
case "linux":
|
|
251
|
-
expect(util.getSystemErrorName(-98)).toBe("EADDRINUSE");
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
402
|
+
await it('should compare nested objects', async () => {
|
|
403
|
+
expect(util.isDeepStrictEqual({ a: { b: 1 } }, { a: { b: 1 } })).toBeTruthy();
|
|
404
|
+
expect(util.isDeepStrictEqual({ a: { b: 1 } }, { a: { b: 2 } })).toBeFalsy();
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
await it('should compare Date objects', async () => {
|
|
408
|
+
const d = new Date('2024-01-01');
|
|
409
|
+
expect(util.isDeepStrictEqual(d, new Date('2024-01-01'))).toBeTruthy();
|
|
410
|
+
expect(util.isDeepStrictEqual(d, new Date('2024-01-02'))).toBeFalsy();
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
await it('should compare RegExp objects', async () => {
|
|
414
|
+
expect(util.isDeepStrictEqual(/abc/g, /abc/g)).toBeTruthy();
|
|
415
|
+
expect(util.isDeepStrictEqual(/abc/g, /abc/i)).toBeFalsy();
|
|
254
416
|
});
|
|
255
417
|
});
|
|
256
|
-
|
|
418
|
+
|
|
419
|
+
// ==================== TextEncoder / TextDecoder ====================
|
|
420
|
+
|
|
421
|
+
await describe('util.TextEncoder/TextDecoder', async () => {
|
|
422
|
+
await it('TextEncoder should be available', async () => {
|
|
423
|
+
expect(util.TextEncoder === TextEncoder).toBeTruthy();
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
await it('TextDecoder should be available', async () => {
|
|
427
|
+
expect(util.TextDecoder === TextDecoder).toBeTruthy();
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
// ==================== isArray ====================
|
|
432
|
+
|
|
433
|
+
await describe('util.isArray', async () => {
|
|
434
|
+
await it('should detect arrays', async () => {
|
|
435
|
+
expect(util.isArray([])).toBeTruthy();
|
|
436
|
+
expect(util.isArray({})).toBeFalsy();
|
|
437
|
+
expect(util.isArray(null)).toBeFalsy();
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
};
|