2022-07-17 21:40:41 +08:00
|
|
|
const { replace } = ''
|
|
|
|
|
|
|
|
// escape
|
|
|
|
const ca = /[&<>'"]/g
|
|
|
|
|
|
|
|
const esca = {
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
"'": ''',
|
|
|
|
'"': '"',
|
|
|
|
}
|
2022-10-07 13:55:39 +08:00
|
|
|
const pe = (m: keyof typeof esca) => esca[m]
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely escape HTML entities such as `&`, `<`, `>`, `"`, and `'`.
|
|
|
|
* @param {string} es the input to safely escape
|
|
|
|
* @returns {string} the escaped input, and it **throws** an error if
|
|
|
|
* the input type is unexpected, except for boolean and numbers,
|
|
|
|
* converted as string.
|
|
|
|
*/
|
2022-10-07 13:55:39 +08:00
|
|
|
export const escape = (es: string): string => replace.call(es, ca, pe)
|