mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Consolidate template modules.
This commit is contained in:
@@ -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;
|
|
||||||
23
template.js
23
template.js
@@ -2,7 +2,6 @@ import assignInWith from './assignInWith.js';
|
|||||||
import attempt from './attempt.js';
|
import attempt from './attempt.js';
|
||||||
import baseValues from './.internal/baseValues.js';
|
import baseValues from './.internal/baseValues.js';
|
||||||
import customDefaultsAssignIn from './.internal/customDefaultsAssignIn.js';
|
import customDefaultsAssignIn from './.internal/customDefaultsAssignIn.js';
|
||||||
import escapeStringChar from './.internal/escapeStringChar.js';
|
|
||||||
import isError from './isError.js';
|
import isError from './isError.js';
|
||||||
import isIterateeCall from './.internal/isIterateeCall.js';
|
import isIterateeCall from './.internal/isIterateeCall.js';
|
||||||
import keys from './keys.js';
|
import keys from './keys.js';
|
||||||
@@ -27,6 +26,16 @@ const reNoMatch = /($^)/;
|
|||||||
/** Used to match unescaped characters in compiled string literals. */
|
/** Used to match unescaped characters in compiled string literals. */
|
||||||
const reUnescapedString = /['\n\r\u2028\u2029\\]/g;
|
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
|
* Creates a compiled template function that can interpolate data properties
|
||||||
* in "interpolate" delimiters, HTML-escape interpolated data properties in
|
* in "interpolate" delimiters, HTML-escape interpolated data properties in
|
||||||
@@ -161,7 +170,9 @@ function template(string, options, guard) {
|
|||||||
, 'g');
|
, 'g');
|
||||||
|
|
||||||
// Use a sourceURL for easier debugging.
|
// 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, (
|
string.replace(reDelimiters, (
|
||||||
match,
|
match,
|
||||||
@@ -174,7 +185,9 @@ function template(string, options, guard) {
|
|||||||
interpolateValue || (interpolateValue = esTemplateValue);
|
interpolateValue || (interpolateValue = esTemplateValue);
|
||||||
|
|
||||||
// Escape characters that can't be included in string literals.
|
// 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.
|
// Replace delimiters with snippets.
|
||||||
if (escapeValue) {
|
if (escapeValue) {
|
||||||
@@ -227,7 +240,9 @@ function template(string, options, guard) {
|
|||||||
source +
|
source +
|
||||||
'return __p\n}';
|
'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
|
// Provide the compiled function's source by its `toString` method or
|
||||||
// the `source` property as a convenience for inlining compiled templates.
|
// the `source` property as a convenience for inlining compiled templates.
|
||||||
|
|||||||
Reference in New Issue
Block a user