UNPKG

1.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.tableDiff = void 0;
4const js_lib_1 = require("@naturalcycles/js-lib");
5/**
6 * Compares 2 objects, logs their differences via `console.table`.
7 *
8 * If `logEmpty` is set will explicitly log that fact, otherwise will log nothing.
9 *
10 * Function is located in nodejs-lib (not js-lib), because it's planned to improve in the future and add e.g colors (via chalk).
11 */
12function tableDiff(a, b, opt = {}) {
13 const { maxFieldLen, aTitle = 'a', bTitle = 'b' } = opt;
14 const diff = {};
15 if (a && b && a !== b) {
16 new Set([...Object.keys(a), ...Object.keys(b)]).forEach(k => {
17 if (a[k] !== b[k]) {
18 diff[k] = {
19 [aTitle]: maxFieldLen && a[k] ? (0, js_lib_1._truncate)(String(a[k]), maxFieldLen) : a[k],
20 [bTitle]: maxFieldLen && b[k] ? (0, js_lib_1._truncate)(String(b[k]), maxFieldLen) : b[k],
21 };
22 }
23 });
24 }
25 if (Object.keys(diff).length) {
26 console.table(diff);
27 }
28 else if (opt.logEmpty) {
29 console.log('no_difference');
30 }
31}
32exports.tableDiff = tableDiff;