Last active
June 25, 2023 12:04
-
-
Save bricss/53e5babedf44bf3ab71334c15102ed0a to your computer and use it in GitHub Desktop.
Date-relative (and relatively universally unique) UUID generation. Inspired by: https://www.npmjs.com/package/druuid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const toBigInt = (value, radix = 36) => { | |
return [...value.toString()].reduce((acc, val) => (acc * BigInt(radix)) + BigInt(parseInt(val, radix)), 0n); | |
}; | |
export const gen = (date = Date.now()) => { | |
return ((BigInt(date) << (64n - 41n)) ^ (BigInt(crypto.getRandomValues(new Uint8Array(32)) | |
.join('')) % (2n ** (64n - 41n)))); | |
}; | |
export const time = (uuid) => { | |
return new Date(Number((BigInt(uuid) >> (64n - 41n)))); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment