Return Color object when in cloned objects

This adds a custom deepClone that clones any Color objects
correctly.
This commit is contained in:
Kevin Sawicki
2015-01-22 10:47:04 -08:00
parent f691c20d68
commit 63335f6b60
2 changed files with 20 additions and 2 deletions

View File

@@ -1393,6 +1393,14 @@ describe "Config", ->
atom.config.set('foo.bar.aColor', false)
expect(atom.config.get('foo.bar.aColor')).toEqual {red: 255, green: 255, blue: 255, alpha: 1}
it "returns a clone of the Color when returned in a parent object", ->
color1 = atom.config.get('foo.bar').aColor
color2 = atom.config.get('foo.bar').aColor
expect(color1.toRGBAString()).toBe 'rgba(255, 255, 255, 1)'
expect(color2.toRGBAString()).toBe 'rgba(255, 255, 255, 1)'
expect(color1).not.toBe color2
expect(color1).toEqual color2
describe 'when the `enum` key is used', ->
beforeEach ->
schema =

View File

@@ -903,13 +903,13 @@ class Config
if value instanceof Color
value = value.clone()
else
value = _.deepClone(value)
value = @deepClone(value)
_.defaults(value, defaultValue) if isPlainObject(value) and isPlainObject(defaultValue)
else
if defaultValue instanceof Color
value = defaultValue.clone()
else
value = _.deepClone(defaultValue)
value = @deepClone(defaultValue)
value
@@ -959,6 +959,16 @@ class Config
catch e
console.warn("'#{keyPath}' could not set the default. Attempted default: #{JSON.stringify(defaults)}; Schema: #{JSON.stringify(@getSchema(keyPath))}")
deepClone: (object) ->
if object instanceof Color
object.clone()
else if _.isArray(object)
object.map (value) => @deepClone(value)
else if isPlainObject(object)
_.mapObject object, (key, value) => [key, @deepClone(value)]
else
object
# `schema` will look something like this
#
# ```coffee