atst/js/lib/escape.js
2020-01-08 11:10:59 -05:00

21 lines
403 B
JavaScript

// https://stackoverflow.com/a/6020820
// List of HTML entities for escaping.
const htmlEscapes = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
'/': '&#x2F;',
}
const htmlEscaper = /[&<>"'\/]/g
// Escape a string for HTML interpolation.
const escape = string => {
return ('' + string).replace(htmlEscaper, match => htmlEscapes[match])
}
export default escape