snowflakify
Version:
The most complete Snowflake ID generator in TypeScript
72 lines (71 loc) • 1.83 kB
TypeScript
import { DestructuredFragment } from './@types';
/**
* Base class for fragments.
*/
export default abstract class FragmentBase {
readonly bits: number;
/**
* The current value of the fragment.
*/
value: bigint;
/**
* The maximum value that can be represented by this fragment.
*
* maxValue is equal to `2 ** bits - 1`
* @readonly
*/
readonly maxValue: bigint;
/**
* The number of bits for left and right shifting.
*
* bitShift is equal to the number of bits on the right side
*/
bitShift: bigint;
/**
* The bit mask used to isolate the fragment's bits from the snowflake.
*
* bitMask is equal to `maxValue << bitShift`
*/
bitMask: bigint;
/**
* The hexadecimal representation of the bit mask as string.
*/
bitMaskHex: string;
/**
* The fragment identifier.
*/
identifier: string;
/**
* @param bits - The number of bits for the fragment.
*
* @throws `[BITS_INVALID_TYPE]` If bits is not a number.
* @throws `[BITS_INVALID_RANGE]` If bits is less than 1
*/
constructor(bits: number);
/**
* Returns the value for the fragment.
*
* @remarks
* Do not use this method outside of a Snowflakify instance.
* @internal
*/
abstract getValue(): bigint;
/**
* Returns a destructured fragment.
*
* @remarks
* Do not use this method outside of a Snowflakify instance.
*
* @param snowflake - The snowflake to destructure.
*
* @returns A destructured fragment object with the following properties:
* - `identifier`: The identifier of the ID.
* - `value`: The value of the fragment for that snowflake.
* @internal
*/
abstract destructure(snowflake: number | bigint | string): DestructuredFragment;
/**
* @internal
*/
setBitShiftAndBitMask(bitsRightSide: number): void;
}