UNPKG

@tduyng/prettyoutput

Version:

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

83 lines 3.58 kB
import { alignString, colorString, repeat } from './utils.js'; /** * Get color of an input * @param {*} input * @param {InputColor} color * @return {string|undefined} - color or undefined if no color */ export 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; } }; export const indentString = (input, options) => `${options.indentation}${input}`; /** * Handling serialization of different input types. */ export const renderSerializable = (input, options, indentation) => { if (Array.isArray(input)) return `${indentation}(empty array)\n`; const color = getColor(input, options.colors); return `${indentation}${colorString(String(input), color)}\n`; }; export const renderMultilineString = (input, options, indentation) => { const color = getColor(input, options.colors); const indentedString = alignString(input, indentString(indentation, options)); const output = `${indentation}"""\n${indentedString}\n${indentation}"""\n`; return colorString(output, color); }; export const renderDash = (options, indentation) => colorString(`${indentation}- `, options.colors?.dash); export const renderMaxDepth = (indentation) => `${indentation}(max depth reached)\n`; export const renderObjectKey = (key, options, indentation) => colorString(`${indentation}${key}: `, options.colors?.keys); /** * Renders the value in a key-value pair for serializable objects. */ export const renderSerializableObjectValue = (key, value, valueColumn, options, indentation) => { if (value === undefined && options.hideUndefined) return undefined; const alignSpaces = repeat(' ', valueColumn - key.length); return `${renderObjectKey(key, options, indentation)}${renderSerializable(value, options, alignSpaces)}`; }; /** * Handles rendering when max depth is reached for objects. */ export const renderMaxDepthObjectValue = (key, valueColumn, options, indentation) => { const alignSpaces = repeat(' ', valueColumn - key.length); return `${renderObjectKey(key, options, indentation)}${renderMaxDepth(alignSpaces)}`; }; export const renderSerializableArrayValue = (value, options, indentation) => `${renderDash(options, indentation)}${renderSerializable(value, options, '')}`; /** * Handles rendering when max depth is reached for arrays. */ export const renderMaxDepthArrayValue = (options, indentation) => `${renderDash(options, indentation)}${renderMaxDepth('')}`; /** * Renders error stack trace. */ export const renderErrorStack = (stack, options, indentation) => { const color = getColor(stack, options.colors); const indentedStack = alignString(stack, renderDash(options, indentation)); return colorString(indentedStack, color); }; /** * Renders object key and stack trace for error objects. */ export const renderObjectErrorStack = (key, stack, options, indentation) => { const renderedKey = renderObjectKey(key, options, indentation); const stackIndentation = indentString(indentation, options); const renderedStack = renderErrorStack(stack, options, stackIndentation); return `${renderedKey}\n${renderedStack}\n`; }; //# sourceMappingURL=renderer.js.map