-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
util.ts
339 lines (302 loc) · 12.1 KB
/
util.ts
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import * as util from 'util';
import assert = require('assert');
import { access, readFile } from 'fs';
{
// Old and new util.inspect APIs
util.inspect(["This is nice"], false, 5);
util.inspect(["This is nice"], false, null);
util.inspect(["This is nice"], {
colors: true,
depth: 5,
customInspect: false,
showProxy: true,
maxArrayLength: 10,
breakLength: 20,
maxStringLength: 123,
compact: true,
sorted(a, b) {
return b.localeCompare(a);
},
getters: false,
});
util.inspect(["This is nice"], {
colors: true,
depth: null,
customInspect: false,
showProxy: true,
maxArrayLength: null,
breakLength: Infinity,
compact: false,
sorted: true,
getters: 'set',
});
util.inspect(["This is nice"], {
compact: 42,
});
assert(typeof util.inspect.custom === 'symbol');
util.inspect.replDefaults = {
colors: true,
};
util.inspect({
[util.inspect.custom]: <util.CustomInspectFunction> ((depth, opts) => opts.stylize('woop', 'module')),
});
util.format('%s:%s', 'foo');
util.format('%s:%s', 'foo', 'bar', 'baz');
util.format(1, 2, 3);
util.format('%% %s');
util.format();
util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 });
// util.callbackify
class callbackifyTest {
static fn(): Promise<void> {
assert(arguments.length === 0);
return Promise.resolve();
}
static fnE(): Promise<void> {
assert(arguments.length === 0);
return Promise.reject(new Error('fail'));
}
static fnT1(arg1: string): Promise<void> {
assert(arguments.length === 1 && arg1 === 'parameter');
return Promise.resolve();
}
static fnT1E(arg1: string): Promise<void> {
assert(arguments.length === 1 && arg1 === 'parameter');
return Promise.reject(new Error('fail'));
}
static fnTResult(): Promise<string> {
assert(arguments.length === 0);
return Promise.resolve('result');
}
static fnTResultE(): Promise<string> {
assert(arguments.length === 0);
return Promise.reject(new Error('fail'));
}
static fnT1TResult(arg1: string): Promise<string> {
assert(arguments.length === 1 && arg1 === 'parameter');
return Promise.resolve('result');
}
static fnT1TResultE(arg1: string): Promise<string> {
assert(arguments.length === 1 && arg1 === 'parameter');
return Promise.reject(new Error('fail'));
}
static test(): void {
const cfn = util.callbackify(callbackifyTest.fn);
const cfnE = util.callbackify(callbackifyTest.fnE);
const cfnT1 = util.callbackify(callbackifyTest.fnT1);
const cfnT1E = util.callbackify(callbackifyTest.fnT1E);
const cfnTResult = util.callbackify(callbackifyTest.fnTResult);
const cfnTResultE = util.callbackify(callbackifyTest.fnTResultE);
const cfnT1TResult = util.callbackify(callbackifyTest.fnT1TResult);
const cfnT1TResultE = util.callbackify(callbackifyTest.fnT1TResultE);
cfn((err: NodeJS.ErrnoException | null, ...args: string[]) => assert(err === null && args.length === 1 && args[0] === undefined));
cfnE((err: NodeJS.ErrnoException, ...args: string[]) => assert(err.message === 'fail' && args.length === 0));
cfnT1('parameter', (err: NodeJS.ErrnoException | null, ...args: string[]) => assert(err === null && args.length === 1 && args[0] === undefined));
cfnT1E('parameter', (err: NodeJS.ErrnoException, ...args: string[]) => assert(err.message === 'fail' && args.length === 0));
cfnTResult((err: NodeJS.ErrnoException | null, ...args: string[]) => assert(err === null && args.length === 1 && args[0] === 'result'));
cfnTResultE((err: NodeJS.ErrnoException, ...args: string[]) => assert(err.message === 'fail' && args.length === 0));
cfnT1TResult('parameter', (err: NodeJS.ErrnoException | null, ...args: string[]) => assert(err === null && args.length === 1 && args[0] === 'result'));
cfnT1TResultE('parameter', (err: NodeJS.ErrnoException, ...args: string[]) => assert(err.message === 'fail' && args.length === 0));
}
}
callbackifyTest.test();
// util.promisify
const readPromised = util.promisify(readFile);
const sampleRead: Promise<any> = readPromised(__filename).then((data: Buffer): void => { }).catch((error: Error): void => { });
const arg0: () => Promise<number> = util.promisify((cb: (err: Error | null, result: number) => void): void => { });
const arg0NoResult: () => Promise<any> = util.promisify((cb: (err: Error | null) => void): void => { });
const arg1: (arg: string) => Promise<number> = util.promisify((arg: string, cb: (err: Error | null, result: number) => void): void => { });
const arg1UnknownError: (arg: string) => Promise<number> = util.promisify((arg: string, cb: (err: Error | null, result: number) => void): void => { });
const arg1NoResult: (arg: string) => Promise<any> = util.promisify((arg: string, cb: (err: Error | null) => void): void => { });
const cbOptionalError: () => Promise<void | {}> = util.promisify((cb: (err?: Error | null) => void): void => { cb(); }); // tslint:disable-line void-return
assert(typeof util.promisify.custom === 'symbol');
// util.deprecate
const foo = () => {};
// $ExpectType () => void
util.deprecate(foo, 'foo() is deprecated, use bar() instead');
// $ExpectType <T extends Function>(fn: T, message: string, code?: string | undefined) => T
util.deprecate(util.deprecate, 'deprecate() is deprecated, use bar() instead');
// $ExpectType <T extends Function>(fn: T, message: string, code?: string | undefined) => T
util.deprecate(util.deprecate, 'deprecate() is deprecated, use bar() instead', 'DEP0001');
// util.isDeepStrictEqual
util.isDeepStrictEqual({foo: 'bar'}, {foo: 'bar'});
// util.TextDecoder()
const td = new util.TextDecoder();
new util.TextDecoder("utf-8");
new util.TextDecoder("utf-8", { fatal: true });
new util.TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
const ignoreBom: boolean = td.ignoreBOM;
const fatal: boolean = td.fatal;
const encoding: string = td.encoding;
td.decode(new Int8Array(1));
td.decode(new Int16Array(1));
td.decode(new Int32Array(1));
td.decode(new Uint8Array(1));
td.decode(new Uint16Array(1));
td.decode(new Uint32Array(1));
td.decode(new Uint8ClampedArray(1));
td.decode(new Float32Array(1));
td.decode(new Float64Array(1));
td.decode(new DataView(new Int8Array(1).buffer));
td.decode(new ArrayBuffer(1));
td.decode(null);
td.decode(null, { stream: true });
td.decode(new Int8Array(1), { stream: true });
const decode: string = td.decode(new Int8Array(1));
// util.TextEncoder()
const te = new util.TextEncoder();
const teEncoding: string = te.encoding;
const teEncodeRes: Uint8Array = te.encode("TextEncoder");
const encIntoRes: util.EncodeIntoResult = te.encodeInto('asdf', new Uint8Array(16));
// util.types
let b: boolean;
b = util.types.isBigInt64Array(15);
b = util.types.isBigUint64Array(15);
b = util.types.isModuleNamespaceObject(15);
const f = (v: any) => {
if (util.types.isArrayBufferView(v)) {
const abv: ArrayBufferView = v;
}
};
// tslint:disable-next-line:no-construct
const maybeBoxed: number | Number = new Number(1);
if (util.types.isBoxedPrimitive(maybeBoxed)) {
const boxed: Number = maybeBoxed;
}
const maybeBoxed2: number | Number = 1;
if (!util.types.isBoxedPrimitive(maybeBoxed2)) {
const boxed: number = maybeBoxed2;
}
}
function testUtilTypes(
object: unknown,
readonlySetOrArray: ReadonlySet<any> | ReadonlyArray<any>,
readonlyMapOrRecord: ReadonlyMap<any, any> | Record<any, any>,
) {
if (util.types.isAnyArrayBuffer(object)) {
const expected: ArrayBufferLike = object;
}
if (util.types.isArgumentsObject(object)) {
object; // $ExpectType IArguments
}
if (util.types.isArrayBufferView(object)) {
object; // $ExpectType ArrayBufferView
}
if (util.types.isBigInt64Array(object)) {
object; // $ExpectType BigInt64Array
}
if (util.types.isBigUint64Array(object)) {
object; // $ExpectType BigUint64Array
}
if (util.types.isBooleanObject(object)) {
object; // $ExpectType Boolean
}
if (util.types.isBoxedPrimitive(object)) {
object; // $ExpectType String | Number | Boolean | BigInt | Symbol
}
if (util.types.isDataView(object)) {
object; // $ExpectType DataView
}
if (util.types.isDate(object)) {
object; // $ExpectType Date
}
if (util.types.isFloat32Array(object)) {
object; // $ExpectType Float32Array
}
if (util.types.isFloat64Array(object)) {
object; // $ExpectType Float64Array
}
if (util.types.isGeneratorFunction(object)) {
object; // $ExpectType GeneratorFunction
}
if (util.types.isGeneratorObject(object)) {
object; // $ExpectType Generator<unknown, any, unknown>
}
if (util.types.isInt8Array(object)) {
object; // $ExpectType Int8Array
}
if (util.types.isInt16Array(object)) {
object; // $ExpectType Int16Array
}
if (util.types.isInt32Array(object)) {
object; // $ExpectType Int32Array
}
if (util.types.isMap(object)) {
object; // $ExpectType Map<any, any>
if (util.types.isMap(readonlyMapOrRecord)) {
readonlyMapOrRecord; // $ExpectType ReadonlyMap<any, any>
}
}
if (util.types.isNativeError(object)) {
object; // $ExpectType Error
([
new EvalError(),
new RangeError(),
new ReferenceError(),
new SyntaxError(),
new TypeError(),
new URIError(),
] as const).forEach((nativeError: EvalError | RangeError | ReferenceError | SyntaxError | TypeError | URIError) => {
if (!util.types.isNativeError(nativeError)) {
nativeError; // $ExpectType never
}
});
}
if (util.types.isNumberObject(object)) {
object; // $ExpectType Number
}
if (util.types.isPromise(object)) {
object; // $ExpectType Promise<any>
}
if (util.types.isRegExp(object)) {
object; // $ExpectType RegExp
}
if (util.types.isSet(object)) {
object; // $ExpectType Set<any>
if (util.types.isSet(readonlySetOrArray)) {
readonlySetOrArray; // $ExpectType ReadonlySet<any>
}
}
if (util.types.isSharedArrayBuffer(object)) {
object; // $ExpectType SharedArrayBuffer
}
if (util.types.isStringObject(object)) {
object; // $ExpectType String
}
if (util.types.isSymbolObject(object)) {
object; // $ExpectType Symbol
}
if (util.types.isTypedArray(object)) {
object; // $ExpectType TypedArray
}
if (util.types.isUint8Array(object)) {
object; // $ExpectType Uint8Array
}
if (util.types.isUint8ClampedArray(object)) {
object; // $ExpectType Uint8ClampedArray
}
if (util.types.isUint16Array(object)) {
object; // $ExpectType Uint16Array
}
if (util.types.isUint32Array(object)) {
object; // $ExpectType Uint32Array
}
if (util.types.isWeakMap(object)) {
object; // $ExpectType WeakMap<any, any>
}
if (util.types.isWeakSet(object)) {
object; // $ExpectType WeakSet<any>
}
}
{
const logger: util.DebugLogger = util.debuglog('section');
logger.enabled; // $ExpectType boolean
util.debuglog('section', (fn: util.DebugLoggerFunction) => { });
util.debug('section', (fn: util.DebugLoggerFunction) => { });
}
{
access('file/that/does/not/exist', (err) => {
const name = util.getSystemErrorName(err!.errno!);
console.error(name);
});
}