Skip to content

Commit 8038395

Browse files
committed
chore(refactor): simplify logic for checking equal diffs
1 parent d1b3650 commit 8038395

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

lib/util/json-diff.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ const { diff } = require('just-diff')
44

55
const j = (obj, replacer = null) => JSON.stringify(obj, replacer, 2)
66

7-
const jsonDiff = (s1, s2, DELETE) => {
8-
// DELETE is a special string that will be the value of updated if it exists
9-
// but should be deleted
10-
11-
const ops = diff(s1, s2).map(({ op, path, value }) => {
7+
// DELETE is a special string that will be the value of updated if it exists
8+
// but should be deleted
9+
const jsonDiff = (s1, s2, DELETE) => diff(s1, s2)
10+
.map(({ op, path, value }) => {
1211
// there could be cases where a whole object is reported
1312
// as missing and the expected value does not need to show
1413
// special DELETED values so filter those out here
@@ -26,13 +25,9 @@ const jsonDiff = (s1, s2, DELETE) => {
2625
} else if (op === 'add' && value !== DELETE) {
2726
return AD
2827
}
29-
}).filter(Boolean).sort((a, b) => a.localeCompare(b))
30-
31-
if (!ops.length) {
32-
return true
33-
}
34-
35-
return ops.join('\n')
36-
}
28+
})
29+
.filter(Boolean)
30+
.sort((a, b) => a.localeCompare(b))
31+
.join('\n')
3732

3833
module.exports = jsonDiff

lib/util/parser.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Base {
7777
// create a patch and strip out the filename. if it ends up an empty string
7878
// then return true since the files are equal
7979
return Diff.createPatch('', t.replace(/\r\n/g, '\n'), s.replace(/\r\n/g, '\n'))
80-
.split('\n').slice(4).join('\n').trim() || true
80+
.split('\n').slice(4).join('\n')
8181
}
8282

8383
diff (t, s) {
@@ -142,9 +142,10 @@ class Base {
142142
].join('\n')
143143
}
144144

145-
// individual diff methods are responsible for formatting or returning
146-
// true if the are equal
147-
return this.diff(target, source)
145+
// individual diff methods are responsible for returning a string
146+
// representing the diff. an empty trimmed string means no diff
147+
const diffRes = this.diff(target, source).trim()
148+
return diffRes || true
148149
}
149150
}
150151

0 commit comments

Comments
 (0)