From 4a8469b74013932bb91ecdf2e376f8895d676d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 12 Jun 2013 18:55:14 -0700 Subject: [PATCH] Define Base.isPlainValue() and use it to implement more flexible #equals() for Color and Rectangle. --- src/basic/Rectangle.js | 11 ++++++----- src/core/Base.js | 8 ++++++++ src/style/Color.js | 2 ++ test/tests/TextItem.js | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/basic/Rectangle.js b/src/basic/Rectangle.js index cd4cc91c..8ef9dd9b 100644 --- a/src/basic/Rectangle.js +++ b/src/basic/Rectangle.js @@ -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; }, /** diff --git a/src/core/Base.js b/src/core/Base.js index 82db58aa..0dd633ad 100644 --- a/src/core/Base.js +++ b/src/core/Base.js @@ -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. diff --git a/src/style/Color.js b/src/style/Color.js index a3bc1aa1..6e094285 100644 --- a/src/style/Color.js +++ b/src/style/Color.js @@ -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); diff --git a/test/tests/TextItem.js b/test/tests/TextItem.js index 6d351d5f..fb43ba15 100644 --- a/test/tests/TextItem.js +++ b/test/tests/TextItem.js @@ -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'); });