2019-01-16 11:28:05 +01:00
|
|
|
export function escape(str: string | number | undefined): string {
|
|
|
|
|
if (str === undefined) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
if (typeof str === "number") {
|
|
|
|
|
return String(str);
|
|
|
|
|
}
|
|
|
|
|
return str
|
|
|
|
|
.replace(/&/g, "&")
|
|
|
|
|
.replace(/</g, "<")
|
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
|
.replace(/"/g, "'")
|
|
|
|
|
.replace(/`/g, "`");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove trailing and leading spaces
|
|
|
|
|
*/
|
|
|
|
|
export function htmlTrim(s: string): string {
|
|
|
|
|
let result = s.replace(/(^\s+|\s+$)/g, "");
|
2019-01-23 22:52:50 +01:00
|
|
|
if (s[0] === " ") {
|
|
|
|
|
result = " " + result;
|
2019-01-16 11:28:05 +01:00
|
|
|
}
|
2019-01-23 22:52:50 +01:00
|
|
|
if (result !== " " && s[s.length - 1] === " ") {
|
|
|
|
|
result = result + " ";
|
2019-01-16 11:28:05 +01:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|