@storm-stack/unique-identifier
Version:
This package provides a simple way to generate various types of unique identifiers.
51 lines (50 loc) • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DEFAULT_SHARD_ID = exports.DEFAULT_EPOCH = void 0;
exports.deconstructSnowflake = deconstructSnowflake;
exports.isValidSnowflake = isValidSnowflake;
exports.snowflake = snowflake;
const DEFAULT_SHARD_ID = exports.DEFAULT_SHARD_ID = 1;
const DEFAULT_EPOCH = exports.DEFAULT_EPOCH = Date.UTC(1970, 0, 1).valueOf();
let sequence = 1;
function ToBinaryString(snowflake2) {
const cached64BitZeros = "0000000000000000000000000000000000000000000000000000000000000000";
const binValue = BigInt(snowflake2).toString(2);
return binValue.length < 64 ? cached64BitZeros.slice(0, Math.max(0, 64 - binValue.length)) + binValue : binValue;
}
function extractBits(snowflake2, start, length) {
return Number.parseInt(length ? ToBinaryString(snowflake2).slice(start, start + length) : ToBinaryString(snowflake2).slice(Math.max(0, start)), 2);
}
function snowflake({
shardId,
epoch,
timestamp
}) {
let result = BigInt(timestamp ? timestamp instanceof Date ? timestamp.valueOf() : new Date(timestamp).valueOf() : Date.now()) - BigInt(epoch ?? DEFAULT_EPOCH) << BigInt(22);
result |= BigInt((shardId ?? DEFAULT_SHARD_ID) % 1024) << BigInt(12);
result |= BigInt(sequence++ % 4096);
return result.toString();
}
function deconstructSnowflake(snowflake2) {
const binary = ToBinaryString(snowflake2);
return {
snowflake: snowflake2,
timestamp: extractBits(snowflake2, 1, 41),
shard_id: extractBits(snowflake2, 42, 10),
sequence: extractBits(snowflake2, 52),
binary
};
}
function isValidSnowflake(snowflake2) {
if (!/^\d{19}$/.test(snowflake2)) {
return false;
}
try {
deconstructSnowflake(snowflake2);
return true;
} catch {
return false;
}
}