UNPKG

1.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.inspectAny = void 0;
4const util_1 = require("util");
5const js_lib_1 = require("@naturalcycles/js-lib");
6const INSPECT_OPT = {
7 breakLength: 80,
8 depth: 10, // default: 2
9};
10/**
11 * Transforms ANY to human-readable string (via util.inspect mainly).
12 * Safe (no error throwing).
13 *
14 * Correclty prints Errors, AppErrors, ErrorObjects: error.message + \n + inspect(error.data)
15 *
16 * Enforces max length (default to 10_000, pass 0 to skip it).
17 *
18 * Logs numbers as-is, e.g: `6`.
19 * Logs strings as-is (without single quotes around, unlike default util.inspect behavior).
20 * Otherwise - just uses util.inspect() with reasonable defaults.
21 *
22 * Returns 'empty_string' if empty string is passed.
23 * Returns 'undefined' if undefined is passed (default util.inspect behavior).
24 *
25 * Based on `_stringifyAny` from `js-lib`, just replaced `JSON.stringify` with `util.inspect`.
26 */
27function inspectAny(obj, opt = {}) {
28 // Inspect handles functions better
29 if (typeof obj === 'function') {
30 return (0, util_1.inspect)(obj, {
31 ...INSPECT_OPT,
32 ...opt,
33 });
34 }
35 return (0, js_lib_1._stringifyAny)(obj, {
36 ...opt,
37 stringifyFn: obj => (0, util_1.inspect)(obj, {
38 ...INSPECT_OPT,
39 ...opt,
40 }),
41 });
42}
43exports.inspectAny = inspectAny;