PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.7
Yatra – Travel Booking & Tour Operator Software v3.0.2.7
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / resources / js / lib / slug.ts

slug.ts in Yatra – Travel Booking & Tour Operator Software 3.0.2.7, at resources/js/lib/slug.ts

64 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Slug Generation Utility
3 * Generates URL-friendly slugs from text (matches WordPress sanitize_title logic)
4 */
5
6 /**
7 * Generate a slug from a string
8 * Mimics WordPress sanitize_title() function
9 *
10 * @param text - The text to convert to a slug
11 * @returns A URL-friendly slug
12 */
13 export const generateSlug = (text: string): string => {
14 if (!text) return "";
15
16 return (
17 text
18 .toLowerCase()
19 .trim()
20 // Replace spaces and underscores with hyphens
21 .replace(/[\s_]+/g, "-")
22 // Remove all non-word characters except hyphens
23 .replace(/[^\w-]+/g, "")
24 // Replace multiple consecutive hyphens with a single hyphen
25 .replace(/-+/g, "-")
26 // Remove leading and trailing hyphens
27 .replace(/^-+|-+$/g, "")
28 );
29 };
30
31 /**
32 * Generate a unique slug by appending a number if needed
33 *
34 * @param baseSlug - The base slug
35 * @param existingSlugs - Array of existing slugs to check against
36 * @returns A unique slug
37 */
38 export const generateUniqueSlug = (
39 baseSlug: string,
40 existingSlugs: string[],
41 ): string => {
42 let slug = generateSlug(baseSlug);
43
44 if (!slug) {
45 slug = "untitled";
46 }
47
48 // If slug is already unique, return it
49 if (!existingSlugs.includes(slug)) {
50 return slug;
51 }
52
53 // Try appending numbers until we find a unique slug
54 let counter = 1;
55 let uniqueSlug = `${slug}-${counter}`;
56
57 while (existingSlugs.includes(uniqueSlug)) {
58 counter++;
59 uniqueSlug = `${slug}-${counter}`;
60 }
61
62 return uniqueSlug;
63 };
64