UNPKG

uuniq

Version:
139 lines (132 loc) 5.61 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/Symbolic.class.ts var Symbolic_class_exports = {}; __export(Symbolic_class_exports, { Symbolic: () => Symbolic }); module.exports = __toCommonJS(Symbolic_class_exports); var import_any_base = __toESM(require("any-base")); var import_lodash2 = __toESM(require("lodash")); // src/Snowflake.class.ts var import_lodash = __toESM(require("lodash")); // src/defaults/SnowflakeOptions.default.ts var SnowflakeOptionsDefault = { epoch: "2025-01-01T00:00:00.000Z", place_id: 0 }; // src/Snowflake.class.ts var parts = { timestamp: 53, place_id: 4, sequence: 10 }; var calculateLimits = (parts2) => { const limits2 = {}; const keys = Object.keys(parts2); for (const key of keys) limits2[key] = Math.pow(2, parts2[key]) - 1; return limits2; }; var calculateShifts = (parts2) => { const shifts2 = {}; let shift = 0; const keys = ["sequence", "place_id", "timestamp"]; for (const key of keys) { shifts2[key] = shift; shift += parts2[key]; } return shifts2; }; var limits = calculateLimits(parts); var shifts = calculateShifts(parts); var Snowflake = class { constructor(options = SnowflakeOptionsDefault) { this.options = import_lodash.default.merge({}, SnowflakeOptionsDefault, options); this.epoch = this.options.epoch instanceof Date ? this.options.epoch.getTime() : typeof this.options.epoch === "string" || typeof this.options.epoch === "number" ? new Date(this.options.epoch).getTime() : (/* @__PURE__ */ new Date("2025-01-01T00:00:00.000Z")).getTime(); if ((this.options.place_id ?? 0) < 0 || (this.options.place_id ?? 0) > limits.place_id) throw new Error(`Field place_id must be between 0 and ${limits.place_id}`); this.options.place_id = (this.options.place_id ?? 0) & limits.place_id; this.sequence = 0; this.last_timestamp = -1; } currentTimestamp() { return Date.now() - this.epoch; } waitForNextTime(last_timestamp) { let timestamp = this.currentTimestamp(); while (last_timestamp >= timestamp) timestamp = this.currentTimestamp(); return timestamp; } generate() { let timestamp = this.currentTimestamp(); if (timestamp < this.last_timestamp) throw new Error("Clock moved backwards."); if (timestamp === this.last_timestamp) { this.sequence = this.sequence + 1 & limits.sequence; if (this.sequence === 0) timestamp = this.waitForNextTime(this.last_timestamp); } else this.sequence = 0; this.last_timestamp = timestamp; return (BigInt(timestamp) << BigInt(shifts.timestamp) | BigInt(this.options.place_id ?? 0) << BigInt(shifts.place_id) | BigInt(this.sequence)).toString(); } resolve(id) { const bigint_id = BigInt(id); return { created_at: new Date(this.epoch + Number(bigint_id >> BigInt(shifts.timestamp) & BigInt(limits.timestamp))).toISOString(), place_id: Number(bigint_id >> BigInt(shifts.place_id) & BigInt(limits.place_id)), sequence: Number(bigint_id & BigInt(limits.sequence)) }; } }; // src/defaults/SymbolicOptions.default.ts var SymbolicOptionsDefault = { epoch: "2025-01-01T00:00:00.000Z", place_id: 0, charset: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" }; // src/Symbolic.class.ts var Symbolic = class { constructor(options = SymbolicOptionsDefault) { this.options = import_lodash2.default.merge({}, SymbolicOptionsDefault, options); this.epoch = this.options.epoch instanceof Date ? this.options.epoch.getTime() : typeof this.options.epoch === "string" || typeof this.options.epoch === "number" ? new Date(this.options.epoch).getTime() : (/* @__PURE__ */ new Date("2025-01-01T00:00:00.000Z")).getTime(); this.Snowflake = new Snowflake({ epoch: this.epoch, place_id: this.options.place_id ?? 0 }); this.encode = (0, import_any_base.default)(import_any_base.default.DEC, this.options.charset ?? ""); this.decode = (0, import_any_base.default)(this.options.charset ?? "", import_any_base.default.DEC); } generate() { return this.encode(this.Snowflake.generate()); } resolve(id) { return this.Snowflake.resolve(this.decode(id)); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Symbolic });