forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.ts
51 lines (48 loc) · 1.34 KB
/
strings.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
/**
* Converts an array of strings to a list, ending with "or"
*
* @example
* // returns the string "a, b or c"
* orList(["a", "b", "c"]);
* @param items array of strings to join
* @returns human readable comma + or separated list
*/
export function orList(items: string[]): string {
if (items.length === 0) {
return "";
} else if (items.length === 1) {
return items[0] as string;
}
return items
.reverse()
.flatMap((item, index) => {
if (index === 0) {
return [item, " or "];
} else if (index !== 1) {
return [", ", item];
} else {
return [item];
}
})
.reverse()
.join("");
}
/**
* Capitalize the first letter of each word of a string in snake_case format
*
* @param str String to capitalize in snake_case format (e.g. "this_is_a_string")
* @returns Capitalized string in title case (e.g. "This Is A String")
*/
export function snakeCaseToTitleCase(str: string): string {
return str
.split("_")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
}
/**
* Capitalize the first letter of a string
*/
export function capitalizeFirstLetter(str: string): string {
str = str.trim();
return str.charAt(0).toUpperCase() + str.slice(1);
}