Spaces:
Runtime error
Runtime error
| // Story 7.3: Slug generation utility for creator URLs | |
| /** | |
| * Generate a URL-safe slug from a channel name | |
| * | |
| * Rules: | |
| * - Convert to lowercase | |
| * - Replace spaces with hyphens | |
| * - Remove special characters (keep only alphanumeric and hyphens) | |
| * - Remove consecutive hyphens | |
| * - Trim hyphens from start/end | |
| * | |
| * @param channelName - The YouTube channel name | |
| * @returns URL-safe slug | |
| * | |
| * @example | |
| * generateCreatorSlug("Tech Reviews Pro") // "tech-reviews-pro" | |
| * generateCreatorSlug("Mr. Beast!!!") // "mr-beast" | |
| * generateCreatorSlug(" Spaces Everywhere ") // "spaces-everywhere" | |
| */ | |
| export function generateCreatorSlug(channelName: string): string { | |
| return channelName | |
| .toLowerCase() | |
| .trim() | |
| // Replace spaces with hyphens | |
| .replace(/\s+/g, '-') | |
| // Remove special characters (keep only alphanumeric and hyphens) | |
| .replace(/[^a-z0-9-]/g, '') | |
| // Remove consecutive hyphens | |
| .replace(/-+/g, '-') | |
| // Trim hyphens from start/end | |
| .replace(/^-+|-+$/g, ''); | |
| } | |
| /** | |
| * Generate a unique slug by appending a number if collision detected | |
| * | |
| * @param baseSlug - The base slug to make unique | |
| * @param existingSlugs - Array of existing slugs to check against | |
| * @returns Unique slug | |
| * | |
| * @example | |
| * ensureUniqueSlug("tech-reviews", ["tech-reviews"]) // "tech-reviews-2" | |
| * ensureUniqueSlug("tech-reviews", ["tech-reviews", "tech-reviews-2"]) // "tech-reviews-3" | |
| */ | |
| export function ensureUniqueSlug(baseSlug: string, existingSlugs: string[]): string { | |
| let slug = baseSlug; | |
| let counter = 2; | |
| while (existingSlugs.includes(slug)) { | |
| slug = `${baseSlug}-${counter}`; | |
| counter++; | |
| } | |
| return slug; | |
| } | |
| /** | |
| * Validate if a string is a valid slug format | |
| * | |
| * @param slug - The slug to validate | |
| * @returns True if valid slug format | |
| */ | |
| export function isValidSlug(slug: string): boolean { | |
| // Must be lowercase alphanumeric with hyphens only | |
| // Cannot start or end with hyphen | |
| // Cannot have consecutive hyphens | |
| const slugRegex = /^[a-z0-9]+(-[a-z0-9]+)*$/; | |
| return slugRegex.test(slug); | |
| } | |