Last active
November 29, 2018 10:15
-
-
Save gitawego/9b5dd754d0fa2eb8107b5efe436018fb to your computer and use it in GitHub Desktop.
replace references
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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