is.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. // eslint-disable-next-line @typescript-eslint/unbound-method
  3. const objectToString = Object.prototype.toString;
  4. /**
  5. * Checks whether given value's type is one of a few Error or Error-like
  6. * {@link isError}.
  7. *
  8. * @param wat A value to be checked.
  9. * @returns A boolean representing the result.
  10. */
  11. function isError(wat) {
  12. switch (objectToString.call(wat)) {
  13. case '[object Error]':
  14. case '[object Exception]':
  15. case '[object DOMException]':
  16. return true;
  17. default:
  18. return isInstanceOf(wat, Error);
  19. }
  20. }
  21. /**
  22. * Checks whether given value is an instance of the given built-in class.
  23. *
  24. * @param wat The value to be checked
  25. * @param className
  26. * @returns A boolean representing the result.
  27. */
  28. function isBuiltin(wat, className) {
  29. return objectToString.call(wat) === `[object ${className}]`;
  30. }
  31. /**
  32. * Checks whether given value's type is ErrorEvent
  33. * {@link isErrorEvent}.
  34. *
  35. * @param wat A value to be checked.
  36. * @returns A boolean representing the result.
  37. */
  38. function isErrorEvent(wat) {
  39. return isBuiltin(wat, 'ErrorEvent');
  40. }
  41. /**
  42. * Checks whether given value's type is DOMError
  43. * {@link isDOMError}.
  44. *
  45. * @param wat A value to be checked.
  46. * @returns A boolean representing the result.
  47. */
  48. function isDOMError(wat) {
  49. return isBuiltin(wat, 'DOMError');
  50. }
  51. /**
  52. * Checks whether given value's type is DOMException
  53. * {@link isDOMException}.
  54. *
  55. * @param wat A value to be checked.
  56. * @returns A boolean representing the result.
  57. */
  58. function isDOMException(wat) {
  59. return isBuiltin(wat, 'DOMException');
  60. }
  61. /**
  62. * Checks whether given value's type is a string
  63. * {@link isString}.
  64. *
  65. * @param wat A value to be checked.
  66. * @returns A boolean representing the result.
  67. */
  68. function isString(wat) {
  69. return isBuiltin(wat, 'String');
  70. }
  71. /**
  72. * Checks whether given string is parameterized
  73. * {@link isParameterizedString}.
  74. *
  75. * @param wat A value to be checked.
  76. * @returns A boolean representing the result.
  77. */
  78. function isParameterizedString(wat) {
  79. return (
  80. typeof wat === 'object' &&
  81. wat !== null &&
  82. '__sentry_template_string__' in wat &&
  83. '__sentry_template_values__' in wat
  84. );
  85. }
  86. /**
  87. * Checks whether given value is a primitive (undefined, null, number, boolean, string, bigint, symbol)
  88. * {@link isPrimitive}.
  89. *
  90. * @param wat A value to be checked.
  91. * @returns A boolean representing the result.
  92. */
  93. function isPrimitive(wat) {
  94. return wat === null || isParameterizedString(wat) || (typeof wat !== 'object' && typeof wat !== 'function');
  95. }
  96. /**
  97. * Checks whether given value's type is an object literal, or a class instance.
  98. * {@link isPlainObject}.
  99. *
  100. * @param wat A value to be checked.
  101. * @returns A boolean representing the result.
  102. */
  103. function isPlainObject(wat) {
  104. return isBuiltin(wat, 'Object');
  105. }
  106. /**
  107. * Checks whether given value's type is an Event instance
  108. * {@link isEvent}.
  109. *
  110. * @param wat A value to be checked.
  111. * @returns A boolean representing the result.
  112. */
  113. function isEvent(wat) {
  114. return typeof Event !== 'undefined' && isInstanceOf(wat, Event);
  115. }
  116. /**
  117. * Checks whether given value's type is an Element instance
  118. * {@link isElement}.
  119. *
  120. * @param wat A value to be checked.
  121. * @returns A boolean representing the result.
  122. */
  123. function isElement(wat) {
  124. return typeof Element !== 'undefined' && isInstanceOf(wat, Element);
  125. }
  126. /**
  127. * Checks whether given value's type is an regexp
  128. * {@link isRegExp}.
  129. *
  130. * @param wat A value to be checked.
  131. * @returns A boolean representing the result.
  132. */
  133. function isRegExp(wat) {
  134. return isBuiltin(wat, 'RegExp');
  135. }
  136. /**
  137. * Checks whether given value has a then function.
  138. * @param wat A value to be checked.
  139. */
  140. function isThenable(wat) {
  141. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  142. return Boolean(wat && wat.then && typeof wat.then === 'function');
  143. }
  144. /**
  145. * Checks whether given value's type is a SyntheticEvent
  146. * {@link isSyntheticEvent}.
  147. *
  148. * @param wat A value to be checked.
  149. * @returns A boolean representing the result.
  150. */
  151. function isSyntheticEvent(wat) {
  152. return isPlainObject(wat) && 'nativeEvent' in wat && 'preventDefault' in wat && 'stopPropagation' in wat;
  153. }
  154. /**
  155. * Checks whether given value is NaN
  156. * {@link isNaN}.
  157. *
  158. * @param wat A value to be checked.
  159. * @returns A boolean representing the result.
  160. */
  161. function isNaN(wat) {
  162. return typeof wat === 'number' && wat !== wat;
  163. }
  164. /**
  165. * Checks whether given value's type is an instance of provided constructor.
  166. * {@link isInstanceOf}.
  167. *
  168. * @param wat A value to be checked.
  169. * @param base A constructor to be used in a check.
  170. * @returns A boolean representing the result.
  171. */
  172. function isInstanceOf(wat, base) {
  173. try {
  174. return wat instanceof base;
  175. } catch (_e) {
  176. return false;
  177. }
  178. }
  179. /**
  180. * Checks whether given value's type is a Vue ViewModel.
  181. *
  182. * @param wat A value to be checked.
  183. * @returns A boolean representing the result.
  184. */
  185. function isVueViewModel(wat) {
  186. // Not using Object.prototype.toString because in Vue 3 it would read the instance's Symbol(Symbol.toStringTag) property.
  187. return !!(typeof wat === 'object' && wat !== null && ((wat ).__isVue || (wat )._isVue));
  188. }
  189. exports.isDOMError = isDOMError;
  190. exports.isDOMException = isDOMException;
  191. exports.isElement = isElement;
  192. exports.isError = isError;
  193. exports.isErrorEvent = isErrorEvent;
  194. exports.isEvent = isEvent;
  195. exports.isInstanceOf = isInstanceOf;
  196. exports.isNaN = isNaN;
  197. exports.isParameterizedString = isParameterizedString;
  198. exports.isPlainObject = isPlainObject;
  199. exports.isPrimitive = isPrimitive;
  200. exports.isRegExp = isRegExp;
  201. exports.isString = isString;
  202. exports.isSyntheticEvent = isSyntheticEvent;
  203. exports.isThenable = isThenable;
  204. exports.isVueViewModel = isVueViewModel;
  205. //# sourceMappingURL=is.js.map