Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict';
  2. /* !
  3. * Chai - checkError utility
  4. * Copyright(c) 2012-2016 Jake Luer <jake@alogicalparadox.com>
  5. * MIT Licensed
  6. */
  7. /**
  8. * ### .checkError
  9. *
  10. * Checks that an error conforms to a given set of criteria and/or retrieves information about it.
  11. *
  12. * @api public
  13. */
  14. /**
  15. * ### .compatibleInstance(thrown, errorLike)
  16. *
  17. * Checks if two instances are compatible (strict equal).
  18. * Returns false if errorLike is not an instance of Error, because instances
  19. * can only be compatible if they're both error instances.
  20. *
  21. * @name compatibleInstance
  22. * @param {Error} thrown error
  23. * @param {Error|ErrorConstructor} errorLike object to compare against
  24. * @namespace Utils
  25. * @api public
  26. */
  27. function compatibleInstance(thrown, errorLike) {
  28. return errorLike instanceof Error && thrown === errorLike;
  29. }
  30. /**
  31. * ### .compatibleConstructor(thrown, errorLike)
  32. *
  33. * Checks if two constructors are compatible.
  34. * This function can receive either an error constructor or
  35. * an error instance as the `errorLike` argument.
  36. * Constructors are compatible if they're the same or if one is
  37. * an instance of another.
  38. *
  39. * @name compatibleConstructor
  40. * @param {Error} thrown error
  41. * @param {Error|ErrorConstructor} errorLike object to compare against
  42. * @namespace Utils
  43. * @api public
  44. */
  45. function compatibleConstructor(thrown, errorLike) {
  46. if (errorLike instanceof Error) {
  47. // If `errorLike` is an instance of any error we compare their constructors
  48. return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor;
  49. } else if (errorLike.prototype instanceof Error || errorLike === Error) {
  50. // If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly
  51. return thrown.constructor === errorLike || thrown instanceof errorLike;
  52. }
  53. return false;
  54. }
  55. /**
  56. * ### .compatibleMessage(thrown, errMatcher)
  57. *
  58. * Checks if an error's message is compatible with a matcher (String or RegExp).
  59. * If the message contains the String or passes the RegExp test,
  60. * it is considered compatible.
  61. *
  62. * @name compatibleMessage
  63. * @param {Error} thrown error
  64. * @param {String|RegExp} errMatcher to look for into the message
  65. * @namespace Utils
  66. * @api public
  67. */
  68. function compatibleMessage(thrown, errMatcher) {
  69. var comparisonString = typeof thrown === 'string' ? thrown : thrown.message;
  70. if (errMatcher instanceof RegExp) {
  71. return errMatcher.test(comparisonString);
  72. } else if (typeof errMatcher === 'string') {
  73. return comparisonString.indexOf(errMatcher) !== -1; // eslint-disable-line no-magic-numbers
  74. }
  75. return false;
  76. }
  77. /**
  78. * ### .getFunctionName(constructorFn)
  79. *
  80. * Returns the name of a function.
  81. * This also includes a polyfill function if `constructorFn.name` is not defined.
  82. *
  83. * @name getFunctionName
  84. * @param {Function} constructorFn
  85. * @namespace Utils
  86. * @api private
  87. */
  88. var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\(\/]+)/;
  89. function getFunctionName(constructorFn) {
  90. var name = '';
  91. if (typeof constructorFn.name === 'undefined') {
  92. // Here we run a polyfill if constructorFn.name is not defined
  93. var match = String(constructorFn).match(functionNameMatch);
  94. if (match) {
  95. name = match[1];
  96. }
  97. } else {
  98. name = constructorFn.name;
  99. }
  100. return name;
  101. }
  102. /**
  103. * ### .getConstructorName(errorLike)
  104. *
  105. * Gets the constructor name for an Error instance or constructor itself.
  106. *
  107. * @name getConstructorName
  108. * @param {Error|ErrorConstructor} errorLike
  109. * @namespace Utils
  110. * @api public
  111. */
  112. function getConstructorName(errorLike) {
  113. var constructorName = errorLike;
  114. if (errorLike instanceof Error) {
  115. constructorName = getFunctionName(errorLike.constructor);
  116. } else if (typeof errorLike === 'function') {
  117. // If `err` is not an instance of Error it is an error constructor itself or another function.
  118. // If we've got a common function we get its name, otherwise we may need to create a new instance
  119. // of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more.
  120. constructorName = getFunctionName(errorLike).trim() ||
  121. getFunctionName(new errorLike()); // eslint-disable-line new-cap
  122. }
  123. return constructorName;
  124. }
  125. /**
  126. * ### .getMessage(errorLike)
  127. *
  128. * Gets the error message from an error.
  129. * If `err` is a String itself, we return it.
  130. * If the error has no message, we return an empty string.
  131. *
  132. * @name getMessage
  133. * @param {Error|String} errorLike
  134. * @namespace Utils
  135. * @api public
  136. */
  137. function getMessage(errorLike) {
  138. var msg = '';
  139. if (errorLike && errorLike.message) {
  140. msg = errorLike.message;
  141. } else if (typeof errorLike === 'string') {
  142. msg = errorLike;
  143. }
  144. return msg;
  145. }
  146. module.exports = {
  147. compatibleInstance: compatibleInstance,
  148. compatibleConstructor: compatibleConstructor,
  149. compatibleMessage: compatibleMessage,
  150. getMessage: getMessage,
  151. getConstructorName: getConstructorName,
  152. };