Define Base.isPlainValue() and use it to implement more flexible #equals() for Color and Rectangle.

This commit is contained in:
Jürg Lehni 2013-06-12 18:55:14 -07:00
parent a163d890e6
commit 4a8469b740
4 changed files with 17 additions and 6 deletions

View file

@ -201,11 +201,12 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
* @return {Boolean} {@true if the rectangles are equal}
*/
equals: function(rect) {
return rect === this || rect && (this.x === rect.x && this.y === rect.y
&& this.width === rect.width && this.height=== rect.height
|| Array.isArray(rect) && this.x === rect[0]
&& this.y === rect[1] && this.width === rect[2]
&& this.height === rect[3]) || false;
if (Base.isPlainValue(rect))
rect = Rectangle.read(arguments);
return rect === this
|| rect && this.x === rect.x && this.y === rect.y
&& this.width === rect.width && this.height=== rect.height
|| false;
},
/**

View file

@ -259,6 +259,14 @@ Base.inject(/** @lends Base# */{
return !!this.getNamed(list, name);
},
/**
* Returns true if obj is either a plain object or an array, as used by
* many argument reading methods.
*/
isPlainValue: function(obj) {
return this.isPlainObject(obj) || Array.isArray(obj);
},
/**
* Serializes the passed object into a format that can be passed to
* JSON.stringify() for JSON serialization.

View file

@ -697,6 +697,8 @@ var Color = Base.extend(new function() {
* @return {Boolean} {@true if the colors are the same}
*/
equals: function(color) {
if (Base.isPlainValue(color))
color = Color.read(arguments);
return color && this._type === color._type
&& this._alpha === color._alpha
&& Base.equals(this._components, color._components);

View file

@ -18,5 +18,5 @@ test('PointText', function() {
content: 'Hello World!'
});
equals(text.point, { x: 100, y: 100 });
equals(text.fillColor, 'black', 'text.fillColor should be black by default');
equals(text.fillColor, { red: 0, green: 0, blue: 0 }, 'text.fillColor should be black by default');
});