tatr-ql 0.1.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.
- package/README.md +2 -0
- package/package.json +32 -0
- package/src/huid.js +37 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tatr-ql",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Task Tracker (Nodejs Version)",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"task",
|
|
7
|
+
"tracker",
|
|
8
|
+
"tater",
|
|
9
|
+
"tql"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/zakarialaoui10/tatr#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/zakarialaoui10/tatr/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/zakarialaoui10/tatr.git"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "zakaria elalaoui",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "index.js",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"ziko": "^2.0.0-beta.10"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/huid.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { readdir } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
export const HUID_RE = /^\d{8}-\d{6}(?:-[A-Za-z0-9-]*)?$/;
|
|
5
|
+
|
|
6
|
+
const utcStamp=(date = new Date())=>{
|
|
7
|
+
const p = (n, width) => String(n).padStart(width, "0");
|
|
8
|
+
return (
|
|
9
|
+
`${date.getUTCFullYear()}${p(date.getUTCMonth() + 1, 2)}${p(date.getUTCDate(), 2)}` +
|
|
10
|
+
`-${p(date.getUTCHours(), 2)}${p(date.getUTCMinutes(), 2)}${p(date.getUTCSeconds(), 2)}`
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const isHuid = value => HUID_RE.test(value)
|
|
15
|
+
|
|
16
|
+
export const createHuid = ({ suffix } = {}) =>{
|
|
17
|
+
const base = utcStamp();
|
|
18
|
+
if (suffix == null || suffix === "") return base;
|
|
19
|
+
if (!/^[A-Za-z0-9-]+$/.test(suffix)) {
|
|
20
|
+
throw new Error("HUID suffix may contain only letters, numbers, and hyphens");
|
|
21
|
+
}
|
|
22
|
+
return `${base}-${suffix}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const uniqueHuid = async (tasksDir, options = {}) =>{
|
|
26
|
+
let huid = createHuid(options);
|
|
27
|
+
while (true) {
|
|
28
|
+
try {
|
|
29
|
+
await readdir(join(tasksDir, huid));
|
|
30
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
31
|
+
huid = createHuid(options);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (error.code === "ENOENT") return huid;
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|