1 | import { StringMap, _truncate } from '@naturalcycles/js-lib'
|
2 |
|
3 | export interface TableDiffOptions {
|
4 | |
5 |
|
6 |
|
7 | logEmpty?: boolean
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 | maxFieldLen?: number
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 | aTitle?: string
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | bTitle?: string
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export function tableDiff(
|
39 | a: Record<string, any>,
|
40 | b: Record<string, any>,
|
41 | opt: TableDiffOptions = {},
|
42 | ): void {
|
43 | const { maxFieldLen, aTitle = 'a', bTitle = 'b' } = opt
|
44 | const diff: StringMap<any> = {}
|
45 |
|
46 | if (a && b && a !== b) {
|
47 | new Set([...Object.keys(a), ...Object.keys(b)]).forEach(k => {
|
48 | if (a[k] !== b[k]) {
|
49 | diff[k] = {
|
50 | [aTitle]: maxFieldLen && a[k] ? _truncate(String(a[k]), maxFieldLen) : a[k],
|
51 | [bTitle]: maxFieldLen && b[k] ? _truncate(String(b[k]), maxFieldLen) : b[k],
|
52 | }
|
53 | }
|
54 | })
|
55 | }
|
56 |
|
57 | if (Object.keys(diff).length) {
|
58 | console.table(diff)
|
59 | } else if (opt.logEmpty) {
|
60 | console.log('no_difference')
|
61 | }
|
62 | }
|