Skip to content

Commit 493f993

Browse files
committed
Add & update packages, tests
1 parent d3c1f12 commit 493f993

35 files changed

+1678
-25
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RowID
22

3-
A time based unique ID solution for TypeScript / JavaScript and Node.
3+
RowID, a time based unique ID solution.
44

55
## Install
66

@@ -67,7 +67,7 @@ This function generates a ID based on input.
6767
```typescript
6868
import { generate } from "rowid";
6969

70-
generate(new Date(), 22);
70+
generate(new Date().getTime(), 22);
7171
```
7272

7373
#### `verify`

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
"private": true,
33
"scripts": {
44
"format": "biome check . --apply",
5-
"build": "cd ./packages/rowid && tsc && tsc-alias",
6-
"test": "cd ./packages/__test__ && pnpm run test",
5+
"build": "npm-run-all --sequential build:id build:cli",
6+
"build:id": "cd ./packages/rowid && tsc && tsc-alias",
7+
"build:cli": "cd ./packages/cli && tsc && tsc-alias",
8+
"test": "npm-run-all --sequential test:id test:cli",
9+
"test:id": "cd ./tests/rowid && pnpm run test",
10+
"test:cli": "cd ./tests/cli && pnpm run test",
11+
"gen": "cd ./tests/gen && pnpm run gen",
712
"": ""
813
},
914
"devDependencies": {
1015
"@biomejs/biome": "^1.7.3",
1116
"@types/node": "^20.14.0",
17+
"npm-run-all": "^4.1.5",
1218
"tsc-alias": "^1.8.10",
1319
"typescript": "^5.4.2"
1420
}

packages/cli/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0 (2024-06-04)
2+
3+
First release

packages/cli/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# RowID CLI
2+
3+
CLI for RowID
4+
5+
## Installation
6+
7+
For NPM:
8+
9+
```bash
10+
npx i -g rowid-cli
11+
```
12+
13+
For Pnpm:
14+
15+
```bash
16+
pnpm i -g rowid-cli
17+
```
18+
19+
## Usage
20+
21+
Check the usage of RowID CLI with the following command:
22+
23+
```bash
24+
rowid -h
25+
```
26+
27+
## License
28+
29+
This project is MIT licensed, you can find the license file [here](./LICENSE).

packages/cli/bin/rowid

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require("../dist/index.js");

packages/cli/package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "rowid-cli",
3+
"version": "0.1.0",
4+
"description": "CLI for RowID",
5+
"keywords": [
6+
"RowID",
7+
"CLI",
8+
"time",
9+
"unique",
10+
"id",
11+
"browser",
12+
"Node",
13+
"frontend",
14+
"backend",
15+
"ts",
16+
"TypeScript",
17+
"js",
18+
"JavaScript"
19+
],
20+
"homepage": "https://github.com/alpheustangs/rowid",
21+
"bugs": "https://github.com/alpheustangs/rowid/issues",
22+
"license": "MIT",
23+
"author": "Alpheus Tang",
24+
"files": ["bin", "dist"],
25+
"bin": {
26+
"rowid": "./bin/rowid"
27+
},
28+
"repository": {
29+
"type": "git",
30+
"url": "https://github.com/alpheustangs/rowid.git",
31+
"directory": "packages/cli"
32+
},
33+
"dependencies": {
34+
"commander": "^12.1.0",
35+
"rowid": "workspace:~"
36+
}
37+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type PackageJson = {
2+
name: string;
3+
version: string;
4+
};
5+
6+
export type { PackageJson };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { PackageJson } from "#/@types/packageJson";
2+
3+
import * as fs from "node:fs";
4+
import * as fsp from "node:fs/promises";
5+
import * as path from "node:path";
6+
7+
const readPackageJson = async (): Promise<PackageJson | null> => {
8+
const _path: string = path.resolve(__dirname, "..", "..", "package.json");
9+
10+
if (!fs.existsSync(_path)) return null;
11+
12+
return await fsp
13+
.readFile(_path, "utf-8")
14+
.then((data: string): PackageJson => JSON.parse(data));
15+
};
16+
17+
export type { PackageJson };
18+
export { readPackageJson };

