Created
March 17, 2016 21:45
-
-
Save ben-bradley/c4dc08da4d9620cab8a4 to your computer and use it in GitHub Desktop.
dynamically navigate and modify an object
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 highlight = (obj, path) => { | |
let steps = path.split('.'), | |
prop = steps.shift(); | |
if (obj[prop] && steps.length) | |
return highlight(obj[prop], steps.join('.')); | |
else if (obj[prop]) | |
obj[prop] = '<span style=\'background-color:yellow;\'>' + obj[prop] + '</span>'; | |
else | |
return undefined; | |
}; | |
const highlightDiffs = (diff) => { | |
let { diffs, management, environment } = diff; | |
for (let d in diffs) { | |
let { prop } = diffs[d]; | |
highlight(management, prop); | |
highlight(environment, prop); | |
} | |
return { | |
highlightedManagement: JSON.stringify(management, null, 2), | |
highlightedEnvironment: JSON.stringify(environment, null, 2) | |
}; | |
}; | |
let diffs = [{ | |
"kind": "edited", | |
"prop": "references.0.a", | |
"management": { | |
"_id":"foo", | |
"description":"bar", | |
"label":"baz", | |
"path":"/foo/bar/baz", | |
"references": [{ "a":"b" }] | |
}, | |
"environment": { | |
"_id":"foo", | |
"description":"bar", | |
"label":"baz", | |
"path":"/foo/bar/baz", | |
"references": [{ "a":"xyz" }] | |
} | |
}].map(highlightDiffs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment