forked from neolution-ch/javascript-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.ts
More file actions
134 lines (114 loc) · 3.65 KB
/
string.ts
File metadata and controls
134 lines (114 loc) · 3.65 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
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
/**
* Indicates whether a specified string is null/undefined or empty
* @param value The string to test
* @returns true if the value parameter is null/undefined or empty
*/
export function isNullOrEmpty(value?: string): boolean {
if (!value || typeof value !== "string") {
return true;
}
return value.length === 0;
}
/**
* Indicates whether a specified string is null/undefined, empty, or consists only of white-space characters
* @param value The string to test
* @returns true if the value parameter is null/undefined, Empty, or if value consists exclusively of white-space characters
*/
export function isNullOrWhitespace(value?: string): boolean {
return isNullOrEmpty(value) || (value as string).trim().length === 0;
}
/**
* Capitalize the string
* @param value The string to capitalize
* @returns The capitalized string
*/
export function capitalize(value?: string): string | undefined {
if (!value || isNullOrWhitespace(value)) {
return value;
}
return value.charAt(0).toUpperCase() + value.slice(1);
}
/**
* Uncapitalize the string
* @param value The string to uncapitalize
* @returns The uncapitalized string
*/
export function uncapitalize(value?: string): string | undefined {
if (!value || isNullOrWhitespace(value)) {
return value;
}
return value.charAt(0).toLowerCase() + value.slice(1);
}
/**
* Truncates a string to a maximum length, adding a suffix if truncated
* @param value The string to truncate
* @param maxLength The maximum length of the resulting string
* @param suffix The suffix to append if truncated (default: "")
* @returns The truncated string
*/
export function truncate(value: string | undefined, maxLength: number, suffix = ""): string | undefined {
if (!value || isNullOrWhitespace(value)) {
return value;
}
if (value.length <= maxLength) {
return value;
}
return `${value.slice(0, maxLength)}${suffix}`;
}
/**
* Removes all occurrences of needle from the start of haystack
* @param haystack string to trim
* @param needle the thing to trim
* @returns the string trimmed from the left side
*/
export function ltrim(haystack: string, needle: string): string {
if (!haystack || !needle) return haystack;
const needleLength = needle.length;
if (needleLength === 0 || haystack.length === 0) {
return haystack;
}
let offset = 0;
while (haystack.indexOf(needle, offset) === offset) {
offset = offset + needleLength;
}
return haystack.slice(offset);
}
/**
* Removes all occurrences of needle from the end of haystack
* @param haystack string to trim
* @param needle the thing to trim
* @returns the string trimmed from the right side
*/
export function rtrim(haystack: string, needle: string): string {
if (!haystack || !needle) {
return haystack;
}
const needleLength = needle.length,
haystackLen = haystack.length;
if (needleLength === 0 || haystackLen === 0) {
return haystack;
}
let offset = haystackLen,
idx = -1;
while (true) {
idx = haystack.lastIndexOf(needle, offset - 1);
if (idx === -1 || idx + needleLength !== offset) {
break;
}
if (idx === 0) {
return "";
}
offset = idx;
}
return haystack.slice(0, offset);
}
/**
* Removes all occurrences of needle from the start and the end of haystack
* @param haystack string to trim
* @param needle the thing to trim
* @returns the string trimmed from the right and left side
*/
export function trim(haystack: string, needle: string): string {
const trimmed = ltrim(haystack, needle);
return rtrim(trimmed, needle);
}