MCPcopy Index your code
hub / github.com/anomalyco/opencode / create

Function create

packages/app/src/utils/id.ts:37–59  ·  view source on GitHub ↗
(prefix: Prefix, descending: boolean, timestamp?: number)

Source from the content-addressed store, hash-verified

35}
36
37function create(prefix: Prefix, descending: boolean, timestamp?: number): string {
38 const currentTimestamp = timestamp ?? Date.now()
39
40 if (currentTimestamp !== lastTimestamp) {
41 lastTimestamp = currentTimestamp
42 counter = 0
43 }
44
45 counter += 1
46
47 let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
48
49 if (descending) {
50 now = ~now
51 }
52
53 const timeBytes = new Uint8Array(6)
54 for (let i = 0; i < 6; i += 1) {
55 timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
56 }
57
58 return prefixes[prefix] + "_" + bytesToHex(timeBytes) + randomBase62(LENGTH - 12)
59}
60
61function bytesToHex(bytes: Uint8Array): string {
62 let hex = ""

Callers 2

createRefCountMapFunction · 0.70
generateIDFunction · 0.70

Calls 2

bytesToHexFunction · 0.85
randomBase62Function · 0.70

Tested by

no test coverage detected