Skip to content

Commit b568edc

Browse files
committed
fix(differ): should not affect recursiveEqual
1 parent 6606120 commit b568edc

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/utils/diff-object.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ const diffObject = (
5959

6060
if (keyLeft === keyRight) {
6161
if (getType(lhs[keyLeft]) !== getType(rhs[keyRight])) {
62-
const _resultLeft = formatValue(lhs[keyLeft]).split('\n');
63-
const _resultRight = formatValue(rhs[keyRight]).split('\n');
62+
const _resultLeft = formatValue(lhs[keyLeft], options.maxDepth, true).split('\n');
63+
const _resultRight = formatValue(rhs[keyRight], options.maxDepth, true).split('\n');
6464
if (options.showModifications) {
6565
const maxLines = Math.max(_resultLeft.length, _resultRight.length);
6666
for (let i = _resultLeft.length; i < maxLines; i++) {
@@ -138,17 +138,17 @@ const diffObject = (
138138
} else {
139139
if (lhs[keyLeft] !== rhs[keyRight]) {
140140
if (options.showModifications) {
141-
linesLeft.push({ level, type: 'modify', text: `"${keyLeft}": ${formatValue(lhs[keyLeft])}` });
142-
linesRight.push({ level, type: 'modify', text: `"${keyRight}": ${formatValue(rhs[keyRight])}` });
141+
linesLeft.push({ level, type: 'modify', text: `"${keyLeft}": ${formatValue(lhs[keyLeft], options.maxDepth)}` });
142+
linesRight.push({ level, type: 'modify', text: `"${keyRight}": ${formatValue(rhs[keyRight], options.maxDepth)}` });
143143
} else {
144-
linesLeft.push({ level, type: 'remove', text: `"${keyLeft}": ${formatValue(lhs[keyLeft])}` });
144+
linesLeft.push({ level, type: 'remove', text: `"${keyLeft}": ${formatValue(lhs[keyLeft], options.maxDepth)}` });
145145
linesLeft.push({ level, type: 'equal', text: '' });
146146
linesRight.push({ level, type: 'equal', text: '' });
147-
linesRight.push({ level, type: 'add', text: `"${keyRight}": ${formatValue(rhs[keyRight])}` });
147+
linesRight.push({ level, type: 'add', text: `"${keyRight}": ${formatValue(rhs[keyRight], options.maxDepth)}` });
148148
}
149149
} else {
150-
linesLeft.push({ level, type: 'equal', text: `"${keyLeft}": ${formatValue(lhs[keyLeft])}` });
151-
linesRight.push({ level, type: 'equal', text: `"${keyRight}": ${formatValue(rhs[keyRight])}` });
150+
linesLeft.push({ level, type: 'equal', text: `"${keyLeft}": ${formatValue(lhs[keyLeft], options.maxDepth)}` });
151+
linesRight.push({ level, type: 'equal', text: `"${keyRight}": ${formatValue(rhs[keyRight], options.maxDepth)}` });
152152
}
153153
}
154154
} else if (keysLeft.length && keysRight.length) {

src/utils/format-value.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ describe('Utility function: formatValue', () => {
1414
});
1515

1616
it('should work for array & object', () => {
17-
expect(formatValue([1, 2, '3'])).toBe('[\n 1,\n 2,\n "3"\n]');
18-
expect(formatValue({ a: 1, b: ['2', true] })).toBe('{\n "a": 1,\n "b": [\n "2",\n true\n ]\n}');
17+
expect(formatValue([1, 2, '3'])).toBe('[1,2,"3"]');
18+
expect(formatValue({ a: 1, b: ['2', true] })).toBe('{"a":1,"b":["2",true]}');
19+
});
20+
21+
it('should work for array & object with pretty', () => {
22+
expect(formatValue([1, 2, '3'], undefined, true)).toBe('[\n 1,\n 2,\n "3"\n]');
23+
expect(formatValue({ a: 1, b: ['2', true] }, undefined, true)).toBe('{\n "a": 1,\n "b": [\n "2",\n true\n ]\n}');
1924
});
2025

2126
it('should escape the characters when necessary for better display', () => {

src/utils/format-value.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import stringify from './stringify';
22

3-
const formatValue = (value: any, depth = Infinity) => {
3+
const formatValue = (value: any, depth = Infinity, pretty = false) => {
44
if (Number.isNaN(value) || value === null) {
55
return 'null';
66
}
77
if (Array.isArray(value) || typeof value === 'object') {
8-
return stringify(value, null, 1, depth);
8+
return stringify(value, null, pretty ? 1 : null, depth);
99
}
1010
if (typeof value === 'string') {
1111
return stringify(value);

0 commit comments

Comments
 (0)