Consolidate escape and unescape modules.

This commit is contained in:
John-David Dalton
2017-01-10 17:09:40 -08:00
parent fc1a360212
commit c5d4bb8dcb
4 changed files with 20 additions and 46 deletions

View File

@@ -1,6 +1,14 @@
import escapeHtmlChar from './.internal/escapeHtmlChar.js';
import toString from './toString.js';
/** Used to map characters to HTML entities. */
const htmlEscapes = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
};
/** Used to match HTML entities and HTML characters. */
const reUnescapedHtml = /[&<>"']/g;
const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
@@ -34,7 +42,7 @@ const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
function escape(string) {
string = toString(string);
return (string && reHasUnescapedHtml.test(string))
? string.replace(reUnescapedHtml, escapeHtmlChar)
? string.replace(reUnescapedHtml, chr => htmlEscapes[chr])
: string;
}