-
Notifications
You must be signed in to change notification settings - Fork 12
/
ExtrasDiff.jsx
78 lines (72 loc) · 2.5 KB
/
ExtrasDiff.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React from 'react';
import ReactDiffViewer from 'react-diff-viewer';
import { TableRow, TableCell } from '@mui/material';
import {
keys, startCase, map, compact, uniq, get, isEmpty, isObject, isEqual, isBoolean, isString, every,
isNumber, isArray
} from 'lodash';
const ExtrasDiff = ({lhs, rhs}) => {
const lhsKeys = keys(lhs)
const rhsKeys = keys(rhs)
const iteratorKeys = uniq(compact([...lhsKeys, ...rhsKeys]))
const formatValue = value => {
if(isBoolean(value) || isNumber(value))
return value.toString()
if(isObject(value)) {
if(isArray(value)) {
if(isEmpty(value))
return '--'
if(every(value, isString))
return value.join(', ')
if(every(value, isNumber))
return map(value, v => v.toString()).join(', ')
}
return JSON.stringify(value, undefined, 2).trim();
}
if(isEmpty(value))
return value
return value.toString().trim()
}
return (
<React.Fragment>
{
map(iteratorKeys, (key, index) => {
const rawLHSValue = get(lhs, key)
const rawRHSValue = get(rhs, key)
const lhsValue = formatValue(rawLHSValue)
const rhsValue = formatValue(rawRHSValue)
const hasDiff = !isEqual(rawLHSValue, rawRHSValue)
return (
<TableRow key={index} colSpan="12" style={{borderLeft: '10px solid lightgray'}}>
<TableCell colSpan='2' style={{width: '10%', fontWeight: 'bold', verticalAlign: 'top'}}>
{startCase(key)}
</TableCell>
{
hasDiff ?
<TableCell colSpan='10' style={{width: '90%'}} className='diff-row'>
<ReactDiffViewer
oldValue={lhsValue}
newValue={rhsValue}
showDiffOnly={false}
splitView
hideLineNumbers
compareMethod='diffLines'
/>
</TableCell> :
<React.Fragment>
<TableCell colSpan='5' style={{width: '45%'}}>
{isObject(rawLHSValue) ? <pre>{lhsValue}</pre> : lhsValue}
</TableCell>
<TableCell colSpan='5' style={{width: '45%'}}>
{isObject(rawRHSValue) ? <pre>{rhsValue}</pre> : rhsValue}
</TableCell>
</React.Fragment>
}
</TableRow>
)
})
}
</React.Fragment>
);
}
export default ExtrasDiff;