UNPKG

idgenie

Version:

A magical and flexible ID generator with support for unique IDs, date/time, prefixes, suffixes, and more.

129 lines (122 loc) 6.06 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('crypto')) : typeof define === 'function' && define.amd ? define(['exports', 'crypto'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.idgenie = {}, global.crypto)); })(this, (function (exports, crypto) { 'use strict'; function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var crypto__namespace = /*#__PURE__*/_interopNamespace(crypto); // Default alphabet: alphanumeric var DEFAULT_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; // Internal counter for counter-based uniqueness var internalCounter = 0; /** * Generates a flexible unique ID based on options. * @param options - Configuration options for ID generation. * @returns A generated unique string ID. * @throws {TypeError} If length is invalid. */ function uniqueId(options) { if (options === void 0) { options = {}; } var _a = options.length, length = _a === void 0 ? 8 : _a, _b = options.randomLength, randomLength = _b === void 0 ? false : _b, _c = options.prefix, prefix = _c === void 0 ? '' : _c, _d = options.suffix, suffix = _d === void 0 ? '' : _d, _e = options.includeDate, includeDate = _e === void 0 ? false : _e, _f = options.includeTime, includeTime = _f === void 0 ? false : _f, _g = options.dateLocale, dateLocale = _g === void 0 ? 'en-IN' : _g, _h = options.timeLocale, timeLocale = _h === void 0 ? 'en-IN' : _h, dateFormatOptions = options.dateFormatOptions, timeFormatOptions = options.timeFormatOptions, _j = options.alphabet, alphabet = _j === void 0 ? DEFAULT_ALPHABET : _j, _k = options.casing, casing = _k === void 0 ? "mixed" : _k, _l = options.separator, separator = _l === void 0 ? "" : _l, _m = options.secure, secure = _m === void 0 ? false : _m, _o = options.mode, mode = _o === void 0 ? "random" : _o, _p = options.counter, counter = _p === void 0 ? false : _p, _q = options.timestamp, timestamp = _q === void 0 ? false : _q; // Handle UUID separately if (mode === "uuid") { return generateUUID(); } // Validate length strictly if (typeof length !== 'number' || length <= 0 || !Number.isFinite(length)) { throw new TypeError("\"length\" must be a positive number. Received: ".concat(length)); } // Random length support var actualLength = !randomLength ? length : Math.floor(Math.random() * (36 - 8 + 1)) + 8; // between 8-36 chars // Generate randomness (secure vs Math.random) var randomPart = secure ? generateSecureRandom(alphabet, actualLength) : generateMathRandom(alphabet, actualLength); // Apply casing var finalRandomPart = randomPart; if (casing === "lower") finalRandomPart = randomPart.toLowerCase(); else if (casing === "upper") finalRandomPart = randomPart.toUpperCase(); // Generate date/time/timestamp strings var dateTimeStr = ""; if (timestamp) { dateTimeStr = Date.now().toString(); } else { var now = new Date(); if (includeDate) { var dateStr = ""; dateStr = now.toLocaleDateString(dateLocale, dateFormatOptions || { year: "numeric", month: "2-digit", day: "2-digit" }).replace(/[^a-zA-Z0-9]/g, separator); dateTimeStr += dateStr; } if (includeTime) { var timeStr = ""; timeStr = now.toLocaleTimeString(timeLocale, timeFormatOptions || { hour: "2-digit", minute: "2-digit", second: "2-digit" }).replace(/[^a-zA-Z0-9]/g, separator); dateTimeStr += separator + timeStr; } } // Counter support var counterStr = ""; if (counter) { counterStr = (internalCounter++).toString(); } // Final ID assembly return "".concat(prefix).concat(finalRandomPart).concat(dateTimeStr ? dateTimeStr + separator : '').concat(counterStr).concat(suffix); } /** * Generates random string using crypto.randomBytes. */ function generateSecureRandom(alphabet, length) { var bytes = crypto__namespace.randomBytes(length); var result = ''; for (var i = 0; i < length; i++) { result += alphabet[bytes[i] % alphabet.length]; } return result; } /** * Generates random string using Math.random(). */ function generateMathRandom(alphabet, length) { var result = ''; for (var i = 0; i < length; i++) { result += alphabet.charAt(Math.floor(Math.random() * alphabet.length)); } return result; } /** * Generates UUID v4. */ function generateUUID() { if (typeof crypto__namespace !== 'undefined' && crypto__namespace.randomUUID) { return crypto__namespace.randomUUID(); } // Fallback: use a polyfill or custom implementation return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } exports.uniqueId = uniqueId; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=idgenie.umd.js.map