@developers-joyride/shortify
Version:
High performance URL shortener library with multi-database support (MongoDB, SQLite, PostgreSQL, MySQL)
33 lines (27 loc) • 819 B
text/typescript
/**
* Simple URL-safe ID generator that can be used in place of nanoid
* to avoid ESM/CJS compatibility issues.
*/
export function generateId(length: number = 8): string {
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
*/
export function customGenerator(alphabet: string, size: number = 8) {
return (): string => {
let id = "";
const length = alphabet.length;
for (let i = 0; i < size; i++) {
id += alphabet.charAt(Math.floor(Math.random() * length));
}
return id;
};
}