Created
March 17, 2017 02:29
-
-
Save serebano/c0e8991331f6bf3b90e33d3dd078fef7 to your computer and use it in GitHub Desktop.
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
| class Model { | |
| constructor(tree, path) { | |
| this.path = path ? path.split(".") : [] | |
| this.tree = tree ? tree : {} | |
| this.changes = [] | |
| } | |
| get(path) { | |
| return this.path.concat(path.split(".")).reduce((o,k) => o && o[k], this.tree) | |
| } | |
| set(path, value) { | |
| const keys = this.path.concat(path.split(".")) | |
| keys.reduce((o,k,i,keys) => keys.length-1===i ? o[k] = value : (o[k]=o[k]||{}), this.tree) | |
| return this.changes.push({ | |
| root: this.path, | |
| path: keys, | |
| args: [value] | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment