0

I have a huge string config in a file and i want to refactor it to have intelisense.
Note there are also variables in it, so using a function would be the best option.
The result of the return function should be exactly what's inside the config string.

  const existingConfig = `
  var configOptions =  {
    enableProp: true,
    style: "plain",
    onReady: function() {
        return true
    }
  }
    `;

I tried something like:

  const newConfig = (val) => {
    return {
      enableProp: true,
      style: val,
      onReady: function() {
        return true
      }
    };
  };
  const res = JSON.stringify(newConfig());

Any ideas?

Cristian Muscalu
  • 9,007
  • 12
  • 44
  • 76
  • 2
    Why does it have to be a string with JS code in it? Why not take the JSON you already have as `res` and use that? Then it'd be as simple as `var configOptions = JSON.parse(res)` and you have plain objects at the end. – VLAZ Aug 31 '22 at 06:52
  • It's a 3-rd party library config and that's the only way it works. – Cristian Muscalu Aug 31 '22 at 06:56
  • `JSON.stringify(newConfig())` will not produce same result, as there is no functions inside JSON (so you will get plain `"onReady": true` inside json) and all keys and values will be surrounded by quotes. – Justinas Aug 31 '22 at 06:56
  • 1
    To answer your question: `const res = "var configOptions = " + JSON.stringify(newConfig())` – Justinas Aug 31 '22 at 06:58
  • @Justinas I missed the function being there. They are completely omitted from JSON, so it's not going to be `"onReady": true` it will just not exist. – VLAZ Aug 31 '22 at 06:58
  • @CristianMuscalu this sounds horrifying – VLAZ Aug 31 '22 at 06:58
  • @VLAZ not as horrifying as working with a string of 700 line javascript config :( – Cristian Muscalu Aug 31 '22 at 07:00
  • Does this answer your question? [Is there any possibility to have JSON.stringify preserve functions?](https://stackoverflow.com/questions/7759200/is-there-any-possibility-to-have-json-stringify-preserve-functions) – Justinas Aug 31 '22 at 07:00
  • @CristianMuscalu that's the exact part I was picturing as horrifying. Well, juggling JS code as a string more generally. – VLAZ Aug 31 '22 at 07:00
  • This is the widget documentation: https://docs.oppwa.com/tutorials/integration-guide/customisation?id=B9E2E69D4649234E821D5520FC049B34.uat01-vm-tx02&resourcePath=%2Fv1%2Fcheckouts%2FB9E2E69D4649234E821D5520FC049B34.uat01-vm-tx02%2Fpayment – Cristian Muscalu Aug 31 '22 at 07:05
  • @Justinas i read the answer you posted, but i don't see how i could use it. Do you have an example maybe? – Cristian Muscalu Aug 31 '22 at 07:12

1 Answers1

1

what about

let configString = (() => {

  var configOptions =  {
    enableProp: true,
    style: "plain",
    onReady: function() {
        return true
    }
  };

}).toString();

configString = configString.slice(
  configString.indexOf("{")+1, 
  configString.lastIndexOf("}")
);

console.log(configString);

You could also do .toString().replace(/^.*?\{|\}.*?$/g, ""); but I don't like it, because the }.*$ part could mean a lot of backtracking. I'm not sure how well it behaves with 700 lines of JS probably containing a lot of }.

Thomas
  • 11,958
  • 1
  • 14
  • 23
  • This is pretty cool and it works on a first try. Thanks! – Cristian Muscalu Aug 31 '22 at 14:53
  • Not sure why the `indexOf`/`lastIndexOf` action at all. The curly brackets should be at the same place every time. The indexes may as well be static. – VLAZ Aug 31 '22 at 15:39
  • @VLAZ because I'm not sure about that. If the formatting is stable across browsers/engines. If it is stable, then you're right. – Thomas Aug 31 '22 at 17:18