Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869// generate-code.mjs// Short code generator for paintings// Uses nanoid with custom alphabet for pronounceable, collision-resistant codes
import { customAlphabet } from 'nanoid';
// Base alphabet - lowercase preferred (3x) but uppercase included for more space// Mix consonants, vowels, and numbers for better distributionconst consonants = 'bcdfghjklmnpqrstvwxyz' + 'bcdfghjklmnpqrstvwxyz' + 'BCDFGHJKLMNPQRSTVWXYZ';const vowels = 'aeiou' + 'aeiou' + 'AEIOU';const numbers = '23456789'; // Exclude 0,1 (look like O,l)
// Configurable settingsconst CODE_LENGTH = 3; // Start with 3 chars, can growconst MAX_ATTEMPTS = 100; // Collision retry limit
// Create nanoid generator with lowercase-only alphabetconst alphabet = consonants + vowels + numbers;const nanoid = customAlphabet(alphabet, CODE_LENGTH);
/** * Generate a pronounceable short code * Pattern: consonant-vowel-consonant for better pronunciation * Example: "waf", "bop", "zet" */export function generateCode(existingCodes = new Set()) { let attempts = 0; while (attempts < MAX_ATTEMPTS) { const code = nanoid(); // Check for collision if (!existingCodes.has(code)) { existingCodes.add(code); return code; } attempts++; } // If we hit max attempts, use longer code const longerNanoid = customAlphabet(alphabet, CODE_LENGTH + 1); return longerNanoid();}
/** * Generate multiple codes in batch * More efficient for migrations */export function generateCodes(count, existingCodes = new Set()) { const codes = []; for (let i = 0; i < count; i++) { codes.push(generateCode(existingCodes)); } return codes;}
/** * Check if a code is pronounceable (has vowel balance) * For validation/testing */export function isPronounceableCode(code) { const hasVowel = /[aeiou]/.test(code); const hasConsonant = /[bcdfghjklmnpqrstvwxyz]/.test(code); return hasVowel && hasConsonant;}