@developers-joyride/shortify
Version:
High performance URL shortener library with multi-database support (MongoDB, SQLite, PostgreSQL, MySQL)
31 lines (30 loc) • 955 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateId = generateId;
exports.customGenerator = customGenerator;
/**
* Simple URL-safe ID generator that can be used in place of nanoid
* to avoid ESM/CJS compatibility issues.
*/
function generateId(length = 8) {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
let result = "";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
/**
* Generate a customized ID with specific character set
*/
function customGenerator(alphabet, size = 8) {
return () => {
let id = "";
const length = alphabet.length;
for (let i = 0; i < size; i++) {
id += alphabet.charAt(Math.floor(Math.random() * length));
}
return id;
};
}