2019-10-20 23:55:04 +02:00
|
|
|
interface String {
|
|
|
|
trimLines(): string;
|
|
|
|
|
2019-11-06 15:07:56 +01:00
|
|
|
trimMultiLines(): string;
|
|
|
|
|
2019-10-20 23:55:04 +02:00
|
|
|
replaceAll(regex: RegExp, replacement: string): string;
|
|
|
|
}
|
|
|
|
|
2019-10-29 12:36:03 +01:00
|
|
|
/**
|
|
|
|
* Returns this string with all leading and trailing whitespace removed from each line.
|
|
|
|
*/
|
2019-11-06 14:47:14 +01:00
|
|
|
String.prototype.trimLines = function(): string {
|
2019-11-06 15:07:56 +01:00
|
|
|
return this.split("\n").map(it => it.trimStart()).join("\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this string with all leading and trailing whitespace removed from each line, and from lines that are split
|
|
|
|
* using a the `\\` symbol.
|
|
|
|
*
|
|
|
|
* That is, when writing multilines, write `\\\` at the end for this method to recognise where the string has been
|
|
|
|
* split.
|
|
|
|
*/
|
|
|
|
String.prototype.trimMultiLines = function(): string {
|
|
|
|
return this.trimLines().split("\\").map(it => it.trimStart()).join("");
|
2019-10-20 23:55:04 +02:00
|
|
|
};
|
|
|
|
|
2019-10-29 12:36:03 +01:00
|
|
|
/**
|
|
|
|
* Returns this string with all matches with the given regex replaced with the given replacement.
|
|
|
|
*
|
|
|
|
* @param regex the regex to find matches with
|
|
|
|
* @param replacement the string to replace matches with
|
|
|
|
*/
|
2019-11-06 14:47:14 +01:00
|
|
|
String.prototype.replaceAll = function(regex: RegExp, replacement: string): string {
|
2019-10-21 17:07:16 +02:00
|
|
|
let string = this.toString();
|
2019-10-20 23:55:04 +02:00
|
|
|
|
|
|
|
while (regex.test(string))
|
|
|
|
string = string.replace(regex, replacement);
|
|
|
|
|
2019-10-29 12:36:03 +01:00
|
|
|
return string;
|
2019-10-20 23:55:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
interface Array<T> {
|
2019-10-29 12:36:03 +01:00
|
|
|
sortAlphabetically(transform: (element: T) => string, caseSensitive: boolean): T[];
|
2019-10-20 23:55:04 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 12:36:03 +01:00
|
|
|
/**
|
|
|
|
* Returns a comparator that compares elements of an array based on their string representation.
|
|
|
|
*
|
|
|
|
* @param transform transforms elements of the array into a string that is used for comparing
|
|
|
|
* @param caseSensitive `true` if and only if the comparator should be sensitive to the case of characters
|
|
|
|
*/
|
2019-11-06 14:47:14 +01:00
|
|
|
Array.prototype.sortAlphabetically = function(transform: (_: any) => string = (it) => it,
|
|
|
|
caseSensitive: boolean = true) {
|
2019-10-20 23:55:04 +02:00
|
|
|
return this.sort((a, b) => {
|
2019-10-29 12:36:03 +01:00
|
|
|
const aName = caseSensitive ? transform(a) : transform(a).toLowerCase();
|
|
|
|
const bName = caseSensitive ? transform(b) : transform(b).toLowerCase();
|
2019-10-20 23:55:04 +02:00
|
|
|
|
|
|
|
if (aName < bName)
|
|
|
|
return -1;
|
|
|
|
else if (aName > bName)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
};
|