@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
33 lines (32 loc) • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tableDiff = void 0;
const js_lib_1 = require("@naturalcycles/js-lib");
/**
* Compares 2 objects, logs their differences via `console.table`.
*
* If `logEmpty` is set will explicitly log that fact, otherwise will log nothing.
*
* 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).
*/
function tableDiff(a, b, opt = {}) {
const { maxFieldLen, aTitle = 'a', bTitle = 'b' } = opt;
const diff = {};
if (a && b && a !== b) {
new Set([...Object.keys(a), ...Object.keys(b)]).forEach(k => {
if (a[k] !== b[k]) {
diff[k] = {
[aTitle]: maxFieldLen && a[k] ? (0, js_lib_1._truncate)(String(a[k]), maxFieldLen) : a[k],
[bTitle]: maxFieldLen && b[k] ? (0, js_lib_1._truncate)(String(b[k]), maxFieldLen) : b[k],
};
}
});
}
if (Object.keys(diff).length) {
console.table(diff);
}
else if (opt.logEmpty) {
console.log('no_difference');
}
}
exports.tableDiff = tableDiff;