/
string.ts
159 lines (147 loc) · 3.07 KB
/
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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* The lowercase letters `abcdefghijklmnopqrstuvwxyz`.
*
* @group String
*/
export const asciiLowercase = 'abcdefghijklmnopqrstuvwxyz'
/**
* The uppercase letters `ABCDEFGHIJKLMNOPQRSTUVWXYZ`.
*
* @group String
*/
export const asciiUppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
/**
* The concatenation of the [[asciiLowercase]] and [[asciiUppercase]] constants.
*
* @group String
*/
export const asciiLetters = asciiLowercase + asciiUppercase
/**
* The string `0123456789`.
*
* @group String
*/
export const digits = '0123456789'
/**
* The string `0123456789abcdefABCDEF`.
*
* @group String
*/
export const hexdigits = '0123456789abcdefABCDEF'
/**
* The string `01234567`.
*
* @group String
*/
export const octdigits = '01234567'
/**
* String of ASCII characters which are considered punctuation characters in the C locale:
* `!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~.`
*
* @group String
*/
export const punctuation = '!"#$%&\'()*+,-./:;<=>?@[]^_`{|}~'
/**
* Check if given value contains at least one letter.
*
* @example
* ```
* isLetter('a')
* // true
* isLetter('-')
* // false
* ```
* @group String
*/
export function isLetter(value: string): boolean {
return value.toLowerCase() != value.toUpperCase()
}
/**
* Truncates string
*
* @example
* ```
* truncate('Hello world', 8)
* // Hello...
* ```
* @group String
*/
export function truncate(
value: string,
length: number,
ending = '...'
): string {
if (value.length > length) {
return value.substring(0, length - ending.length) + ending
} else {
return value
}
}
/**
* Extract words from text.
*
* @example
* ```
* extractWords('Hello-world!')
* // ['Hello', 'world']
* ```
* @group String
*/
export function extractWords(value: string): string[] {
return value
.split(/[ -./\\()"',;<>~!@#$%^&*|+=[\]{}`~?:]+/)
.filter((v) => v !== '')
}
/**
* Convert a dash/dot/underscore/space separated string to camelCase
*
* @example
* ```
* camelCase('foo-bar');
* // 'fooBar'
* ```
* @group String
*/
export function camelCase(value: string): string {
return value
.replace(/^[_.\- ]+/, '')
.toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, (_, p1) => p1.toUpperCase())
.replace(/\d+(\w|$)/g, (m) => m.toUpperCase())
}
/**
* Convert a dash/dot/underscore/space separated string to PascalCase
*
* @example
* ```
* pascalCase('foo-bar');
* // 'FooBar'
* ```
* @group String
*/
export function pascalCase(value: string): string {
const parsed = camelCase(value)
return parsed.charAt(0).toUpperCase() + parsed.slice(1)
}
/**
* Convert the first letter in each to word upper case
*
* @example
* ```
* titleCase('hello world');
* // 'Hello World'
* ```
* @group String
*/
export function titleCase(value: string): string {
return value
.split('')
.map((char, index, chars) => {
const prevChar = chars[index - 1]
if (prevChar && isLetter(prevChar)) {
return char.toLowerCase()
}
return char.toUpperCase()
})
.join('')
}