pojo-dump
Version:
JSON printer, print JSON or any POJO to terminal or debug window
149 lines (148 loc) • 6.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.logTree = exports.toTree = void 0;
const printTree_1 = require("tree-dump/lib/printTree");
const toLine_1 = require("./toLine");
const util_1 = require("./util");
const isPrimitive = (value) => typeof value !== 'object' || value === null;
const isShortPrimitive = (value) => {
if (typeof value === 'string')
return value.length < 32;
return isPrimitive(value);
};
const isShortValue = (value) => {
if (isShortPrimitive(value))
return true;
if (Array.isArray(value))
return value.length === 0;
else if (value && typeof value === 'object' && Object.keys(value).length === 0) {
if (value instanceof Date || value instanceof RegExp)
return true;
return !value.constructor || value.constructor === Object;
}
return false;
};
const isOneLineValue = (value) => {
if (isPrimitive(value))
return true;
if (Array.isArray(value)) {
const length = value.length;
if (length === 0)
return true;
if (length < 5) {
for (const item of value)
if (!isShortValue(item))
return false;
return true;
}
}
else if (value && typeof value === 'object') {
if (!value.constructor || value.constructor !== Object)
return false;
const keys = Object.keys(value);
const length = keys.length;
if (length === 0)
return true;
if (length < 3) {
for (const key of keys) {
if (!isShortPrimitive(key))
return false;
if (!isShortPrimitive(value[key]))
return false;
}
return true;
}
}
return false;
};
const isSimpleString = (str) => /^[a-z0-9]+$/i.test(str);
const wrap = (line, tab, targetLineWidth) => {
const targetLineSize = Math.max(32, targetLineWidth - tab.length);
if (line.length <= targetLineSize)
return line;
let lines = '';
let pos = 0;
while (pos < line.length) {
const nextPos = pos + targetLineSize;
lines += (lines ? ' ↵\n' + tab : '') + line.slice(pos, nextPos);
pos = nextPos;
}
return lines;
};
const wrappedStringify = (value, tab) => wrap((0, toLine_1.toLine)(value), tab, 89);
const printEntry = (key, val, tab) => {
const stringifyKey = typeof key === 'string' ? !isSimpleString(key) : true;
const formattedKey = stringifyKey ? (0, toLine_1.toLine)(key) : key + '';
const formattedKeyLength = formattedKey.length;
const wrappedKey = formattedKeyLength > 32 ? wrap(formattedKey, tab, 42) : formattedKey;
const oneLine = isOneLineValue(val);
const lastKeyLineWidth = formattedKeyLength > 32 ? (formattedKeyLength % 42) + 3 : formattedKeyLength;
let valueFormatted = oneLine
? ' ' + (0, util_1.dim)('=') + ' ' + wrappedStringify(val, tab + ' '.repeat(lastKeyLineWidth) + ' ')
: (0, exports.toTree)(val, tab, '');
if (!oneLine && valueFormatted[0] !== '\n')
valueFormatted =
' ' +
(0, util_1.dim)('=') +
' ' +
wrap(valueFormatted, tab + ' '.repeat(3 + Math.min(42, formattedKeyLength)), Math.max(32, 89 - tab.length - formattedKeyLength - 3));
return wrappedKey + valueFormatted;
};
const toTree = (value, tab = '', prefix = '╿\n') => {
if (Array.isArray(value)) {
if (value.length === 0)
return '[]';
return (prefix +
(0, printTree_1.printTree)(tab, value.map((v, i) => (tab) => {
const oneLine = isOneLineValue(v);
const index = i + '';
return `[${index}]${oneLine ? (0, util_1.dim)(':') + ' ' + wrappedStringify(v, tab + ' '.repeat(index.length) + ' ') : (0, exports.toTree)(v, tab + ' ', '')}`;
})).slice(tab ? 0 : 1));
}
else if (value && typeof value === 'object') {
if (value instanceof Date ||
value instanceof RegExp ||
ArrayBuffer.isView(value) ||
value instanceof ArrayBuffer ||
value instanceof DataView)
return wrap((0, toLine_1.toLine)(value), tab, 89);
if (value instanceof Map) {
return (prefix +
(0, printTree_1.printTree)(tab, [
(tab) => 'Map {}' +
(0, printTree_1.printTree)(tab, Array.from(value.entries()).map(([k, v]) => (tab) => printEntry(k, v, tab))),
]).slice(tab ? 0 : 1));
}
if (value instanceof Set) {
return (prefix +
(0, printTree_1.printTree)(tab, [
(tab) => {
const arr = Array.from(value);
if (!arr.length)
return 'Set {}';
if (isOneLineValue(arr)) {
const line = 'Set {' + (0, toLine_1.toLine)(arr).slice(1, -1) + '}';
return wrap(line, tab, 89);
}
return 'Set {}' + (0, exports.toTree)(arr, tab, '');
},
]).slice(tab ? 0 : 1));
}
if (value.constructor && value.constructor !== Object) {
const obj = {};
for (const key of Object.keys(value))
obj[key] = value[key];
const name = value.constructor.name + ' {}';
return prefix + (0, printTree_1.printTree)(tab, [(tab) => name + (0, exports.toTree)(obj, tab, '')]).slice(tab ? 0 : 1);
}
const keys = Object.keys(value);
if (keys.length === 0)
return (0, toLine_1.toLine)(value);
return (prefix +
(0, printTree_1.printTree)(tab, keys.map((k) => (tab) => printEntry(k, value[k], tab))).slice(tab ? 0 : 1));
}
return wrappedStringify(value, tab);
};
exports.toTree = toTree;
const logTree = (value, tab, prefix) => console.log((0, exports.toTree)(value, tab, prefix));
exports.logTree = logTree;