ftmocks-utils 1.3.0 → 1.3.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,21 +1,31 @@
1
1
  const path = require("path");
2
2
  const fs = require("fs");
3
+ const { FtJSON } = require("./json-utils");
3
4
 
4
5
  const charDifference = (str1, str2) => {
5
- let count1 = {},
6
- count2 = {};
6
+ if (str1 && str2) {
7
+ let count1 = {};
8
+ let count2 = {};
7
9
 
8
- for (let ch of str1) count1[ch] = (count1[ch] || 0) + 1;
9
- for (let ch of str2) count2[ch] = (count2[ch] || 0) + 1;
10
+ for (let ch of str1) count1[ch] = (count1[ch] || 0) + 1;
11
+ for (let ch of str2) count2[ch] = (count2[ch] || 0) + 1;
10
12
 
11
- let diff = 0;
12
- let chars = new Set([...Object.keys(count1), ...Object.keys(count2)]);
13
+ let diff = 0;
14
+ let chars = new Set([...Object.keys(count1), ...Object.keys(count2)]);
13
15
 
14
- for (let ch of chars) {
15
- diff += Math.abs((count1[ch] || 0) - (count2[ch] || 0));
16
- }
16
+ for (let ch of chars) {
17
+ diff += Math.abs((count1[ch] || 0) - (count2[ch] || 0));
18
+ }
17
19
 
18
- return diff;
20
+ return diff;
21
+ } else {
22
+ if (!str1 && str2) {
23
+ return str2.length;
24
+ } else if (str1 && !str2) {
25
+ return str1.length;
26
+ }
27
+ return 0;
28
+ }
19
29
  };
20
30
 
21
31
  const nameToFolder = (name) => {
@@ -52,37 +62,6 @@ const capitalizeHeaders = (headers) => {
52
62
  );
53
63
  };
54
64
 
