forked from eclipse-basyx/basyx-aas-web-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.ts
More file actions
81 lines (73 loc) · 2.8 KB
/
StringUtils.ts
File metadata and controls
81 lines (73 loc) · 2.8 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
/**
* Converts the first character of a string to uppercase.
*
* @param {string} string - The input string to modify.
* @returns {string} A new string with the first character capitalized.
*/
export function capitalizeFirstLetter(string: string): string {
if (!string || string.length === 0) return '';
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Converts the first character of a string to lowercase.
*
* @param {string} string - The input string to modify.
* @returns {string} A new string with the first character lowercased.
*/
export function firstLetterToLowerCase(string: string): string {
if (!string || string.length === 0) return '';
return string[0].toLowerCase() + string.slice(1);
}
/**
* Strips the last character from a given string.
*
* This function takes a string as input and returns a new string
* with the last character removed. If the string is empty has only
* one character, it returns an empty string.
*
* @param {string} string - The input string from which the last character
* will be removed.
* @returns {string} - The string without the last character, or an empty
* string if the input is empty has only one character.
*/
export function stripLastCharacter(string: string): string {
if (!string || string.length < 2) return '';
return string.substring(0, string.length - 1);
}
/**
* Checks if the provided string is empty or only contains whitespace.
*
* @param {string} val - The string to be checked.
* @returns {boolean} - Returns true if the string is empty or only contains whitespace; otherwise, returns false.
*/
export function isEmptyString(val: string): boolean {
return !val || val.trim() === '';
}
/**
* Sanitizes a string to be safe for path/file segments by replacing unsupported characters.
* Rejects dot-only segments and reserved Windows device names.
*
* @param {string} value - The value to sanitize.
* @param {string} fallback - The value to return if sanitization results in an empty string.
* @returns {string} A sanitized segment or the fallback.
*/
export function safeSegment(value: string, fallback: string): string {
const cleaned = value
?.trim()
.replace(/[^a-zA-Z0-9._-]/g, '-')
.replace(/-+/g, '-');
if (!cleaned) {
return fallback;
}
// Strip leading and trailing dots to avoid dot-only or hidden path segments.
const dotStripped = cleaned.replace(/^\.+/, '').replace(/\.+$/, '');
if (!dotStripped || dotStripped === '.' || dotStripped === '..') {
return fallback;
}
// Reject reserved Windows device names.
const windowsReservedPattern = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(\..*)?$/i;
if (windowsReservedPattern.test(dotStripped)) {
return fallback;
}
return dotStripped;
}