UNPKG

@tduyng/prettyoutput

Version:

Library to format JSON objects into a colorful, YAML-style output. Ideal for pretty printing logs with high performance.

99 lines 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderObjectErrorStack = exports.renderErrorStack = exports.renderMaxDepthArrayValue = exports.renderSerializableArrayValue = exports.renderMaxDepthObjectValue = exports.renderSerializableObjectValue = exports.renderObjectKey = exports.renderMaxDepth = exports.renderDash = exports.renderMultilineString = exports.renderSerializable = exports.indentString = exports.getColor = void 0; const utils_js_1 = require("./utils.js"); /** * Get color of an input * @param {*} input * @param {InputColor} color * @return {string|undefined} - color or undefined if no color */ const getColor = (input, color) => { if (!color) return undefined; switch (typeof input) { case 'string': return color.string; case 'boolean': return input ? color.true : color.false; case 'number': return color.number; case 'object': return input === null ? color.null : undefined; case 'undefined': return color.undefined; default: return undefined; } }; exports.getColor = getColor; const indentString = (input, options) => `${options.indentation}${input}`; exports.indentString = indentString; /** * Handling serialization of different input types. */ const renderSerializable = (input, options, indentation) => { if (Array.isArray(input)) return `${indentation}(empty array)\n`; const color = (0, exports.getColor)(input, options.colors); return `${indentation}${(0, utils_js_1.colorString)(String(input), color)}\n`; }; exports.renderSerializable = renderSerializable; const renderMultilineString = (input, options, indentation) => { const color = (0, exports.getColor)(input, options.colors); const indentedString = (0, utils_js_1.alignString)(input, (0, exports.indentString)(indentation, options)); const output = `${indentation}"""\n${indentedString}\n${indentation}"""\n`; return (0, utils_js_1.colorString)(output, color); }; exports.renderMultilineString = renderMultilineString; const renderDash = (options, indentation) => (0, utils_js_1.colorString)(`${indentation}- `, options.colors?.dash); exports.renderDash = renderDash; const renderMaxDepth = (indentation) => `${indentation}(max depth reached)\n`; exports.renderMaxDepth = renderMaxDepth; const renderObjectKey = (key, options, indentation) => (0, utils_js_1.colorString)(`${indentation}${key}: `, options.colors?.keys); exports.renderObjectKey = renderObjectKey; /** * Renders the value in a key-value pair for serializable objects. */ const renderSerializableObjectValue = (key, value, valueColumn, options, indentation) => { if (value === undefined && options.hideUndefined) return undefined; const alignSpaces = (0, utils_js_1.repeat)(' ', valueColumn - key.length); return `${(0, exports.renderObjectKey)(key, options, indentation)}${(0, exports.renderSerializable)(value, options, alignSpaces)}`; }; exports.renderSerializableObjectValue = renderSerializableObjectValue; /** * Handles rendering when max depth is reached for objects. */ const renderMaxDepthObjectValue = (key, valueColumn, options, indentation) => { const alignSpaces = (0, utils_js_1.repeat)(' ', valueColumn - key.length); return `${(0, exports.renderObjectKey)(key, options, indentation)}${(0, exports.renderMaxDepth)(alignSpaces)}`; }; exports.renderMaxDepthObjectValue = renderMaxDepthObjectValue; const renderSerializableArrayValue = (value, options, indentation) => `${(0, exports.renderDash)(options, indentation)}${(0, exports.renderSerializable)(value, options, '')}`; exports.renderSerializableArrayValue = renderSerializableArrayValue; /** * Handles rendering when max depth is reached for arrays. */ const renderMaxDepthArrayValue = (options, indentation) => `${(0, exports.renderDash)(options, indentation)}${(0, exports.renderMaxDepth)('')}`; exports.renderMaxDepthArrayValue = renderMaxDepthArrayValue; /** * Renders error stack trace. */ const renderErrorStack = (stack, options, indentation) => { const color = (0, exports.getColor)(stack, options.colors); const indentedStack = (0, utils_js_1.alignString)(stack, (0, exports.renderDash)(options, indentation)); return (0, utils_js_1.colorString)(indentedStack, color); }; exports.renderErrorStack = renderErrorStack; /** * Renders object key and stack trace for error objects. */ const renderObjectErrorStack = (key, stack, options, indentation) => { const renderedKey = (0, exports.renderObjectKey)(key, options, indentation); const stackIndentation = (0, exports.indentString)(indentation, options); const renderedStack = (0, exports.renderErrorStack)(stack, options, stackIndentation); return `${renderedKey}\n${renderedStack}\n`; }; exports.renderObjectErrorStack = renderObjectErrorStack; //# sourceMappingURL=renderer.js.map