MCPcopy Create free account
hub / github.com/GNOME/gjs / formatObject

Function formatObject

modules/script/_bootstrap/default.js:82–125  ·  view source on GitHub ↗
(obj, properties, printedObjects = new WeakSet())

Source from the content-addressed store, hash-verified

80 }
81
82 function formatObject(obj, properties, printedObjects = new WeakSet()) {
83 printedObjects.add(obj);
84 if (Array.isArray(obj) || _isTypedArray(obj))
85 return formatArray(obj, properties, printedObjects).toString();
86
87 if (obj instanceof Date)
88 return formatDate(obj);
89
90 const formattedObject = [];
91 let keys = Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj)).map(k => [k, obj[k]]);
92 // properties is only passed to us in the debugger
93 if (properties?.cur)
94 keys = keys.concat(properties.cur);
95
96 for (const [propertyKey, value] of keys) {
97 const key = formatPropertyKey(propertyKey);
98 switch (typeof value) {
99 case 'object':
100 if (printedObjects.has(value))
101 formattedObject.push(`${key}: [Circular]`);
102 else if (value === null)
103 formattedObject.push(`${key}: null`);
104 else if (_hasStandardToString(value))
105 formattedObject.push(`${key}: ${formatObject(value, properties?.children[propertyKey], printedObjects)}`);
106 else
107 formattedObject.push(`${key}: ${value.toString()}`);
108 break;
109 case 'function':
110 formattedObject.push(`${key}: ${formatFunction(value)}`);
111 break;
112 case 'string':
113 formattedObject.push(`${key}: "${value}"`);
114 break;
115 case 'symbol':
116 formattedObject.push(`${key}: ${formatSymbol(value)}`);
117 break;
118 default:
119 formattedObject.push(`${key}: ${value}`);
120 break;
121 }
122 }
123 return Object.keys(formattedObject).length === 0 ? '{}'
124 : `{ ${formattedObject.join(', ')} }`;
125 }
126
127 function formatArray(arr, properties, printedObjects) {
128 const formattedArray = [];

Callers 1

prettyPrintFunction · 0.85

Calls 9

_isTypedArrayFunction · 0.85
formatArrayFunction · 0.85
formatDateFunction · 0.85
formatPropertyKeyFunction · 0.85
_hasStandardToStringFunction · 0.85
formatFunctionFunction · 0.85
formatSymbolFunction · 0.85
addMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected