From 085dc1aa01783e0ce924f39f2b2ed5464c4572e1 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 12 Jan 2017 11:05:53 -0800 Subject: [PATCH] Consolidate `template` modules. --- .internal/escapeStringChar.js | 22 ---------------------- template.js | 23 +++++++++++++++++++---- 2 files changed, 19 insertions(+), 26 deletions(-) delete mode 100644 .internal/escapeStringChar.js diff --git a/.internal/escapeStringChar.js b/.internal/escapeStringChar.js deleted file mode 100644 index 6835aea72..000000000 --- a/.internal/escapeStringChar.js +++ /dev/null @@ -1,22 +0,0 @@ -/** Used to escape characters for inclusion in compiled string literals. */ -const stringEscapes = { - '\\': '\\', - "'": "'", - '\n': 'n', - '\r': 'r', - '\u2028': 'u2028', - '\u2029': 'u2029' -}; - -/** - * Used by `template` to escape characters for inclusion in compiled string literals. - * - * @private - * @param {string} chr The matched character to escape. - * @returns {string} Returns the escaped character. - */ -function escapeStringChar(chr) { - return `\\${ stringEscapes[chr] }`; -} - -export default escapeStringChar; diff --git a/template.js b/template.js index f0d8de26a..d9717a238 100644 --- a/template.js +++ b/template.js @@ -2,7 +2,6 @@ import assignInWith from './assignInWith.js'; import attempt from './attempt.js'; import baseValues from './.internal/baseValues.js'; import customDefaultsAssignIn from './.internal/customDefaultsAssignIn.js'; -import escapeStringChar from './.internal/escapeStringChar.js'; import isError from './isError.js'; import isIterateeCall from './.internal/isIterateeCall.js'; import keys from './keys.js'; @@ -27,6 +26,16 @@ const reNoMatch = /($^)/; /** Used to match unescaped characters in compiled string literals. */ const reUnescapedString = /['\n\r\u2028\u2029\\]/g; +/** Used to escape characters for inclusion in compiled string literals. */ +const stringEscapes = { + '\\': '\\', + "'": "'", + '\n': 'n', + '\r': 'r', + '\u2028': 'u2028', + '\u2029': 'u2029' +}; + /** * Creates a compiled template function that can interpolate data properties * in "interpolate" delimiters, HTML-escape interpolated data properties in @@ -161,7 +170,9 @@ function template(string, options, guard) { , 'g'); // Use a sourceURL for easier debugging. - const sourceURL = 'sourceURL' in options ? `//# sourceURL=${ options.sourceURL }\n` : ''; + const sourceURL = 'sourceURL' in options + ? `//# sourceURL=${ options.sourceURL }\n` + : ''; string.replace(reDelimiters, ( match, @@ -174,7 +185,9 @@ function template(string, options, guard) { interpolateValue || (interpolateValue = esTemplateValue); // Escape characters that can't be included in string literals. - source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar); + source += string + .slice(index, offset) + .replace(reUnescapedString, chr => `\\${ stringEscapes[chr] }`); // Replace delimiters with snippets. if (escapeValue) { @@ -227,7 +240,9 @@ function template(string, options, guard) { source + 'return __p\n}'; - const result = attempt(() => Function(importsKeys, `${ sourceURL }return ${ source }`))(...importsValues); + const result = attempt(() => ( + Function(importsKeys, `${ sourceURL }return ${ source }`))(...importsValues) + ); // Provide the compiled function's source by its `toString` method or // the `source` property as a convenience for inlining compiled templates.