UNPKG

@civ-clone/core-data-object

Version:

A data exchange object for converting a subset of a class instances to plain JSON.

37 lines (27 loc) 826 B
import { ObjectMap, PlainObject } from '../DataObject'; export const reconstituteData: (objectMap: ObjectMap) => PlainObject = ({ hierarchy, objects, }: ObjectMap): PlainObject => { const seenObjects: PlainObject[] = []; const getReferences = (value: any): PlainObject => { if (seenObjects.includes(value)) { return value; } seenObjects.push(value); if (Array.isArray(value)) { return value.map((value) => getReferences(value)); } if (value && value['#ref']) { return getReferences(objects[value['#ref']]); } if (value instanceof Object) { Object.entries(value).forEach(([key, childValue]) => { value[key] = getReferences(childValue); }); } return value; }; return getReferences(hierarchy); }; export default reconstituteData;