UNPKG

snowflakify

Version:

The most complete Snowflake ID generator in TypeScript

59 lines 2.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto_1 = require("crypto"); const FragmentBase_js_1 = __importDefault(require("../FragmentBase.js")); /** * RandomFragment class for random IDs. * @public */ class RandomFragment extends FragmentBase_js_1.default { /** * @remarks * When using a custom random function, please ensure it returns a positive number * no greater than `2 ** bits - 1`. * * @param bits - The number of bits for the fragment. * @param fn - Optional custom random function. * * @throws `[RND_FUNCTION_RETURN_TYPE]` If custom function does not return number or bigint, or if the value is out of range. */ constructor(bits, fn) { super(bits); this.fn = fn; if (fn && !['number', 'bigint'].includes(typeof fn())) throw new TypeError(`[RND_FUNCTION_RETURN_TYPE]: RandomFragment custom function return is of type: ${typeof fn()}; Expected number or bigint.`); } getValue() { if (this.fn) { const rndNum = BigInt(this.fn()); if (rndNum < 0n) throw new TypeError(`[RND_FUNCTION_BAD_RETURN]: RandomFragment custom function returned a negative value.`); if (rndNum >= 1n << BigInt(this.bits)) throw new TypeError(`[RND_FUNCTION_BAD_RETURN]: RandomFragment custom function returned a value bigger than (2 ** ${this.bits}) - 1.`); return rndNum; } if (this.bits <= 47) return BigInt((0, crypto_1.randomInt)(0, Number(1n << BigInt(this.bits)))); // 47 bit parts due to randomInt range limitation // max param limit of Number.MAX_SAFE_INTEGER let rndNum = BigInt(0); for (let i = 0; i < this.bits; i += 47) { rndNum |= BigInt((0, crypto_1.randomInt)(0, Number(1n << BigInt(Math.min(this.bits - i, 47))))) << BigInt(i); } return rndNum; } destructure(snowflake) { const bits = BigInt(snowflake) & this.bitMask; return { identifier: this.identifier, value: bits >> this.bitShift, }; } } exports.default = RandomFragment; //# sourceMappingURL=RandomFragment.js.map