Skip to content

Instantly share code, notes, and snippets.

@gitawego
Last active November 29, 2018 10:15
Show Gist options
  • Select an option

  • Save gitawego/9b5dd754d0fa2eb8107b5efe436018fb to your computer and use it in GitHub Desktop.

Select an option

Save gitawego/9b5dd754d0fa2eb8107b5efe436018fb to your computer and use it in GitHub Desktop.
replace references
const data = {
"app": {
"TITLE": "DoITnow",
"PGAAS": "PostgreSQL - [app:TITLE]",
"DB": "Database"
},
"postgres": {
"TITLE": "[app:PGAAS] Database",
"DESC": "This is [postgres:TITLE] Management consol in [app:TITLE]"
}
}
function parser(data) {
return JSON.parse(JSON.stringify(data), (key, value) => {
if (typeof value === 'string') {
return replacer(value, data);
}
return value;
});
}
function replacer(value, data) {
const reg = /\[([\D\_].*?):([\D\_].*?)]/gi;
return value.replace(reg, (full, ns, nsKey) => {
let keyValue = data[ns][nsKey];
if (reg.test(keyValue)) {
keyValue = replacer(keyValue, data);
}
return keyValue;
})
}
const resp = parser(data);
console.log(resp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment