js.foresight-devtools
Version:
Visual debugging tools for ForesightJS - mouse trajectory prediction and element interaction visualization
66 lines (65 loc) • 2.55 kB
JavaScript
/**
* Converts a JavaScript object into a readable method call string format.
*
* This utility is designed for copying object configurations and transforming them
* into method call syntax for easy copy-pasting of global settings or configurations.
*
* @template T - The type of the input object, constrained to Record<string, any>
* @param obj - The object to convert to method call format
* @param methodName - The name of the method to wrap the object in (e.g., 'ForesightManager.initialize')
* @returns A formatted string representing the object as a method call with proper indentation
*
*/
export function objectToMethodCall(obj, methodName) {
var entries = Object.entries(obj);
// Filter out deprecated keys
var filteredEntries = entries.filter(function (_a) {
var key = _a[0];
var keyStr = String(key);
if (keyStr === "resizeScrollThrottleDelay") {
return false;
}
return true;
});
var formattedEntries = filteredEntries
.map(function (_a) {
var key = _a[0], value = _a[1];
return " ".concat(String(key), ": ").concat(formatValue(value));
})
.join(",\n");
return "".concat(methodName, "({\n").concat(formattedEntries, "\n})");
}
/**
* Formats a value with proper indentation and type-appropriate string representation.
*
* @param value - The value to format (can be any type)
* @param indent - The current indentation level (defaults to 2 spaces)
* @returns A formatted string representation of the value
*/
var formatValue = function (value, indent) {
if (indent === void 0) { indent = 2; }
var spaces = " ".repeat(indent);
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
var entries = Object.entries(value);
if (entries.length === 0)
return "{}";
var formattedEntries = entries
.map(function (_a) {
var key = _a[0], val = _a[1];
return "".concat(spaces, " ").concat(key, ": ").concat(formatValue(val, indent + 2));
})
.join(",\n");
return "{\n".concat(formattedEntries, "\n").concat(spaces, "}");
}
if (typeof value === "string")
return "'".concat(value, "'");
if (typeof value === "boolean" || typeof value === "number")
return String(value);
if (value === null)
return "null";
if (value === undefined)
return "undefined";
if (Array.isArray(value))
return JSON.stringify(value);
return String(value);
};