onenex-snowflake
Version:
Library to help you create a Snowflake Id or parse the same. This solves the problem of generating unique identifiers at scale by Onenex.
84 lines (83 loc) • 2.63 kB
TypeScript
/**
* Interface of a Snowflake after `Generator.deconstruct()`.
* @property {bigint} snowflake - Snowflake deconstructed from
* @property {bigint} timestamp - The timestamp the snowflake was generated
* @property {bigint} shard_id - The shard_id used when generating
* @property {bigint} increment - The increment of this snowflake
* @property {string} binary - The 64Bit snowflake binary string
* @interface DeconstructedSnowflake
*/
declare interface DeconstructedSnowflake {
timestamp: number;
shard_id: number;
sequence: number;
binary: string;
}
export declare class Snowflake {
/**
* The generators epoch timestamp in milliseconds.
*
* Defaults to "1st of January, 2000, 00:00".
*
* @type {number}
*/
readonly EPOCH: number;
/**
* The id of the shard running this generator.
*
* Defaults to "1".
*
* @type {number}
*/
readonly SHARD_ID: number;
/**
* The sequence of the current running generator.
*
* Defaults to "1".
*
* @type {number}
*/
_SEQUENCE: number;
/**
* Generates a single snowflake.
* @param {Date|number} [timestamp = Date.now] - Timestamp to generate from
* @returns {bigint}
*/
/**
* Generates a single snowflake binary string.
*/
private readonly cached64BitZeros;
get getSequence(): number;
set setSequence(value: number);
generate({ timestamp, shard_id, }?: {
timestamp?: Date | number;
shard_id?: number;
}): string;
generateShortId({ timestamp, shard_id, }?: {
timestamp?: Date | number;
shard_id?: number;
}): string;
/**
* Deconstruct a snowflake to its values using the `Generator.epoch`.
* @param {string|string[]} snowflake - Snowflake(s) to deconstruct
* @returns {DeconstructedSnowflake|DeconstructedSnowflake[]}
*/
parse(snowflake: string): DeconstructedSnowflake;
isValid(snowflake: string): boolean;
/**
* Extract bits and their values from a snowflake.
* @param {string} snowflake - Snowflake to extract from
* @param {number|bigint} shift - Number of bits to shift before extracting
* @param {number|bigint} length - Number of bits to extract before stopping
* @returns {bigint}
*/
extractBits(snowflake: string, start: number, length?: number): number;
/**
* Transform a snowflake into its 64Bit binary string.
* @param {string} snowflake - Snowflake to transform
* @returns {string}
* @private
*/
binary(snowflake: string): string;
}
export {};