From c5d4bb8dcb7f98e07e9a325b3167aa4789dfdf17 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 10 Jan 2017 17:09:40 -0800 Subject: [PATCH] Consolidate `escape` and `unescape` modules. --- .internal/escapeHtmlChar.js | 21 --------------------- .internal/unescapeHtmlChar.js | 21 --------------------- escape.js | 12 ++++++++++-- unescape.js | 12 ++++++++++-- 4 files changed, 20 insertions(+), 46 deletions(-) delete mode 100644 .internal/escapeHtmlChar.js delete mode 100644 .internal/unescapeHtmlChar.js diff --git a/.internal/escapeHtmlChar.js b/.internal/escapeHtmlChar.js deleted file mode 100644 index 94b25e2f3..000000000 --- a/.internal/escapeHtmlChar.js +++ /dev/null @@ -1,21 +0,0 @@ -import basePropertyOf from './.internal/basePropertyOf.js'; - -/** Used to map characters to HTML entities. */ -const htmlEscapes = { - '&': '&', - '<': '<', - '>': '>', - '"': '"', - "'": ''' -}; - -/** - * Used by `escape` to convert characters to HTML entities. - * - * @private - * @param {string} chr The matched character to escape. - * @returns {string} Returns the escaped character. - */ -const escapeHtmlChar = basePropertyOf(htmlEscapes); - -export default escapeHtmlChar; diff --git a/.internal/unescapeHtmlChar.js b/.internal/unescapeHtmlChar.js deleted file mode 100644 index c6a6328d9..000000000 --- a/.internal/unescapeHtmlChar.js +++ /dev/null @@ -1,21 +0,0 @@ -import basePropertyOf from './.internal/basePropertyOf.js'; - -/** Used to map HTML entities to characters. */ -const htmlUnescapes = { - '&': '&', - '<': '<', - '>': '>', - '"': '"', - ''': "'" -}; - -/** - * Used by `unescape` to convert HTML entities to characters. - * - * @private - * @param {string} chr The matched character to unescape. - * @returns {string} Returns the unescaped character. - */ -const unescapeHtmlChar = basePropertyOf(htmlUnescapes); - -export default unescapeHtmlChar; diff --git a/escape.js b/escape.js index 19cdda166..0e244b23c 100644 --- a/escape.js +++ b/escape.js @@ -1,6 +1,14 @@ -import escapeHtmlChar from './.internal/escapeHtmlChar.js'; import toString from './toString.js'; +/** Used to map characters to HTML entities. */ +const htmlEscapes = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''' +}; + /** 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; } diff --git a/unescape.js b/unescape.js index 4194dcf1e..bc8162a1a 100644 --- a/unescape.js +++ b/unescape.js @@ -1,5 +1,13 @@ import toString from './toString.js'; -import unescapeHtmlChar from './.internal/unescapeHtmlChar.js'; + +/** Used to map HTML entities to characters. */ +const htmlUnescapes = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + ''': "'" +}; /** Used to match HTML entities and HTML characters. */ const reEscapedHtml = /&(?:amp|lt|gt|quot|#39);/g; @@ -25,7 +33,7 @@ const reHasEscapedHtml = RegExp(reEscapedHtml.source); function unescape(string) { string = toString(string); return (string && reHasEscapedHtml.test(string)) - ? string.replace(reEscapedHtml, unescapeHtmlChar) + ? string.replace(reEscapedHtml, entity => htmlUnescapes[entity]) : string; }