-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-utils.ts
More file actions
41 lines (36 loc) · 1.05 KB
/
string-utils.ts
File metadata and controls
41 lines (36 loc) · 1.05 KB
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
/**
* Converts snake_case strings to camelCase
*/
export function snakeToCamel(str: string): string {
return str.replace(/(_\w)/g, (match) => match[1]?.toUpperCase() || '');
}
/**
* Converts camelCase strings to snake_case
*/
export function camelToSnake(str: string): string {
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
}
/**
* Converts kebab-case strings to camelCase
*/
export function kebabToCamel(str: string): string {
return str.replace(/(-\w)/g, (match) => match[1]?.toUpperCase() || '');
}
/**
* Converts camelCase strings to kebab-case
*/
export function camelToKebab(str: string): string {
return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
}
/**
* Capitalizes the first letter of a string
*/
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* Converts a string to Title Case
*/
export function toTitleCase(str: string): string {
return str.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
}