Skip to content

Instantly share code, notes, and snippets.

@serebano
Created March 17, 2017 02:29
Show Gist options
  • Select an option

  • Save serebano/c0e8991331f6bf3b90e33d3dd078fef7 to your computer and use it in GitHub Desktop.

Select an option

Save serebano/c0e8991331f6bf3b90e33d3dd078fef7 to your computer and use it in GitHub Desktop.
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