Skip to content

Commit 6452686

Browse files
committed
add compare
1 parent 9985c2b commit 6452686

File tree

4 files changed

+3921
-13
lines changed

4 files changed

+3921
-13
lines changed

lib/diffJSON.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@ const sortArrays = require('./sortArrays');
22
const detailedDiff = require('deep-object-diff').detailedDiff;
33
const flatten = require('flat');
44

5+
const runCompare = (li, ri, comparison) => {
6+
let left = li;
7+
let right = ri;
8+
const splitCompare = comparison.split('($)').filter(x => x);
9+
10+
for (const substr of splitCompare) { // eslint-disable-line
11+
if (!substr.includes('*')) {
12+
throw new Error('missing * in customCompare');
13+
}
14+
const [l, r] = substr.split('*');
15+
const lm = left.substr(0, left.indexOf(l));
16+
const rm = right.substr(0, right.indexOf(l));
17+
if (lm !== rm) {
18+
return false;
19+
}
20+
left = left.substr(left.indexOf(r));
21+
right = right.substr(right.indexOf(r));
22+
}
23+
24+
if (left !== right) {
25+
return false;
26+
}
27+
28+
return true;
29+
};
30+
531
module.exports = async (lj, rj, options, arr = []) => {
632
const diffTable = arr;
733
let leftJSON = sortArrays(lj, options);
@@ -60,6 +86,15 @@ module.exports = async (lj, rj, options, arr = []) => {
6086
if (ignore.includes(key)) return;
6187
const leftValue = leftFlatJSON[key];
6288
const rightValue = updated[key];
89+
if (options.customDiff && options.customDiff[key]) {
90+
const re = new RegExp(options.customDiff[key]);
91+
if (re.test(leftValue) && re.test(rightValue)) {
92+
return;
93+
}
94+
}
95+
if (options.customCompare && options.customCompare[key]) {
96+
if (runCompare(leftValue, rightValue, options.customCompare[key])) return;
97+
}
6398
diffTable.push({
6499
key,
65100
left: leftValue,

lib/diffURLs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = async (leftURL, rightURL, opts) => {
1515
options.ignore = opts.ignore || [];
1616
options.skipcertificate = opts.skipcertificate || false;
1717
options.expectedStatusCode = opts.expectedStatusCode ? [opts.expectedStatusCode] : [200, 201, 202, 203, 204, 205, 206, 207, 208, 209];
18+
options.customDiff = opts.customDiff || {};
19+
options.customCompare = opts.customCompare || {};
1820

1921
if (options.sortKey) {
2022
options.sortKeys.unshift(options.sortKey);

0 commit comments

Comments
 (0)