create points from objects with x and y properties (#4465)

This commit is contained in:
Nathan Cahill 2016-04-20 09:05:31 -06:00 committed by Per Liedman
parent ddf8f0575e
commit 1eafc015c1
2 changed files with 10 additions and 0 deletions

View File

@ -96,6 +96,9 @@ describe("Point", function () {
it('creates a point from an array of coordinates', function () {
expect(L.point([50, 30])).to.eql(new L.Point(50, 30));
});
it("creates a point from an object with x and y properties", function () {
expect(L.point({x: 50, y: 30})).to.eql(new L.Point(50, 30));
});
it('does not fail on invalid arguments', function () {
expect(L.point(undefined)).to.be(undefined);
expect(L.point(null)).to.be(null);

View File

@ -177,6 +177,10 @@ L.Point.prototype = {
// @alternative
// @factory L.point(coords: Number[])
// Expects an array of the form `[x, y]` instead.
// @alternative
// @factory L.point(coords: Object)
// Expects a plain object of the form `{x: Number, y: Number}` instead.
L.point = function (x, y, round) {
if (x instanceof L.Point) {
return x;
@ -187,5 +191,8 @@ L.point = function (x, y, round) {
if (x === undefined || x === null) {
return x;
}
if (typeof x === 'object' && 'x' in x && 'y' in x) {
return new L.Point(x.x, x.y);
}
return new L.Point(x, y, round);
};