UNPKG

donobu

Version:

Create browser automations with an LLM agent and replay them as Playwright scripts.

116 lines 4.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonUtils = void 0; class JsonUtils { /** * Private constructor to prevent instantiation of this utility class. * All methods are static and should be called directly on the class. */ constructor() { } /** * Converts an object to a JSON-serializable format with comprehensive type handling and circular reference protection. * * Handles special cases including: * - Date objects (converted to ISO strings) * - RegExp objects (converted to string representation) * - Error objects (converted to structured object with name, message, stack) * - Node.js-specific objects (Timeout, EventEmitter, etc. - converted to descriptive strings) * - Circular references (replaced with '[Circular Reference]' string) * - Functions and symbols (excluded from output) * * @param object - The object to convert to JSON-safe format * @returns A JSON-serializable version of the input object, or the original value for primitives * * @example * ```typescript * const complexObj = { * date: new Date(), * error: new Error('test'), * regex: /test/g, * func: () => {}, // Will be excluded * circular: null as any * }; * complexObj.circular = complexObj; // Create circular reference * * const safe = JsonUtils.objectToJson(complexObj); * // Result: { date: "2023-...", error: {...}, regex: "/test/g", circular: "[Circular Reference]" } * ``` */ static objectToJson(object) { // Handle undefined or null if (object === undefined || object === null) { return object; } // Create a circular reference handler const seen = new WeakMap(); const replacer = (_key, value) => { // Handle primitive types directly if (value === null || value === undefined) { return value; } if (typeof value !== 'object' && !Array.isArray(value)) { return value; } // Handle Date objects if (value instanceof Date) { return value.toISOString(); } // Handle RegExp objects if (value instanceof RegExp) { return value.toString(); } // Handle Error objects if (value instanceof Error) { return { name: value.name, message: value.message, stack: value.stack, }; } // Check for circular references if (typeof value === 'object') { if (seen.has(value)) { return '[Circular Reference]'; } seen.set(value, true); // Handle special objects that might cause issues if (value.constructor?.name) { // Handle Node.js-specific objects like Timeout, EventEmitter, etc. const constructor = value.constructor.name; if ([ 'Timeout', 'TimersList', 'EventEmitter', 'Socket', 'Stream', ].includes(constructor)) { return `[${constructor} Object]`; } } // For normal objects and arrays, process recursively if (Array.isArray(value)) { return value; } else { // Create a safe copy of the object const safeObj = {}; for (const prop in value) { if (Object.prototype.hasOwnProperty.call(value, prop)) { // Skip functions and other non-serializable types if (typeof value[prop] === 'function' || typeof value[prop] === 'symbol') { continue; } safeObj[prop] = value[prop]; } } return safeObj; } } return value; }; return JSON.parse(JSON.stringify(object, replacer)); } } exports.JsonUtils = JsonUtils; //# sourceMappingURL=JsonUtils.js.map