MCPcopy
hub / github.com/loggerhead/json4u / stringifyNode

Method stringifyNode

src/lib/parser/tree.ts:230–303  ·  view source on GitHub ↗
(
    node: Node,
    options: StringifyOptions = {},
    indentLevel: number = 0,
    offset: number = 0, // the starting offset of the current node in the stringify text.
    boundOffset: number = 0, // the starting offset of the current node's bound in the stringify text.
    genTabs: ReturnType<typeof getGenTabsFn> = getGenTabsFn(options.tabWidth || 2),
  )

Source from the content-addressed store, hash-verified

228
229 // TODO support pretty format
230 stringifyNode(
231 node: Node,
232 options: StringifyOptions = {},
233 indentLevel: number = 0,
234 offset: number = 0, // the starting offset of the current node in the stringify text.
235 boundOffset: number = 0, // the starting offset of the current node's bound in the stringify text.
236 genTabs: ReturnType<typeof getGenTabsFn> = getGenTabsFn(options.tabWidth || 2),
237 ): string {
238 if (!isIterable(node)) {
239 const stringified = getRawValue(node)!;
240
241 if (!options.pure) {
242 node.length = stringified.length;
243 node.offset = offset;
244 node.boundOffset = boundOffset ?? node.offset;
245 computeAndSetBoundLength(node);
246 }
247
248 return stringified;
249 }
250
251 // for array or object, the bound width is equal to the node width
252 if (!options.pure) {
253 node.boundOffset = boundOffset ?? 0;
254 node.offset = offset;
255 }
256
257 const isFormat = options?.format === true;
258 const isObject = node.type === "object";
259 let stringified = isObject ? "{" : "[";
260
261 const keys = getChildrenKeys(node);
262
263 if (isObject) {
264 if (options?.sort === "asc") {
265 keys.sort();
266 } else if (options?.sort === "desc") {
267 keys.sort().reverse();
268 }
269 }
270
271 for (let i = 0; i < keys.length; i++) {
272 const key = keys[i];
273 const child = this.getChild(node, key)!;
274
275 if (isFormat) {
276 stringified += "\n" + genTabs(indentLevel + 1);
277 }
278
279 const childBoundOffset = offset + stringified.length;
280 if (isObject) {
281 const keyText = escape(key);
282 stringified += `"${keyText}":${isFormat ? " " : ""}`;
283 }
284 const childOffset = offset + stringified.length;
285
286 stringified += this.stringifyNode(child, options, indentLevel + 1, childOffset, childBoundOffset, genTabs);
287

Callers 4

stringifyMethod · 0.95
fixOffsetMethod · 0.95
toCsvJSONFunction · 0.80
jsonPathFunction · 0.80

Calls 7

getChildMethod · 0.95
isIterableFunction · 0.90
getRawValueFunction · 0.90
computeAndSetBoundLengthFunction · 0.90
getChildrenKeysFunction · 0.90
escapeFunction · 0.90
getGenTabsFnFunction · 0.85

Tested by

no test coverage detected