UNPKG

@mincho-js/debug-log

Version:

A utility library for easier console debugging and comparison of JavaScript object values

84 lines (83 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const chalk = require("chalk"); const boxen = require("boxen"); const colorize = require("@pinojs/json-colorizer"); const diff = require("deep-diff"); const prettifyDeepDiff = require("@mincho-js/pretify-deep-diff"); const consola = { log(...args) { console.log(...args); }, info(...args) { console.log(chalk.cyan("\nℹ"), ...args); }, box(arg1, ...args) { const isExistArgs = args.length !== 0; console.log( boxen(isExistArgs ? args.join(" ") : arg1, { title: isExistArgs ? arg1 : void 0, padding: { top: 1, bottom: 1, left: 2, right: 2 }, borderStyle: "round" }) ); } }; let count = 0; function getCount() { return (count++).toString().padStart(4, "0"); } function debugLog(name) { const message = name === void 0 ? `DEBUG-${getCount()}` : `DEBUG-${getCount()}: ${name}`; consola.info(chalk.bold(message)); } function jsonPrint(nameOrObj, obj) { if (obj === void 0) { const json = JSON.stringify(nameOrObj, null, 2); consola.box(colorize(json)); } else { const json = JSON.stringify(obj, null, 2); consola.box(nameOrObj, colorize(json)); } } function jsonLog(nameOrObj, obj) { if (obj === void 0) { debugLog(); jsonPrint(nameOrObj); } else { debugLog(nameOrObj); jsonPrint(obj); } } function jsonExpect(nameOrObj, obj1, obj2) { if (obj2 === void 0) { debugLog(); const changes = diff(nameOrObj, obj1); if (changes === void 0) { jsonPrint("Same Contents", nameOrObj); } else { jsonPrint("Expected", nameOrObj); jsonPrint("Real", obj1); console.log(prettifyDeepDiff(changes ?? [])); } } else { debugLog(nameOrObj); const changes = diff(obj1, obj2); if (changes === void 0) { jsonPrint("Same Contents", obj1); } else { jsonPrint("Expected", obj1); jsonPrint("Real", obj2); console.log(prettifyDeepDiff(changes ?? [])); } } } exports.debugLog = debugLog; exports.jsonExpect = jsonExpect; exports.jsonLog = jsonLog; exports.jsonPrint = jsonPrint;