55
- const areJsonEqual = (jsonObj1, jsonObj2) => {
56
- // Check if both are objects and not null
57
- if (
58
- typeof jsonObj1 === "object" &&
59
- jsonObj1 !== null &&
60
- typeof jsonObj2 === "object" &&
61
- jsonObj2 !== null
62
- ) {
63
- // Get the keys of both objects
64
- const keys1 = Object.keys(jsonObj1);
65
- const keys2 = Object.keys(jsonObj2);
66
-
67
- // Check if the number of keys is different
68
- if (keys1.length !== keys2.length) {
69
- return false;
70
- }
71
-
72
- // Recursively check each key-value pair
73
- for (let key of keys1) {
74
- if (!keys2.includes(key) || !areJsonEqual(jsonObj1[key], jsonObj2[key])) {
75
- return false;
76
- }
77
- }
78
-
79
- return true;
80
- } else {
81
- // For non-object types, use strict equality comparison
82
- return jsonObj1 === jsonObj2;
83
- }
84
- };
85
-
86
65
  const clearNulls = (postData) => {
87
66
  Object.keys(postData || {}).forEach((key) => {
88
67
  if (postData[key] === null) {
@@ -162,7 +141,6 @@ module.exports = {
162
141
  getFallbackDir,
163
142
  capitalizeHeader,
164
143
  capitalizeHeaders,
165
- areJsonEqual,
166
144
  clearNulls,
167
145
  processURL,
168
146
  getHeaders,
package/src/index.js CHANGED
@@ -6,13 +6,13 @@ const {
6
6
  nameToFolder,
7
7
  getMockDir,
8
8
  getFallbackDir,
9
- areJsonEqual,
10
9
  clearNulls,
11
10
  processURL,
12
11
  getHeaders,
13
12
  countFilesInDirectory,
14
13
  getTestByName,
15
14
  } = require("./common-utils");
15
+ const { FtJSON } = require("./json-utils");
16
16
 
17
17
  const getDefaultMockDataFromConfig = (testConfig) => {
18
18
  const defaultPath = path.join(
@@ -103,11 +103,11 @@ const isSameRequest = (req1, req2) => {
103
103
  (!req1.postData && req2.postData) ||
104
104
  (req1.postData && !req2.postData)
105
105
  ) {
106
- matched = areJsonEqual(req1.postData || {}, req2.postData || {});
106
+ matched = FtJSON.areJsonEqual(req1.postData || {}, req2.postData || {});
107
107
  } else if (
108
108
  req1.postData &&
109
109
  req2.postData &&
110
- !areJsonEqual(req1.postData, req2.postData)
110
+ !FtJSON.areJsonEqual(req1.postData, req2.postData)
111
111
  ) {
112
112
  matched = false;
113
113
  }
@@ -131,8 +131,8 @@ const getSameRequestRank = (req1, req2) => {
131
131
  rank = rank + queryDiff;
132
132
  // Compare post data
133
133
  const charDiff = charDifference(
134
- JSON.stringify(req1.postData || {}),
135
- JSON.stringify(req2.postData || {})
134
+ FtJSON.stringify(req1.postData || {}),
135
+ FtJSON.stringify(req2.postData || {})
136
136
  );
137
137
  rank = rank + charDiff;
138
138
  }
@@ -146,7 +146,7 @@ function compareMockToRequest(mock, req) {
146
146
  );
147
147
  const reqURL = processURL(req.originalUrl, mock.fileContent.ignoreParams);
148
148
  const postData = mock.fileContent.request?.postData?.text
149
- ? JSON.parse(mock.fileContent.request?.postData?.text)
149
+ ? FtJSON.parse(mock.fileContent.request?.postData?.text)
150
150
  : mock.fileContent.request?.postData;
151
151
  return isSameRequest(
152
152
  { url: mockURL, method: mock.fileContent.method, postData },
@@ -166,14 +166,14 @@ function compareMockToFetchRequest(mock, fetchReq) {
166
166
  );
167
167
  const reqURL = processURL(fetchReq.url, mock.fileContent.ignoreParams);
168
168
  const postData = mock.fileContent.request?.postData?.text
169
- ? JSON.parse(mock.fileContent.request?.postData?.text)
169
+ ? FtJSON.parse(mock.fileContent.request?.postData?.text)
170
170
  : mock.fileContent.request?.postData;
171
171
  return isSameRequest(
172
172
  { url: mockURL, method: mock.fileContent.method, postData },
173
173
  {
174
174
  method: fetchReq.options.method || "GET",
175
175
  postData: fetchReq.options.body?.length
176
- ? JSON.parse(fetchReq.options.body)
176
+ ? FtJSON.parse(fetchReq.options.body)
177
177
  : fetchReq.options.body,
178
178
  url: reqURL,
179
179
  }
@@ -193,14 +193,14 @@ function getCompareRankMockToFetchRequest(mock, fetchReq) {
193
193
  );
194
194
  const reqURL = processURL(fetchReq.url, mock.fileContent.ignoreParams);
195
195
  const postData = mock.fileContent.request?.postData?.text
196
- ? JSON.parse(mock.fileContent.request?.postData?.text)
196
+ ? FtJSON.parse(mock.fileContent.request?.postData?.text)
197
197
  : mock.fileContent.request?.postData;
198
198
  return getSameRequestRank(
199
199
  { url: mockURL, method: mock.fileContent.method, postData },
200
200
  {
201
201
  method: fetchReq.options.method || "GET",
202
202
  postData: fetchReq.options.body?.length
203
- ? JSON.parse(fetchReq.options.body)
203
+ ? FtJSON.parse(fetchReq.options.body)
204
204
  : fetchReq.options.body,
205
205
  url: reqURL,
206
206
  }
@@ -530,7 +530,7 @@ async function initiateJestFetch(jest, ftmocksConifg, testName) {
530
530
  return Promise.resolve({
531
531
  status,
532
532
  headers: new Map(Object.entries(headers)),
533
- json: () => Promise.resolve(JSON.parse(content)),
533
+ json: () => Promise.resolve(FtJSON.parse(content)),
534
534
  });
535
535
  });
536
536
 
@@ -767,17 +767,17 @@ const isSameResponse = (req1, req2) => {
767
767
  (!req1.response.content && req2.response.content) ||
768
768
  (req1.response.content && !req2.response.content)
769
769
  ) {
770
- matched = areJsonEqual(
771
- JSON.parse(req1.response.content) || {},
772
- JSON.parse(req2.response.content) || {}
770
+ matched = FtJSON.areJsonEqual(
771
+ FtJSON.parse(req1.response.content) || {},
772
+ FtJSON.parse(req2.response.content) || {}
773
773
  );
774
774
  // console.log('not matched at post Data 0', req1.postData, req2.postData);
775
775
  } else if (
776
776
  req1.response.content &&
777
777
  req2.response.content &&
778
- !areJsonEqual(
779
- JSON.parse(req1.response.content) || {},
780
- JSON.parse(req2.response.content) || {}
778
+ !FtJSON.areJsonEqual(
779
+ FtJSON.parse(req1.response.content) || {},
780
+ FtJSON.parse(req2.response.content) || {}
781
781
  )
782
782
  ) {
783
783
  matched = false;
@@ -0,0 +1,55 @@
1
+ class FtJSON {
2
+ static parse(text, reviver, reference) {
3
+ try {
4
+ return JSON.parse(text, reviver);
5
+ } catch (error) {
6
+ console.error("FtJSON parse error:", error, reference);
7
+ return text;
8
+ }
9
+ }
10
+
11
+ static stringify(value, replacer, space, reference) {
12
+ try {
13
+ return JSON.stringify(value, replacer, space);
14
+ } catch (error) {
15
+ console.error("FtJSON stringify error:", error, reference);
16
+ return value;
17
+ }
18
+ }
19
+
20
+ static areJsonEqual(jsonObj1, jsonObj2) {
21
+ // Check if both are objects and not null
22
+ if (
23
+ typeof jsonObj1 === "object" &&
24
+ jsonObj1 !== null &&
25
+ typeof jsonObj2 === "object" &&
26
+ jsonObj2 !== null
27
+ ) {
28
+ // Get the keys of both objects
29
+ const keys1 = Object.keys(jsonObj1);
30
+ const keys2 = Object.keys(jsonObj2);
31
+
32
+ // Check if the number of keys is different
33
+ if (keys1.length !== keys2.length) {
34
+ return false;
35
+ }
36
+
37
+ // Recursively check each key-value pair
38
+ for (let key of keys1) {
39
+ if (
40
+ !keys2.includes(key) ||
41
+ !FtJSON.areJsonEqual(jsonObj1[key], jsonObj2[key])
42
+ ) {
43
+ return false;
44
+ }
45
+ }
46
+
47
+ return true;
48
+ } else {
49
+ // For non-object types, use strict equality comparison
50
+ return jsonObj1 === jsonObj2;
51
+ }
52
+ }
53
+ }
54
+
55
+ module.exports = { FtJSON };