---
# System prepended metadata

title: DGM Interview with Sang

---

# DGM Interview with Sang

## 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 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 migrateComponent = (src: SrcComponentInstance[]): {[id in Id]?: DestComponentInstance} => {
    return src.map((srcInstance: SrcComponentInstance) => {
        // convert the parent component
        let destInstance: DestComponentInstance = {
            id: uuid(),
            name: srcInstance.name,
            args: JSON.parse(JSON.stringify(srcInstance.args)),
        };

        // convert string contents
        const stringIds = srcInstance.contents.filter((content: SrcComponentInstance | string) => {
            return typeof content === 'string'
        }).map((content: string) => {
            // register DestTextInstance component in db
            let destTextInstance: DestTextInstance = {
                id: uuid(),
                content: content
            };
            return destTextInstance.id;
        }
               
        // filter child components and convert them
        let childComponents: SrcComponentInstance[] = srcInstance.contents.filter((content: SrcComponentInstance | string) => {
            return typeof content !== 'string';
        });
        const childIds = migrateComponent(childComponents);
        destInstance.contents = [...childIds, ...stringIds];
        // register destInstance component in db
        return destInstance.id;
    });
    
}

const convert = (src: SrcComponentInstance[]): {[id in Id]?: DestComponentInstance} => {
    return migrateComponent(src);
}
```