Return a cloned object from config.get()

This prevents mutations to the values of the default settings.
This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-05-16 10:27:51 -07:00
parent 097ee9f2a9
commit 2b49a04227
6 changed files with 45 additions and 27 deletions

View File

@@ -126,6 +126,35 @@ _.mixin
endsWith: (string, suffix) ->
string.indexOf(suffix, string.length - suffix.length) isnt -1
# Transform the given object into another object.
#
# `object` - The object to transform.
# `iterator` -
# A function that takes `(key, value)` arguments and returns a
# `[key, value]` tuple.
#
# Returns a new object based with the key/values returned by the iterator.
mapObject: (object, iterator) ->
newObject = {}
for key, value of object
[key, value] = iterator(key, value)
newObject[key] = value
newObject
# Deep clones the given JSON object.
#
# `object` - The JSON object to clone.
#
# Returns a deep clone of the JSON object.
deepClone: (object) ->
if _.isArray(object)
object.map (value) -> _.deepClone(value)
else if _.isObject(object)
@mapObject object, (key, value) => [key, @deepClone(value)]
else
object
valueForKeyPath: (object, keyPath) ->
keys = keyPath.split('.')
for key in keys