@bemoje/profiler 2.2.0 → 2.2.2
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/lib/index.cjs +1144 -7
- package/lib/index.d.cts +161 -2
- package/lib/index.d.ts +161 -2
- package/lib/index.js +1142 -1
- package/package.json +26 -29
- package/lib/Profiler/Profiler.cjs +0 -146
- package/lib/Profiler/Profiler.d.cts +0 -161
- package/lib/Profiler/Profiler.d.ts +0 -161
- package/lib/Profiler/Profiler.js +0 -144
package/lib/index.cjs
CHANGED
|
@@ -1,12 +1,1149 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var util = require('util');
|
|
4
|
+
var predicate = require('es-toolkit/predicate');
|
|
5
|
+
var types = require('util/types');
|
|
6
|
+
var math = require('es-toolkit/math');
|
|
7
|
+
var colors = require('ansi-colors');
|
|
8
|
+
var memoizee = require('memoizee');
|
|
9
|
+
var enhancedMs = require('enhanced-ms');
|
|
4
10
|
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
5
12
|
|
|
13
|
+
var colors__default = /*#__PURE__*/_interopDefault(colors);
|
|
14
|
+
var memoizee__default = /*#__PURE__*/_interopDefault(memoizee);
|
|
6
15
|
|
|
7
|
-
Object.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
16
|
+
var __defProp = Object.defineProperty;
|
|
17
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
18
|
+
var FunctionProfiler = class _FunctionProfiler {
|
|
19
|
+
static {
|
|
20
|
+
__name(this, "FunctionProfiler");
|
|
21
|
+
}
|
|
22
|
+
id;
|
|
23
|
+
static instances = [];
|
|
24
|
+
calls = 0;
|
|
25
|
+
totalTimeNs;
|
|
26
|
+
minTimeNs;
|
|
27
|
+
maxTimeNs;
|
|
28
|
+
constructor(id) {
|
|
29
|
+
this.id = id;
|
|
30
|
+
_FunctionProfiler.instances.push(this);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Average execution time in nanoseconds
|
|
34
|
+
*/
|
|
35
|
+
get avgTimeNs() {
|
|
36
|
+
if (!this.calls || !this.totalTimeNs) {
|
|
37
|
+
return void 0;
|
|
38
|
+
}
|
|
39
|
+
return Math.floor(this.totalTimeNs / this.calls);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get the result of the profiling
|
|
43
|
+
*/
|
|
44
|
+
getResult() {
|
|
45
|
+
return {
|
|
46
|
+
calls: this.calls,
|
|
47
|
+
totalTimeUs: this.totalTimeNs ? this.nsToUs(this.totalTimeNs) : void 0,
|
|
48
|
+
avgTimeUs: this.avgTimeNs ? this.nsToUs(this.avgTimeNs) : void 0,
|
|
49
|
+
minTimeUs: this.minTimeNs == null ? this.minTimeNs : this.nsToUs(this.minTimeNs),
|
|
50
|
+
maxTimeUs: this.maxTimeNs == null ? this.maxTimeNs : this.nsToUs(this.maxTimeNs)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @see IFunctionSpyStrategy.onInvoke
|
|
55
|
+
*/
|
|
56
|
+
onInvoke() {
|
|
57
|
+
return process.hrtime.bigint();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @see IFunctionSpyStrategy.onReturn
|
|
61
|
+
*/
|
|
62
|
+
onReturn(t0, retval) {
|
|
63
|
+
this.add(t0, process.hrtime.bigint());
|
|
64
|
+
return retval;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Add a new time measurement
|
|
68
|
+
*/
|
|
69
|
+
add(t0, t1) {
|
|
70
|
+
const elapsed = Number(t1 - t0);
|
|
71
|
+
this.calls++;
|
|
72
|
+
this.totalTimeNs = (this.totalTimeNs ?? 0) + elapsed;
|
|
73
|
+
this.minTimeNs = Math.min(this.minTimeNs ?? elapsed, elapsed);
|
|
74
|
+
this.maxTimeNs = Math.max(this.maxTimeNs ?? elapsed, elapsed);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Nanoseconds to microseconds
|
|
78
|
+
*/
|
|
79
|
+
nsToUs(ns) {
|
|
80
|
+
return Math.round(ns / 1e3);
|
|
81
|
+
}
|
|
82
|
+
[util.inspect.custom]() {
|
|
83
|
+
return this.getResult();
|
|
84
|
+
}
|
|
85
|
+
toJSON() {
|
|
86
|
+
return this.getResult();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// ../object/src/setNonEnumerable.ts
|
|
91
|
+
function setNonEnumerable(object, ...properties) {
|
|
92
|
+
for (const prop of properties) {
|
|
93
|
+
Object.defineProperty(object, prop, {
|
|
94
|
+
enumerable: false
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return object;
|
|
98
|
+
}
|
|
99
|
+
__name(setNonEnumerable, "setNonEnumerable");
|
|
100
|
+
|
|
101
|
+
// src/internal/FunctionIdentifier/AbstractFunctionIdentifier.ts
|
|
102
|
+
var AbstractFunctionIdentifier = class {
|
|
103
|
+
static {
|
|
104
|
+
__name(this, "AbstractFunctionIdentifier");
|
|
105
|
+
}
|
|
106
|
+
target;
|
|
107
|
+
constructor(target) {
|
|
108
|
+
this.target = target;
|
|
109
|
+
setNonEnumerable(this, "target");
|
|
110
|
+
}
|
|
111
|
+
toJSON() {
|
|
112
|
+
return this.name;
|
|
113
|
+
}
|
|
114
|
+
[util.inspect.custom]() {
|
|
115
|
+
return this.name;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var AbstractMethodIdentifier = class extends AbstractFunctionIdentifier {
|
|
119
|
+
static {
|
|
120
|
+
__name(this, "AbstractMethodIdentifier");
|
|
121
|
+
}
|
|
122
|
+
key;
|
|
123
|
+
type;
|
|
124
|
+
constructor(target, key, type = "method") {
|
|
125
|
+
super(target), this.key = key, this.type = type;
|
|
126
|
+
setNonEnumerable(this, "type", "key");
|
|
127
|
+
}
|
|
128
|
+
keytoString() {
|
|
129
|
+
return predicate.isSymbol(this.key) ? `[${this.key.description}]` : this.key;
|
|
130
|
+
}
|
|
131
|
+
get methodName() {
|
|
132
|
+
if (this.type === "method") {
|
|
133
|
+
return `${this.keytoString()}()`;
|
|
134
|
+
}
|
|
135
|
+
if (this.type === "get") {
|
|
136
|
+
return this.keytoString();
|
|
137
|
+
}
|
|
138
|
+
return `${this.keytoString()}[set]`;
|
|
139
|
+
}
|
|
140
|
+
get name() {
|
|
141
|
+
return this.parentName + (predicate.isSymbol(this.key) ? "" : ".") + this.methodName;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/internal/FunctionIdentifier/ModuleMethodIdentifier.ts
|
|
146
|
+
var ModuleMethodIdentifier = class extends AbstractMethodIdentifier {
|
|
147
|
+
static {
|
|
148
|
+
__name(this, "ModuleMethodIdentifier");
|
|
149
|
+
}
|
|
150
|
+
parentName;
|
|
151
|
+
constructor(parentName, target, key, type = "method") {
|
|
152
|
+
super(target, key, type), this.parentName = parentName;
|
|
153
|
+
setNonEnumerable(this, "parentName");
|
|
154
|
+
}
|
|
155
|
+
get targetType() {
|
|
156
|
+
return "module";
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// src/internal/ProfilerFactory/ModuleMethodProfilerFactory.ts
|
|
161
|
+
var ModuleMethodProfilerFactory = class {
|
|
162
|
+
static {
|
|
163
|
+
__name(this, "ModuleMethodProfilerFactory");
|
|
164
|
+
}
|
|
165
|
+
moduleName;
|
|
166
|
+
target;
|
|
167
|
+
constructor(moduleName, target) {
|
|
168
|
+
this.moduleName = moduleName;
|
|
169
|
+
this.target = target;
|
|
170
|
+
}
|
|
171
|
+
createProfiler(key, methodType) {
|
|
172
|
+
return new FunctionProfiler(new ModuleMethodIdentifier(this.moduleName, this.target, key, methodType));
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// ../fn/src/setNameAndLength.ts
|
|
177
|
+
function setNameAndLength(source, target, lengthDelta = 0) {
|
|
178
|
+
return Object.defineProperties(target, {
|
|
179
|
+
name: {
|
|
180
|
+
configurable: true,
|
|
181
|
+
value: source.name
|
|
182
|
+
},
|
|
183
|
+
length: {
|
|
184
|
+
configurable: true,
|
|
185
|
+
value: source.length + lengthDelta
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
__name(setNameAndLength, "setNameAndLength");
|
|
190
|
+
function functionSpy(func, spy, options = {}) {
|
|
191
|
+
if (options.async === false) {
|
|
192
|
+
return wrapSync(func, spy, options);
|
|
193
|
+
} else if (options.async || types.isAsyncFunction(func)) {
|
|
194
|
+
return wrapAsync(func, spy, options);
|
|
195
|
+
} else {
|
|
196
|
+
return wrapMaybeAsync(func, spy, options);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
__name(functionSpy, "functionSpy");
|
|
200
|
+
function wrapMaybeAsync(func, spy, options = {}) {
|
|
201
|
+
const ignore = options.ignore;
|
|
202
|
+
if (ignore && ignore(func, spy)) {
|
|
203
|
+
return func;
|
|
204
|
+
}
|
|
205
|
+
return setNameAndLength(func, function(...args) {
|
|
206
|
+
if (ignore && ignore(func, spy, this)) {
|
|
207
|
+
return func.apply(this, args);
|
|
208
|
+
}
|
|
209
|
+
const data = spy.onInvoke(this, args);
|
|
210
|
+
const retval = func.apply(this, args);
|
|
211
|
+
return retval instanceof Promise ? retval.then((retval2) => {
|
|
212
|
+
return spy.onReturn(data, retval2);
|
|
213
|
+
}) : spy.onReturn(data, retval);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
__name(wrapMaybeAsync, "wrapMaybeAsync");
|
|
217
|
+
function wrapSync(func, spy, options = {}) {
|
|
218
|
+
const ignore = options.ignore;
|
|
219
|
+
if (ignore && ignore(func, spy)) {
|
|
220
|
+
return func;
|
|
221
|
+
}
|
|
222
|
+
return setNameAndLength(func, function(...args) {
|
|
223
|
+
if (ignore && ignore(func, spy, this)) {
|
|
224
|
+
return func.apply(this, args);
|
|
225
|
+
}
|
|
226
|
+
const data = spy.onInvoke(this, args);
|
|
227
|
+
const retval = func.apply(this, args);
|
|
228
|
+
return spy.onReturn(data, retval);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
__name(wrapSync, "wrapSync");
|
|
232
|
+
function wrapAsync(func, spy, options = {}) {
|
|
233
|
+
const ignore = options.ignore;
|
|
234
|
+
if (ignore && ignore(func, spy)) {
|
|
235
|
+
return func;
|
|
236
|
+
}
|
|
237
|
+
return setNameAndLength(func, async function(...args) {
|
|
238
|
+
if (ignore && ignore(func, spy, this)) {
|
|
239
|
+
return await func.apply(this, args);
|
|
240
|
+
}
|
|
241
|
+
const data = spy.onInvoke(this, args);
|
|
242
|
+
const retval = await func.apply(this, args);
|
|
243
|
+
return spy.onReturn(data, retval);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
__name(wrapAsync, "wrapAsync");
|
|
247
|
+
|
|
248
|
+
// ../fn/src/setName.ts
|
|
249
|
+
function setName(name, target) {
|
|
250
|
+
return Object.defineProperty(target, "name", {
|
|
251
|
+
value: typeof name === "string" ? name : name.name,
|
|
252
|
+
configurable: true,
|
|
253
|
+
enumerable: false
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
__name(setName, "setName");
|
|
257
|
+
function wrapMethods(target, strat) {
|
|
258
|
+
for (const [key, type, des] of iterateMethods(target)) {
|
|
259
|
+
if (strat.filter && !strat.filter(target, key, type, des)) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
switch (type) {
|
|
263
|
+
case "method": {
|
|
264
|
+
if (!strat.onMethod) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
const orig = des.value;
|
|
268
|
+
const wrapped = strat.onMethod(target, key, orig);
|
|
269
|
+
if (!wrapped) {
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
Object.defineProperty(target, key, {
|
|
273
|
+
value: setNameAndLength(orig, wrapped),
|
|
274
|
+
configurable: des.configurable,
|
|
275
|
+
enumerable: des.enumerable
|
|
276
|
+
});
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
case "get": {
|
|
280
|
+
if (!strat.onGetter) {
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const orig = des.get;
|
|
284
|
+
const wrapped = strat.onGetter(target, key, orig);
|
|
285
|
+
if (!wrapped) {
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
Object.defineProperty(target, key, {
|
|
289
|
+
get: setNameAndLength(orig, wrapped),
|
|
290
|
+
configurable: des.configurable,
|
|
291
|
+
enumerable: des.enumerable
|
|
292
|
+
});
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
case "set": {
|
|
296
|
+
if (!strat.onSetter) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const orig = des.set;
|
|
300
|
+
const wrapped = strat.onSetter(target, key, orig);
|
|
301
|
+
if (!wrapped) {
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
Object.defineProperty(target, key, {
|
|
305
|
+
set: setNameAndLength(orig, wrapped),
|
|
306
|
+
configurable: des.configurable,
|
|
307
|
+
enumerable: des.enumerable
|
|
308
|
+
});
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return target;
|
|
314
|
+
}
|
|
315
|
+
__name(wrapMethods, "wrapMethods");
|
|
316
|
+
function* iterateMethods(target) {
|
|
317
|
+
for (const key of Reflect.ownKeys(target)) {
|
|
318
|
+
const des = Object.getOwnPropertyDescriptor(target, key);
|
|
319
|
+
if (!des || des.configurable === false) {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
if (predicate.isFunction(des.value)) {
|
|
323
|
+
yield [
|
|
324
|
+
key,
|
|
325
|
+
"method",
|
|
326
|
+
des
|
|
327
|
+
];
|
|
328
|
+
} else {
|
|
329
|
+
if (predicate.isFunction(des.get)) {
|
|
330
|
+
yield [
|
|
331
|
+
key,
|
|
332
|
+
"get",
|
|
333
|
+
des
|
|
334
|
+
];
|
|
335
|
+
}
|
|
336
|
+
if (predicate.isFunction(des.set)) {
|
|
337
|
+
yield [
|
|
338
|
+
key,
|
|
339
|
+
"set",
|
|
340
|
+
des
|
|
341
|
+
];
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
__name(iterateMethods, "iterateMethods");
|
|
347
|
+
|
|
348
|
+
// src/internal/FunctionProfiler/profilerWrap.ts
|
|
349
|
+
function profilerWrap(func, profiler) {
|
|
350
|
+
const ignore = /* @__PURE__ */ __name(() => {
|
|
351
|
+
return !Profiler.enabled;
|
|
352
|
+
}, "ignore");
|
|
353
|
+
return profiler.id.type === "get" || profiler.id.type === "set" ? functionSpy(func, profiler, {
|
|
354
|
+
ignore,
|
|
355
|
+
async: false
|
|
356
|
+
}) : functionSpy(func, profiler, {
|
|
357
|
+
ignore
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
__name(profilerWrap, "profilerWrap");
|
|
361
|
+
|
|
362
|
+
// src/internal/ProfilerWrapMethodsStrategy/ProfilerWrapMethodsStrategy.ts
|
|
363
|
+
var ProfilerWrapMethodsStrategy = class {
|
|
364
|
+
static {
|
|
365
|
+
__name(this, "ProfilerWrapMethodsStrategy");
|
|
366
|
+
}
|
|
367
|
+
profilerFactory;
|
|
368
|
+
ignoreKeys;
|
|
369
|
+
constructor(profilerFactory, ignoreKeys) {
|
|
370
|
+
this.profilerFactory = profilerFactory;
|
|
371
|
+
this.ignoreKeys = new Set(ignoreKeys);
|
|
372
|
+
}
|
|
373
|
+
filter(target, key) {
|
|
374
|
+
if (key === "constructor") {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
if (this.ignoreKeys.has(key)) {
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
onMethod(target, key, method) {
|
|
383
|
+
return profilerWrap(method, this.profilerFactory.createProfiler(key, "method"));
|
|
384
|
+
}
|
|
385
|
+
onGetter(target, key, getter) {
|
|
386
|
+
return profilerWrap(getter, this.profilerFactory.createProfiler(key, "get"));
|
|
387
|
+
}
|
|
388
|
+
onSetter(target, key, setter) {
|
|
389
|
+
return profilerWrap(setter, this.profilerFactory.createProfiler(key, "set"));
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// src/internal/FunctionIdentifier/PrototypeMethodIdentifier.ts
|
|
394
|
+
var PrototypeMethodIdentifier = class extends AbstractMethodIdentifier {
|
|
395
|
+
static {
|
|
396
|
+
__name(this, "PrototypeMethodIdentifier");
|
|
397
|
+
}
|
|
398
|
+
get targetType() {
|
|
399
|
+
return "prototype";
|
|
400
|
+
}
|
|
401
|
+
get parentName() {
|
|
402
|
+
return `${this.target.constructor.name}.${this.targetType}`;
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
// src/internal/ProfilerFactory/PrototypeMethodProfilerFactory.ts
|
|
407
|
+
var PrototypeMethodProfilerFactory = class {
|
|
408
|
+
static {
|
|
409
|
+
__name(this, "PrototypeMethodProfilerFactory");
|
|
410
|
+
}
|
|
411
|
+
target;
|
|
412
|
+
constructor(target) {
|
|
413
|
+
this.target = target;
|
|
414
|
+
}
|
|
415
|
+
createProfiler(key, methodType) {
|
|
416
|
+
return new FunctionProfiler(new PrototypeMethodIdentifier(this.target, key, methodType));
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
// src/internal/FunctionIdentifier/StandaloneFunctionIdentifier.ts
|
|
421
|
+
var StandaloneFunctionIdentifier = class extends AbstractFunctionIdentifier {
|
|
422
|
+
static {
|
|
423
|
+
__name(this, "StandaloneFunctionIdentifier");
|
|
424
|
+
}
|
|
425
|
+
get targetType() {
|
|
426
|
+
return "standalone";
|
|
427
|
+
}
|
|
428
|
+
get type() {
|
|
429
|
+
return "function";
|
|
430
|
+
}
|
|
431
|
+
get name() {
|
|
432
|
+
return `${this.target.name}()`;
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
// src/internal/FunctionIdentifier/StaticMethodIdentifier.ts
|
|
437
|
+
var StaticMethodIdentifier = class extends AbstractMethodIdentifier {
|
|
438
|
+
static {
|
|
439
|
+
__name(this, "StaticMethodIdentifier");
|
|
440
|
+
}
|
|
441
|
+
get targetType() {
|
|
442
|
+
return "static";
|
|
443
|
+
}
|
|
444
|
+
get parentName() {
|
|
445
|
+
return this.target.name;
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
// src/internal/ProfilerFactory/StaticMethodProfilerFactory.ts
|
|
450
|
+
var StaticMethodProfilerFactory = class {
|
|
451
|
+
static {
|
|
452
|
+
__name(this, "StaticMethodProfilerFactory");
|
|
453
|
+
}
|
|
454
|
+
target;
|
|
455
|
+
constructor(target) {
|
|
456
|
+
this.target = target;
|
|
457
|
+
}
|
|
458
|
+
createProfiler(key, methodType) {
|
|
459
|
+
return new FunctionProfiler(new StaticMethodIdentifier(this.target, key, methodType));
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
// ../number/src/NumberFormatter.ts
|
|
464
|
+
var locales = /* @__PURE__ */ new Map([
|
|
465
|
+
[
|
|
466
|
+
"en-US",
|
|
467
|
+
[
|
|
468
|
+
",",
|
|
469
|
+
"."
|
|
470
|
+
]
|
|
471
|
+
],
|
|
472
|
+
[
|
|
473
|
+
"da-DK",
|
|
474
|
+
[
|
|
475
|
+
".",
|
|
476
|
+
","
|
|
477
|
+
]
|
|
478
|
+
]
|
|
479
|
+
]);
|
|
480
|
+
var NumberFormatter = class {
|
|
481
|
+
static {
|
|
482
|
+
__name(this, "NumberFormatter");
|
|
483
|
+
}
|
|
484
|
+
// The number of decimals to round to.
|
|
485
|
+
precision;
|
|
486
|
+
// The character to use as thousand separator.
|
|
487
|
+
thousandSeparator = ",";
|
|
488
|
+
// The character to use as decimal separator.
|
|
489
|
+
decimalSeparator = ".";
|
|
490
|
+
/**
|
|
491
|
+
* Create a new number formatter.
|
|
492
|
+
* @param precision The number of decimals to round to.
|
|
493
|
+
*/
|
|
494
|
+
constructor(precision = 2) {
|
|
495
|
+
this.precision = precision;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Set separator strings for a given locale.
|
|
499
|
+
*/
|
|
500
|
+
static defineLocale(locale, thousandSeparator, decimalSeparator) {
|
|
501
|
+
locales.set(locale, [
|
|
502
|
+
thousandSeparator,
|
|
503
|
+
decimalSeparator
|
|
504
|
+
]);
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Returns a copy of the locales map.
|
|
508
|
+
*/
|
|
509
|
+
static getLocales() {
|
|
510
|
+
const result = /* @__PURE__ */ new Map();
|
|
511
|
+
for (const entry of locales.entries()) {
|
|
512
|
+
result.set(entry[0], [
|
|
513
|
+
entry[1][0],
|
|
514
|
+
entry[1][1]
|
|
515
|
+
]);
|
|
516
|
+
}
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Set the locale for the number formatter.
|
|
521
|
+
*/
|
|
522
|
+
locale(locale) {
|
|
523
|
+
const sep = locales.get(locale);
|
|
524
|
+
if (!sep) {
|
|
525
|
+
throw new Error(`Invalid locale: ${locale}. Valid: ${Array.from(locales.keys())}}.`);
|
|
526
|
+
}
|
|
527
|
+
this.thousandSeparator = sep[0];
|
|
528
|
+
this.decimalSeparator = sep[1];
|
|
529
|
+
return this;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Format a number to a string.
|
|
533
|
+
*/
|
|
534
|
+
format(number) {
|
|
535
|
+
const negative = number >= 0 ? "" : "-";
|
|
536
|
+
const split = Math.abs(math.round(number, this.precision)).toString().split(".");
|
|
537
|
+
const decimals = this.precision ? this.decimalSeparator + (split[1] || "").padEnd(this.precision, "0") : "";
|
|
538
|
+
let result = "";
|
|
539
|
+
for (let i = 0; i < split[0].length; i++) {
|
|
540
|
+
if (i % 3 === 0 && i !== 0) {
|
|
541
|
+
result = this.thousandSeparator + result;
|
|
542
|
+
}
|
|
543
|
+
result = split[0][split[0].length - i - 1] + result;
|
|
544
|
+
}
|
|
545
|
+
return negative + result + decimals;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Parse a formatted string to a number.
|
|
549
|
+
*/
|
|
550
|
+
parse(string) {
|
|
551
|
+
string = string.replaceAll(this.thousandSeparator, "").replace(this.decimalSeparator, ".").replaceAll(/[^\d.-]/g, "").split(".").map((s) => {
|
|
552
|
+
return parseInt(s);
|
|
553
|
+
}).join(".");
|
|
554
|
+
const n = Number(string);
|
|
555
|
+
return n;
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
// ../decorators/src/assertDescriptorValueIsFunction.ts
|
|
560
|
+
function assertDescriptorValueIsFunction(key, descriptor) {
|
|
561
|
+
if (!(typeof descriptor.value === "function" && descriptor.value !== Function.prototype)) {
|
|
562
|
+
throw new TypeError(`"value" not a function for ${key} with descriptor: ${JSON.stringify(descriptor)}.`);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
__name(assertDescriptorValueIsFunction, "assertDescriptorValueIsFunction");
|
|
566
|
+
|
|
567
|
+
// ../map/src/mapGetOrDefault.ts
|
|
568
|
+
function mapGetOrDefault(map, key, factory) {
|
|
569
|
+
let value = map.get(key);
|
|
570
|
+
if (value !== void 0 || map.has(key)) {
|
|
571
|
+
return value;
|
|
572
|
+
}
|
|
573
|
+
value = factory(key, map);
|
|
574
|
+
map.set(key, value);
|
|
575
|
+
return value;
|
|
576
|
+
}
|
|
577
|
+
__name(mapGetOrDefault, "mapGetOrDefault");
|
|
578
|
+
|
|
579
|
+
// ../map/src/TimeoutWeakMap.ts
|
|
580
|
+
var TimeoutWeakMap = class {
|
|
581
|
+
static {
|
|
582
|
+
__name(this, "TimeoutWeakMap");
|
|
583
|
+
}
|
|
584
|
+
timeoutMs;
|
|
585
|
+
wmap = /* @__PURE__ */ new WeakMap();
|
|
586
|
+
/**
|
|
587
|
+
* @param timeoutMs Default timeout in milliseconds for entries
|
|
588
|
+
*/
|
|
589
|
+
constructor(timeoutMs) {
|
|
590
|
+
this.timeoutMs = timeoutMs;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Retrieves the value for the given key and refreshes its timeout.
|
|
594
|
+
*
|
|
595
|
+
* @param key The key to retrieve
|
|
596
|
+
* @returns The value if found, undefined otherwise
|
|
597
|
+
*/
|
|
598
|
+
get(key) {
|
|
599
|
+
const vt = this.wmap.get(key);
|
|
600
|
+
if (!vt) {
|
|
601
|
+
return void 0;
|
|
602
|
+
}
|
|
603
|
+
const [value, timeout] = vt;
|
|
604
|
+
timeout.refresh();
|
|
605
|
+
return value;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Sets a key-value pair with an optional custom timeout.
|
|
609
|
+
*
|
|
610
|
+
* @param key The key to set
|
|
611
|
+
* @param value The value to associate with the key
|
|
612
|
+
* @param timeoutMs Optional timeout override for this entry
|
|
613
|
+
* @returns This instance for chaining
|
|
614
|
+
*/
|
|
615
|
+
set(key, value, timeoutMs) {
|
|
616
|
+
this.wmap.set(key, [
|
|
617
|
+
value,
|
|
618
|
+
setTimeout(() => {
|
|
619
|
+
this.delete(key);
|
|
620
|
+
}, timeoutMs ?? this.timeoutMs).unref()
|
|
621
|
+
]);
|
|
622
|
+
return this;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Removes a key-value pair and clears its timeout.
|
|
626
|
+
*
|
|
627
|
+
* @param key The key to delete
|
|
628
|
+
* @returns True if the key existed and was deleted, false otherwise
|
|
629
|
+
*/
|
|
630
|
+
delete(key) {
|
|
631
|
+
const vt = this.wmap.get(key);
|
|
632
|
+
if (!vt) {
|
|
633
|
+
return false;
|
|
634
|
+
}
|
|
635
|
+
const timeout = vt[1];
|
|
636
|
+
clearTimeout(timeout);
|
|
637
|
+
return this.wmap.delete(key);
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Checks if a key exists in the map.
|
|
641
|
+
* Does not refresh the timeout.
|
|
642
|
+
*
|
|
643
|
+
* @param key The key to check
|
|
644
|
+
* @returns True if the key exists, false otherwise
|
|
645
|
+
*/
|
|
646
|
+
has(key) {
|
|
647
|
+
return this.wmap.has(key);
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Loads multiple key-value pairs into the map.
|
|
651
|
+
*
|
|
652
|
+
* @param entries Iterable of key-value pairs to load
|
|
653
|
+
* @param timeoutMs Optional timeout override for all entries
|
|
654
|
+
* @returns This instance for chaining
|
|
655
|
+
*/
|
|
656
|
+
load(entries, timeoutMs) {
|
|
657
|
+
for (const [key, value] of entries) {
|
|
658
|
+
this.set(key, value, timeoutMs);
|
|
659
|
+
}
|
|
660
|
+
return this;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Updates an existing value or creates a new one using an update function.
|
|
664
|
+
*
|
|
665
|
+
* @param key The key to update
|
|
666
|
+
* @param update Function that receives the current value and returns the new value
|
|
667
|
+
* @returns This instance for chaining
|
|
668
|
+
*/
|
|
669
|
+
update(key, update) {
|
|
670
|
+
return this.set(key, update(this.get(key), key, this));
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Gets a value or creates it using a factory function if it doesn't exist.
|
|
674
|
+
* Refreshes timeout if the value exists.
|
|
675
|
+
*
|
|
676
|
+
* @param key The key to get or create
|
|
677
|
+
* @param factory Function to create the value if it doesn't exist
|
|
678
|
+
* @param timeoutMs Optional timeout override for new entries
|
|
679
|
+
* @returns The existing or newly created value
|
|
680
|
+
*/
|
|
681
|
+
getOrDefault(key, factory, timeoutMs) {
|
|
682
|
+
const vt = this.wmap.get(key);
|
|
683
|
+
if (vt) {
|
|
684
|
+
const [value, timeout] = vt;
|
|
685
|
+
timeout.refresh();
|
|
686
|
+
return value;
|
|
687
|
+
} else {
|
|
688
|
+
const value = factory(key, this);
|
|
689
|
+
this.set(key, value, timeoutMs);
|
|
690
|
+
return value;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
function memoizeSync(arg = {}) {
|
|
695
|
+
const opts = typeof arg === "object" ? arg : {
|
|
696
|
+
maxAge: typeof arg === "number" ? arg : enhancedMs.ms(arg)
|
|
697
|
+
};
|
|
698
|
+
return /* @__PURE__ */ __name(function decorator(target, key, descriptor) {
|
|
699
|
+
if (!descriptor) {
|
|
700
|
+
throw new TypeError("descriptor is undefined");
|
|
701
|
+
}
|
|
702
|
+
const orig = descriptor.value;
|
|
703
|
+
assertDescriptorValueIsFunction(key, descriptor);
|
|
704
|
+
const options = {
|
|
705
|
+
length: false,
|
|
706
|
+
...opts
|
|
707
|
+
};
|
|
708
|
+
if (opts.instancesShareCache) {
|
|
709
|
+
Reflect.deleteProperty(options, "instancesShareCache");
|
|
710
|
+
descriptor.value = memoizee__default.default(orig, options);
|
|
711
|
+
} else {
|
|
712
|
+
const wmap = /* @__PURE__ */ new WeakMap();
|
|
713
|
+
descriptor.value = function(...args) {
|
|
714
|
+
const memoized = mapGetOrDefault(wmap, this, () => {
|
|
715
|
+
return memoizee__default.default(orig, options);
|
|
716
|
+
});
|
|
717
|
+
return memoized.apply(this, args);
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
return descriptor;
|
|
721
|
+
}, "decorator");
|
|
722
|
+
}
|
|
723
|
+
__name(memoizeSync, "memoizeSync");
|
|
724
|
+
function lazyProp(targetOrTimeout, key, descriptor) {
|
|
725
|
+
if (typeof targetOrTimeout === "number" || typeof targetOrTimeout === "string") {
|
|
726
|
+
const maxAge = typeof targetOrTimeout === "number" ? targetOrTimeout : enhancedMs.ms(targetOrTimeout);
|
|
727
|
+
return createLazyPropDecorator(new TimeoutWeakMap(maxAge));
|
|
728
|
+
} else {
|
|
729
|
+
const decorator = createLazyPropDecorator(/* @__PURE__ */ new WeakMap());
|
|
730
|
+
return decorator(targetOrTimeout, key, descriptor);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
__name(lazyProp, "lazyProp");
|
|
734
|
+
function createLazyPropDecorator(map) {
|
|
735
|
+
return function(target, key, descriptor) {
|
|
736
|
+
const { get, value } = descriptor;
|
|
737
|
+
if (predicate.isFunction(get)) {
|
|
738
|
+
descriptor.get = function() {
|
|
739
|
+
return mapGetOrDefault(map, this, () => {
|
|
740
|
+
return get.call(this);
|
|
741
|
+
});
|
|
742
|
+
};
|
|
743
|
+
return descriptor;
|
|
744
|
+
}
|
|
745
|
+
if (predicate.isFunction(value)) {
|
|
746
|
+
return memoizeSync()(target, key, descriptor);
|
|
747
|
+
}
|
|
748
|
+
throw new TypeError('neither "get" nor "value" are functions');
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
__name(createLazyPropDecorator, "createLazyPropDecorator");
|
|
752
|
+
|
|
753
|
+
// ../table/src/TableFormatter.ts
|
|
754
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
755
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
756
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
757
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
758
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
759
|
+
}
|
|
760
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
761
|
+
function _ts_metadata(k, v) {
|
|
762
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
763
|
+
}
|
|
764
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
765
|
+
var TableFormatter = class _TableFormatter {
|
|
766
|
+
static {
|
|
767
|
+
__name(this, "TableFormatter");
|
|
768
|
+
}
|
|
769
|
+
static defaults = {
|
|
770
|
+
columnSeparator: " | ",
|
|
771
|
+
headerRowSeparator: "-",
|
|
772
|
+
grayOutRow: void 0,
|
|
773
|
+
color: {
|
|
774
|
+
separator: colors__default.default.dim.gray,
|
|
775
|
+
columnHeader: colors__default.default.cyan,
|
|
776
|
+
rowHeader: /* @__PURE__ */ __name((s) => {
|
|
777
|
+
return s;
|
|
778
|
+
}, "rowHeader"),
|
|
779
|
+
string: colors__default.default.green,
|
|
780
|
+
positiveNumber: colors__default.default.yellow,
|
|
781
|
+
negativeNumber: colors__default.default.yellow,
|
|
782
|
+
booleanTrue: colors__default.default.magenta,
|
|
783
|
+
booleanFalse: colors__default.default.magenta
|
|
784
|
+
}
|
|
785
|
+
};
|
|
786
|
+
table;
|
|
787
|
+
options;
|
|
788
|
+
constructor(table, options) {
|
|
789
|
+
this.table = table;
|
|
790
|
+
this.options = this.handleOptions(options);
|
|
791
|
+
}
|
|
792
|
+
get numberColumnPrecisions() {
|
|
793
|
+
return this.table.reduce((acc, row) => {
|
|
794
|
+
return row.map((cell, c) => {
|
|
795
|
+
if (typeof cell !== "number") {
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
if (Number.isInteger(cell)) {
|
|
799
|
+
return 0;
|
|
800
|
+
}
|
|
801
|
+
const digits = cell.toString().split(".")[1].length;
|
|
802
|
+
return Math.max(acc[c] || 0, digits);
|
|
803
|
+
});
|
|
804
|
+
}, []);
|
|
805
|
+
}
|
|
806
|
+
get stringsTable() {
|
|
807
|
+
return this.table.map((row) => {
|
|
808
|
+
return row.map((cell, c) => {
|
|
809
|
+
return this.cellToString(cell, c);
|
|
810
|
+
});
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
get columnWidths() {
|
|
814
|
+
return this.stringsTable.reduce((acc, row) => {
|
|
815
|
+
return row.map((cell, c) => {
|
|
816
|
+
return Math.max(acc[c] || 0, cell.length);
|
|
817
|
+
});
|
|
818
|
+
}, []);
|
|
819
|
+
}
|
|
820
|
+
get tableWidth() {
|
|
821
|
+
const separatorsWidth = (this.columnWidths.length - 1) * this.options.columnSeparator.length;
|
|
822
|
+
const columnsWidth = this.columnWidths.reduce((acc, width) => {
|
|
823
|
+
return acc + width;
|
|
824
|
+
}, 0);
|
|
825
|
+
return columnsWidth + separatorsWidth;
|
|
826
|
+
}
|
|
827
|
+
get columnSeparatorFormatted() {
|
|
828
|
+
return this.options.color.separator(this.options.columnSeparator);
|
|
829
|
+
}
|
|
830
|
+
get headerRowSeparatorLineFormatted() {
|
|
831
|
+
return this.options.color.separator("".padEnd(this.tableWidth, this.options.headerRowSeparator));
|
|
832
|
+
}
|
|
833
|
+
get formattedRows() {
|
|
834
|
+
const result = this.stringsTable.map((row, r) => {
|
|
835
|
+
return row.map((cell, c) => {
|
|
836
|
+
return this.formatCell(cell, c, r);
|
|
837
|
+
});
|
|
838
|
+
});
|
|
839
|
+
if (!this.options.color || !this.options.grayOutRow) {
|
|
840
|
+
return result;
|
|
841
|
+
}
|
|
842
|
+
return result.map((row, r) => {
|
|
843
|
+
if (!this.options.grayOutRow(this.table[r])) {
|
|
844
|
+
return row;
|
|
845
|
+
}
|
|
846
|
+
return row.map((cell) => {
|
|
847
|
+
return this.options.color.separator(colors__default.default.stripColor(cell));
|
|
848
|
+
});
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
get formattedLines() {
|
|
852
|
+
const lines = this.formattedRows.map((row) => {
|
|
853
|
+
return row.join(this.columnSeparatorFormatted);
|
|
854
|
+
});
|
|
855
|
+
lines.splice(1, 0, this.headerRowSeparatorLineFormatted);
|
|
856
|
+
const headersLine = lines[0];
|
|
857
|
+
lines.push(this.headerRowSeparatorLineFormatted);
|
|
858
|
+
lines.push(headersLine);
|
|
859
|
+
return lines;
|
|
860
|
+
}
|
|
861
|
+
toString() {
|
|
862
|
+
return this.formattedLines.join("\n");
|
|
863
|
+
}
|
|
864
|
+
handleOptions(options = {}) {
|
|
865
|
+
options = {
|
|
866
|
+
...options
|
|
867
|
+
};
|
|
868
|
+
if (!options.columnSeparator) {
|
|
869
|
+
options.columnSeparator = _TableFormatter.defaults.columnSeparator;
|
|
870
|
+
}
|
|
871
|
+
if (!options.headerRowSeparator) {
|
|
872
|
+
options.headerRowSeparator = _TableFormatter.defaults.headerRowSeparator;
|
|
873
|
+
}
|
|
874
|
+
options.color = options.color ? options.color === true ? _TableFormatter.defaults.color : {
|
|
875
|
+
..._TableFormatter.defaults.color,
|
|
876
|
+
...options.color
|
|
877
|
+
} : defaultColorsNoop;
|
|
878
|
+
return options;
|
|
879
|
+
}
|
|
880
|
+
cellToString(cell, c) {
|
|
881
|
+
if (cell == null) {
|
|
882
|
+
return "";
|
|
883
|
+
} else if (typeof cell === "string") {
|
|
884
|
+
return cell;
|
|
885
|
+
} else if (typeof cell === "boolean") {
|
|
886
|
+
return cell.toString();
|
|
887
|
+
} else if (typeof cell === "number") {
|
|
888
|
+
return new NumberFormatter(this.numberColumnPrecisions[c] ?? 0).format(cell);
|
|
889
|
+
} else if (Array.isArray(cell)) {
|
|
890
|
+
return `[${cell.join(", ")}]`;
|
|
891
|
+
} else {
|
|
892
|
+
throw new TypeError(`Unexpected cell value type: ${typeof cell}`);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
formatCell(cell, c, r) {
|
|
896
|
+
const originalCell = this.table[r][c];
|
|
897
|
+
if (originalCell == null) {
|
|
898
|
+
return this.formatNil(cell, c);
|
|
899
|
+
} else if (typeof originalCell === "string") {
|
|
900
|
+
return this.formatString(cell, r, c);
|
|
901
|
+
} else if (typeof originalCell === "boolean") {
|
|
902
|
+
return this.formatBoolean(cell, c);
|
|
903
|
+
} else if (typeof originalCell === "number") {
|
|
904
|
+
return this.formatNumber(cell, originalCell, c);
|
|
905
|
+
} else if (Array.isArray(originalCell)) {
|
|
906
|
+
return this.formatString(cell, r, c);
|
|
907
|
+
} else {
|
|
908
|
+
throw new TypeError(`Unexpected cell value type: ${typeof originalCell}`);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
formatNil(cell, c) {
|
|
912
|
+
return this.alignLeft(cell, c);
|
|
913
|
+
}
|
|
914
|
+
formatString(cell, r, c) {
|
|
915
|
+
cell = this.alignLeft(cell, c);
|
|
916
|
+
if (this.isColumnHeader(r)) {
|
|
917
|
+
return this.options.color.columnHeader(cell);
|
|
918
|
+
}
|
|
919
|
+
if (this.isRowHeader(c)) {
|
|
920
|
+
return this.options.color.rowHeader(cell);
|
|
921
|
+
}
|
|
922
|
+
return this.options.color.string(cell);
|
|
923
|
+
}
|
|
924
|
+
formatBoolean(cell, c) {
|
|
925
|
+
const padded = this.alignLeft(cell, c);
|
|
926
|
+
if (cell === "true") {
|
|
927
|
+
return this.options.color.booleanTrue(padded);
|
|
928
|
+
}
|
|
929
|
+
if (cell === "false") {
|
|
930
|
+
return this.options.color.booleanFalse(padded);
|
|
931
|
+
}
|
|
932
|
+
throw new Error(`Unexpected boolean representation: ${cell}`);
|
|
933
|
+
}
|
|
934
|
+
formatNumber(cell, originalCell, c) {
|
|
935
|
+
cell = this.alignRight(cell, c);
|
|
936
|
+
if ((originalCell ?? 0) < 0) {
|
|
937
|
+
return this.options.color.negativeNumber(cell);
|
|
938
|
+
}
|
|
939
|
+
return this.options.color.positiveNumber(cell);
|
|
940
|
+
}
|
|
941
|
+
alignLeft(cell, c) {
|
|
942
|
+
return cell.padEnd(this.columnWidths[c], " ");
|
|
943
|
+
}
|
|
944
|
+
alignRight(cell, c) {
|
|
945
|
+
return cell.padStart(this.columnWidths[c], " ");
|
|
946
|
+
}
|
|
947
|
+
isColumnHeader(r) {
|
|
948
|
+
return r === 0;
|
|
949
|
+
}
|
|
950
|
+
isRowHeader(c) {
|
|
951
|
+
return c === 0;
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
_ts_decorate([
|
|
955
|
+
lazyProp,
|
|
956
|
+
_ts_metadata("design:type", Array),
|
|
957
|
+
_ts_metadata("design:paramtypes", [])
|
|
958
|
+
], TableFormatter.prototype, "numberColumnPrecisions", null);
|
|
959
|
+
_ts_decorate([
|
|
960
|
+
lazyProp,
|
|
961
|
+
_ts_metadata("design:type", Array),
|
|
962
|
+
_ts_metadata("design:paramtypes", [])
|
|
963
|
+
], TableFormatter.prototype, "stringsTable", null);
|
|
964
|
+
_ts_decorate([
|
|
965
|
+
lazyProp,
|
|
966
|
+
_ts_metadata("design:type", Array),
|
|
967
|
+
_ts_metadata("design:paramtypes", [])
|
|
968
|
+
], TableFormatter.prototype, "columnWidths", null);
|
|
969
|
+
_ts_decorate([
|
|
970
|
+
lazyProp,
|
|
971
|
+
_ts_metadata("design:type", Number),
|
|
972
|
+
_ts_metadata("design:paramtypes", [])
|
|
973
|
+
], TableFormatter.prototype, "tableWidth", null);
|
|
974
|
+
_ts_decorate([
|
|
975
|
+
lazyProp,
|
|
976
|
+
_ts_metadata("design:type", void 0),
|
|
977
|
+
_ts_metadata("design:paramtypes", [])
|
|
978
|
+
], TableFormatter.prototype, "columnSeparatorFormatted", null);
|
|
979
|
+
_ts_decorate([
|
|
980
|
+
lazyProp,
|
|
981
|
+
_ts_metadata("design:type", void 0),
|
|
982
|
+
_ts_metadata("design:paramtypes", [])
|
|
983
|
+
], TableFormatter.prototype, "headerRowSeparatorLineFormatted", null);
|
|
984
|
+
_ts_decorate([
|
|
985
|
+
lazyProp,
|
|
986
|
+
_ts_metadata("design:type", Array),
|
|
987
|
+
_ts_metadata("design:paramtypes", [])
|
|
988
|
+
], TableFormatter.prototype, "formattedRows", null);
|
|
989
|
+
_ts_decorate([
|
|
990
|
+
lazyProp,
|
|
991
|
+
_ts_metadata("design:type", void 0),
|
|
992
|
+
_ts_metadata("design:paramtypes", [])
|
|
993
|
+
], TableFormatter.prototype, "formattedLines", null);
|
|
994
|
+
var defaultColorsNoop = {
|
|
995
|
+
separator: /* @__PURE__ */ __name((s) => {
|
|
996
|
+
return s;
|
|
997
|
+
}, "separator"),
|
|
998
|
+
columnHeader: /* @__PURE__ */ __name((s) => {
|
|
999
|
+
return s;
|
|
1000
|
+
}, "columnHeader"),
|
|
1001
|
+
rowHeader: /* @__PURE__ */ __name((s) => {
|
|
1002
|
+
return s;
|
|
1003
|
+
}, "rowHeader"),
|
|
1004
|
+
string: /* @__PURE__ */ __name((s) => {
|
|
1005
|
+
return s;
|
|
1006
|
+
}, "string"),
|
|
1007
|
+
positiveNumber: /* @__PURE__ */ __name((s) => {
|
|
1008
|
+
return s;
|
|
1009
|
+
}, "positiveNumber"),
|
|
1010
|
+
negativeNumber: /* @__PURE__ */ __name((s) => {
|
|
1011
|
+
return s;
|
|
1012
|
+
}, "negativeNumber"),
|
|
1013
|
+
booleanTrue: /* @__PURE__ */ __name((s) => {
|
|
1014
|
+
return s;
|
|
1015
|
+
}, "booleanTrue"),
|
|
1016
|
+
booleanFalse: /* @__PURE__ */ __name((s) => {
|
|
1017
|
+
return s;
|
|
1018
|
+
}, "booleanFalse")
|
|
1019
|
+
};
|
|
1020
|
+
var Profiler = class _Profiler {
|
|
1021
|
+
static {
|
|
1022
|
+
__name(this, "Profiler");
|
|
1023
|
+
}
|
|
1024
|
+
static enabled = true;
|
|
1025
|
+
static get data() {
|
|
1026
|
+
return FunctionProfiler.instances.slice();
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
*
|
|
1030
|
+
*/
|
|
1031
|
+
static class(targetOrOptions, options) {
|
|
1032
|
+
if (predicate.isFunction(targetOrOptions)) {
|
|
1033
|
+
const target = targetOrOptions;
|
|
1034
|
+
_Profiler.classStatic(target, options?.ignoreStaticKeys);
|
|
1035
|
+
_Profiler.classPrototype(target, options?.ignorePrototypeKeys);
|
|
1036
|
+
return target;
|
|
1037
|
+
} else {
|
|
1038
|
+
return (target) => {
|
|
1039
|
+
return _Profiler.class(target, targetOrOptions);
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
static classStatic(target, ignoreKeys) {
|
|
1044
|
+
if (_Profiler.enabled) {
|
|
1045
|
+
wrapMethods(target, new ProfilerWrapMethodsStrategy(new StaticMethodProfilerFactory(target), [
|
|
1046
|
+
...ignoreKeys ?? [],
|
|
1047
|
+
...target.profiler?.ignoreStaticKeys ?? []
|
|
1048
|
+
]));
|
|
1049
|
+
}
|
|
1050
|
+
return target;
|
|
1051
|
+
}
|
|
1052
|
+
static classPrototype(target, ignoreKeys) {
|
|
1053
|
+
if (_Profiler.enabled) {
|
|
1054
|
+
wrapMethods(target.prototype, new ProfilerWrapMethodsStrategy(new PrototypeMethodProfilerFactory(target.prototype), [
|
|
1055
|
+
...ignoreKeys ?? [],
|
|
1056
|
+
...target.profiler?.ignorePrototypeKeys ?? []
|
|
1057
|
+
]));
|
|
1058
|
+
}
|
|
1059
|
+
return target;
|
|
1060
|
+
}
|
|
1061
|
+
static module(target, moduleName, ignoreKeys) {
|
|
1062
|
+
if (_Profiler.enabled) {
|
|
1063
|
+
wrapMethods(target, new ProfilerWrapMethodsStrategy(new ModuleMethodProfilerFactory(moduleName, target), ignoreKeys));
|
|
1064
|
+
}
|
|
1065
|
+
return target;
|
|
1066
|
+
}
|
|
1067
|
+
static fn(nameOrFunc, maybeFunc) {
|
|
1068
|
+
const func = predicate.isFunction(nameOrFunc) ? nameOrFunc : setName(nameOrFunc, maybeFunc);
|
|
1069
|
+
if (!func.name) {
|
|
1070
|
+
throw new Error(`Function must have a name:
|
|
1071
|
+
${func.toString()}`);
|
|
1072
|
+
}
|
|
1073
|
+
if (_Profiler.enabled) {
|
|
1074
|
+
return profilerWrap(func, new FunctionProfiler(new StandaloneFunctionIdentifier(func)));
|
|
1075
|
+
}
|
|
1076
|
+
return func;
|
|
1077
|
+
}
|
|
1078
|
+
static getResults(options) {
|
|
1079
|
+
const results = _Profiler.data.map((profiler) => {
|
|
1080
|
+
return [
|
|
1081
|
+
profiler.id.name,
|
|
1082
|
+
profiler.getResult()
|
|
1083
|
+
];
|
|
1084
|
+
});
|
|
1085
|
+
const sortkey1 = options?.sortBy ?? "calls";
|
|
1086
|
+
const sortValue1 = /* @__PURE__ */ __name((entry) => {
|
|
1087
|
+
return entry[1][sortkey1] ?? -1;
|
|
1088
|
+
}, "sortValue1");
|
|
1089
|
+
const sortKey2 = sortkey1 === "calls" ? "totalTimeUs" : "calls";
|
|
1090
|
+
const sortValue2 = /* @__PURE__ */ __name((entry) => {
|
|
1091
|
+
return entry[1][sortKey2] ?? -1;
|
|
1092
|
+
}, "sortValue2");
|
|
1093
|
+
return results.sort((a, b) => {
|
|
1094
|
+
const comparision = sortValue1(a) - sortValue1(b);
|
|
1095
|
+
if (comparision !== 0) {
|
|
1096
|
+
return comparision;
|
|
1097
|
+
}
|
|
1098
|
+
return sortValue2(a) - sortValue2(b);
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
static printResults(options) {
|
|
1102
|
+
let entries = this.getResults(options);
|
|
1103
|
+
if (options?.update) {
|
|
1104
|
+
entries = options.update(entries);
|
|
1105
|
+
}
|
|
1106
|
+
const headers = [
|
|
1107
|
+
"Execution time (microseconds)",
|
|
1108
|
+
"calls",
|
|
1109
|
+
"total",
|
|
1110
|
+
"avg",
|
|
1111
|
+
"min",
|
|
1112
|
+
"max"
|
|
1113
|
+
];
|
|
1114
|
+
const rows = entries.map(([method, stats]) => {
|
|
1115
|
+
return [
|
|
1116
|
+
method,
|
|
1117
|
+
stats.calls,
|
|
1118
|
+
stats.totalTimeUs,
|
|
1119
|
+
stats.avgTimeUs,
|
|
1120
|
+
stats.minTimeUs,
|
|
1121
|
+
stats.maxTimeUs
|
|
1122
|
+
];
|
|
1123
|
+
});
|
|
1124
|
+
const table = [
|
|
1125
|
+
headers,
|
|
1126
|
+
...rows
|
|
1127
|
+
];
|
|
1128
|
+
const formatter = new TableFormatter(table, {
|
|
1129
|
+
color: !options?.noColor,
|
|
1130
|
+
grayOutRow: /* @__PURE__ */ __name((row) => {
|
|
1131
|
+
return row[1] === 0;
|
|
1132
|
+
}, "grayOutRow")
|
|
1133
|
+
});
|
|
1134
|
+
console.log();
|
|
1135
|
+
formatter.formattedLines.forEach((line) => {
|
|
1136
|
+
return console.log(line);
|
|
1137
|
+
});
|
|
1138
|
+
console.log();
|
|
1139
|
+
}
|
|
1140
|
+
static [util.inspect.custom](depth, options = {}) {
|
|
1141
|
+
options.compact = true;
|
|
1142
|
+
return Object.fromEntries(this.getResults());
|
|
1143
|
+
}
|
|
1144
|
+
static toJSON() {
|
|
1145
|
+
return this.getResults();
|
|
1146
|
+
}
|
|
1147
|
+
};
|
|
1148
|
+
|
|
1149
|
+
exports.Profiler = Profiler;
|