ast-compare 3.0.5 → 3.0.6

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.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @name ast-compare
3
3
  * @fileoverview Compare anything: AST, objects, arrays, strings and nested thereof
4
- * @version 3.0.5
4
+ * @version 3.0.6
5
5
  * @author Roy Revelt, Codsen Ltd
6
6
  * @license MIT
7
7
  * {@link https://codsen.com/os/ast-compare/}
@@ -14,133 +14,158 @@ import { isMatch } from 'matcher';
14
14
 
15
15
  /* istanbul ignore next */
16
16
  function isBlank(something) {
17
- if (isObj(something)) {
18
- return !Object.keys(something).length;
19
- }
20
- if (Array.isArray(something) || typeof something === "string") {
21
- return !something.length;
22
- }
23
- return false;
24
- }
25
- function compare(b, s, originalOpts) {
26
- let sKeys;
27
- let bKeys;
28
- let found;
29
- let bOffset = 0;
30
- const defaults = {
31
- hungryForWhitespace: false,
32
- matchStrictly: false,
33
- verboseWhenMismatches: false,
34
- useWildcards: false
35
- };
36
- const opts = { ...defaults,
37
- ...originalOpts
38
- };
39
- if (opts.hungryForWhitespace && opts.matchStrictly && isObj(b) && empty(b) && isObj(s) && !Object.keys(s).length) {
40
- return true;
41
- }
42
- if ((!opts.hungryForWhitespace || opts.hungryForWhitespace && !empty(b) && empty(s)) && isObj(b) && Object.keys(b).length !== 0 && isObj(s) && Object.keys(s).length === 0 || typeDetect(b) !== typeDetect(s) && (!opts.hungryForWhitespace || opts.hungryForWhitespace && !empty(b))) {
43
- return false;
44
- }
45
- if (typeof b === "string" && typeof s === "string") {
46
- if (opts.hungryForWhitespace && empty(b) && empty(s)) {
47
- return true;
17
+ if (isObj(something)) {
18
+ return !Object.keys(something).length;
48
19
  }
49
- if (opts.verboseWhenMismatches) {
50
- return b === s ? true : `Given string ${s} is not matched! We have ${b} on the other end.`;
20
+ if (Array.isArray(something) || typeof something === "string") {
21
+ return !something.length;
51
22
  }
52
- return opts.useWildcards ? isMatch(b, s, {
53
- caseSensitive: true
54
- }) : b === s;
55
- }
56
- if (Array.isArray(b) && Array.isArray(s)) {
57
- if (opts.hungryForWhitespace && empty(s) && (!opts.matchStrictly || opts.matchStrictly && b.length === s.length)) {
58
- return true;
23
+ return false;
24
+ }
25
+ function compare(b, s, originalOpts) {
26
+ let sKeys;
27
+ let bKeys;
28
+ let found;
29
+ let bOffset = 0;
30
+ const defaults = {
31
+ hungryForWhitespace: false,
32
+ matchStrictly: false,
33
+ verboseWhenMismatches: false,
34
+ useWildcards: false,
35
+ };
36
+ const opts = { ...defaults, ...originalOpts };
37
+ if (opts.hungryForWhitespace &&
38
+ opts.matchStrictly &&
39
+ isObj(b) &&
40
+ empty(b) &&
41
+ isObj(s) &&
42
+ !Object.keys(s).length) {
43
+ return true;
59
44
  }
60
- if (!opts.hungryForWhitespace && s.length > b.length || opts.matchStrictly && s.length !== b.length) {
61
- if (!opts.verboseWhenMismatches) {
45
+ if (((!opts.hungryForWhitespace ||
46
+ (opts.hungryForWhitespace && !empty(b) && empty(s))) &&
47
+ isObj(b) &&
48
+ Object.keys(b).length !== 0 &&
49
+ isObj(s) &&
50
+ Object.keys(s).length === 0) ||
51
+ (typeDetect(b) !== typeDetect(s) &&
52
+ (!opts.hungryForWhitespace || (opts.hungryForWhitespace && !empty(b))))) {
62
53
  return false;
63
- }
64
- return `The length of a given array, ${JSON.stringify(s, null, 4)} is ${s.length} but the length of an array on the other end, ${JSON.stringify(b, null, 4)} is ${b.length}`;
65
- }
66
- if (s.length === 0) {
67
- if (b.length === 0) {
68
- return true;
69
- }
70
- if (opts.verboseWhenMismatches) {
71
- return `The given array has no elements, but the array on the other end, ${JSON.stringify(b, null, 4)} does have some`;
72
- }
73
- return false;
74
54
  }
75
- for (let i = 0, sLen = s.length; i < sLen; i++) {
76
- found = false;
77
- for (let j = bOffset, bLen = b.length; j < bLen; j++) {
78
- bOffset += 1;
79
- if (compare(b[j], s[i], opts) === true) {
80
- found = true;
81
- break;
55
+ if (typeof b === "string" && typeof s === "string") {
56
+ if (opts.hungryForWhitespace && empty(b) && empty(s)) {
57
+ return true;
82
58
  }
83
- }
84
- if (!found) {
85
- if (!opts.verboseWhenMismatches) {
86
- return false;
59
+ if (opts.verboseWhenMismatches) {
60
+ return b === s
61
+ ? true
62
+ : `Given string ${s} is not matched! We have ${b} on the other end.`;
87
63
  }
88
- return `The given array ${JSON.stringify(s, null, 4)} is not a subset of an array on the other end, ${JSON.stringify(b, null, 4)}`;
89
- }
64
+ return opts.useWildcards ? isMatch(b, s, { caseSensitive: true }) : b === s;
90
65
  }
91
- } else if (isObj(b) && isObj(s)) {
92
- sKeys = new Set(Object.keys(s));
93
- bKeys = new Set(Object.keys(b));
94
- if (opts.matchStrictly && sKeys.size !== bKeys.size) {
95
- if (!opts.verboseWhenMismatches) {
96
- return false;
97
- }
98
- const uniqueKeysOnS = new Set([...sKeys].filter(x => !bKeys.has(x)));
99
- const sMessage = uniqueKeysOnS.size ? ` First object has unique keys: ${JSON.stringify(uniqueKeysOnS, null, 4)}.` : "";
100
- const uniqueKeysOnB = new Set([...bKeys].filter(x => !sKeys.has(x)));
101
- const bMessage = uniqueKeysOnB.size ? ` Second object has unique keys:
102
- ${JSON.stringify(uniqueKeysOnB, null, 4)}.` : "";
103
- return `When matching strictly, we found that both objects have different amount of keys.${sMessage}${bMessage}`;
104
- }
105
- for (const sKey of sKeys) {
106
- if (!Object.prototype.hasOwnProperty.call(b, sKey)) {
107
- if (!opts.useWildcards || opts.useWildcards && !sKey.includes("*")) {
108
- if (!opts.verboseWhenMismatches) {
109
- return false;
110
- }
111
- return `The given object has key "${sKey}" which the other-one does not have.`;
66
+ if (Array.isArray(b) && Array.isArray(s)) {
67
+ if (opts.hungryForWhitespace &&
68
+ empty(s) &&
69
+ (!opts.matchStrictly || (opts.matchStrictly && b.length === s.length))) {
70
+ return true;
112
71
  }
113
- if (Object.keys(b).some(bKey => isMatch(bKey, sKey, {
114
- caseSensitive: true
115
- }))) {
116
- return true;
72
+ if ((!opts.hungryForWhitespace && s.length > b.length) ||
73
+ (opts.matchStrictly && s.length !== b.length)) {
74
+ if (!opts.verboseWhenMismatches) {
75
+ return false;
76
+ }
77
+ return `The length of a given array, ${JSON.stringify(s, null, 4)} is ${s.length} but the length of an array on the other end, ${JSON.stringify(b, null, 4)} is ${b.length}`;
117
78
  }
118
- if (!opts.verboseWhenMismatches) {
119
- return false;
120
- }
121
- return `The given object has key "${sKey}" which the other-one does not have.`;
122
- }
123
- if (b[sKey] != null && typeDetect(b[sKey]) !== typeDetect(s[sKey])) {
124
- if (!(empty(b[sKey]) && empty(s[sKey]) && opts.hungryForWhitespace)) {
125
- if (!opts.verboseWhenMismatches) {
79
+ if (s.length === 0) {
80
+ if (b.length === 0) {
81
+ return true;
82
+ }
83
+ if (opts.verboseWhenMismatches) {
84
+ return `The given array has no elements, but the array on the other end, ${JSON.stringify(b, null, 4)} does have some`;
85
+ }
126
86
  return false;
127
- }
128
- return `The given key ${sKey} is of a different type on both objects. On the first-one, it's ${typeDetect(s[sKey])}, on the second-one, it's ${typeDetect(b[sKey])}`;
129
87
  }
130
- } else if (compare(b[sKey], s[sKey], opts) !== true) {
131
- if (!opts.verboseWhenMismatches) {
132
- return false;
88
+ for (let i = 0, sLen = s.length; i < sLen; i++) {
89
+ found = false;
90
+ for (let j = bOffset, bLen = b.length; j < bLen; j++) {
91
+ bOffset += 1;
92
+ if (compare(b[j], s[i], opts) === true) {
93
+ found = true;
94
+ break;
95
+ }
96
+ }
97
+ if (!found) {
98
+ if (!opts.verboseWhenMismatches) {
99
+ return false;
100
+ }
101
+ return `The given array ${JSON.stringify(s, null, 4)} is not a subset of an array on the other end, ${JSON.stringify(b, null, 4)}`;
102
+ }
133
103
  }
134
- return `The given piece ${JSON.stringify(s[sKey], null, 4)} and ${JSON.stringify(b[sKey], null, 4)} don't match.`;
135
- }
136
104
  }
137
- } else {
138
- if (opts.hungryForWhitespace && empty(b) && empty(s) && (!opts.matchStrictly || opts.matchStrictly && isBlank(s))) {
139
- return true;
105
+ else if (isObj(b) && isObj(s)) {
106
+ sKeys = new Set(Object.keys(s));
107
+ bKeys = new Set(Object.keys(b));
108
+ if (opts.matchStrictly && sKeys.size !== bKeys.size) {
109
+ if (!opts.verboseWhenMismatches) {
110
+ return false;
111
+ }
112
+ const uniqueKeysOnS = new Set([...sKeys].filter((x) => !bKeys.has(x)));
113
+ const sMessage = uniqueKeysOnS.size
114
+ ? ` First object has unique keys: ${JSON.stringify(uniqueKeysOnS, null, 4)}.`
115
+ : "";
116
+ const uniqueKeysOnB = new Set([...bKeys].filter((x) => !sKeys.has(x)));
117
+ const bMessage = uniqueKeysOnB.size
118
+ ? ` Second object has unique keys:
119
+ ${JSON.stringify(uniqueKeysOnB, null, 4)}.`
120
+ : "";
121
+ return `When matching strictly, we found that both objects have different amount of keys.${sMessage}${bMessage}`;
122
+ }
123
+ for (const sKey of sKeys) {
124
+ if (!Object.prototype.hasOwnProperty.call(b, sKey)) {
125
+ if (!opts.useWildcards || (opts.useWildcards && !sKey.includes("*"))) {
126
+ if (!opts.verboseWhenMismatches) {
127
+ return false;
128
+ }
129
+ return `The given object has key "${sKey}" which the other-one does not have.`;
130
+ }
131
+ if (Object.keys(b).some((bKey) => isMatch(bKey, sKey, { caseSensitive: true }))) {
132
+ return true;
133
+ }
134
+ if (!opts.verboseWhenMismatches) {
135
+ return false;
136
+ }
137
+ return `The given object has key "${sKey}" which the other-one does not have.`;
138
+ }
139
+ if (b[sKey] != null &&
140
+ typeDetect(b[sKey]) !==
141
+ typeDetect(s[sKey])) {
142
+ if (!(empty(b[sKey]) &&
143
+ empty(s[sKey]) &&
144
+ opts.hungryForWhitespace)) {
145
+ if (!opts.verboseWhenMismatches) {
146
+ return false;
147
+ }
148
+ return `The given key ${sKey} is of a different type on both objects. On the first-one, it's ${typeDetect(s[sKey])}, on the second-one, it's ${typeDetect(b[sKey])}`;
149
+ }
150
+ }
151
+ else if (compare(b[sKey], s[sKey], opts) !== true) {
152
+ if (!opts.verboseWhenMismatches) {
153
+ return false;
154
+ }
155
+ return `The given piece ${JSON.stringify(s[sKey], null, 4)} and ${JSON.stringify(b[sKey], null, 4)} don't match.`;
156
+ }
157
+ }
158
+ }
159
+ else {
160
+ if (opts.hungryForWhitespace &&
161
+ empty(b) &&
162
+ empty(s) &&
163
+ (!opts.matchStrictly || (opts.matchStrictly && isBlank(s)))) {
164
+ return true;
165
+ }
166
+ return b === s;
140
167
  }
141
- return b === s;
142
- }
143
- return true;
168
+ return true;
144
169
  }
145
170
 
146
171
  export { compare };
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @name ast-compare
3
3
  * @fileoverview Compare anything: AST, objects, arrays, strings and nested thereof
4
- * @version 3.0.5
4
+ * @version 3.0.6
5
5
  * @author Roy Revelt, Codsen Ltd
6
6
  * @license MIT
7
7
  * {@link https://codsen.com/os/ast-compare/}
@@ -11,7 +11,7 @@
11
11
  /**
12
12
  * @name ast-monkey-util
13
13
  * @fileoverview Utility library of AST helper functions
14
- * @version 2.0.5
14
+ * @version 2.0.6
15
15
  * @author Roy Revelt, Codsen Ltd
16
16
  * @license MIT
17
17
  * {@link https://codsen.com/os/ast-monkey-util/}
@@ -19,7 +19,7 @@
19
19
  /**
20
20
  * @name ast-monkey-traverse
21
21
  * @fileoverview Utility library to traverse AST
22
- * @version 3.0.5
22
+ * @version 3.0.6
23
23
  * @author Roy Revelt, Codsen Ltd
24
24
  * @license MIT
25
25
  * {@link https://codsen.com/os/ast-monkey-traverse/}
@@ -27,7 +27,7 @@
27
27
  /**
28
28
  * @name ast-contains-only-empty-space
29
29
  * @fileoverview Does AST contain only empty space?
30
- * @version 3.0.5
30
+ * @version 3.0.6
31
31
  * @author Roy Revelt, Codsen Ltd
32
32
  * @license MIT
33
33
  * {@link https://codsen.com/os/ast-contains-only-empty-space/}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ast-compare",
3
- "version": "3.0.5",
3
+ "version": "3.0.6",
4
4
  "description": "Compare anything: AST, objects, arrays, strings and nested thereof",
5
5
  "keywords": [
6
6
  "array",
@@ -41,13 +41,12 @@
41
41
  "types": "types/index.d.ts",
42
42
  "scripts": {
43
43
  "build": "rollup -c",
44
- "ci_test": "npm run build && npm run format && tap --no-only --reporter=silent --output-file=testStats.md && npm run clean_cov",
45
- "clean_cov": "../../scripts/leaveCoverageTotalOnly.js",
44
+ "build:esbuild": "node '../../scripts/esbuild.js'",
45
+ "build:esbuild:dev": "cross-env MODE=dev node '../../scripts/esbuild.js'",
46
+ "ci_test": "npm run build && npm run format && tap --no-only --reporter=silent",
46
47
  "clean_types": "../../scripts/cleanTypes.js",
47
48
  "dev": "rollup -c --dev",
48
49
  "devunittest": "npm run dev && tap --only -R 'base'",
49
- "esbuild": "node '../../scripts/esbuild.js'",
50
- "esbuild_dev": "cross-env MODE=dev node '../../scripts/esbuild.js'",
51
50
  "format": "npm run lect && npm run prettier && npm run lint",
52
51
  "lect": "lect",
53
52
  "lint": "../../node_modules/eslint/bin/eslint.js . --ext .js --ext .ts --fix --config \"../../.eslintrc.json\" --quiet",
@@ -56,17 +55,14 @@
56
55
  "republish": "npm publish || :",
57
56
  "tap": "tap",
58
57
  "pretest": "npm run build",
59
- "test": "npm run lint && npm run unittest && npm run test:examples && npm run clean_cov && npm run format",
58
+ "test": "npm run test:ci && npm run perf",
59
+ "test:ci": "npm run unittest && npm run test:examples && npm run format",
60
60
  "test:examples": "../../scripts/test-examples.js && npm run lect && npm run prettier",
61
61
  "tsc": "tsc",
62
- "unittest": "tap --no-only --output-file=testStats.md --reporter=terse && tsc -p tsconfig.json --noEmit && npm run clean_cov && npm run perf"
62
+ "unittest": "tap --no-only --reporter=terse && tsc -p tsconfig.json --noEmit"
63
63
  },
64
64
  "tap": {
65
65
  "check-coverage": false,
66
- "coverage-report": [
67
- "json-summary",
68
- "text"
69
- ],
70
66
  "node-arg": [
71
67
  "--no-warnings",
72
68
  "--experimental-loader",
@@ -90,8 +86,8 @@
90
86
  }
91
87
  },
92
88
  "dependencies": {
93
- "@babel/runtime": "^7.16.0",
94
- "ast-contains-only-empty-space": "^3.0.5",
89
+ "@babel/runtime": "^7.16.3",
90
+ "ast-contains-only-empty-space": "^3.0.6",
95
91
  "lodash.isplainobject": "^4.0.6",
96
92
  "matcher": "^5.0.0",
97
93
  "type-detect": "^4.0.8"
@@ -105,8 +101,8 @@
105
101
  "@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.0",
106
102
  "@babel/plugin-proposal-object-rest-spread": "^7.16.0",
107
103
  "@babel/plugin-proposal-optional-chaining": "^7.16.0",
108
- "@babel/plugin-transform-runtime": "^7.16.0",
109
- "@babel/preset-env": "^7.16.0",
104
+ "@babel/plugin-transform-runtime": "^7.16.4",
105
+ "@babel/preset-env": "^7.16.4",
110
106
  "@babel/preset-typescript": "^7.16.0",
111
107
  "@babel/register": "^7.16.0",
112
108
  "@istanbuljs/esm-loader-hook": "^0.1.2",
@@ -116,27 +112,27 @@
116
112
  "@rollup/plugin-strip": "^2.1.0",
117
113
  "@rollup/plugin-typescript": "^8.3.0",
118
114
  "@types/lodash.isplainobject": "^4.0.6",
119
- "@types/node": "^16.11.6",
115
+ "@types/node": "^16.11.9",
120
116
  "@types/tap": "^15.0.5",
121
117
  "@types/type-detect": "^4.0.1",
122
- "@typescript-eslint/eslint-plugin": "^5.3.0",
123
- "@typescript-eslint/parser": "^5.3.0",
118
+ "@typescript-eslint/eslint-plugin": "^5.4.0",
119
+ "@typescript-eslint/parser": "^5.4.0",
124
120
  "core-js": "^3.19.1",
125
121
  "cross-env": "^7.0.3",
126
- "eslint": "^8.2.0",
127
- "lect": "^0.18.5",
128
- "rollup": "^2.59.0",
122
+ "eslint": "^8.3.0",
123
+ "lect": "^0.18.6",
124
+ "rollup": "^2.60.0",
129
125
  "rollup-plugin-ascii": "^0.0.3",
130
126
  "rollup-plugin-banner": "^0.2.1",
131
127
  "rollup-plugin-cleanup": "^3.2.1",
132
128
  "rollup-plugin-dts": "^4.0.1",
133
129
  "rollup-plugin-terser": "^7.0.2",
134
- "tap": "^15.0.10",
130
+ "tap": "^15.1.2",
135
131
  "tslib": "^2.3.1",
136
- "type-fest": "^2.5.2",
137
- "typescript": "^4.4.4"
132
+ "type-fest": "^2.5.4",
133
+ "typescript": "^4.5.2"
138
134
  },
139
135
  "engines": {
140
- "node": ">=12"
136
+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
141
137
  }
142
138
  }