Spaces:
Runtime error
Runtime error
File size: 2,063 Bytes
ceb943f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | // 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);
}
|