index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * lodash 3.2.0 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <https://lodash.com/license>
  8. */
  9. var root = require('lodash._root');
  10. /** Used as references for various `Number` constants. */
  11. var INFINITY = 1 / 0;
  12. /** `Object#toString` result references. */
  13. var symbolTag = '[object Symbol]';
  14. /** Used to match HTML entities and HTML characters. */
  15. var reUnescapedHtml = /[&<>"'`]/g,
  16. reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
  17. /** Used to map characters to HTML entities. */
  18. var htmlEscapes = {
  19. '&': '&amp;',
  20. '<': '&lt;',
  21. '>': '&gt;',
  22. '"': '&quot;',
  23. "'": '&#39;',
  24. '`': '&#96;'
  25. };
  26. /**
  27. * Used by `_.escape` to convert characters to HTML entities.
  28. *
  29. * @private
  30. * @param {string} chr The matched character to escape.
  31. * @returns {string} Returns the escaped character.
  32. */
  33. function escapeHtmlChar(chr) {
  34. return htmlEscapes[chr];
  35. }
  36. /** Used for built-in method references. */
  37. var objectProto = Object.prototype;
  38. /**
  39. * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  40. * of values.
  41. */
  42. var objectToString = objectProto.toString;
  43. /** Built-in value references. */
  44. var Symbol = root.Symbol;
  45. /** Used to convert symbols to primitives and strings. */
  46. var symbolProto = Symbol ? Symbol.prototype : undefined,
  47. symbolToString = Symbol ? symbolProto.toString : undefined;
  48. /**
  49. * Checks if `value` is object-like. A value is object-like if it's not `null`
  50. * and has a `typeof` result of "object".
  51. *
  52. * @static
  53. * @memberOf _
  54. * @category Lang
  55. * @param {*} value The value to check.
  56. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  57. * @example
  58. *
  59. * _.isObjectLike({});
  60. * // => true
  61. *
  62. * _.isObjectLike([1, 2, 3]);
  63. * // => true
  64. *
  65. * _.isObjectLike(_.noop);
  66. * // => false
  67. *
  68. * _.isObjectLike(null);
  69. * // => false
  70. */
  71. function isObjectLike(value) {
  72. return !!value && typeof value == 'object';
  73. }
  74. /**
  75. * Checks if `value` is classified as a `Symbol` primitive or object.
  76. *
  77. * @static
  78. * @memberOf _
  79. * @category Lang
  80. * @param {*} value The value to check.
  81. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  82. * @example
  83. *
  84. * _.isSymbol(Symbol.iterator);
  85. * // => true
  86. *
  87. * _.isSymbol('abc');
  88. * // => false
  89. */
  90. function isSymbol(value) {
  91. return typeof value == 'symbol' ||
  92. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  93. }
  94. /**
  95. * Converts `value` to a string if it's not one. An empty string is returned
  96. * for `null` and `undefined` values. The sign of `-0` is preserved.
  97. *
  98. * @static
  99. * @memberOf _
  100. * @category Lang
  101. * @param {*} value The value to process.
  102. * @returns {string} Returns the string.
  103. * @example
  104. *
  105. * _.toString(null);
  106. * // => ''
  107. *
  108. * _.toString(-0);
  109. * // => '-0'
  110. *
  111. * _.toString([1, 2, 3]);
  112. * // => '1,2,3'
  113. */
  114. function toString(value) {
  115. // Exit early for strings to avoid a performance hit in some environments.
  116. if (typeof value == 'string') {
  117. return value;
  118. }
  119. if (value == null) {
  120. return '';
  121. }
  122. if (isSymbol(value)) {
  123. return Symbol ? symbolToString.call(value) : '';
  124. }
  125. var result = (value + '');
  126. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  127. }
  128. /**
  129. * Converts the characters "&", "<", ">", '"', "'", and "\`" in `string` to
  130. * their corresponding HTML entities.
  131. *
  132. * **Note:** No other characters are escaped. To escape additional
  133. * characters use a third-party library like [_he_](https://mths.be/he).
  134. *
  135. * Though the ">" character is escaped for symmetry, characters like
  136. * ">" and "/" don't need escaping in HTML and have no special meaning
  137. * unless they're part of a tag or unquoted attribute value.
  138. * See [Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands)
  139. * (under "semi-related fun fact") for more details.
  140. *
  141. * Backticks are escaped because in IE < 9, they can break out of
  142. * attribute values or HTML comments. See [#59](https://html5sec.org/#59),
  143. * [#102](https://html5sec.org/#102), [#108](https://html5sec.org/#108), and
  144. * [#133](https://html5sec.org/#133) of the [HTML5 Security Cheatsheet](https://html5sec.org/)
  145. * for more details.
  146. *
  147. * When working with HTML you should always [quote attribute values](http://wonko.com/post/html-escaping)
  148. * to reduce XSS vectors.
  149. *
  150. * @static
  151. * @memberOf _
  152. * @category String
  153. * @param {string} [string=''] The string to escape.
  154. * @returns {string} Returns the escaped string.
  155. * @example
  156. *
  157. * _.escape('fred, barney, & pebbles');
  158. * // => 'fred, barney, &amp; pebbles'
  159. */
  160. function escape(string) {
  161. string = toString(string);
  162. return (string && reHasUnescapedHtml.test(string))
  163. ? string.replace(reUnescapedHtml, escapeHtmlChar)
  164. : string;
  165. }
  166. module.exports = escape;