UNPKG

logpipes

Version:

Console.log transformation pipes

120 lines 5.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.simplifyValue = exports.simplifyJson = exports.getDefaultJsonSimplifierOptions = void 0; /** Returns default properties used by 'simplifyJson' function. */ function getDefaultJsonSimplifierOptions() { return { maxDepthLimit: 10, maxArrayLength: 100, maxObjectPropertyCount: 100, isIgnoredProperty: () => false, replacePropertyValue: (_, value) => value, depthLimitValue: '[Depth limit ~]', arrayLengthLimitValue: '[Array, length: $length ~]', objectPropertyCountLimitValue: '[Object, properties: $count ~]', circularReferenceValue: '[Circular ~]', functionValue: '[Function ~]', symbolValue: '[Symbol ~]', }; } exports.getDefaultJsonSimplifierOptions = getDefaultJsonSimplifierOptions; const DEFAULT_JSON_SIMPLIFIER_OPTIONS = getDefaultJsonSimplifierOptions(); /** Properties aren't returned by Object.entries(), but available on the Error object. */ const ERROR_OBJECT_PROPERTIES = ['cause', 'message', 'name', 'stack']; /** * Converts given value to a 'simplified' JSON object. * A 'simplified' object is an object that can be restored into the original form using JSON.parse(JSON.stringify(obj)). */ function simplifyJson(value, inputOptions = {}, recursionLevel = 0, visitedObjects = new Set) { const options = Object.assign(Object.assign({}, DEFAULT_JSON_SIMPLIFIER_OPTIONS), inputOptions); if (recursionLevel > options.maxDepthLimit) { return options.depthLimitValue; } value = simplifyValue(value); if (typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number' || value === null || value === undefined) { return value; } if (visitedObjects.has(value)) { return options.circularReferenceValue; } visitedObjects.add(value); if (Array.isArray(value)) { if (value.length > options.maxArrayLength) { return options.arrayLengthLimitValue.replace('$length', `${value.length}`); } return value.map(v => simplifyJson(v, options, recursionLevel + 1, visitedObjects)); } const entries = Object.entries(value); if (entries.length > options.maxObjectPropertyCount) { return options.objectPropertyCountLimitValue.replace('$count', `${entries.length}`); } const simplifiedJson = {}; for (const [propertyName, propertyValue] of entries) { if (recursionLevel === 0 && options.isIgnoredProperty(propertyName)) { continue; // Ignore the property. The property was moved to the top level. } simplifiedJson[propertyName] = simplifyJson(propertyValue, options, recursionLevel + 1, visitedObjects); } // Handle special properties. for (const propertyName of ERROR_OBJECT_PROPERTIES) { if (!simplifiedJson[propertyName] && !options.isIgnoredProperty(propertyName)) { const propertyValue = value[propertyName]; if (propertyValue !== undefined) { simplifiedJson[propertyName] = simplifyJson(propertyValue, options, recursionLevel + 1, visitedObjects); } } } if (options.replacePropertyValue !== DEFAULT_JSON_SIMPLIFIER_OPTIONS.replacePropertyValue) { for (const [propertyName, propertyValue] of Object.entries(simplifiedJson)) { simplifiedJson[propertyName] = options.replacePropertyValue(propertyName, propertyValue); } } return simplifiedJson; } exports.simplifyJson = simplifyJson; /** Simplifies a single property value with no recursion. */ function simplifyValue(value, options = {}) { if (value === null || value === undefined) { return value; } switch (typeof value) { case 'undefined': case 'boolean': case 'string': return value; case 'bigint': return `BigInt(${value.toString()})`; case 'number': if (isNaN(value)) { return 'NaN'; } else if (value === Infinity) { return 'Infinity'; } else if (value === -Infinity) { return '-Infinity'; } return value; case 'function': return options.functionValue || DEFAULT_JSON_SIMPLIFIER_OPTIONS.functionValue; case 'symbol': return options.symbolValue || DEFAULT_JSON_SIMPLIFIER_OPTIONS.symbolValue; case 'object': if (value instanceof Set) { return [...value.keys()]; } if (value instanceof Map) { return Object.fromEntries([...value.entries()]); } if (value instanceof String || value instanceof Number || value instanceof Boolean) { return value.valueOf(); } if (value instanceof Date) { return value.toISOString(); } break; } return value; } exports.simplifyValue = simplifyValue; //# sourceMappingURL=JsonSimplifier.js.map