forked from tools/josh
1
0
Fork 0
josh/src/js/extensions.ts

38 lines
900 B
TypeScript
Raw Normal View History

interface String {
trimLines(): string;
replaceAll(regex: RegExp, replacement: string): string;
}
String.prototype.trimLines = function (): string {
return this.split("\n").map(it => it.trim()).join("\n");
};
String.prototype.replaceAll = function (regex, replacement) {
2019-10-21 17:07:16 +02:00
let string = this.toString();
while (regex.test(string))
string = string.replace(regex, replacement);
return "" + string;
};
interface Array<T> {
2019-10-21 17:07:16 +02:00
sortAlphabetically(transform: (element: T) => string): T[];
}
2019-10-21 17:07:16 +02:00
Array.prototype.sortAlphabetically = function (transform: (_: any) => string = (it) => it) {
return this.sort((a, b) => {
const aName = transform(a).toLowerCase();
const bName = transform(b).toLowerCase();
if (aName < bName)
return -1;
else if (aName > bName)
return 1;
else
return 0;
});
};