# DGM Interview with Cagatay ## Implementation Challenge: Data Conversion You're migrating data from one CMS to another, and the origin CMS stores a page layout with a type like this: ```typescript= interface SrcComponentInstance { // the name of the target component name: string; // arguments given to the component args: { // name of the argument name: string; // value of the argument value: JSONValue; }[]; // the contents of the component contents: (SrcComponentInstance | string)[]; }; ``` The target CMS uses a different approach, giving a unique identifier to every single component and text snippet and using those references. ```typescript= type Id = string; type Language = 'eng' | 'fra' | 'spa'; interface DestComponentInstance { // unique identifier for this component id: Id; // the name of the target component name: string; // the arugements given to the component args: {[k in string]?: JSONValue}; // the ids of each child instance of the component contents: Id[]; }; interface DestTextInstance { // unique identifier for this text id: Id; // the content text, depending on the language content: {[k in Language]?: string}; }; ``` Write a function that converts a list of top level `SrcComponentInstance`s to `DestComponentInstance`s: ```typescript= const textInstances: DestTextInstance[] = [] const convertSrcToDestInstance = (src: SrcComponentInstance, id?: Id): DestComponentInstance[] => { const results: DestComponentInstance[] = [] results.push({ id: id || generateId(), name: src.name, args: src.args.reduce((result, arg) => { result[arg.name] = arg.value; return result; }, {}), contents: src.contents.map(content => { const id = generateId() if (typeof content === 'string') { textInstances.push({ id, contents: { eng: content, spa: translate('spa', content), } }) } results.push(...convertSrcToDestInstance(content, id)) return id }) }) return results } const convert = (src: SrcComponentInstance[]): {[id in string]?: DestComponentInstance} => { } ```