UNPKG

raygun

Version:

Raygun package for Node.js, written in TypeScript

54 lines (53 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.humanString = humanString; // Humanize only one level deep var maxLevels = 1; var symbolSeparator = ", "; var symbolEqual = "="; /** * Humanize error objects with the following conditions: * - Keeps the output to a single line * - Maximum one level deep * * Example: * - Object `{ name: "Test" }` becomes string `name=Test` as all is the top level * - Object `{ one: { name: "Test" }}` becomes string `one={name=Test}` as one level deep is explored * - Object `{ one: { two: { name: "Test" }}}` becomes string `one={two=...}` as the max level deep is reached * * @param obj - input object to stringify * @param level - level of recursion, should start at 0 * @returns a formatted String */ // eslint-disable-next-line @typescript-eslint/no-explicit-any function humanString(obj, level) { if (level === void 0) { level = 0; } if (!obj) { return "null"; } // Not an object, attempt to stringify if (typeof obj !== "object") { return String(obj); } // Maximum deep levels reached // return String instead of exploring object if (level > maxLevels) { return "..."; } var out = ""; // Iterate over all object properties Object.keys(obj).forEach(function (key) { // Recursive call increases level out += symbolSeparator + key + symbolEqual + humanString(obj[key], level + 1); }); // Remove the initial `, ` before return var cleanOutput = out.substring(symbolSeparator.length); // Surround deeper levels with { } if (level > 0) { return "{".concat(cleanOutput, "}"); } else { return cleanOutput; } }