(descending: boolean, timestamp = Date.now())
| 12 | } |
| 13 | |
| 14 | export function create(descending: boolean, timestamp = Date.now()) { |
| 15 | if (timestamp !== lastTimestamp) { |
| 16 | lastTimestamp = timestamp |
| 17 | counter = 0 |
| 18 | } |
| 19 | counter++ |
| 20 | |
| 21 | const current = BigInt(timestamp) * 0x1000n + BigInt(counter) |
| 22 | const value = descending ? ~current : current |
| 23 | const time = Array.from({ length: 6 }, (_, index) => |
| 24 | Number((value >> BigInt(40 - 8 * index)) & 0xffn) |
| 25 | .toString(16) |
| 26 | .padStart(2, "0"), |
| 27 | ).join("") |
| 28 | const bytes = crypto.getRandomValues(new Uint8Array(length - 12)) |
| 29 | return time + Array.from(bytes, (byte) => chars[byte % 62]).join("") |
| 30 | } |
no test coverage detected