Skip to content

Commit 4a8469b

Browse files
committed
Define Base.isPlainValue() and use it to implement more flexible #equals() for Color and Rectangle.
1 parent a163d89 commit 4a8469b

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/basic/Rectangle.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
201201
* @return {Boolean} {@true if the rectangles are equal}
202202
*/
203203
equals: function(rect) {
204-
return rect === this || rect && (this.x === rect.x && this.y === rect.y
205-
&& this.width === rect.width && this.height=== rect.height
206-
|| Array.isArray(rect) && this.x === rect[0]
207-
&& this.y === rect[1] && this.width === rect[2]
208-
&& this.height === rect[3]) || false;
204+
if (Base.isPlainValue(rect))
205+
rect = Rectangle.read(arguments);
206+
return rect === this
207+
|| rect && this.x === rect.x && this.y === rect.y
208+
&& this.width === rect.width && this.height=== rect.height
209+
|| false;
209210
},
210211

211212
/**

src/core/Base.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ Base.inject(/** @lends Base# */{
259259
return !!this.getNamed(list, name);
260260
},
261261

262+
/**
263+
* Returns true if obj is either a plain object or an array, as used by
264+
* many argument reading methods.
265+
*/
266+
isPlainValue: function(obj) {
267+
return this.isPlainObject(obj) || Array.isArray(obj);
268+
},
269+
262270
/**
263271
* Serializes the passed object into a format that can be passed to
264272
* JSON.stringify() for JSON serialization.

src/style/Color.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,8 @@ var Color = Base.extend(new function() {
697697
* @return {Boolean} {@true if the colors are the same}
698698
*/
699699
equals: function(color) {
700+
if (Base.isPlainValue(color))
701+
color = Color.read(arguments);
700702
return color && this._type === color._type
701703
&& this._alpha === color._alpha
702704
&& Base.equals(this._components, color._components);

test/tests/TextItem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ test('PointText', function() {
1818
content: 'Hello World!'
1919
});
2020
equals(text.point, { x: 100, y: 100 });
21-
equals(text.fillColor, 'black', 'text.fillColor should be black by default');
21+
equals(text.fillColor, { red: 0, green: 0, blue: 0 }, 'text.fillColor should be black by default');
2222
});

0 commit comments

Comments
 (0)