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)
this.__read = arg0 === null ? 1 : 0;
} else {
if (Array.isArray(arg0)) {
this.x = arg0[0];
this.y = arg0.length > 1 ? arg0[1] : arg0[0];
} else if (arg0.x != null) {
this.x = arg0.x;
this.y = arg0.y;
} else if (arg0.width != null) {
this.x = arg0.width;
this.y = arg0.height;
} else if (arg0.angle != null) {
this.x = arg0.length;
var obj = type === 'string' ? arg0.split(/[\s,]+/) || [] : arg0;
if (Array.isArray(obj)) {
this.x = obj[0];
this.y = obj.length > 1 ? obj[1] : obj[0];
} else if ('x' in obj) {
this.x = obj.x;
this.y = obj.y;
} else if ('width' in obj) {
this.x = obj.width;
this.y = obj.height;
} else if ('angle' in obj) {
this.x = obj.length;
this.y = 0;
this.setAngle(arg0.angle);
this.setAngle(obj.angle);
} else {
this.x = this.y = 0;
if (this.__read)

View file

@ -105,15 +105,16 @@ var Size = Base.extend(/** @lends Size# */{
if (this.__read)
this.__read = arg0 === null ? 1 : 0;
} else {
if (Array.isArray(arg0)) {
this.width = arg0[0];
this.height = arg0.length > 1 ? arg0[1] : arg0[0];
} else if (arg0.width != null) {
this.width = arg0.width;
this.height = arg0.height;
} else if (arg0.x != null) {
this.width = arg0.x;
this.height = arg0.y;
var obj = type === 'string' ? arg0.split(/[\s,]+/) || [] : arg0;
if (Array.isArray(obj)) {
this.width = obj[0];
this.height = obj.length > 1 ? obj[1] : obj[0];
} else if ('width' in obj) {
this.width = obj.width;
this.height = obj.height;
} else if ('x' in obj) {
this.width = obj.x;
this.height = obj.y;
} else {
this.width = this.height = 0;
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() {
var point = new Point(new Size(10, 20));
equals(point, new Point(10, 20));
equals(new Point(new Size(10, 20)), new Point(10, 20));
});
test('new Point({ width: 10, height: 20})', function() {
var point = new Point({width: 10, height: 20});
equals(point, new Point(10, 20));
equals(new Point({width: 10, height: 20}), new Point(10, 20));
});
test('new Point({ angle: 45, length: 20})', function() {
var point = new Point({ angle: 40, length: 20 });
equals(point, new Point(15.32089, 12.85575));
equals(new Point({ angle: 40, length: 20 }), 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() {

View file

@ -36,3 +36,9 @@ test('new Size({ x: 10, y: 20})', function() {
var size = new Size({x: 10, y: 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));
});