(prefix: keyof typeof prefixes, descending: boolean, timestamp?: number)
| 51 | } |
| 52 | |
| 53 | export function create(prefix: keyof typeof prefixes, descending: boolean, timestamp?: number): string { |
| 54 | const currentTimestamp = timestamp ?? Date.now() |
| 55 | |
| 56 | if (currentTimestamp !== lastTimestamp) { |
| 57 | lastTimestamp = currentTimestamp |
| 58 | counter = 0 |
| 59 | } |
| 60 | counter++ |
| 61 | |
| 62 | let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter) |
| 63 | |
| 64 | now = descending ? ~now : now |
| 65 | |
| 66 | const timeBytes = Buffer.alloc(6) |
| 67 | for (let i = 0; i < 6; i++) { |
| 68 | timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff)) |
| 69 | } |
| 70 | |
| 71 | return prefixes[prefix] + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12) |
| 72 | } |
| 73 | } |
no test coverage detected