index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * Lodash (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
  5. * Released under MIT license <https://lodash.com/license>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. */
  9. var reInterpolate = require('lodash._reinterpolate');
  10. /** Used as references for various `Number` constants. */
  11. var INFINITY = 1 / 0;
  12. /** `Object#toString` result references. */
  13. var nullTag = '[object Null]',
  14. symbolTag = '[object Symbol]',
  15. undefinedTag = '[object Undefined]';
  16. /** Used to match HTML entities and HTML characters. */
  17. var reUnescapedHtml = /[&<>"']/g,
  18. reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
  19. /** Used to match template delimiters. */
  20. var reEscape = /<%-([\s\S]+?)%>/g,
  21. reEvaluate = /<%([\s\S]+?)%>/g;
  22. /** Used to map characters to HTML entities. */
  23. var htmlEscapes = {
  24. '&': '&amp;',
  25. '<': '&lt;',
  26. '>': '&gt;',
  27. '"': '&quot;',
  28. "'": '&#39;'
  29. };
  30. /** Detect free variable `global` from Node.js. */
  31. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  32. /** Detect free variable `self`. */
  33. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  34. /** Used as a reference to the global object. */
  35. var root = freeGlobal || freeSelf || Function('return this')();
  36. /**
  37. * A specialized version of `_.map` for arrays without support for iteratee
  38. * shorthands.
  39. *
  40. * @private
  41. * @param {Array} [array] The array to iterate over.
  42. * @param {Function} iteratee The function invoked per iteration.
  43. * @returns {Array} Returns the new mapped array.
  44. */
  45. function arrayMap(array, iteratee) {
  46. var index = -1,
  47. length = array == null ? 0 : array.length,
  48. result = Array(length);
  49. while (++index < length) {
  50. result[index] = iteratee(array[index], index, array);
  51. }
  52. return result;
  53. }
  54. /**
  55. * The base implementation of `_.propertyOf` without support for deep paths.
  56. *
  57. * @private
  58. * @param {Object} object The object to query.
  59. * @returns {Function} Returns the new accessor function.
  60. */
  61. function basePropertyOf(object) {
  62. return function(key) {
  63. return object == null ? undefined : object[key];
  64. };
  65. }
  66. /**
  67. * Used by `_.escape` to convert characters to HTML entities.
  68. *
  69. * @private
  70. * @param {string} chr The matched character to escape.
  71. * @returns {string} Returns the escaped character.
  72. */
  73. var escapeHtmlChar = basePropertyOf(htmlEscapes);
  74. /** Used for built-in method references. */
  75. var objectProto = Object.prototype;
  76. /** Used to check objects for own properties. */
  77. var hasOwnProperty = objectProto.hasOwnProperty;
  78. /**
  79. * Used to resolve the
  80. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  81. * of values.
  82. */
  83. var nativeObjectToString = objectProto.toString;
  84. /** Built-in value references. */
  85. var Symbol = root.Symbol,
  86. symToStringTag = Symbol ? Symbol.toStringTag : undefined;
  87. /** Used to convert symbols to primitives and strings. */
  88. var symbolProto = Symbol ? Symbol.prototype : undefined,
  89. symbolToString = symbolProto ? symbolProto.toString : undefined;
  90. /**
  91. * By default, the template delimiters used by lodash are like those in
  92. * embedded Ruby (ERB) as well as ES2015 template strings. Change the
  93. * following template settings to use alternative delimiters.
  94. *
  95. * @static
  96. * @memberOf _
  97. * @type {Object}
  98. */
  99. var templateSettings = {
  100. /**
  101. * Used to detect `data` property values to be HTML-escaped.
  102. *
  103. * @memberOf _.templateSettings
  104. * @type {RegExp}
  105. */
  106. 'escape': reEscape,
  107. /**
  108. * Used to detect code to be evaluated.
  109. *
  110. * @memberOf _.templateSettings
  111. * @type {RegExp}
  112. */
  113. 'evaluate': reEvaluate,
  114. /**
  115. * Used to detect `data` property values to inject.
  116. *
  117. * @memberOf _.templateSettings
  118. * @type {RegExp}
  119. */
  120. 'interpolate': reInterpolate,
  121. /**
  122. * Used to reference the data object in the template text.
  123. *
  124. * @memberOf _.templateSettings
  125. * @type {string}
  126. */
  127. 'variable': '',
  128. /**
  129. * Used to import variables into the compiled template.
  130. *
  131. * @memberOf _.templateSettings
  132. * @type {Object}
  133. */
  134. 'imports': {
  135. /**
  136. * A reference to the `lodash` function.
  137. *
  138. * @memberOf _.templateSettings.imports
  139. * @type {Function}
  140. */
  141. '_': { 'escape': escape }
  142. }
  143. };
  144. /**
  145. * The base implementation of `getTag` without fallbacks for buggy environments.
  146. *
  147. * @private
  148. * @param {*} value The value to query.
  149. * @returns {string} Returns the `toStringTag`.
  150. */
  151. function baseGetTag(value) {
  152. if (value == null) {
  153. return value === undefined ? undefinedTag : nullTag;
  154. }
  155. return (symToStringTag && symToStringTag in Object(value))
  156. ? getRawTag(value)
  157. : objectToString(value);
  158. }
  159. /**
  160. * The base implementation of `_.toString` which doesn't convert nullish
  161. * values to empty strings.
  162. *
  163. * @private
  164. * @param {*} value The value to process.
  165. * @returns {string} Returns the string.
  166. */
  167. function baseToString(value) {
  168. // Exit early for strings to avoid a performance hit in some environments.
  169. if (typeof value == 'string') {
  170. return value;
  171. }
  172. if (isArray(value)) {
  173. // Recursively convert values (susceptible to call stack limits).
  174. return arrayMap(value, baseToString) + '';
  175. }
  176. if (isSymbol(value)) {
  177. return symbolToString ? symbolToString.call(value) : '';
  178. }
  179. var result = (value + '');
  180. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  181. }
  182. /**
  183. * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.
  184. *
  185. * @private
  186. * @param {*} value The value to query.
  187. * @returns {string} Returns the raw `toStringTag`.
  188. */
  189. function getRawTag(value) {
  190. var isOwn = hasOwnProperty.call(value, symToStringTag),
  191. tag = value[symToStringTag];
  192. try {
  193. value[symToStringTag] = undefined;
  194. var unmasked = true;
  195. } catch (e) {}
  196. var result = nativeObjectToString.call(value);
  197. if (unmasked) {
  198. if (isOwn) {
  199. value[symToStringTag] = tag;
  200. } else {
  201. delete value[symToStringTag];
  202. }
  203. }
  204. return result;
  205. }
  206. /**
  207. * Converts `value` to a string using `Object.prototype.toString`.
  208. *
  209. * @private
  210. * @param {*} value The value to convert.
  211. * @returns {string} Returns the converted string.
  212. */
  213. function objectToString(value) {
  214. return nativeObjectToString.call(value);
  215. }
  216. /**
  217. * Checks if `value` is classified as an `Array` object.
  218. *
  219. * @static
  220. * @memberOf _
  221. * @since 0.1.0
  222. * @category Lang
  223. * @param {*} value The value to check.
  224. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  225. * @example
  226. *
  227. * _.isArray([1, 2, 3]);
  228. * // => true
  229. *
  230. * _.isArray(document.body.children);
  231. * // => false
  232. *
  233. * _.isArray('abc');
  234. * // => false
  235. *
  236. * _.isArray(_.noop);
  237. * // => false
  238. */
  239. var isArray = Array.isArray;
  240. /**
  241. * Checks if `value` is object-like. A value is object-like if it's not `null`
  242. * and has a `typeof` result of "object".
  243. *
  244. * @static
  245. * @memberOf _
  246. * @since 4.0.0
  247. * @category Lang
  248. * @param {*} value The value to check.
  249. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  250. * @example
  251. *
  252. * _.isObjectLike({});
  253. * // => true
  254. *
  255. * _.isObjectLike([1, 2, 3]);
  256. * // => true
  257. *
  258. * _.isObjectLike(_.noop);
  259. * // => false
  260. *
  261. * _.isObjectLike(null);
  262. * // => false
  263. */
  264. function isObjectLike(value) {
  265. return value != null && typeof value == 'object';
  266. }
  267. /**
  268. * Checks if `value` is classified as a `Symbol` primitive or object.
  269. *
  270. * @static
  271. * @memberOf _
  272. * @since 4.0.0
  273. * @category Lang
  274. * @param {*} value The value to check.
  275. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  276. * @example
  277. *
  278. * _.isSymbol(Symbol.iterator);
  279. * // => true
  280. *
  281. * _.isSymbol('abc');
  282. * // => false
  283. */
  284. function isSymbol(value) {
  285. return typeof value == 'symbol' ||
  286. (isObjectLike(value) && baseGetTag(value) == symbolTag);
  287. }
  288. /**
  289. * Converts `value` to a string. An empty string is returned for `null`
  290. * and `undefined` values. The sign of `-0` is preserved.
  291. *
  292. * @static
  293. * @memberOf _
  294. * @since 4.0.0
  295. * @category Lang
  296. * @param {*} value The value to convert.
  297. * @returns {string} Returns the converted string.
  298. * @example
  299. *
  300. * _.toString(null);
  301. * // => ''
  302. *
  303. * _.toString(-0);
  304. * // => '-0'
  305. *
  306. * _.toString([1, 2, 3]);
  307. * // => '1,2,3'
  308. */
  309. function toString(value) {
  310. return value == null ? '' : baseToString(value);
  311. }
  312. /**
  313. * Converts the characters "&", "<", ">", '"', and "'" in `string` to their
  314. * corresponding HTML entities.
  315. *
  316. * **Note:** No other characters are escaped. To escape additional
  317. * characters use a third-party library like [_he_](https://mths.be/he).
  318. *
  319. * Though the ">" character is escaped for symmetry, characters like
  320. * ">" and "/" don't need escaping in HTML and have no special meaning
  321. * unless they're part of a tag or unquoted attribute value. See
  322. * [Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands)
  323. * (under "semi-related fun fact") for more details.
  324. *
  325. * When working with HTML you should always
  326. * [quote attribute values](http://wonko.com/post/html-escaping) to reduce
  327. * XSS vectors.
  328. *
  329. * @static
  330. * @since 0.1.0
  331. * @memberOf _
  332. * @category String
  333. * @param {string} [string=''] The string to escape.
  334. * @returns {string} Returns the escaped string.
  335. * @example
  336. *
  337. * _.escape('fred, barney, & pebbles');
  338. * // => 'fred, barney, &amp; pebbles'
  339. */
  340. function escape(string) {
  341. string = toString(string);
  342. return (string && reHasUnescapedHtml.test(string))
  343. ? string.replace(reUnescapedHtml, escapeHtmlChar)
  344. : string;
  345. }
  346. module.exports = templateSettings;