is.js 5.3 KB

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