packages/cli/src/index.ts

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import type { PackageJson } from "#/@types/packageJson";
2+
3+
import { Command } from "commander";
4+
import {
5+
RowID,
6+
decode,
7+
encode,
8+
generate,
9+
getRandomDigits,
10+
verify,
11+
} from "rowid";
12+
13+
import { readPackageJson } from "#/functions/readPackageJson";
14+
15+
(async (): Promise<void> => {
16+
try {
17+
// declarations
18+
const program: Command = new Command();
19+
const pkj: PackageJson | null = await readPackageJson();
20+
21+
// RowID
22+
program
23+
.name("rowid")
24+
.description("RowID, a time based unique ID solution")
25+
.version(
26+
`v${pkj ? pkj.version : "0.0.0"}`,
27+
"-v, --version",
28+
"get the version of RowID CLI",
29+
)
30+
.option(
31+
"-n, --number <number>",
32+
"specify the number of random digits",
33+
)
34+
.action(async (args: { number: string }): Promise<void> => {
35+
try {
36+
console.log(
37+
RowID(args.number ? Number(args.number) : void 0),
38+
);
39+
} catch (e: unknown) {
40+
console.error(e);
41+
}
42+
});
43+
44+
// encode
45+
program
46+
.command("encode")
47+
.description("encode a date to RowID")
48+
.argument("<date>", "the date to encode")
49+
.action(async (date: string): Promise<void> => {
50+
try {
51+
const _date: number = new Date(date).getTime();
52+
console.log(encode(_date));
53+
} catch (e: unknown) {
54+
console.error(e);
55+
}
56+
});
57+
58+
// decode
59+
program
60+
.command("decode")
61+
.description("decode a RowID to date")
62+
.argument("<rowid>", "the RowID to decode")
63+
.action(async (rowid: string): Promise<void> => {
64+
try {
65+
console.log(decode(rowid));
66+
} catch (e: unknown) {
67+
console.error(e);
68+
}
69+
});
70+
71+
// generate
72+
program
73+
.command("generate")
74+
.description("generate a RowID")
75+
.argument("<date>", "the date to generate")
76+
.argument("[number]", "the number of random digits")
77+
.action(
78+
async (
79+
date: string,
80+
number: string | undefined,
81+
): Promise<void> => {
82+
try {
83+
console.log(
84+
JSON.stringify(
85+
generate(
86+
new Date(date).getTime(),
87+
number ? Number(number) : void 0,
88+
),
89+
),
90+
);
91+
} catch (e: unknown) {
92+
console.error(e);
93+
}
94+
},
95+
);
96+
97+
// verify
98+
program
99+
.command("verify")
100+
.description("verify a RowID")
101+
.argument("<rowid>", "the RowID to verify")
102+
.action(async (rowid: string): Promise<void> => {
103+
try {
104+
console.log(JSON.stringify(verify(rowid)));
105+
} catch (e: unknown) {
106+
console.error(e);
107+
}
108+
});
109+
110+
// get random digits
111+
program
112+
.command("random")
113+
.description("generate random digits")
114+
.argument("<number>", "the number of random digits")
115+
.action(async (number: string): Promise<void> => {
116+
try {
117+
console.log(getRandomDigits(Number(number)));
118+
} catch (e: unknown) {
119+
console.error(e);
120+
}
121+
});
122+
123+
// parse
124+
await program.parseAsync();
125+
} catch (e: unknown) {
126+
console.log(e);
127+
}
128+
})();

packages/cli/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"target": "ES5",
5+
"module": "CommonJS",
6+
"rootDir": "./src",
7+
"baseUrl": ".",
8+
"paths": {
9+
"#/*": ["./src/*"]
10+
},
11+
"outDir": "./dist"
12+
},
13+
"include": ["./src/**/*"]
14+
}

packages/rowid/CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## 0.2.0 (2024-06-04)
2+
3+
### Breaking Changes
4+
5+
- Update the character list
6+
7+
## 0.1.0 (2024-05-09)
8+
9+
First release

packages/rowid/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "rowid",
3-
"version": "0.1.0",
4-
"description": "A time based unique ID solution for TypeScript / JavaScript and Node",
3+
"version": "0.2.0",
4+
"description": "A time based unique ID solution",
55
"keywords": [
66
"RowID",
77
"time",

packages/rowid/src/configs/common.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const timeDigits: number = 10;
22
const randomDigits: number = 22;
33

4-
const alphabet: string = "123456789ABCDEFGHJKMNPQRSTVWXYZ";
5-
const alphabetLength: number = alphabet.length;
4+
const charList: string = "2346789ABCDEFGHJKMNPQRTVWXYZ";
5+
const charListLength: number = charList.length;
66

7-
export { timeDigits, randomDigits, alphabet, alphabetLength };
7+
export { timeDigits, randomDigits, charList, charListLength };

packages/rowid/src/functions/decode.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { alphabet, alphabetLength, timeDigits } from "#/configs/common";
1+
import { charList, charListLength, timeDigits } from "#/configs/common";
22

33
// decode
44
const decode = (encoded: string): Date => {
@@ -13,7 +13,7 @@ const decode = (encoded: string): Date => {
1313
}
1414

1515
// check regex
16-
if (!encoded.match(new RegExp(`^[${alphabet}]+$`))) {
16+
if (!encoded.match(new RegExp(`^[${charList}]+$`))) {
1717
throw new TypeError("Input is not a valid RowID");
1818
}
1919

@@ -25,7 +25,7 @@ const decode = (encoded: string): Date => {
2525

2626
// decode
2727
for (let i: number = 0; i < timeDigits; i++) {
28-
timestamp = timestamp * alphabetLength + alphabet.indexOf(_encoded[i]);
28+
timestamp = timestamp * charListLength + charList.indexOf(_encoded[i]);
2929
}
3030

3131
// result

packages/rowid/src/functions/encode.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { alphabet, alphabetLength, timeDigits } from "#/configs/common";
1+
import { charList, charListLength, timeDigits } from "#/configs/common";
22

33
const encodeFn = (timestamp: number): string => {
44
// remaining
@@ -8,8 +8,8 @@ const encodeFn = (timestamp: number): string => {
88
let encoded: string = "";
99

1010
for (let i: number = 0; i < timeDigits; i++) {
11-
encoded = alphabet[remaining % alphabetLength] + encoded;
12-
remaining = Math.floor(remaining / alphabetLength);
11+
encoded = charList[remaining % charListLength] + encoded;
12+
remaining = Math.floor(remaining / charListLength);
1313
}
1414

1515
// result

packages/rowid/src/functions/getRandomDigits.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { alphabet, alphabetLength } from "#/configs/common";
1+
import { charList, charListLength } from "#/configs/common";
22

33
const getRandomByte = (): number => {
44
// use window.crypto when available
@@ -33,7 +33,7 @@ const randomDigitsFn = (count: number): string => {
3333

3434
// implementation
3535
for (let i: number = 0; i < count; i++) {
36-
randomDigits += alphabet[getRandomByte() % alphabetLength];
36+
randomDigits += charList[getRandomByte() % charListLength];
3737
}
3838

3939
// result

0 commit comments

Comments
 (0)