forked from
slices.network/slices
Highly ambitious ATProtocol AppView service and sdks
1/**
2 * Convert a string to kebab-case (dash-separated lowercase)
3 *
4 * Examples:
5 * "MyProject" -> "my-project"
6 * "my_project" -> "my-project"
7 * "My Cool Project" -> "my-cool-project"
8 * "myProject123" -> "my-project123"
9 */
10export function dasherize(str: string): string {
11 return str
12 .replace(/([a-z])([A-Z])/g, '$1-$2') // camelCase to kebab-case
13 .replace(/[\s_]+/g, '-') // spaces and underscores to dashes
14 .replace(/[^a-z0-9-]/gi, '') // remove invalid characters
15 .replace(/-+/g, '-') // collapse multiple dashes
16 .replace(/^-|-$/g, '') // trim dashes from start/end
17 .toLowerCase();
18}