Skip to content

Instantly share code, notes, and snippets.

@renaudtertrais
Last active June 13, 2016 08:03
Show Gist options
  • Save renaudtertrais/3d3c4ea8d713d3d6b50d to your computer and use it in GitHub Desktop.
Save renaudtertrais/3d3c4ea8d713d3d6b50d to your computer and use it in GitHub Desktop.
DataSystem = () ->
this.data = {}
this
proto = DataSystem.prototype
proto.set = ( keys , value , obj ) ->
obj = obj || this.data
if typeof keys is 'string'
keys = keys.split('.')
if keys.length > 1
key = keys.shift()
if typeof obj[key] is 'undefined'
obj[key] = {}
child = obj[key]
this.set( keys , value , child )
else
obj[keys[0]] = value
this
proto.update = ( keys , values , deep , obj ) ->
obj = obj || this.data
unless typeof values is 'object'
return this.set( keys , values , obj )
if typeof keys is 'string'
keys = keys.split('.')
if keys.length > 1
key = keys.shift()
if typeof obj[key] is 'undefined'
obj[key] = {}
child = obj[key]
this.update( keys , values , deep , child)
else
for key, value of values
if deep
if typeof obj[keys] is 'undefined'
obj[keys] = {}
child = obj[keys]
this.update( key , value , deep , child)
else
obj[keys][key] = value
this
proto.get = ( keys , obj ) ->
obj = obj || this.data
if typeof keys is 'string'
keys = keys.split('.')
if keys.length > 1
key = keys.shift()
child = obj[key]
this.get(keys,child)
else
obj[keys[0]]
proto.delete = ( keys , obj ) ->
obj = obj || this.data
if typeof keys is 'string'
keys = keys.split('.')
if keys.length > 1
key = keys.shift()
child = obj[key]
this.delete(keys,child)
else
delete obj[keys[0]]
this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment