index.js 5.9 KB

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