@gjsify/util 0.4.0 → 0.4.3

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/src/index.spec.ts DELETED
@@ -1,440 +0,0 @@
1
- import { describe, it, expect } from '@gjsify/unit';
2
- import * as util from 'node:util';
3
-
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
9
- const ANSI_PATTERN = new RegExp(
10
- [
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=><~]))",
13
- ].join("|"),
14
- "g",
15
- );
16
-
17
- function stripColor(string: string): string {
18
- return string.replace(ANSI_PATTERN, "");
19
- }
20
-
21
- export default async () => {
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('');
27
- });
28
-
29
- await it('should format empty string', async () => {
30
- expect(util.format('')).toBe('');
31
- });
32
-
33
- await it('should format object', async () => {
34
- expect(util.format({})).toBe('{}');
35
- });
36
-
37
- await it('should format null', async () => {
38
- expect(util.format(null)).toBe('null');
39
- });
40
-
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');
48
- });
49
-
50
- await it('should join multiple args with space', async () => {
51
- expect(util.format('foo', 'bar', 'baz')).toBe('foo bar baz');
52
- });
53
-
54
- await it('should format Symbol', async () => {
55
- expect(util.format(Symbol('foo'))).toBe('Symbol(foo)');
56
- });
57
- });
58
-
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');
65
- });
66
- });
67
-
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');
74
- });
75
-
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');
79
- });
80
-
81
- await it('should format -0', async () => {
82
- expect(util.format('%d', -0.0)).toBe('-0');
83
- });
84
-
85
- await it('should format Symbol as NaN', async () => {
86
- expect(util.format('%d', Symbol())).toBe('NaN');
87
- });
88
-
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');
104
- });
105
- });
106
-
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');
125
- });
126
- });
127
-
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');
142
- });
143
- });
144
-
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');
174
- });
175
- });
176
-
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]');
188
- });
189
- });
190
-
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');
214
- });
215
- });
216
-
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();
252
- });
253
- });
254
-
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');
266
- });
267
- });
268
-
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();
299
- });
300
- });
301
-
302
- // ==================== types ====================
303
-
304
- await describe('util.types', async () => {
305
- await it('isDate', async () => {
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();
378
- });
379
- });
380
-
381
- // ==================== isDeepStrictEqual ====================
382
-
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
- });
390
-
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
- });
396
-
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
- });
401
-
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();
416
- });
417
- });
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
- };