(prefix: Prefix, descending: boolean, timestamp?: number)
| 35 | } |
| 36 | |
| 37 | function 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 | |
| 61 | function bytesToHex(bytes: Uint8Array): string { |
| 62 | let hex = "" |
no test coverage detected