forked from rage/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.js
More file actions
44 lines (39 loc) · 976 Bytes
/
strings.js
File metadata and controls
44 lines (39 loc) · 976 Bytes
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
export function nthIndex(str, pat, n) {
var L = str.length,
i = -1
while (n-- && i++ < L) {
i = str.indexOf(pat, i)
if (i < 0) break
}
return i
}
export function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
export function removeLeadingZeros(string) {
return string.replace(/^0+/, "")
}
export function splitGroupNameToWordAndNumber(string) {
return string.split(/(\d+)/)
}
export function improveGroupName(string) {
var stringParts = splitGroupNameToWordAndNumber(string)
return (
capitalizeFirstLetter(stringParts[0]) +
" " +
removeLeadingZeros(stringParts[1])
)
}
export function normalizeExerciseId(string) {
return encodeURIComponent(
string
.toLowerCase()
.replace(/ö/g, "o")
.replace(/Ö/g, "O")
.replace(/ä/g, "a")
.replace(/Ä/g, "A")
.replace(/\s+/g, "-")
.replace(/[^A-Za-z0-9_-]/g, "")
.replace(/-+/g, "-"),
)
}