Ad

In this kata you are restructuring given object with agregated properties by underscore into nested object

Sample transformation :

const before = {
a_b_c : 'value'
}

restructured into:

const after = {
a : {
b: {
c: 'value'
}
}
}

export function transform(obj: any) {
    const _RegEx = new RegExp(/_/);
    const underscoredProp = Object.keys(obj).find(key =>
        _RegEx.test(key)) || '';
    const value = obj[underscoredProp];
    const propertList = underscoredProp.split('_').reverse();
    let toAdd = {};

    propertList.map((prop,index) => {
       toAdd = index==0 ?{[prop]:value}  : {[prop]:toAdd}
        
    })

    return toAdd;
}