@bemoje/profiler 2.0.0 → 2.0.1
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 +106 -0
- package/index.d.ts +1 -1
- package/index.mjs +195 -209
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# @bemoje/profiler
|
|
2
|
+
|
|
3
|
+
Performance profiling utilities for measuring execution time of functions, class methods, and modules.
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/api/esm.html)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @bemoje/profiler
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### Profile a Standalone Function
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { Profiler } from '@bemoje/profiler'
|
|
20
|
+
|
|
21
|
+
const add = Profiler.fn('add', (a: number, b: number) => a + b)
|
|
22
|
+
|
|
23
|
+
add(1, 2) // 3
|
|
24
|
+
add(3, 4) // 7
|
|
25
|
+
|
|
26
|
+
Profiler.printResults()
|
|
27
|
+
// Execution time (microseconds) calls total avg min max
|
|
28
|
+
// add() 2 5 2 2 3
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Profile a Class
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { Profiler } from '@bemoje/profiler'
|
|
35
|
+
|
|
36
|
+
@Profiler.class
|
|
37
|
+
class Calculator {
|
|
38
|
+
add(a: number, b: number) {
|
|
39
|
+
return a + b
|
|
40
|
+
}
|
|
41
|
+
multiply(a: number, b: number) {
|
|
42
|
+
return a * b
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Or without decorators:
|
|
47
|
+
Profiler.class(Calculator)
|
|
48
|
+
|
|
49
|
+
// All methods are now profiled (prototype + static)
|
|
50
|
+
const calc = new Calculator()
|
|
51
|
+
calc.add(1, 2)
|
|
52
|
+
calc.multiply(3, 4)
|
|
53
|
+
|
|
54
|
+
Profiler.printResults()
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Profile a Module
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { Profiler } from '@bemoje/profiler'
|
|
61
|
+
import * as mathUtils from './mathUtils'
|
|
62
|
+
|
|
63
|
+
Profiler.module(mathUtils, 'mathUtils')
|
|
64
|
+
|
|
65
|
+
mathUtils.sum([1, 2, 3])
|
|
66
|
+
mathUtils.average([1, 2, 3])
|
|
67
|
+
|
|
68
|
+
Profiler.printResults()
|
|
69
|
+
// Execution time (microseconds) calls total avg min max
|
|
70
|
+
// mathUtils.sum() 1 12 12 12 12
|
|
71
|
+
// mathUtils.average() 1 8 8 8 8
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Options and Control
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { Profiler } from '@bemoje/profiler'
|
|
78
|
+
|
|
79
|
+
// Disable profiling globally (wraps become no-ops)
|
|
80
|
+
Profiler.enabled = false
|
|
81
|
+
|
|
82
|
+
// Ignore specific keys when profiling a class
|
|
83
|
+
Profiler.class(MyClass, { ignoreStaticKeys: ['create'], ignorePrototypeKeys: ['toString'] })
|
|
84
|
+
|
|
85
|
+
// Sort results by total time
|
|
86
|
+
Profiler.printResults({ sortBy: 'totalTimeUs' })
|
|
87
|
+
|
|
88
|
+
// Get raw results as data
|
|
89
|
+
const results = Profiler.getResults({ sortBy: 'calls' })
|
|
90
|
+
// [['methodName', { calls, totalTimeUs, avgTimeUs, minTimeUs, maxTimeUs }], ...]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## API Reference
|
|
94
|
+
|
|
95
|
+
| Export | Description |
|
|
96
|
+
| ------------------------- | --------------------------------------------------------- |
|
|
97
|
+
| `Profiler` | Main class with static methods for profiling |
|
|
98
|
+
| `Profiler.fn` | Wrap a standalone function with profiling |
|
|
99
|
+
| `Profiler.class` | Profile all methods of a class (decorator or direct call) |
|
|
100
|
+
| `Profiler.classStatic` | Profile only static methods of a class |
|
|
101
|
+
| `Profiler.classPrototype` | Profile only prototype methods of a class |
|
|
102
|
+
| `Profiler.module` | Profile all exported functions of a module object |
|
|
103
|
+
| `Profiler.getResults` | Get profiling results as structured data |
|
|
104
|
+
| `Profiler.printResults` | Print a formatted profiling table to the console |
|
|
105
|
+
| `Profiler.enabled` | Enable or disable profiling globally |
|
|
106
|
+
| `Profiler.data` | Access all profiler instances |
|
package/index.d.ts
CHANGED
package/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { inspect } from 'util';
|
|
2
2
|
import { isSymbol, isFunction } from 'es-toolkit/predicate';
|
|
3
3
|
import { isAsyncFunction } from 'util/types';
|
|
4
|
+
import { round } from 'es-toolkit';
|
|
4
5
|
import colors from 'ansi-colors';
|
|
5
6
|
import memoizee from 'memoizee';
|
|
6
7
|
import { ms } from 'enhanced-ms';
|
|
@@ -79,134 +80,6 @@ var FunctionProfiler = class _FunctionProfiler {
|
|
|
79
80
|
}
|
|
80
81
|
};
|
|
81
82
|
|
|
82
|
-
// ../map/src/mapGetOrDefault.ts
|
|
83
|
-
function mapGetOrDefault(map, key, factory) {
|
|
84
|
-
let value = map.get(key);
|
|
85
|
-
if (value !== void 0 || map.has(key)) {
|
|
86
|
-
return value;
|
|
87
|
-
}
|
|
88
|
-
value = factory(key, map);
|
|
89
|
-
map.set(key, value);
|
|
90
|
-
return value;
|
|
91
|
-
}
|
|
92
|
-
__name(mapGetOrDefault, "mapGetOrDefault");
|
|
93
|
-
|
|
94
|
-
// ../map/src/TimeoutWeakMap.ts
|
|
95
|
-
var TimeoutWeakMap = class {
|
|
96
|
-
static {
|
|
97
|
-
__name(this, "TimeoutWeakMap");
|
|
98
|
-
}
|
|
99
|
-
timeoutMs;
|
|
100
|
-
wmap = /* @__PURE__ */ new WeakMap();
|
|
101
|
-
/**
|
|
102
|
-
* @param timeoutMs Default timeout in milliseconds for entries
|
|
103
|
-
*/
|
|
104
|
-
constructor(timeoutMs) {
|
|
105
|
-
this.timeoutMs = timeoutMs;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Retrieves the value for the given key and refreshes its timeout.
|
|
109
|
-
*
|
|
110
|
-
* @param key The key to retrieve
|
|
111
|
-
* @returns The value if found, undefined otherwise
|
|
112
|
-
*/
|
|
113
|
-
get(key) {
|
|
114
|
-
const vt = this.wmap.get(key);
|
|
115
|
-
if (!vt) {
|
|
116
|
-
return void 0;
|
|
117
|
-
}
|
|
118
|
-
const [value, timeout] = vt;
|
|
119
|
-
timeout.refresh();
|
|
120
|
-
return value;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Sets a key-value pair with an optional custom timeout.
|
|
124
|
-
*
|
|
125
|
-
* @param key The key to set
|
|
126
|
-
* @param value The value to associate with the key
|
|
127
|
-
* @param timeoutMs Optional timeout override for this entry
|
|
128
|
-
* @returns This instance for chaining
|
|
129
|
-
*/
|
|
130
|
-
set(key, value, timeoutMs) {
|
|
131
|
-
this.wmap.set(key, [
|
|
132
|
-
value,
|
|
133
|
-
setTimeout(() => {
|
|
134
|
-
this.delete(key);
|
|
135
|
-
}, timeoutMs ?? this.timeoutMs).unref()
|
|
136
|
-
]);
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Removes a key-value pair and clears its timeout.
|
|
141
|
-
*
|
|
142
|
-
* @param key The key to delete
|
|
143
|
-
* @returns True if the key existed and was deleted, false otherwise
|
|
144
|
-
*/
|
|
145
|
-
delete(key) {
|
|
146
|
-
const vt = this.wmap.get(key);
|
|
147
|
-
if (!vt) {
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
const timeout = vt[1];
|
|
151
|
-
clearTimeout(timeout);
|
|
152
|
-
return this.wmap.delete(key);
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Checks if a key exists in the map.
|
|
156
|
-
* Does not refresh the timeout.
|
|
157
|
-
*
|
|
158
|
-
* @param key The key to check
|
|
159
|
-
* @returns True if the key exists, false otherwise
|
|
160
|
-
*/
|
|
161
|
-
has(key) {
|
|
162
|
-
return this.wmap.has(key);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Loads multiple key-value pairs into the map.
|
|
166
|
-
*
|
|
167
|
-
* @param entries Iterable of key-value pairs to load
|
|
168
|
-
* @param timeoutMs Optional timeout override for all entries
|
|
169
|
-
* @returns This instance for chaining
|
|
170
|
-
*/
|
|
171
|
-
load(entries, timeoutMs) {
|
|
172
|
-
for (const [key, value] of entries) {
|
|
173
|
-
this.set(key, value, timeoutMs);
|
|
174
|
-
}
|
|
175
|
-
return this;
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Updates an existing value or creates a new one using an update function.
|
|
179
|
-
*
|
|
180
|
-
* @param key The key to update
|
|
181
|
-
* @param update Function that receives the current value and returns the new value
|
|
182
|
-
* @returns This instance for chaining
|
|
183
|
-
*/
|
|
184
|
-
update(key, update) {
|
|
185
|
-
return this.set(key, update(this.get(key), key, this));
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Gets a value or creates it using a factory function if it doesn't exist.
|
|
189
|
-
* Refreshes timeout if the value exists.
|
|
190
|
-
*
|
|
191
|
-
* @param key The key to get or create
|
|
192
|
-
* @param factory Function to create the value if it doesn't exist
|
|
193
|
-
* @param timeoutMs Optional timeout override for new entries
|
|
194
|
-
* @returns The existing or newly created value
|
|
195
|
-
*/
|
|
196
|
-
getOrDefault(key, factory, timeoutMs) {
|
|
197
|
-
const vt = this.wmap.get(key);
|
|
198
|
-
if (vt) {
|
|
199
|
-
const [value, timeout] = vt;
|
|
200
|
-
timeout.refresh();
|
|
201
|
-
return value;
|
|
202
|
-
} else {
|
|
203
|
-
const value = factory(key, this);
|
|
204
|
-
this.set(key, value, timeoutMs);
|
|
205
|
-
return value;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
|
|
210
83
|
// ../object/src/setNonEnumerable.ts
|
|
211
84
|
function setNonEnumerable(object, ...properties) {
|
|
212
85
|
for (const prop of properties) {
|
|
@@ -293,8 +166,8 @@ var ModuleMethodProfilerFactory = class {
|
|
|
293
166
|
}
|
|
294
167
|
};
|
|
295
168
|
|
|
296
|
-
// ../fn/src/
|
|
297
|
-
function
|
|
169
|
+
// ../fn/src/setNameAndLength.ts
|
|
170
|
+
function setNameAndLength(source, target, lengthDelta = 0) {
|
|
298
171
|
return Object.defineProperties(target, {
|
|
299
172
|
name: {
|
|
300
173
|
configurable: true,
|
|
@@ -302,11 +175,11 @@ function preserveNameAndLength(source, target, adjustLengthBy = 0) {
|
|
|
302
175
|
},
|
|
303
176
|
length: {
|
|
304
177
|
configurable: true,
|
|
305
|
-
value: source.length +
|
|
178
|
+
value: source.length + lengthDelta
|
|
306
179
|
}
|
|
307
180
|
});
|
|
308
181
|
}
|
|
309
|
-
__name(
|
|
182
|
+
__name(setNameAndLength, "setNameAndLength");
|
|
310
183
|
function functionSpy(func, spy, options = {}) {
|
|
311
184
|
if (options.async === false) {
|
|
312
185
|
return wrapSync(func, spy, options);
|
|
@@ -322,19 +195,15 @@ function wrapMaybeAsync(func, spy, options = {}) {
|
|
|
322
195
|
if (ignore && ignore(func, spy)) {
|
|
323
196
|
return func;
|
|
324
197
|
}
|
|
325
|
-
return
|
|
198
|
+
return setNameAndLength(func, function(...args) {
|
|
326
199
|
if (ignore && ignore(func, spy, this)) {
|
|
327
200
|
return func.apply(this, args);
|
|
328
201
|
}
|
|
329
202
|
const data = spy.onInvoke(this, args);
|
|
330
203
|
const retval = func.apply(this, args);
|
|
331
|
-
|
|
332
|
-
return
|
|
333
|
-
|
|
334
|
-
});
|
|
335
|
-
} else {
|
|
336
|
-
return spy.onReturn(data, retval);
|
|
337
|
-
}
|
|
204
|
+
return retval instanceof Promise ? retval.then((retval2) => {
|
|
205
|
+
return spy.onReturn(data, retval2);
|
|
206
|
+
}) : spy.onReturn(data, retval);
|
|
338
207
|
});
|
|
339
208
|
}
|
|
340
209
|
__name(wrapMaybeAsync, "wrapMaybeAsync");
|
|
@@ -343,7 +212,7 @@ function wrapSync(func, spy, options = {}) {
|
|
|
343
212
|
if (ignore && ignore(func, spy)) {
|
|
344
213
|
return func;
|
|
345
214
|
}
|
|
346
|
-
return
|
|
215
|
+
return setNameAndLength(func, function(...args) {
|
|
347
216
|
if (ignore && ignore(func, spy, this)) {
|
|
348
217
|
return func.apply(this, args);
|
|
349
218
|
}
|
|
@@ -358,7 +227,7 @@ function wrapAsync(func, spy, options = {}) {
|
|
|
358
227
|
if (ignore && ignore(func, spy)) {
|
|
359
228
|
return func;
|
|
360
229
|
}
|
|
361
|
-
return
|
|
230
|
+
return setNameAndLength(func, async function(...args) {
|
|
362
231
|
if (ignore && ignore(func, spy, this)) {
|
|
363
232
|
return await func.apply(this, args);
|
|
364
233
|
}
|
|
@@ -383,48 +252,55 @@ function wrapMethods(target, strat) {
|
|
|
383
252
|
if (strat.filter && !strat.filter(target, key, type, des)) {
|
|
384
253
|
continue;
|
|
385
254
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
continue;
|
|
403
|
-
}
|
|
404
|
-
const orig = des.get;
|
|
405
|
-
const wrapped = strat.onGetter(target, key, orig);
|
|
406
|
-
if (!wrapped) {
|
|
407
|
-
continue;
|
|
255
|
+
switch (type) {
|
|
256
|
+
case "method": {
|
|
257
|
+
if (!strat.onMethod) {
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
const orig = des.value;
|
|
261
|
+
const wrapped = strat.onMethod(target, key, orig);
|
|
262
|
+
if (!wrapped) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
Object.defineProperty(target, key, {
|
|
266
|
+
value: setNameAndLength(orig, wrapped),
|
|
267
|
+
configurable: des.configurable,
|
|
268
|
+
enumerable: des.enumerable
|
|
269
|
+
});
|
|
270
|
+
break;
|
|
408
271
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
272
|
+
case "get": {
|
|
273
|
+
if (!strat.onGetter) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const orig = des.get;
|
|
277
|
+
const wrapped = strat.onGetter(target, key, orig);
|
|
278
|
+
if (!wrapped) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
Object.defineProperty(target, key, {
|
|
282
|
+
get: setNameAndLength(orig, wrapped),
|
|
283
|
+
configurable: des.configurable,
|
|
284
|
+
enumerable: des.enumerable
|
|
285
|
+
});
|
|
286
|
+
break;
|
|
417
287
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
288
|
+
case "set": {
|
|
289
|
+
if (!strat.onSetter) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const orig = des.set;
|
|
293
|
+
const wrapped = strat.onSetter(target, key, orig);
|
|
294
|
+
if (!wrapped) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
Object.defineProperty(target, key, {
|
|
298
|
+
set: setNameAndLength(orig, wrapped),
|
|
299
|
+
configurable: des.configurable,
|
|
300
|
+
enumerable: des.enumerable
|
|
301
|
+
});
|
|
302
|
+
break;
|
|
422
303
|
}
|
|
423
|
-
Object.defineProperty(target, key, {
|
|
424
|
-
set: preserveNameAndLength(orig, wrapped),
|
|
425
|
-
configurable: des.configurable,
|
|
426
|
-
enumerable: des.enumerable
|
|
427
|
-
});
|
|
428
304
|
}
|
|
429
305
|
}
|
|
430
306
|
return target;
|
|
@@ -467,16 +343,12 @@ function profilerWrap(func, profiler) {
|
|
|
467
343
|
const ignore = /* @__PURE__ */ __name(() => {
|
|
468
344
|
return !Profiler.enabled;
|
|
469
345
|
}, "ignore");
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
}
|
|
476
|
-
return functionSpy(func, profiler, {
|
|
477
|
-
ignore
|
|
478
|
-
});
|
|
479
|
-
}
|
|
346
|
+
return profiler.id.type === "get" || profiler.id.type === "set" ? functionSpy(func, profiler, {
|
|
347
|
+
ignore,
|
|
348
|
+
async: false
|
|
349
|
+
}) : functionSpy(func, profiler, {
|
|
350
|
+
ignore
|
|
351
|
+
});
|
|
480
352
|
}
|
|
481
353
|
__name(profilerWrap, "profilerWrap");
|
|
482
354
|
|
|
@@ -581,20 +453,6 @@ var StaticMethodProfilerFactory = class {
|
|
|
581
453
|
}
|
|
582
454
|
};
|
|
583
455
|
|
|
584
|
-
// ../number/src/roundWith.ts
|
|
585
|
-
function roundWith(number, precision, round2 = Math.round) {
|
|
586
|
-
const pair1 = `${number}e`.split("e");
|
|
587
|
-
const pair2 = `${round2(+`${pair1[0]}e${+pair1[1] + precision}`)}e`.split("e");
|
|
588
|
-
return +`${pair2[0]}e${+pair2[1] - precision}`;
|
|
589
|
-
}
|
|
590
|
-
__name(roundWith, "roundWith");
|
|
591
|
-
|
|
592
|
-
// ../number/src/round.ts
|
|
593
|
-
function round(number, precision = 0) {
|
|
594
|
-
return roundWith(number, precision, Math.round);
|
|
595
|
-
}
|
|
596
|
-
__name(round, "round");
|
|
597
|
-
|
|
598
456
|
// ../number/src/NumberFormatter.ts
|
|
599
457
|
var locales = /* @__PURE__ */ new Map([
|
|
600
458
|
[
|
|
@@ -683,7 +541,7 @@ var NumberFormatter = class {
|
|
|
683
541
|
* Parse a formatted string to a number.
|
|
684
542
|
*/
|
|
685
543
|
parse(string) {
|
|
686
|
-
string = string.replaceAll(this.thousandSeparator, "").replace(this.decimalSeparator, ".").
|
|
544
|
+
string = string.replaceAll(this.thousandSeparator, "").replace(this.decimalSeparator, ".").replaceAll(/[^\d.-]/g, "").split(".").map((s) => {
|
|
687
545
|
return parseInt(s);
|
|
688
546
|
}).join(".");
|
|
689
547
|
const n = Number(string);
|
|
@@ -698,6 +556,134 @@ function assertDescriptorValueIsFunction(key, descriptor) {
|
|
|
698
556
|
}
|
|
699
557
|
}
|
|
700
558
|
__name(assertDescriptorValueIsFunction, "assertDescriptorValueIsFunction");
|
|
559
|
+
|
|
560
|
+
// ../map/src/mapGetOrDefault.ts
|
|
561
|
+
function mapGetOrDefault(map, key, factory) {
|
|
562
|
+
let value = map.get(key);
|
|
563
|
+
if (value !== void 0 || map.has(key)) {
|
|
564
|
+
return value;
|
|
565
|
+
}
|
|
566
|
+
value = factory(key, map);
|
|
567
|
+
map.set(key, value);
|
|
568
|
+
return value;
|
|
569
|
+
}
|
|
570
|
+
__name(mapGetOrDefault, "mapGetOrDefault");
|
|
571
|
+
|
|
572
|
+
// ../map/src/TimeoutWeakMap.ts
|
|
573
|
+
var TimeoutWeakMap = class {
|
|
574
|
+
static {
|
|
575
|
+
__name(this, "TimeoutWeakMap");
|
|
576
|
+
}
|
|
577
|
+
timeoutMs;
|
|
578
|
+
wmap = /* @__PURE__ */ new WeakMap();
|
|
579
|
+
/**
|
|
580
|
+
* @param timeoutMs Default timeout in milliseconds for entries
|
|
581
|
+
*/
|
|
582
|
+
constructor(timeoutMs) {
|
|
583
|
+
this.timeoutMs = timeoutMs;
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Retrieves the value for the given key and refreshes its timeout.
|
|
587
|
+
*
|
|
588
|
+
* @param key The key to retrieve
|
|
589
|
+
* @returns The value if found, undefined otherwise
|
|
590
|
+
*/
|
|
591
|
+
get(key) {
|
|
592
|
+
const vt = this.wmap.get(key);
|
|
593
|
+
if (!vt) {
|
|
594
|
+
return void 0;
|
|
595
|
+
}
|
|
596
|
+
const [value, timeout] = vt;
|
|
597
|
+
timeout.refresh();
|
|
598
|
+
return value;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Sets a key-value pair with an optional custom timeout.
|
|
602
|
+
*
|
|
603
|
+
* @param key The key to set
|
|
604
|
+
* @param value The value to associate with the key
|
|
605
|
+
* @param timeoutMs Optional timeout override for this entry
|
|
606
|
+
* @returns This instance for chaining
|
|
607
|
+
*/
|
|
608
|
+
set(key, value, timeoutMs) {
|
|
609
|
+
this.wmap.set(key, [
|
|
610
|
+
value,
|
|
611
|
+
setTimeout(() => {
|
|
612
|
+
this.delete(key);
|
|
613
|
+
}, timeoutMs ?? this.timeoutMs).unref()
|
|
614
|
+
]);
|
|
615
|
+
return this;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Removes a key-value pair and clears its timeout.
|
|
619
|
+
*
|
|
620
|
+
* @param key The key to delete
|
|
621
|
+
* @returns True if the key existed and was deleted, false otherwise
|
|
622
|
+
*/
|
|
623
|
+
delete(key) {
|
|
624
|
+
const vt = this.wmap.get(key);
|
|
625
|
+
if (!vt) {
|
|
626
|
+
return false;
|
|
627
|
+
}
|
|
628
|
+
const timeout = vt[1];
|
|
629
|
+
clearTimeout(timeout);
|
|
630
|
+
return this.wmap.delete(key);
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Checks if a key exists in the map.
|
|
634
|
+
* Does not refresh the timeout.
|
|
635
|
+
*
|
|
636
|
+
* @param key The key to check
|
|
637
|
+
* @returns True if the key exists, false otherwise
|
|
638
|
+
*/
|
|
639
|
+
has(key) {
|
|
640
|
+
return this.wmap.has(key);
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Loads multiple key-value pairs into the map.
|
|
644
|
+
*
|
|
645
|
+
* @param entries Iterable of key-value pairs to load
|
|
646
|
+
* @param timeoutMs Optional timeout override for all entries
|
|
647
|
+
* @returns This instance for chaining
|
|
648
|
+
*/
|
|
649
|
+
load(entries, timeoutMs) {
|
|
650
|
+
for (const [key, value] of entries) {
|
|
651
|
+
this.set(key, value, timeoutMs);
|
|
652
|
+
}
|
|
653
|
+
return this;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Updates an existing value or creates a new one using an update function.
|
|
657
|
+
*
|
|
658
|
+
* @param key The key to update
|
|
659
|
+
* @param update Function that receives the current value and returns the new value
|
|
660
|
+
* @returns This instance for chaining
|
|
661
|
+
*/
|
|
662
|
+
update(key, update) {
|
|
663
|
+
return this.set(key, update(this.get(key), key, this));
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Gets a value or creates it using a factory function if it doesn't exist.
|
|
667
|
+
* Refreshes timeout if the value exists.
|
|
668
|
+
*
|
|
669
|
+
* @param key The key to get or create
|
|
670
|
+
* @param factory Function to create the value if it doesn't exist
|
|
671
|
+
* @param timeoutMs Optional timeout override for new entries
|
|
672
|
+
* @returns The existing or newly created value
|
|
673
|
+
*/
|
|
674
|
+
getOrDefault(key, factory, timeoutMs) {
|
|
675
|
+
const vt = this.wmap.get(key);
|
|
676
|
+
if (vt) {
|
|
677
|
+
const [value, timeout] = vt;
|
|
678
|
+
timeout.refresh();
|
|
679
|
+
return value;
|
|
680
|
+
} else {
|
|
681
|
+
const value = factory(key, this);
|
|
682
|
+
this.set(key, value, timeoutMs);
|
|
683
|
+
return value;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
};
|
|
701
687
|
function memoizeSync(arg = {}) {
|
|
702
688
|
const opts = typeof arg === "object" ? arg : {
|
|
703
689
|
maxAge: typeof arg === "number" ? arg : ms(arg)
|
|
@@ -800,7 +786,7 @@ var TableFormatter = class _TableFormatter {
|
|
|
800
786
|
return this.table.reduce((acc, row) => {
|
|
801
787
|
return row.map((cell, c) => {
|
|
802
788
|
if (typeof cell !== "number") {
|
|
803
|
-
return
|
|
789
|
+
return;
|
|
804
790
|
}
|
|
805
791
|
if (Number.isInteger(cell)) {
|
|
806
792
|
return 0;
|
|
@@ -878,10 +864,10 @@ var TableFormatter = class _TableFormatter {
|
|
|
878
864
|
if (!options.headerRowSeparator) {
|
|
879
865
|
options.headerRowSeparator = _TableFormatter.defaults.headerRowSeparator;
|
|
880
866
|
}
|
|
881
|
-
options.color =
|
|
867
|
+
options.color = options.color ? options.color === true ? _TableFormatter.defaults.color : {
|
|
882
868
|
..._TableFormatter.defaults.color,
|
|
883
869
|
...options.color
|
|
884
|
-
};
|
|
870
|
+
} : defaultColorsNoop;
|
|
885
871
|
return options;
|
|
886
872
|
}
|
|
887
873
|
cellToString(cell, c) {
|