nehoid
Version:
Advanced unique ID generation utility with multi-layer encoding, collision detection, and context-aware features
95 lines • 3.51 kB
JavaScript
import { __processor__ } from "nehonix-uri-processor";
import { Encoder } from "./encoder.js";
export class Generator {
static generateRandomString(length, alphabet) {
return Array.from({ length }, () => alphabet[Math.floor(Math.random() * alphabet.length)]).join("");
}
static generate(options = {}) {
const opts = { ...this.DEFAULT_OPTIONS, ...options };
const segments = [];
if (opts.includeTimestamp) {
const timestamp = Date.now().toString(36);
segments.push(timestamp);
}
for (let i = 0; i < opts.segments; i++) {
const randomString = this.generateRandomString(opts.size, opts.alphabet);
const multiEnc = __processor__.encodeMultiple(randomString, opts.encoding && []);
const encoded = Array.isArray(opts.encoding)
? multiEnc.results[multiEnc.results.length - 1].encoded
: __processor__.encode(randomString, opts.encoding);
segments.push(encoded);
}
let id = segments.join(opts.separator);
if (opts.compression !== "none") {
id = Encoder.compress(id, opts.compression);
}
return opts.prefix ? `${opts.prefix}${opts.separator}${id}` : id;
}
static async safe(options) {
let attempts = 0;
let id;
while (attempts < options.maxAttempts) {
id = this.generate();
if (await options.checkFunction(id)) {
return id;
}
attempts++;
if (options.backoffType === "exponential") {
await new Promise((resolve) => setTimeout(resolve, Math.pow(2, attempts)));
}
else {
await new Promise((resolve) => setTimeout(resolve, 100 * attempts));
}
}
throw new Error(`Failed to generate unique ID after ${options.maxAttempts} attempts`);
}
static batch(options) {
const ids = new Set();
const { count, format = "standard", ensureUnique = true } = options;
while (ids.size < count) {
const id = format === "standard"
? this.generate()
: format === "uuid"
? this.uuid()
: format === "nano"
? this.nano()
: this.short();
if (!ensureUnique || !ids.has(id)) {
ids.add(id);
}
}
return Array.from(ids);
}
static uuid() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
static nano(size = 12) {
return this.generateRandomString(size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
}
static short(length = 8) {
return this.generate({
size: length,
segments: 1,
encoding: "urlSafeBase64",
});
}
static hex(length = 32) {
return this.generateRandomString(length, "0123456789abcdef");
}
}
Generator.DEFAULT_OPTIONS = {
size: 8,
segments: 4,
separator: "-",
encoding: "rawHex",
prefix: "",
includeTimestamp: false,
alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
compression: "none",
reversible: false,
};
//# sourceMappingURL=generator.js.map