check-error 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +19 -5
  2. package/package.json +5 -4
package/index.js CHANGED
@@ -1,3 +1,17 @@
1
+ function isErrorInstance(obj) {
2
+ // eslint-disable-next-line prefer-reflect
3
+ return obj instanceof Error || Object.prototype.toString.call(obj) === '[object Error]';
4
+ }
5
+
6
+ function isErrorClass(obj) {
7
+ return obj === Error || (typeof obj === 'function' && obj.name === 'Error');
8
+ }
9
+
10
+ function isRegExp(obj) {
11
+ // eslint-disable-next-line prefer-reflect
12
+ return Object.prototype.toString.call(obj) === '[object RegExp]';
13
+ }
14
+
1
15
  /**
2
16
  * ### .compatibleInstance(thrown, errorLike)
3
17
  *
@@ -13,7 +27,7 @@
13
27
  */
14
28
 
15
29
  function compatibleInstance(thrown, errorLike) {
16
- return errorLike instanceof Error && thrown === errorLike;
30
+ return isErrorInstance(errorLike) && thrown === errorLike;
17
31
  }
18
32
 
19
33
  /**
@@ -33,10 +47,10 @@ function compatibleInstance(thrown, errorLike) {
33
47
  */
34
48
 
35
49
  function compatibleConstructor(thrown, errorLike) {
36
- if (errorLike instanceof Error) {
50
+ if (isErrorInstance(errorLike)) {
37
51
  // If `errorLike` is an instance of any error we compare their constructors
38
52
  return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor;
39
- } else if (errorLike.prototype instanceof Error || errorLike === Error) {
53
+ } else if (isErrorClass(Object.getPrototypeOf(errorLike)) || isErrorClass(errorLike)) {
40
54
  // If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly
41
55
  return thrown.constructor === errorLike || thrown instanceof errorLike;
42
56
  }
@@ -60,7 +74,7 @@ function compatibleConstructor(thrown, errorLike) {
60
74
 
61
75
  function compatibleMessage(thrown, errMatcher) {
62
76
  const comparisonString = typeof thrown === 'string' ? thrown : thrown.message;
63
- if (errMatcher instanceof RegExp) {
77
+ if (isRegExp(errMatcher)) {
64
78
  return errMatcher.test(comparisonString);
65
79
  } else if (typeof errMatcher === 'string') {
66
80
  return comparisonString.indexOf(errMatcher) !== -1; // eslint-disable-line no-magic-numbers
@@ -82,7 +96,7 @@ function compatibleMessage(thrown, errMatcher) {
82
96
 
83
97
  function getConstructorName(errorLike) {
84
98
  let constructorName = errorLike;
85
- if (errorLike instanceof Error) {
99
+ if (isErrorInstance(errorLike)) {
86
100
  constructorName = errorLike.constructor.name;
87
101
  } else if (typeof errorLike === 'function') {
88
102
  // If `err` is not an instance of Error it is an error constructor itself or another function.
package/package.json CHANGED
@@ -1,4 +1,5 @@
1
1
  {
2
+ "version": "2.1.0",
2
3
  "name": "check-error",
3
4
  "description": "Error comparison and information related utility for node and the browser",
4
5
  "keywords": [
@@ -32,7 +33,7 @@
32
33
  "semantic-release": "semantic-release pre && npm publish && semantic-release post",
33
34
  "pretest": "npm run lint && npm run build",
34
35
  "test": "npm run test:node && npm run test:browser",
35
- "test:browser": "web-test-runner --node-resolve test/",
36
+ "test:browser": "web-test-runner",
36
37
  "test:node": "mocha"
37
38
  },
38
39
  "config": {
@@ -53,7 +54,8 @@
53
54
  "rules": {
54
55
  "complexity": "off",
55
56
  "max-statements": "off",
56
- "prefer-arrow-callback": "off"
57
+ "prefer-arrow-callback": "off",
58
+ "prefer-reflect": "off"
57
59
  }
58
60
  },
59
61
  "devDependencies": {
@@ -74,6 +76,5 @@
74
76
  },
75
77
  "engines": {
76
78
  "node": ">= 16"
77
- },
78
- "version": "2.0.0"
79
+ }
79
80
  }