Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bricss
Last active June 25, 2023 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricss/53e5babedf44bf3ab71334c15102ed0a to your computer and use it in GitHub Desktop.
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
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