"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/dxid.js var dxid_exports = {}; __export(dxid_exports, { decode32: () => decode32, default: () => dxid_default, encode32: () => encode32, id: () => id, luhn32: () => luhn32, normalize: () => normalize, parse: () => parse, stringify: () => stringify }); module.exports = __toCommonJS(dxid_exports); var base32Chars = "bcdfghjklmnpqrstvwxyz_0123456789"; var separator = "-"; var s2b = new Array(123); for (let i = 0; i < base32Chars.length; i++) { s2b[base32Chars.charCodeAt(i)] = i; } var normalize = (dxid) => ( // remove aeiou ? should we ignore instead of crashing if voyel? dxid.replaceAll(separator, "").toLowerCase() ); var luhn32 = (base32url) => { let sum = 0; let isDouble = true; for (let i = base32url.length - 1; i >= 0; i--) { const char = base32url.charAt(i); const charIndex = base32Chars.indexOf(char); if (charIndex === -1) { throw new RangeError(`Invalid character: ${char}`); } let digit = charIndex; if (isDouble) { digit *= 2; if (digit > 31) { digit -= 31; } } sum += digit; isDouble = !isDouble; } const luhnDigit = (32 - sum % 32) % 32; return base32Chars[luhnDigit]; }; var encode32 = (id2) => { if (id2 === 0) { return base32Chars[id2]; } let base32String = ""; let number = id2; while (number > 0) { const digit = number % 32; base32String = base32Chars[digit] + base32String; number = Math.floor(number / 32); } return base32String; }; var decode32 = (base32) => { let number = 0; for (let i = 0; i < base32.length; i++) { number = number * 32 + s2b[base32.charCodeAt(i)]; } return number; }; var stringify = (number, addUnderscore) => { if (!Number.isSafeInteger(number)) { if (typeof number !== "number") throw new Error(`The id must be an integer, not a ${typeof number}`); throw new RangeError( `The id must be smaller than safe integer <${Number.MAX_SAFE_INTEGER}` ); } if (number < 0) { throw new RangeError("The id must be positive"); } const payload = encode32(number); const dxid = luhn32(payload) + payload; if (addUnderscore !== false && /^\d+$/.test(dxid)) { const half = dxid.length / 2; return dxid.slice(0, half) + separator + dxid.slice(half); } return dxid; }; var parse = (ubase32, throwError) => { if (throwError === false && Number(ubase32)) { return Number(ubase32); } const base32 = normalize(ubase32); const checksum = base32[0]; const payload = base32.substring(1); let luhn; try { luhn = luhn32(payload); } catch (e) { if (throwError === false) { return false; } throw e; } if (luhn !== checksum || base32.length < 2) { if (throwError === false) { return false; } throw new RangeError("invalid dxid"); } const id2 = decode32(payload); if (!Number.isSafeInteger(id2)) { throw new RangeError( `dxid too long, the id would be greater than safe integer > ${Number.MAX_SAFE_INTEGER}` ); } return decode32(payload); }; var id = (dxidorid) => { if (/^\d+$/.test(dxidorid)) { return stringify(+dxidorid); } return parse(dxidorid); }; var dxid_default = { id, luhn32, stringify, parse, encode32, decode32 }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { decode32, encode32, id, luhn32, normalize, parse, stringify });