@tduyng/prettyoutput
Version:
Library to format JSON objects into a colorful, YAML-style output. Ideal for pretty printing logs with high performance.
119 lines • 4.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prettyoutput = exports.prettyOutput = void 0;
const renderer_js_1 = require("./renderer.js");
const utils_js_1 = require("./utils.js");
const defaultStack = (input) => ({
input,
noRender: true,
indentation: '',
depth: 0,
});
const parseOptions = (opts = {}) => {
const optsColors = opts.colors || {};
const color = {
keys: optsColors.keys || 'green',
dash: optsColors.dash || 'green',
number: optsColors.number || 'blue',
string: optsColors.string,
true: optsColors.true || 'green',
false: optsColors.false || 'red',
null: optsColors.null || 'grey',
undefined: optsColors.undefined || 'grey',
};
return {
indentation: (0, utils_js_1.indent)(opts.indentationLength || 2),
maxDepth: opts.maxDepth ?? 3,
colors: !opts.noColor ? color : undefined,
alignKeyValues: opts.alignKeyValues !== false,
hideUndefined: opts.hideUndefined ?? false,
};
};
const prettyOutput = (input, opts, indentLevel = 0) => {
const options = parseOptions(opts);
const stack = [{ indentation: (0, utils_js_1.indent)(indentLevel), depth: 0, input }];
let output = '';
while (stack.length > 0) {
const item = stack.pop();
if (!item)
continue;
const { indentation, depth, input, noRender } = item;
if (noRender) {
output += input;
continue;
}
if (depth > options.maxDepth) {
output += (0, renderer_js_1.renderMaxDepth)(indentation);
continue;
}
if ((0, utils_js_1.isSerializable)(input)) {
output += (0, renderer_js_1.renderSerializable)(input, options, indentation);
continue;
}
if (typeof input === 'string') {
output += (0, renderer_js_1.renderMultilineString)(input, options, indentation);
continue;
}
if (Array.isArray(input)) {
handleArrayRendering(input, options, stack, indentation, depth);
continue;
}
if (typeof input === 'object' && input !== null) {
handleObjectRendering(input, options, stack, indentation, depth);
}
}
return output;
};
exports.prettyOutput = prettyOutput;
exports.prettyoutput = prettyOutput;
const handleArrayRendering = (input, options, stack, indentation, depth) => {
for (let i = input.length - 1; i >= 0; i--) {
const value = input[i];
if ((0, utils_js_1.isSerializable)(value)) {
stack.push(defaultStack((0, renderer_js_1.renderSerializableArrayValue)(value, options, indentation)));
continue;
}
if (depth + 1 > options.maxDepth) {
stack.push(defaultStack((0, renderer_js_1.renderMaxDepthArrayValue)(options, indentation)));
continue;
}
stack.push({
input: value,
indentation: (0, renderer_js_1.indentString)(indentation, options),
depth: depth + 1,
});
stack.push(defaultStack(`${(0, renderer_js_1.renderDash)(options, indentation)}\n`));
}
};
const handleObjectRendering = (input, options, stack, indentation, depth) => {
const keys = Object.getOwnPropertyNames(input);
const valueColumn = options.alignKeyValues ? (0, utils_js_1.maxLength)(keys) : 0;
for (let i = keys.length - 1; i >= 0; i--) {
const key = keys[i];
if (!key)
continue;
const value = input[key];
if (input instanceof Error && key === 'stack') {
stack.push(defaultStack((0, renderer_js_1.renderObjectErrorStack)(key, value, options, indentation)));
continue;
}
if ((0, utils_js_1.isSerializable)(value)) {
const result = (0, renderer_js_1.renderSerializableObjectValue)(key, value, valueColumn, options, indentation);
if (result !== undefined)
stack.push(defaultStack(result));
continue;
}
if (depth + 1 > options.maxDepth) {
stack.push(defaultStack((0, renderer_js_1.renderMaxDepthObjectValue)(key, valueColumn, options, indentation)));
continue;
}
stack.push({
input: value,
depth: depth + 1,
indentation: (0, renderer_js_1.indentString)(indentation, options),
});
stack.push(defaultStack(`${(0, renderer_js_1.renderObjectKey)(key, options, indentation)}\n`));
}
};
exports.default = prettyOutput;
//# sourceMappingURL=index.js.map