Add support for construction from strings to Point and Size.

This commit is contained in:
Jürg Lehni 2016-05-27 11:37:19 +02:00
parent fefb4302e8
commit 9aa29e0afc
4 changed files with 38 additions and 27 deletions

View file

@ -142,19 +142,20 @@ var Point = Base.extend(/** @lends Point# */{
if (this.__read) if (this.__read)
this.__read = arg0 === null ? 1 : 0; this.__read = arg0 === null ? 1 : 0;
} else { } else {
if (Array.isArray(arg0)) { var obj = type === 'string' ? arg0.split(/[\s,]+/) || [] : arg0;
this.x = arg0[0]; if (Array.isArray(obj)) {
this.y = arg0.length > 1 ? arg0[1] : arg0[0]; this.x = obj[0];
} else if (arg0.x != null) { this.y = obj.length > 1 ? obj[1] : obj[0];
this.x = arg0.x; } else if ('x' in obj) {
this.y = arg0.y; this.x = obj.x;
} else if (arg0.width != null) { this.y = obj.y;
this.x = arg0.width; } else if ('width' in obj) {
this.y = arg0.height; this.x = obj.width;
} else if (arg0.angle != null) { this.y = obj.height;
this.x = arg0.length; } else if ('angle' in obj) {
this.x = obj.length;
this.y = 0; this.y = 0;
this.setAngle(arg0.angle); this.setAngle(obj.angle);
} else { } else {
this.x = this.y = 0; this.x = this.y = 0;
if (this.__read) if (this.__read)

View file

@ -105,15 +105,16 @@ var Size = Base.extend(/** @lends Size# */{
if (this.__read) if (this.__read)
this.__read = arg0 === null ? 1 : 0; this.__read = arg0 === null ? 1 : 0;
} else { } else {
if (Array.isArray(arg0)) { var obj = type === 'string' ? arg0.split(/[\s,]+/) || [] : arg0;
this.width = arg0[0]; if (Array.isArray(obj)) {
this.height = arg0.length > 1 ? arg0[1] : arg0[0]; this.width = obj[0];
} else if (arg0.width != null) { this.height = obj.length > 1 ? obj[1] : obj[0];
this.width = arg0.width; } else if ('width' in obj) {
this.height = arg0.height; this.width = obj.width;
} else if (arg0.x != null) { this.height = obj.height;
this.width = arg0.x; } else if ('x' in obj) {
this.height = arg0.y; this.width = obj.x;
this.height = obj.y;
} else { } else {
this.width = this.height = 0; this.width = this.height = 0;
if (this.__read) if (this.__read)

View file

@ -31,18 +31,21 @@ test('new Point({x: 10, y: 20})', function() {
}); });
test('new Point(new Size(10, 20))', function() { test('new Point(new Size(10, 20))', function() {
var point = new Point(new Size(10, 20)); equals(new Point(new Size(10, 20)), new Point(10, 20));
equals(point, new Point(10, 20));
}); });
test('new Point({ width: 10, height: 20})', function() { test('new Point({ width: 10, height: 20})', function() {
var point = new Point({width: 10, height: 20}); equals(new Point({width: 10, height: 20}), new Point(10, 20));
equals(point, new Point(10, 20));
}); });
test('new Point({ angle: 45, length: 20})', function() { test('new Point({ angle: 45, length: 20})', function() {
var point = new Point({ angle: 40, length: 20 }); equals(new Point({ angle: 40, length: 20 }), new Point(15.32089, 12.85575));
equals(point, new Point(15.32089, 12.85575)); });
test('new Point("10, 20")', function() {
equals(new Point('10, 20'), new Point(10, 20));
equals(new Point('10,20'), new Point(10, 20));
equals(new Point('10 20'), new Point(10, 20));
}); });
test('normalize(length)', function() { test('normalize(length)', function() {

View file

@ -36,3 +36,9 @@ test('new Size({ x: 10, y: 20})', function() {
var size = new Size({x: 10, y: 20}); var size = new Size({x: 10, y: 20});
equals(size.toString(), '{ width: 10, height: 20 }'); equals(size.toString(), '{ width: 10, height: 20 }');
}); });
test('new Size("10, 20")', function() {
equals(new Size('10, 20'), new Size(10, 20));
equals(new Size('10,20'), new Size(10, 20));
equals(new Size('10 20'), new Size(10, 20));
});