@pluslabs/utils 0.4.1 → 0.4.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.
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
type
|
|
1
|
+
type TLogger = {
|
|
2
|
+
info: (message: string) => void;
|
|
3
|
+
};
|
|
4
|
+
type StampOptions = {
|
|
5
|
+
enableInProduction?: boolean;
|
|
6
|
+
};
|
|
7
|
+
declare const context: {
|
|
2
8
|
id: string;
|
|
3
9
|
idx: string;
|
|
4
10
|
step: number;
|
|
5
11
|
now: number;
|
|
6
12
|
end: () => void;
|
|
13
|
+
logger?: TLogger;
|
|
7
14
|
};
|
|
8
15
|
/**
|
|
9
16
|
* The idea is that you can create a stamp with an id, and then use that id to mark different steps in your code.
|
|
@@ -11,13 +18,42 @@ type context = {
|
|
|
11
18
|
* @param id String to identify the stamp
|
|
12
19
|
* @returns
|
|
13
20
|
*/
|
|
14
|
-
export declare const createStamp: (id?: string) => {
|
|
15
|
-
stamp: (name: string) =>
|
|
16
|
-
|
|
21
|
+
export declare const createStamp: (id?: string, logger?: TLogger, options?: StampOptions) => {
|
|
22
|
+
stamp: (name: string) => {
|
|
23
|
+
id: string;
|
|
24
|
+
idx: string;
|
|
25
|
+
step: number;
|
|
26
|
+
now: number;
|
|
27
|
+
end: () => void;
|
|
28
|
+
logger?: TLogger;
|
|
29
|
+
};
|
|
30
|
+
end: () => {
|
|
31
|
+
id: string;
|
|
32
|
+
idx: string;
|
|
33
|
+
step: number;
|
|
34
|
+
now: number;
|
|
35
|
+
end: () => void;
|
|
36
|
+
logger?: TLogger;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export declare function stamp(this: typeof context, name: string): {
|
|
40
|
+
id: string;
|
|
41
|
+
idx: string;
|
|
42
|
+
step: number;
|
|
43
|
+
now: number;
|
|
44
|
+
end: () => void;
|
|
45
|
+
logger?: TLogger;
|
|
17
46
|
};
|
|
18
|
-
export declare function stamp(this: context, name: string): context;
|
|
19
47
|
export declare namespace stamp {
|
|
20
|
-
var end: (
|
|
48
|
+
var end: typeof import("./index.js").end;
|
|
21
49
|
}
|
|
50
|
+
export declare function end(this: typeof context): {
|
|
51
|
+
id: string;
|
|
52
|
+
idx: string;
|
|
53
|
+
step: number;
|
|
54
|
+
now: number;
|
|
55
|
+
end: () => void;
|
|
56
|
+
logger?: TLogger;
|
|
57
|
+
};
|
|
22
58
|
export {};
|
|
23
59
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/stamp/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/stamp/index.ts"],"names":[],"mappings":"AAEA,KAAK,OAAO,GAAG;IACX,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC,CAAC;AAEF,KAAK,YAAY,GAAG;IAChB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,QAAA,MAAM,OAAO,EAAE;IACX,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,IAAI,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;CAQpB,CAAC;AAMF;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,WAAO,EAAE,SAAS,OAAO,EAAE,UAAU,YAAY;;YAzBrE,MAAM;aACL,MAAM;cACL,MAAM;aACP,MAAM;aACN,MAAM,IAAI;iBACN,OAAO;;;YALZ,MAAM;aACL,MAAM;cACL,MAAM;aACP,MAAM;aACN,MAAM,IAAI;iBACN,OAAO;;CAsCnB,CAAC;AAEF,wBAAgB,KAAK,CAAC,IAAI,EAAE,OAAO,OAAO,EAAE,IAAI,EAAE,MAAM;QA7ChD,MAAM;SACL,MAAM;UACL,MAAM;SACP,MAAM;SACN,MAAM,IAAI;aACN,OAAO;EA2DnB;yBAnBe,KAAK;;;AAuBrB,wBAAgB,GAAG,CAAC,IAAI,EAAE,OAAO,OAAO;QApEhC,MAAM;SACL,MAAM;UACL,MAAM;SACP,MAAM;SACN,MAAM,IAAI;aACN,OAAO;EAuEnB"}
|
|
@@ -1,18 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const isProd = process.env.NODE_ENV === 'production';
|
|
2
|
+
const context = {
|
|
3
|
+
id: '',
|
|
4
|
+
idx: '',
|
|
5
|
+
step: 0,
|
|
6
|
+
now: 0,
|
|
7
|
+
end: () => { },
|
|
8
|
+
logger: undefined
|
|
9
|
+
};
|
|
10
|
+
const noopLogger = {
|
|
11
|
+
info: () => { }
|
|
12
|
+
};
|
|
3
13
|
/**
|
|
4
14
|
* The idea is that you can create a stamp with an id, and then use that id to mark different steps in your code.
|
|
5
15
|
* `createStamp` should be called at the beginning of the execution context and then createStamp
|
|
6
16
|
* @param id String to identify the stamp
|
|
7
17
|
* @returns
|
|
8
18
|
*/
|
|
9
|
-
export const createStamp = (id = '') => {
|
|
19
|
+
export const createStamp = (id = '', logger, options) => {
|
|
10
20
|
const ctx = {
|
|
11
21
|
id,
|
|
12
22
|
idx: (Date.now() * Math.random() * 1e6).toString(36),
|
|
13
23
|
now: Date.now(),
|
|
14
24
|
step: Date.now(),
|
|
15
|
-
end: () => { }
|
|
25
|
+
end: () => { },
|
|
26
|
+
logger: logger || (!(options === null || options === void 0 ? void 0 : options.enableInProduction) && !isProd ? undefined : noopLogger)
|
|
16
27
|
};
|
|
17
28
|
// Bind the end function to the context
|
|
18
29
|
// so we can curry it
|
|
@@ -28,11 +39,18 @@ export function stamp(name) {
|
|
|
28
39
|
}
|
|
29
40
|
const diff = Date.now() - this.step;
|
|
30
41
|
this.step = Date.now();
|
|
31
|
-
|
|
42
|
+
const delta = Date.now() - this.now;
|
|
43
|
+
const message = `[STAMP][${this.idx}][${this.id}] [step=${name}] [duration=${diff}ms] [total=${delta}ms]`;
|
|
44
|
+
if (this.logger) {
|
|
45
|
+
this.logger.info(message);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
console.log(message);
|
|
49
|
+
}
|
|
32
50
|
return this;
|
|
33
51
|
}
|
|
34
52
|
stamp.end = end;
|
|
35
|
-
function end() {
|
|
53
|
+
export function end() {
|
|
36
54
|
this.id = '';
|
|
37
55
|
this.now = 0;
|
|
38
56
|
this.step = 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/stamp/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/stamp/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC;AAUrD,MAAM,OAAO,GAOT;IACA,EAAE,EAAE,EAAE;IACN,GAAG,EAAE,EAAE;IACP,IAAI,EAAE,CAAC;IACP,GAAG,EAAE,CAAC;IACN,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;IACb,MAAM,EAAE,SAAS;CACpB,CAAC;AAEF,MAAM,UAAU,GAAY;IACxB,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;CACjB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,MAAgB,EAAE,OAAsB,EAAE,EAAE;IAC7E,MAAM,GAAG,GAAG;QACR,EAAE;QACF,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;QACpD,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE;QACf,IAAI,EAAE,IAAI,CAAC,GAAG,EAAE;QAChB,G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AAE,GAAG,EAAE,CAAC;IACb,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC;IACb,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAEd,MAAM,GAAG,GAAG,IAAsB,CAAC;IAEnC,OAAO,GAAG,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
package/src/stamp/index.ts
CHANGED
|
@@ -1,12 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
// since yarn cant do subdirectories from a monorepo
|
|
1
|
+
const isProd = process.env.NODE_ENV === 'production';
|
|
3
2
|
|
|
4
|
-
type
|
|
3
|
+
type TLogger = {
|
|
4
|
+
info: (message: string) => void;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type StampOptions = {
|
|
8
|
+
enableInProduction?: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const context: {
|
|
5
12
|
id: string;
|
|
6
13
|
idx: string;
|
|
7
14
|
step: number;
|
|
8
15
|
now: number;
|
|
9
16
|
end: () => void;
|
|
17
|
+
logger?: TLogger;
|
|
18
|
+
} = {
|
|
19
|
+
id: '',
|
|
20
|
+
idx: '',
|
|
21
|
+
step: 0,
|
|
22
|
+
now: 0,
|
|
23
|
+
end: () => {},
|
|
24
|
+
logger: undefined
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const noopLogger: TLogger = {
|
|
28
|
+
info: () => {}
|
|
10
29
|
};
|
|
11
30
|
|
|
12
31
|
/**
|
|
@@ -15,14 +34,15 @@ type context = {
|
|
|
15
34
|
* @param id String to identify the stamp
|
|
16
35
|
* @returns
|
|
17
36
|
*/
|
|
18
|
-
export const createStamp = (id = '') => {
|
|
37
|
+
export const createStamp = (id = '', logger?: TLogger, options?: StampOptions) => {
|
|
19
38
|
const ctx = {
|
|
20
39
|
id,
|
|
21
40
|
idx: (Date.now() * Math.random() * 1e6).toString(36),
|
|
22
41
|
now: Date.now(),
|
|
23
42
|
step: Date.now(),
|
|
24
|
-
end: () => {}
|
|
25
|
-
|
|
43
|
+
end: () => {},
|
|
44
|
+
logger: logger || (!options?.enableInProduction && !isProd ? undefined : noopLogger)
|
|
45
|
+
};
|
|
26
46
|
|
|
27
47
|
// Bind the end function to the context
|
|
28
48
|
// so we can curry it
|
|
@@ -34,29 +54,35 @@ export const createStamp = (id = '') => {
|
|
|
34
54
|
};
|
|
35
55
|
};
|
|
36
56
|
|
|
37
|
-
export function stamp(this: context, name: string) {
|
|
57
|
+
export function stamp(this: typeof context, name: string) {
|
|
38
58
|
if (!this.id) {
|
|
39
59
|
return this;
|
|
40
60
|
}
|
|
41
61
|
|
|
42
62
|
const diff = Date.now() - this.step;
|
|
63
|
+
|
|
43
64
|
this.step = Date.now();
|
|
65
|
+
const delta = Date.now() - this.now;
|
|
44
66
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
)
|
|
67
|
+
const message = `[STAMP][${this.idx}][${this.id}] [step=${name}] [duration=${diff}ms] [total=${delta}ms]`;
|
|
68
|
+
|
|
69
|
+
if (this.logger) {
|
|
70
|
+
this.logger.info(message);
|
|
71
|
+
} else {
|
|
72
|
+
console.log(message);
|
|
73
|
+
}
|
|
48
74
|
|
|
49
75
|
return this;
|
|
50
76
|
}
|
|
51
77
|
|
|
52
78
|
stamp.end = end;
|
|
53
79
|
|
|
54
|
-
function end(this: context) {
|
|
80
|
+
export function end(this: typeof context) {
|
|
55
81
|
this.id = '';
|
|
56
82
|
this.now = 0;
|
|
57
83
|
this.step = 0;
|
|
58
84
|
|
|
59
|
-
const ctx = this as context;
|
|
85
|
+
const ctx = this as typeof context;
|
|
60
86
|
|
|
61
87
|
return ctx;
|
|
62
88
|
}
|