-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.ts
31 lines (26 loc) · 834 Bytes
/
string.ts
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
export function hyphenToUnderscore(word: string) {
return word.replaceAll('-', '_')
}
export function capitalizeFirstLetter(word: string) {
return word.charAt(0).toUpperCase() + word.slice(1)
}
export function removeExtraSpaces(text: string) {
return text.trim().replace(/\s+/g, ' ')
}
export function removeBreakLines(text: string) {
return text.replace(/(\r\n|\r|\n)/g, '')
}
export function noIndent(text: string) {
return text.replace(/(\n)\s+/g, '$1').trim()
}
export function toUpperSnakeCase(str: string): string {
return (
str
// Convert camelCase to snake_case
.replace(/([a-z])([A-Z])/g, '$1_$2')
// Convert sequences of uppercase letters followed by lowercase letters to snake_case
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
.replace(/-/g, '_')
.toUpperCase()
)
}