PageRenderTime 70ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/old/node_modules/gulp-notify/node_modules/lodash.template/index.js

https://gitlab.com/jacobberglund/monomono
JavaScript | 389 lines | 145 code | 34 blank | 210 comment | 31 complexity | 74218ee2d0357c878a523814c9d6bc46 MD5 | raw file
  1. /**
  2. * lodash 3.3.0 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modern modularize exports="npm" -o ./`
  4. * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.7.0 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <https://lodash.com/license>
  8. */
  9. var baseCopy = require('lodash._basecopy'),
  10. baseToString = require('lodash._basetostring'),
  11. baseValues = require('lodash._basevalues'),
  12. isIterateeCall = require('lodash._isiterateecall'),
  13. reInterpolate = require('lodash._reinterpolate'),
  14. escape = require('lodash.escape'),
  15. keys = require('lodash.keys'),
  16. templateSettings = require('lodash.templatesettings');
  17. /** `Object#toString` result references. */
  18. var errorTag = '[object Error]';
  19. /** Used to match empty string literals in compiled template source. */
  20. var reEmptyStringLeading = /\b__p \+= '';/g,
  21. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  22. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  23. /**
  24. * Used to match ES template delimiters.
  25. * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literal-lexical-components)
  26. * for more details.
  27. */
  28. var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
  29. /** Used to ensure capturing order of template delimiters. */
  30. var reNoMatch = /($^)/;
  31. /** Used to match unescaped characters in compiled string literals. */
  32. var reUnescapedString = /['\n\r\u2028\u2029\\]/g;
  33. /** Used to escape characters for inclusion in compiled string literals. */
  34. var stringEscapes = {
  35. '\\': '\\',
  36. "'": "'",
  37. '\n': 'n',
  38. '\r': 'r',
  39. '\u2028': 'u2028',
  40. '\u2029': 'u2029'
  41. };
  42. /**
  43. * Used by `_.template` to escape characters for inclusion in compiled
  44. * string literals.
  45. *
  46. * @private
  47. * @param {string} chr The matched character to escape.
  48. * @returns {string} Returns the escaped character.
  49. */
  50. function escapeStringChar(chr) {
  51. return '\\' + stringEscapes[chr];
  52. }
  53. /**
  54. * Checks if `value` is object-like.
  55. *
  56. * @private
  57. * @param {*} value The value to check.
  58. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  59. */
  60. function isObjectLike(value) {
  61. return (value && typeof value == 'object') || false;
  62. }
  63. /** Used for native method references. */
  64. var objectProto = Object.prototype;
  65. /** Used to check objects for own properties. */
  66. var hasOwnProperty = objectProto.hasOwnProperty;
  67. /**
  68. * Used to resolve the `toStringTag` of values.
  69. * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring)
  70. * for more details.
  71. */
  72. var objToString = objectProto.toString;
  73. /**
  74. * Used by `_.template` to customize its `_.assign` use.
  75. *
  76. * **Note:** This method is like `assignDefaults` except that it ignores
  77. * inherited property values when checking if a property is `undefined`.
  78. *
  79. * @private
  80. * @param {*} objectValue The destination object property value.
  81. * @param {*} sourceValue The source object property value.
  82. * @param {string} key The key associated with the object and source values.
  83. * @param {Object} object The destination object.
  84. * @returns {*} Returns the value to assign to the destination object.
  85. */
  86. function assignOwnDefaults(objectValue, sourceValue, key, object) {
  87. return (typeof objectValue == 'undefined' || !hasOwnProperty.call(object, key))
  88. ? sourceValue
  89. : objectValue;
  90. }
  91. /**
  92. * The base implementation of `_.assign` without support for argument juggling,
  93. * multiple sources, and `this` binding `customizer` functions.
  94. *
  95. * @private
  96. * @param {Object} object The destination object.
  97. * @param {Object} source The source object.
  98. * @param {Function} [customizer] The function to customize assigning values.
  99. * @returns {Object} Returns the destination object.
  100. */
  101. function baseAssign(object, source, customizer) {
  102. var props = keys(source);
  103. if (!customizer) {
  104. return baseCopy(source, object, props);
  105. }
  106. var index = -1,
  107. length = props.length;
  108. while (++index < length) {
  109. var key = props[index],
  110. value = object[key],
  111. result = customizer(value, source[key], key, object, source);
  112. if ((result === result ? result !== value : value === value) ||
  113. (typeof value == 'undefined' && !(key in object))) {
  114. object[key] = result;
  115. }
  116. }
  117. return object;
  118. }
  119. /**
  120. * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`,
  121. * `SyntaxError`, `TypeError`, or `URIError` object.
  122. *
  123. * @static
  124. * @memberOf _
  125. * @category Lang
  126. * @param {*} value The value to check.
  127. * @returns {boolean} Returns `true` if `value` is an error object, else `false`.
  128. * @example
  129. *
  130. * _.isError(new Error);
  131. * // => true
  132. *
  133. * _.isError(Error);
  134. * // => false
  135. */
  136. function isError(value) {
  137. return (isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag) || false;
  138. }
  139. /**
  140. * Creates a compiled template function that can interpolate data properties
  141. * in "interpolate" delimiters, HTML-escape interpolated data properties in
  142. * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data
  143. * properties may be accessed as free variables in the template. If a setting
  144. * object is provided it takes precedence over `_.templateSettings` values.
  145. *
  146. * **Note:** In the development build `_.template` utilizes sourceURLs for easier debugging.
  147. * See the [HTML5 Rocks article on sourcemaps](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)
  148. * for more details.
  149. *
  150. * For more information on precompiling templates see
  151. * [lodash's custom builds documentation](https://lodash.com/custom-builds).
  152. *
  153. * For more information on Chrome extension sandboxes see
  154. * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval).
  155. *
  156. * @static
  157. * @memberOf _
  158. * @category String
  159. * @param {string} [string=''] The template string.
  160. * @param {Object} [options] The options object.
  161. * @param {RegExp} [options.escape] The HTML "escape" delimiter.
  162. * @param {RegExp} [options.evaluate] The "evaluate" delimiter.
  163. * @param {Object} [options.imports] An object to import into the template as free variables.
  164. * @param {RegExp} [options.interpolate] The "interpolate" delimiter.
  165. * @param {string} [options.sourceURL] The sourceURL of the template's compiled source.
  166. * @param {string} [options.variable] The data object variable name.
  167. * @param- {Object} [otherOptions] Enables the legacy `options` param signature.
  168. * @returns {Function} Returns the compiled template function.
  169. * @example
  170. *
  171. * // using the "interpolate" delimiter to create a compiled template
  172. * var compiled = _.template('hello <%= user %>!');
  173. * compiled({ 'user': 'fred' });
  174. * // => 'hello fred!'
  175. *
  176. * // using the HTML "escape" delimiter to escape data property values
  177. * var compiled = _.template('<b><%- value %></b>');
  178. * compiled({ 'value': '<script>' });
  179. * // => '<b>&lt;script&gt;</b>'
  180. *
  181. * // using the "evaluate" delimiter to execute JavaScript and generate HTML
  182. * var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
  183. * compiled({ 'users': ['fred', 'barney'] });
  184. * // => '<li>fred</li><li>barney</li>'
  185. *
  186. * // using the internal `print` function in "evaluate" delimiters
  187. * var compiled = _.template('<% print("hello " + user); %>!');
  188. * compiled({ 'user': 'barney' });
  189. * // => 'hello barney!'
  190. *
  191. * // using the ES delimiter as an alternative to the default "interpolate" delimiter
  192. * var compiled = _.template('hello ${ user }!');
  193. * compiled({ 'user': 'pebbles' });
  194. * // => 'hello pebbles!'
  195. *
  196. * // using custom template delimiters
  197. * _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
  198. * var compiled = _.template('hello {{ user }}!');
  199. * compiled({ 'user': 'mustache' });
  200. * // => 'hello mustache!'
  201. *
  202. * // using backslashes to treat delimiters as plain text
  203. * var compiled = _.template('<%= "\\<%- value %\\>" %>');
  204. * compiled({ 'value': 'ignored' });
  205. * // => '<%- value %>'
  206. *
  207. * // using the `imports` option to import `jQuery` as `jq`
  208. * var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
  209. * var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
  210. * compiled({ 'users': ['fred', 'barney'] });
  211. * // => '<li>fred</li><li>barney</li>'
  212. *
  213. * // using the `sourceURL` option to specify a custom sourceURL for the template
  214. * var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
  215. * compiled(data);
  216. * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
  217. *
  218. * // using the `variable` option to ensure a with-statement isn't used in the compiled template
  219. * var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
  220. * compiled.source;
  221. * // => function(data) {
  222. * var __t, __p = '';
  223. * __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
  224. * return __p;
  225. * }
  226. *
  227. * // using the `source` property to inline compiled templates for meaningful
  228. * // line numbers in error messages and a stack trace
  229. * fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  230. * var JST = {\
  231. * "main": ' + _.template(mainText).source + '\
  232. * };\
  233. * ');
  234. */
  235. function template(string, options, otherOptions) {
  236. // Based on John Resig's `tmpl` implementation (http://ejohn.org/blog/javascript-micro-templating/)
  237. // and Laura Doktorova's doT.js (https://github.com/olado/doT).
  238. var settings = templateSettings.imports._.templateSettings || templateSettings;
  239. if (otherOptions && isIterateeCall(string, options, otherOptions)) {
  240. options = otherOptions = null;
  241. }
  242. string = baseToString(string);
  243. options = baseAssign(baseAssign({}, otherOptions || options), settings, assignOwnDefaults);
  244. var imports = baseAssign(baseAssign({}, options.imports), settings.imports, assignOwnDefaults),
  245. importsKeys = keys(imports),
  246. importsValues = baseValues(imports, importsKeys);
  247. var isEscaping,
  248. isEvaluating,
  249. index = 0,
  250. interpolate = options.interpolate || reNoMatch,
  251. source = "__p += '";
  252. // Compile the regexp to match each delimiter.
  253. var reDelimiters = RegExp(
  254. (options.escape || reNoMatch).source + '|' +
  255. interpolate.source + '|' +
  256. (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
  257. (options.evaluate || reNoMatch).source + '|$'
  258. , 'g');
  259. // Use a sourceURL for easier debugging.
  260. var sourceURL = 'sourceURL' in options ? '//# sourceURL=' + options.sourceURL + '\n' : '';
  261. string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
  262. interpolateValue || (interpolateValue = esTemplateValue);
  263. // Escape characters that can't be included in string literals.
  264. source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);
  265. // Replace delimiters with snippets.
  266. if (escapeValue) {
  267. isEscaping = true;
  268. source += "' +\n__e(" + escapeValue + ") +\n'";
  269. }
  270. if (evaluateValue) {
  271. isEvaluating = true;
  272. source += "';\n" + evaluateValue + ";\n__p += '";
  273. }
  274. if (interpolateValue) {
  275. source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  276. }
  277. index = offset + match.length;
  278. // The JS engine embedded in Adobe products requires returning the `match`
  279. // string in order to produce the correct `offset` value.
  280. return match;
  281. });
  282. source += "';\n";
  283. // If `variable` is not specified wrap a with-statement around the generated
  284. // code to add the data object to the top of the scope chain.
  285. var variable = options.variable;
  286. if (!variable) {
  287. source = 'with (obj) {\n' + source + '\n}\n';
  288. }
  289. // Cleanup code by stripping empty strings.
  290. source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
  291. .replace(reEmptyStringMiddle, '$1')
  292. .replace(reEmptyStringTrailing, '$1;');
  293. // Frame code as the function body.
  294. source = 'function(' + (variable || 'obj') + ') {\n' +
  295. (variable
  296. ? ''
  297. : 'obj || (obj = {});\n'
  298. ) +
  299. "var __t, __p = ''" +
  300. (isEscaping
  301. ? ', __e = _.escape'
  302. : ''
  303. ) +
  304. (isEvaluating
  305. ? ', __j = Array.prototype.join;\n' +
  306. "function print() { __p += __j.call(arguments, '') }\n"
  307. : ';\n'
  308. ) +
  309. source +
  310. 'return __p\n}';
  311. var result = attempt(function() {
  312. return Function(importsKeys, sourceURL + 'return ' + source).apply(undefined, importsValues);
  313. });
  314. // Provide the compiled function's source by its `toString` method or
  315. // the `source` property as a convenience for inlining compiled templates.
  316. result.source = source;
  317. if (isError(result)) {
  318. throw result;
  319. }
  320. return result;
  321. }
  322. /**
  323. * Attempts to invoke `func`, returning either the result or the caught error
  324. * object. Any additional arguments are provided to `func` when it is invoked.
  325. *
  326. * @static
  327. * @memberOf _
  328. * @category Utility
  329. * @param {*} func The function to attempt.
  330. * @returns {*} Returns the `func` result or error object.
  331. * @example
  332. *
  333. * // avoid throwing errors for invalid selectors
  334. * var elements = _.attempt(function(selector) {
  335. * return document.querySelectorAll(selector);
  336. * }, '>_>');
  337. *
  338. * if (_.isError(elements)) {
  339. * elements = [];
  340. * }
  341. */
  342. function attempt() {
  343. var length = arguments.length,
  344. func = arguments[0];
  345. try {
  346. var args = Array(length ? length - 1 : 0);
  347. while (--length > 0) {
  348. args[length - 1] = arguments[length];
  349. }
  350. return func.apply(undefined, args);
  351. } catch(e) {
  352. return isError(e) ? e : new Error(e);
  353. }
  354. }
  355. module.exports = template;