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?
CodePudding user response:
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 }
.