mwg API
    Preparing search index...

    Function nodesToObjects

    • Converts compiled nodes into plain objects using the same schema as compile. Attributes become fields; numbers and booleans are converted by their declared types. Children are grouped by tag in source order. A tag with cardinality.max: 1 becomes one object, otherwise an array. Absent children and optional attributes stay absent. Strings, references, enum values and coordinates stay strings. The result carries no parser metadata and can be written directly as JSON. Diagnostics must be checked before use.

      Parameters

      Returns MwlReadResult<MwlObject[]>

      import { compile, nodesToObjects } from '@datamoc/mwg/mwl';
      import type { MwlTagSchema } from '@datamoc/mwg/mwl';
      const schemas = {
      bridge: { attributes: { budget: 'number' }, children: ['anchor'] },
      anchor: { attributes: { x: 'number', fixed: 'boolean' }, children: [] },
      } satisfies Record<string, MwlTagSchema>;
      const game = compile("[{ tag: 'bridge', budget: 120, children: [{ tag: 'anchor', x: 4, fixed: true }] }]", { schemas });
      const result = nodesToObjects(game.roots, schemas);
      if (result.diagnostics.length) throw new Error(JSON.stringify(result.diagnostics));
      console.log(result.value[0].budget); // 120 as a number