snowflakify
Version:
The most complete Snowflake ID generator in TypeScript
36 lines (35 loc) • 1.04 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
/**
* Base class for fragments.
*/
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)}`;
}
}
exports.default = FragmentBase;
//# sourceMappingURL=FragmentBase.js.map