UNPKG

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