dressed
Version:
A sleek, serverless-ready Discord bot framework.
20 lines • 833 B
JavaScript
const DISCORD_EPOCH = BigInt(1420070400000);
/** Serialize data into a snowflake */
export function encodeSnowflake({ timestamp, worker, process, increment, }) {
const snowflake = ((BigInt(timestamp) - DISCORD_EPOCH) << BigInt(22)) |
(BigInt(worker & 0b11111) << BigInt(17)) |
(BigInt(process & 0b11111) << BigInt(12)) |
BigInt(increment & 0xfff);
return snowflake.toString();
}
/** Deserialize the contents of a snowflake */
export function decodeSnowflake(snowflake) {
const id = BigInt(snowflake);
return {
timestamp: Number((id >> BigInt(22)) + DISCORD_EPOCH),
worker: Number((id & BigInt(0x3e0000)) >> BigInt(17)),
process: Number((id & BigInt(0x1f000)) >> BigInt(12)),
increment: Number(id & BigInt(0xfff)),
};
}
//# sourceMappingURL=snowflakes.js.map