index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  14. * Used to match `RegExp`
  15. * [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns).
  16. */
  17. var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
  18. reHasRegExpChar = RegExp(reRegExpChar.source);
  19. /** Detect free variable `global` from Node.js. */
  20. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  21. /** Detect free variable `self`. */
  22. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  23. /** Used as a reference to the global object. */
  24. var root = freeGlobal || freeSelf || Function('return this')();
  25. /** Used for built-in method references. */
  26. var objectProto = Object.prototype;
  27. /**
  28. * Used to resolve the
  29. * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  30. * of values.
  31. */
  32. var objectToString = objectProto.toString;
  33. /** Built-in value references. */
  34. var Symbol = root.Symbol;
  35. /** Used to convert symbols to primitives and strings. */
  36. var symbolProto = Symbol ? Symbol.prototype : undefined,
  37. symbolToString = symbolProto ? symbolProto.toString : undefined;
  38. /**
  39. * The base implementation of `_.toString` which doesn't convert nullish
  40. * values to empty strings.
  41. *
  42. * @private
  43. * @param {*} value The value to process.
  44. * @returns {string} Returns the string.
  45. */
  46. function baseToString(value) {
  47. // Exit early for strings to avoid a performance hit in some environments.
  48. if (typeof value == 'string') {
  49. return value;
  50. }
  51. if (isSymbol(value)) {
  52. return symbolToString ? symbolToString.call(value) : '';
  53. }
  54. var result = (value + '');
  55. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  56. }
  57. /**
  58. * Checks if `value` is object-like. A value is object-like if it's not `null`
  59. * and has a `typeof` result of "object".
  60. *
  61. * @static
  62. * @memberOf _
  63. * @since 4.0.0
  64. * @category Lang
  65. * @param {*} value The value to check.
  66. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  67. * @example
  68. *
  69. * _.isObjectLike({});
  70. * // => true
  71. *
  72. * _.isObjectLike([1, 2, 3]);
  73. * // => true
  74. *
  75. * _.isObjectLike(_.noop);
  76. * // => false
  77. *
  78. * _.isObjectLike(null);
  79. * // => false
  80. */
  81. function isObjectLike(value) {
  82. return !!value && typeof value == 'object';
  83. }
  84. /**
  85. * Checks if `value` is classified as a `Symbol` primitive or 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 a symbol, else `false`.
  93. * @example
  94. *
  95. * _.isSymbol(Symbol.iterator);
  96. * // => true
  97. *
  98. * _.isSymbol('abc');
  99. * // => false
  100. */
  101. function isSymbol(value) {
  102. return typeof value == 'symbol' ||
  103. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  104. }
  105. /**
  106. * Converts `value` to a string. An empty string is returned for `null`
  107. * and `undefined` values. The sign of `-0` is preserved.
  108. *
  109. * @static
  110. * @memberOf _
  111. * @since 4.0.0
  112. * @category Lang
  113. * @param {*} value The value to process.
  114. * @returns {string} Returns the string.
  115. * @example
  116. *
  117. * _.toString(null);
  118. * // => ''
  119. *
  120. * _.toString(-0);
  121. * // => '-0'
  122. *
  123. * _.toString([1, 2, 3]);
  124. * // => '1,2,3'
  125. */
  126. function toString(value) {
  127. return value == null ? '' : baseToString(value);
  128. }
  129. /**
  130. * Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
  131. * "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
  132. *
  133. * @static
  134. * @memberOf _
  135. * @since 3.0.0
  136. * @category String
  137. * @param {string} [string=''] The string to escape.
  138. * @returns {string} Returns the escaped string.
  139. * @example
  140. *
  141. * _.escapeRegExp('[lodash](https://lodash.com/)');
  142. * // => '\[lodash\]\(https://lodash\.com/\)'
  143. */
  144. function escapeRegExp(string) {
  145. string = toString(string);
  146. return (string && reHasRegExpChar.test(string))
  147. ? string.replace(reRegExpChar, '\\$&')
  148. : string;
  149. }
  150. module.exports = escapeRegExp;