util: trimPrefix and trimSuffix methods

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-07-06 13:24:04 +02:00
parent ddcd63c92a
commit 70c0e12f74
2 changed files with 57 additions and 0 deletions

View File

@@ -111,4 +111,26 @@ export class Util {
}
return false;
}
public static trimPrefix(str: string, suffix: string): string {
if (!str || !suffix) {
return str;
}
const index = str.indexOf(suffix);
if (index !== 0) {
return str;
}
return str.substring(suffix.length);
}
public static trimSuffix(str: string, suffix: string): string {
if (!str || !suffix) {
return str;
}
const index = str.lastIndexOf(suffix);
if (index === -1 || index + suffix.length !== str.length) {
return str;
}
return str.substring(0, index);
}
}