@jsnw/common-utils 1.0.0 → 1.2.0

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.
@@ -1,71 +0,0 @@
1
- export class Monitor {
2
- marks = new Map();
3
- /**
4
- * @template {(...args: any[]) => any} F
5
- * @param {F} fn
6
- * @returns {ReturnType<F> extends Promise<any> ? Promise<number> : number}
7
- */
8
- static measureFunctionTime(fn) {
9
- const timeStart = Date.now();
10
- const promiseOrResult = fn();
11
- if (promiseOrResult instanceof Promise)
12
- return Promise.resolve(promiseOrResult)
13
- .then(() => Date.now() - timeStart);
14
- return (Date.now() - timeStart);
15
- }
16
- /**
17
- * @param {string} label
18
- * @returns {Monitor}
19
- */
20
- static autoStart(label) {
21
- const instance = new Monitor();
22
- instance.start(label);
23
- return instance;
24
- }
25
- constructor() { }
26
- /**
27
- * @param {string} label
28
- */
29
- start(label) {
30
- if (!this.marks.has(label)) {
31
- this.marks.set(label, [[Date.now()]]);
32
- }
33
- else {
34
- return;
35
- }
36
- }
37
- /**
38
- * @param {string} label
39
- * @param {string} [description]
40
- */
41
- mark(label, description) {
42
- if (!this.marks.has(label))
43
- throw new TypeError(`Label ${label} not found`);
44
- this.marks.get(label).push([Date.now(), description]);
45
- }
46
- /**
47
- * @param {string} label
48
- * @returns {MonitorResult}
49
- */
50
- stop(label) {
51
- const endTime = Date.now();
52
- const marks = this.marks.get(label);
53
- if (!marks)
54
- throw new TypeError(`Label ${label} not found`);
55
- const [firstTs] = marks[0], processedMarks = [];
56
- for (let i = 1; i < marks.length; i++) {
57
- processedMarks.push({
58
- ts: marks[i][0],
59
- diff: marks[i][0] - firstTs,
60
- description: marks[i][1]
61
- });
62
- }
63
- this.marks.delete(label);
64
- return {
65
- marks: processedMarks,
66
- startTs: firstTs,
67
- stopTs: endTime,
68
- totalTime: endTime - firstTs
69
- };
70
- }
71
- }
package/dist/esm/noop.js DELETED
@@ -1,8 +0,0 @@
1
- /**
2
- * A no-operation function that performs no action.
3
- * This function can be used as a placeholder or default value
4
- * where a function is required but no operation is necessary.
5
- *
6
- * @returns {void}
7
- */
8
- export const noop = () => { };
@@ -1,20 +0,0 @@
1
- /**
2
- * Executes a function and returns its result as a tuple `[value, error]`.
3
- * Works with both sync and async functions. Never throws.
4
- *
5
- * @template {() => any} F
6
- * @template {Error} E
7
- * @param {F} fn
8
- */
9
- export function resultify(fn) {
10
- try {
11
- const promiseOrResult = fn();
12
- if (promiseOrResult instanceof Promise) {
13
- return promiseOrResult.then(res => [res, null]).catch(err => [null, err]);
14
- }
15
- return [promiseOrResult, null];
16
- }
17
- catch (e) {
18
- return [null, e];
19
- }
20
- }
package/dist/esm/wait.js DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * Waits asynchronously for the specified amount of milliseconds before resolving.
3
- *
4
- * @param {number} wait_ms - The number of milliseconds to wait. If less than 1, the promise resolves immediately.
5
- * @param {AbortSignal} [signal]
6
- * @returns {Promise<void>} A promise that resolves after the specified wait time.
7
- */
8
- export const wait = (wait_ms, signal) => {
9
- if (wait_ms < 1)
10
- return Promise.resolve();
11
- if (!Number.isFinite(wait_ms) && !signal)
12
- throw new TypeError('Invalid wait_ms: must be a finite number (or you should provide an AbortSignal as a second argument)');
13
- if (signal?.aborted)
14
- return Promise.resolve();
15
- if (!signal)
16
- return new Promise(resolve => setTimeout(resolve, wait_ms));
17
- return new Promise(resolve => {
18
- let timer_ref = null;
19
- const on_abort = () => {
20
- if (timer_ref)
21
- clearTimeout(timer_ref);
22
- resolve();
23
- };
24
- signal.addEventListener('abort', on_abort, { once: true });
25
- timer_ref = setTimeout(() => {
26
- signal.removeEventListener('abort', on_abort);
27
- resolve();
28
- }, wait_ms);
29
- });
30
- };
@@ -1,9 +0,0 @@
1
- /**
2
- * Splits an array into smaller arrays of a specified size.
3
- *
4
- * @template T
5
- * @param {T[]} arr - The array to be split into chunks.
6
- * @param {number} chunk_size - The size of each chunk.
7
- * @returns {T[][]} An array containing the chunks.
8
- */
9
- export declare const chunk: <T extends any>(arr: T[], chunk_size: number) => T[][];
@@ -1,8 +0,0 @@
1
- /**
2
- * Splits a string into chunks of a specified size
3
- *
4
- * @param {string} str - The string to be split into chunks
5
- * @param {number} chunkSize - The size of each chunk
6
- * @returns {string[]} An array containing the chunks
7
- */
8
- export declare function chunkStr(str: string, chunkSize: number): string[];
@@ -1,10 +0,0 @@
1
- export type InFlightDeduper = {
2
- use: <F extends () => any>(key: string, fn: F) => Promise<Awaited<ReturnType<F>>>;
3
- };
4
- /**
5
- * Creates a deduplicator for in-flight promises or sync functions.
6
- * Prevents duplicate executions for the same key while a promise is pending.
7
- * Clears the entry after settlement.
8
- * @returns {InFlightDeduper}
9
- */
10
- export declare function createInFlightDeduper(): InFlightDeduper;
@@ -1,6 +0,0 @@
1
- /**
2
- * Accepts a promise and ensures that its resolution or rejection is ignored.
3
- *
4
- * @param {Promise<any>} promise - The promise to be ignored.
5
- */
6
- export declare const ignorePromise: (promise: Promise<any>) => void;
@@ -1,9 +0,0 @@
1
- export * from './noop';
2
- export * from './ignorePromise';
3
- export * from './chunk';
4
- export * from './chunkStr';
5
- export * from './wait';
6
- export * from './isObjectEmpty';
7
- export * from './createInFlightDeduper';
8
- export * from './resultify';
9
- export * from './monitor';
@@ -1,5 +0,0 @@
1
- /**
2
- * @param {object} obj
3
- * @returns {boolean}
4
- */
5
- export declare function isObjectEmpty(obj: object): boolean;
@@ -1,40 +0,0 @@
1
- export type MonitorMark = {
2
- ts: number;
3
- diff: number;
4
- description?: string;
5
- };
6
- export type MonitorResult = {
7
- marks: MonitorMark[];
8
- startTs: number;
9
- stopTs: number;
10
- totalTime: number;
11
- };
12
- export declare class Monitor {
13
- private readonly marks;
14
- /**
15
- * @template {(...args: any[]) => any} F
16
- * @param {F} fn
17
- * @returns {ReturnType<F> extends Promise<any> ? Promise<number> : number}
18
- */
19
- static measureFunctionTime<F extends (...args: any[]) => any>(fn: F): ReturnType<F> extends Promise<any> ? Promise<number> : number;
20
- /**
21
- * @param {string} label
22
- * @returns {Monitor}
23
- */
24
- static autoStart(label: string): Monitor;
25
- constructor();
26
- /**
27
- * @param {string} label
28
- */
29
- start(label: string): void;
30
- /**
31
- * @param {string} label
32
- * @param {string} [description]
33
- */
34
- mark(label: string, description?: string): void;
35
- /**
36
- * @param {string} label
37
- * @returns {MonitorResult}
38
- */
39
- stop(label: string): MonitorResult;
40
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * A no-operation function that performs no action.
3
- * This function can be used as a placeholder or default value
4
- * where a function is required but no operation is necessary.
5
- *
6
- * @returns {void}
7
- */
8
- export declare const noop: () => void;
@@ -1,10 +0,0 @@
1
- export type ErrorResult<R, E> = readonly [R, null] | readonly [null, E];
2
- /**
3
- * Executes a function and returns its result as a tuple `[value, error]`.
4
- * Works with both sync and async functions. Never throws.
5
- *
6
- * @template {() => any} F
7
- * @template {Error} E
8
- * @param {F} fn
9
- */
10
- export declare function resultify<F extends () => any, E = Error>(fn: F): ReturnType<F> extends Promise<infer R> ? Promise<ErrorResult<R, E>> : ErrorResult<ReturnType<F>, E>;
@@ -1,8 +0,0 @@
1
- /**
2
- * Waits asynchronously for the specified amount of milliseconds before resolving.
3
- *
4
- * @param {number} wait_ms - The number of milliseconds to wait. If less than 1, the promise resolves immediately.
5
- * @param {AbortSignal} [signal]
6
- * @returns {Promise<void>} A promise that resolves after the specified wait time.
7
- */
8
- export declare const wait: (wait_ms: number, signal?: AbortSignal) => Promise<void>;