UNPKG

@naturalcycles/nodejs-lib

Version:
53 lines 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const js_lib_1 = require("@naturalcycles/js-lib"); const util_1 = require("util"); /** * Transforms ANY to human-readable string (via util.inspect mainly). * Safe (no error throwing). * * Correclty prints Errors, AppErrors, ErrorObjects: error.message + \n + inspect(error.data) * * Enforces max length (default to 10_000, pass 0 to skip it). * * Logs numbers as-is, e.g: `6`. * Logs strings as-is (without single quotes around, unlike default util.inspect behavior). * Otherwise - just uses util.inspect() with reasonable defaults. * * Returns 'empty_string' if empty string is passed. * Returns 'undefined' if undefined is passed (default util.inspect behavior). */ function inspectAny(obj, opt = {}) { let s; if (obj instanceof Error) { // Stack includes message s = (!opt.noErrorStack && obj.stack) || [obj === null || obj === void 0 ? void 0 : obj.name, obj.message].filter(Boolean).join(': '); if (obj instanceof js_lib_1.AppError) { s = [s, Object.keys(obj.data).length > 0 && inspectAny(obj.data, opt)] .filter(Boolean) .join('\n'); } } else if (js_lib_1._isErrorObject(obj)) { s = [obj.message, Object.keys(obj.data).length > 0 && inspectAny(obj.data, opt)] .filter(Boolean) .join('\n'); } else if (typeof obj === 'string') { s = obj.trim() || 'empty_string'; } else { s = util_1.inspect(obj, { breakLength: 80, depth: 6, ...opt, }); } // Handle maxLen if (opt.maxLen && s.length > opt.maxLen) { s = s.substr(0, opt.maxLen) + `... ${Math.ceil(s.length / 1024)} KB message truncated`; } return s; } exports.inspectAny = inspectAny; //# sourceMappingURL=string.util.js.map