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

Method stringifyNestNodes

src/lib/parser/tree.ts:317–347  ·  view source on GitHub ↗

* Stringifies nested JSON sub-trees and applies the changes to the original text. * It replaces the original string literals with their stringified content and updates node offsets and lengths to maintain tree integrity. * @param {StringifyOptions} options - The options to use for stringifying

(options: StringifyOptions = {})

Source from the content-addressed store, hash-verified

315 * @param {StringifyOptions} options - The options to use for stringifying the nested nodes.
316 */
317 stringifyNestNodes(options: StringifyOptions = {}) {
318 let acc = 0;
319 const edits: jsonc.Edit[] = [];
320
321 const fixOffset = (node: Node) => {
322 // fix offset for nest nodes
323 if (this.nestNodeMap?.[node.id]) {
324 const edit = {
325 offset: node.offset,
326 length: node.length,
327 content: this.stringifyNode(node, options, 0, node.offset + acc, node.boundOffset + acc),
328 };
329
330 edits.push(edit);
331 acc += edit.content.length - edit.length;
332 } else {
333 // fix offset for successor node of the nest node
334 node.offset += acc;
335 node.boundOffset += acc;
336
337 // fix length for parent of the nest node
338 const old = acc;
339 this.mapChildren(node, fixOffset);
340 node.length += acc - old;
341 node.boundLength += acc - old;
342 }
343 };
344
345 fixOffset(this.root());
346 this.text = jsonc.applyEdits(this.text, edits).trim();
347 }
348
349 toJSON(node = this.root()): string | number | boolean | object | any[] | null {
350 if (!isIterable(node)) {

Callers 1

parseAndFormatFunction · 0.80

Calls 1

rootMethod · 0.95

Tested by

no test coverage detected