123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
-
- var baseCopy = require('lodash._basecopy'),
- baseToString = require('lodash._basetostring'),
- baseValues = require('lodash._basevalues'),
- isIterateeCall = require('lodash._isiterateecall'),
- reInterpolate = require('lodash._reinterpolate'),
- keys = require('lodash.keys'),
- restParam = require('lodash.restparam'),
- templateSettings = require('lodash.templatesettings');
-
-
- var errorTag = '[object Error]';
-
-
- var reEmptyStringLeading = /\b__p \+= '';/g,
- reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
- reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
-
-
- var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
-
-
- var reNoMatch = /($^)/;
-
-
- var reUnescapedString = /['\n\r\u2028\u2029\\]/g;
-
-
- var stringEscapes = {
- '\\': '\\',
- "'": "'",
- '\n': 'n',
- '\r': 'r',
- '\u2028': 'u2028',
- '\u2029': 'u2029'
- };
-
-
- function escapeStringChar(chr) {
- return '\\' + stringEscapes[chr];
- }
-
-
- function isObjectLike(value) {
- return !!value && typeof value == 'object';
- }
-
-
- var objectProto = Object.prototype;
-
-
- var hasOwnProperty = objectProto.hasOwnProperty;
-
-
- var objToString = objectProto.toString;
-
-
- function assignOwnDefaults(objectValue, sourceValue, key, object) {
- return (objectValue === undefined || !hasOwnProperty.call(object, key))
- ? sourceValue
- : objectValue;
- }
-
-
- function assignWith(object, source, customizer) {
- var index = -1,
- props = keys(source),
- length = props.length;
-
- while (++index < length) {
- var key = props[index],
- value = object[key],
- result = customizer(value, source[key], key, object, source);
-
- if ((result === result ? (result !== value) : (value === value)) ||
- (value === undefined && !(key in object))) {
- object[key] = result;
- }
- }
- return object;
- }
-
-
- function baseAssign(object, source) {
- return source == null
- ? object
- : baseCopy(source, keys(source), object);
- }
-
-
- function isError(value) {
- return isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag;
- }
-
-
- function template(string, options, otherOptions) {
-
-
- var settings = templateSettings.imports._.templateSettings || templateSettings;
-
- if (otherOptions && isIterateeCall(string, options, otherOptions)) {
- options = otherOptions = undefined;
- }
- string = baseToString(string);
- options = assignWith(baseAssign({}, otherOptions || options), settings, assignOwnDefaults);
-
- var imports = assignWith(baseAssign({}, options.imports), settings.imports, assignOwnDefaults),
- importsKeys = keys(imports),
- importsValues = baseValues(imports, importsKeys);
-
- var isEscaping,
- isEvaluating,
- index = 0,
- interpolate = options.interpolate || reNoMatch,
- source = "__p += '";
-
-
- var reDelimiters = RegExp(
- (options.escape || reNoMatch).source + '|' +
- interpolate.source + '|' +
- (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
- (options.evaluate || reNoMatch).source + '|$'
- , 'g');
-
-
- var sourceURL = 'sourceURL' in options ? '//# sourceURL=' + options.sourceURL + '\n' : '';
-
- string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
- interpolateValue || (interpolateValue = esTemplateValue);
-
-
- source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);
-
-
- if (escapeValue) {
- isEscaping = true;
- source += "' +\n__e(" + escapeValue + ") +\n'";
- }
- if (evaluateValue) {
- isEvaluating = true;
- source += "';\n" + evaluateValue + ";\n__p += '";
- }
- if (interpolateValue) {
- source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
- }
- index = offset + match.length;
-
-
-
- return match;
- });
-
- source += "';\n";
-
-
-
- var variable = options.variable;
- if (!variable) {
- source = 'with (obj) {\n' + source + '\n}\n';
- }
-
- source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
- .replace(reEmptyStringMiddle, '$1')
- .replace(reEmptyStringTrailing, '$1;');
-
-
- source = 'function(' + (variable || 'obj') + ') {\n' +
- (variable
- ? ''
- : 'obj || (obj = {});\n'
- ) +
- "var __t, __p = ''" +
- (isEscaping
- ? ', __e = _.escape'
- : ''
- ) +
- (isEvaluating
- ? ', __j = Array.prototype.join;\n' +
- "function print() { __p += __j.call(arguments, '') }\n"
- : ';\n'
- ) +
- source +
- 'return __p\n}';
-
- var result = attempt(function() {
- return Function(importsKeys, sourceURL + 'return ' + source).apply(undefined, importsValues);
- });
-
-
-
- result.source = source;
- if (isError(result)) {
- throw result;
- }
- return result;
- }
-
-
- var attempt = restParam(function(func, args) {
- try {
- return func.apply(undefined, args);
- } catch(e) {
- return isError(e) ? e : new Error(e);
- }
- });
-
- module.exports = template;
|