@pluslabs/utils 0.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/package.json +57 -0
- package/src/index.ts +1 -0
- package/src/stamp/index.ts +49 -0
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluslabs/utils",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A set of utilities used across projects",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"javascript",
|
|
7
|
+
"typescript",
|
|
8
|
+
"pluslabs",
|
|
9
|
+
"magic"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/pluslabs/utils",
|
|
12
|
+
"bugs": "https://github.com/pluslabs/utils/issues",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Magic, Inc",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"main": "dist/main/index.js",
|
|
20
|
+
"module": "dist/module/index.js",
|
|
21
|
+
"types": "dist/module/index.d.ts",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"repository": "pluslabs/utils",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"clean": "rimraf dist",
|
|
26
|
+
"format": "prettier --write \"{src,test}/**/*.ts\"",
|
|
27
|
+
"build": "run-s clean format build:*",
|
|
28
|
+
"build:main": "tsc -p tsconfig.json",
|
|
29
|
+
"build:module": "tsc -p tsconfig.module.json",
|
|
30
|
+
"test": "run-s test:types test:run",
|
|
31
|
+
"test:run": "jest --runInBand",
|
|
32
|
+
"test:watch": "jest --watch --verbose false --silent false",
|
|
33
|
+
"test:types": "run-s build:module && tsd --files test/*.test-d.ts",
|
|
34
|
+
"docs": "typedoc --entryPoints src/index.ts --out docs --includes src/**/*.ts",
|
|
35
|
+
"docs:json": "typedoc --entryPoints src/index.ts --includes src/**/*.ts --json docs/spec.json --excludeExternals"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/jest": "^29.2.5",
|
|
39
|
+
"husky": "^4.3.0",
|
|
40
|
+
"jest": "^29.3.1",
|
|
41
|
+
"npm-run-all": "^4.1.5",
|
|
42
|
+
"prettier": "^2.5.1",
|
|
43
|
+
"pretty-quick": "^3.1.3",
|
|
44
|
+
"rimraf": "^3.0.2",
|
|
45
|
+
"ts-jest": "^29.0.5",
|
|
46
|
+
"ts-loader": "^8.0.11",
|
|
47
|
+
"ts-node": "^10.9.1",
|
|
48
|
+
"tsd": "^0.30.4",
|
|
49
|
+
"typedoc": "^0.22.16",
|
|
50
|
+
"typescript": "^4.5.5"
|
|
51
|
+
},
|
|
52
|
+
"husky": {
|
|
53
|
+
"hooks": {
|
|
54
|
+
"pre-commit": "pretty-quick --staged"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './stamp';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const context: {
|
|
2
|
+
id: string;
|
|
3
|
+
idx: string;
|
|
4
|
+
step: number;
|
|
5
|
+
now: number;
|
|
6
|
+
} = {
|
|
7
|
+
id: '',
|
|
8
|
+
idx: '',
|
|
9
|
+
step: 0,
|
|
10
|
+
now: 0
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
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.
|
|
15
|
+
* `createStamp` should be called at the beginning of the execution context and then createStamp
|
|
16
|
+
* @param id String to identify the stamp
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export const createStamp = (id = '') => {
|
|
20
|
+
if (!context.id) {
|
|
21
|
+
context.id = id;
|
|
22
|
+
context.idx = (Date.now() * Math.random() * 1e6).toString(36);
|
|
23
|
+
context.now = Date.now();
|
|
24
|
+
context.step = Date.now();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return stamp;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function stamp(name: string) {
|
|
31
|
+
if (!context.id) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const diff = Date.now() - context.step;
|
|
36
|
+
context.step = Date.now();
|
|
37
|
+
|
|
38
|
+
console.log(
|
|
39
|
+
`[STAMP][${context.idx}][${context.id}] ${name} took ${diff}ms, total: ${
|
|
40
|
+
Date.now() - context.now
|
|
41
|
+
}ms`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function end() {
|
|
46
|
+
context.id = '';
|
|
47
|
+
context.now = 0;
|
|
48
|
+
context.step = 0;
|
|
49
|
+
}
|