UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

69 lines (68 loc) 2.42 kB
import type { Reviver } from '../types.js'; /** * Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects * in various cases. * * Used, for example, by _stringify() to pretty-print objects/arrays. * * Defaults to _safeJsonStringify. * * Node.js project can set it to _inspect, which allows to use `util.inspect` * as pretty-printing function. * * It's recommended that this function is circular-reference-safe. */ export declare function setGlobalStringifyFunction(fn: JsonStringifyFunction): void; export declare function resetGlobalStringifyFunction(): void; export type JsonStringifyFunction = (obj: any, reviver?: Reviver, space?: number) => string; export interface StringifyOptions { /** * @default 10_000 * Default limit is less than in Node, cause it's likely to be used e.g in Browser alert() */ maxLen?: number; /** * Set to true to print Error.stack instead of just Error.message. * * @default false */ includeErrorStack?: boolean; /** * Set to false to skip including Error.cause. * * @default true */ includeErrorCause?: boolean; /** * Set to true to include Error.data. * * @default false */ includeErrorData?: boolean; /** * Allows to pass custom "stringify function". * E.g in Node.js you can pass `util.inspect` instead. * * Defaults to `globalStringifyFunction`, which defaults to `_safeJsonStringify` */ stringifyFn?: JsonStringifyFunction; } /** * Inspired by `_inspect` from nodejs-lib, which is based on util.inpect that is not available in the Browser. * Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect * * Transforms ANY to human-readable string (via JSON.stringify pretty). * Safe (no error throwing). * * Correctly prints Errors, AppErrors, ErrorObjects: error.message + \n + _stringify(error.data) * * Enforces max length (default to 1000, 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 JSON.stringify(). * * Returns 'empty_string' if empty string is passed. * Returns 'undefined' if undefined is passed (default util.inspect behavior). */ export declare function _stringify(obj: any, opt?: StringifyOptions): string;