mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Return Color object when in cloned objects
This adds a custom deepClone that clones any Color objects correctly.
This commit is contained in:
@@ -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 =
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user