summaryrefslogtreecommitdiffhomepage
path: root/js/src/id
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-05-26 14:52:38 -0400
committerDax Raad <[email protected]>2025-05-26 14:52:38 -0400
commitb87ba57819a3dfa458b34c9cec9362c7028adf6e (patch)
treee12932e8dd7ac54c92ca337b9c975d7b4b143561 /js/src/id
parent802389a90eaa9a173a98305003b9e58b95584cd1 (diff)
downloadopencode-b87ba57819a3dfa458b34c9cec9362c7028adf6e.tar.gz
opencode-b87ba57819a3dfa458b34c9cec9362c7028adf6e.zip
shutdown
Diffstat (limited to 'js/src/id')
-rw-r--r--js/src/id/id.ts17
1 files changed, 12 insertions, 5 deletions
diff --git a/js/src/id/id.ts b/js/src/id/id.ts
index 0cb1afe9d..62c6a12bf 100644
--- a/js/src/id/id.ts
+++ b/js/src/id/id.ts
@@ -30,11 +30,20 @@ export namespace Identifier {
descending: boolean,
given?: string,
): string {
- if (given) {
- if (given.startsWith(prefixes[prefix])) return given;
+ if (!given) {
+ return generateNewID(prefix, descending);
+ }
+
+ if (!given.startsWith(prefixes[prefix])) {
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`);
}
+ return given;
+ }
+ function generateNewID(
+ prefix: keyof typeof prefixes,
+ descending: boolean,
+ ): string {
const currentTimestamp = Date.now();
if (currentTimestamp !== lastTimestamp) {
@@ -45,9 +54,7 @@ export namespace Identifier {
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter);
- if (descending) {
- now = ~now;
- }
+ now = descending ? ~now : now;
const timeBytes = Buffer.alloc(6);
for (let i = 0; i < 6; i++) {