Dynamic config replacement

by Saksham Malhotra

JavaScript

let pageConfig = {
  "component": "ol-dropdown-9x-14y-2z",
  "emits": {
    "dropdownChange": "reportType"
  },
  "answers": [{
      "text": "filter.reportType.purchases",
      "value": "PURCHASE"
    },
    {
      "text": "filter.reportType.grounding",
      "value": "TURN_IN"
    },
    {
      "text": "filter.reportType.arbitration",
      "value": "ARBITRATION"
    }
  ],
  "translations": {
    "label": "filter.reportType.label",
    "placeholder": "filter.reportType.placeholder"
  },
  "position": {
    "tablet": {
      "column": {
        "start": 8,
        "end": 13
      },
      "row": {
        "start": 1,
        "end": 2
      }
    },
    "desktop": {
      "column": {
        "start": 5,
        "end": 7
      },
      "row": {
        "start": 1,
        "end": 3
      }
    },
    "substitutes": {
      "tablet": "tabletPositionSubstitute",
      "dummy": "dummySubstitute"
    }
  },
  "substitutes": {
    "answers": "answersSubstitute"
  }
}

let substitutes = {
  "answersSubstitute": [{
    "text": "filter.reportType.purchases",
    "value": "NEW_REPORT"
  }],
  "tabletPositionSubstitute": {
    "column": {
      "start": 999,
      "end": 999
    },
    "row": {
      "start": 888,
      "end": 888
    }
  },
}

let anotherSubstitutes = {
  "answers": [{
    "text": "filter.reportType.purchases",
    "value": "NEW_REPORT"
  }],
  "position.tablet": {
    "column": {
      "start": 999,
      "end": 999
    },
    "row": {
      "start": 888,
      "end": 888
    }
  },
}

function parseConfigs(configs) {
  configs.substitutes && Object.entries(configs.substitutes).forEach(([key, value]) => {
    if (substitutes[value]) {
      configs[key] = substitutes[value];
    }
  })

  Object.keys(configs).forEach(configKey => {
    if (typeof configs === 'object') {
      parseConfigs(configs[configKey])
    }
  })
}
console.log(JSON.stringify(pageConfig));
parseConfigs(pageConfig);
console.log(JSON.stringify(pageConfig));