fake-node 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
package/modules/util.ts DELETED
@@ -1,116 +0,0 @@
1
-
2
- export function callbackify(original: (...args: any[]) => Promise<any>): (...args: [...any[], (err: Error | null, value: any) => void]) => void {
3
- return function(...args: [...any[], (err: Error | null, value: any) => void]): void {
4
- const callback = args[args.length - 1];
5
- original(...args.slice(0, -1)).then((value: any) => callback(null, value)).catch((reason) => callback(reason instanceof Error ?
6
- reason : new Error(reason), null));
7
- };
8
- }
9
-
10
- const PERCENT_REGEX = /(?<!%)(%[sdifjoOc%])/;
11
-
12
- export function format(format: string, ...args: any[]): string {
13
- const parts = format.split(PERCENT_REGEX);
14
- let out = '';
15
- let j = 0;
16
- for (let i = 0; i < parts.length; i++) {
17
- const part = parts[i];
18
- if (i % 2 === 0) {
19
- out += part;
20
- } else if (part === '%%') {
21
- out += '%';
22
- } else {
23
- const arg = args[j];
24
- if (part === '%s') {
25
- if (typeof arg === 'object') {
26
- if (arg === null) {
27
- out += String(part);
28
- } else {
29
- if (arg.toString === Object.prototype.toString) {
30
- out += inspect(arg, {depth: 0, colors: false, compact: 3});
31
- } else {
32
- out += arg.toString();
33
- }
34
- }
35
- } else if (typeof part === 'bigint') {
36
- out += String(part) + 'n';
37
- } else {
38
- out += String(part);
39
- }
40
- } else if (part === '%d') {
41
- if (typeof arg === 'bigint') {
42
- out += String(arg) + 'n';
43
- } else if (typeof arg === 'symbol') {
44
- out += String(arg);
45
- } else {
46
- out += String(Number(arg));
47
- }
48
- } else if (part === '%i') {
49
- if (typeof arg === 'symbol') {
50
- out += String(arg);
51
- } else {
52
- out += String(parseInt(arg, 10));
53
- }
54
- } else if (part === '%f') {
55
- if (typeof arg === 'symbol') {
56
- out += String(arg);
57
- } else {
58
- out += String(parseFloat(arg));
59
- }
60
- } else if (part === '%j') {
61
- out += JSON.stringify(part);
62
- } else if (part === '%o') {
63
- out += inspect(part, {showHidden: true, showProxy: true});
64
- } else if (part === '%O') {
65
- out += inspect(part);
66
- }
67
- j++;
68
- }
69
- }
70
- return out;
71
- }
72
-
73
- interface InspectOptions {
74
- showHidden?: boolean;
75
- depth?: number;
76
- colors?: boolean;
77
- customInspect?: boolean;
78
- showProxy?: boolean;
79
- maxArrayLength?: number;
80
- maxStringLength?: number;
81
- breakLength?: number;
82
- compact?: false | number;
83
- sorted?: true | ((a: any, b: any) => number);
84
- getters?: boolean | 'get';
85
- numericSeperator?: boolean;
86
- }
87
-
88
- export function inspect(object: any, {showHidden = false, depth = 2, colors = false, customInspect = true, showProxy = false, maxArrayLength = 100, maxStringLength = 10000, breakLength = 80, compact = 3, sorted = true, getters = false, numericSeperator = false}: InspectOptions = {}): string {
89
- if (typeof object !== 'object' || object === 'null') {
90
- return String(object);
91
- } else {
92
- return '<util.inspect is not implemented yet>';
93
- }
94
-
95
- }
96
-
97
- export function promisify(original: (...args: [...any[], (err: Error | null, value: any) => void]) => void): (...args: any[]) => Promise<any> {
98
- return async (...args: any[]) => {
99
- return new Promise((resolve: (value: any) => void, reject: (reason?: any) => void) => {
100
- original(...args, (err: Error | null, value: any) => {
101
- if (err) {
102
- reject(err);
103
- } else {
104
- resolve(value);
105
- }
106
- });
107
-
108
- });
109
- };
110
- }
111
-
112
- const ANSI_CONTROL_REGEX = /\x1b\[[0-?]*[ -\/]*[@-~]/g;
113
-
114
- export function stripVTControlCharacters(str: string) {
115
- return str.replace(ANSI_CONTROL_REGEX, '');
116
- }