snowflakify
Version:
The most complete Snowflake ID generator in TypeScript
33 lines (32 loc) • 944 B
JavaScript
/**
* Base class for fragments.
*/
export default class FragmentBase {
/**
* @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) {
this.bits = bits;
if (typeof bits !== 'number')
throw new TypeError('[BITS_INVALID_TYPE]: Fragment bits must be a number.');
if (bits < 1)
throw new RangeError(
'[BITS_INVALID_RANGE]: Fragment bits must be greater than 0',
);
this.value = BigInt(0);
this.maxValue = BigInt(2 ** bits) - BigInt(1);
this.identifier = this.constructor.name;
}
/**
* @internal
*/
setBitShiftAndBitMask(bitsRightSide) {
this.bitShift = BigInt(bitsRightSide);
this.bitMask = this.maxValue << this.bitShift;
this.bitMaskHex = `0x${this.bitMask.toString(16)}`;
}
}
//# sourceMappingURL=FragmentBase.js.map