Skip to content

Added Point.toString() #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/v1/spatial-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export class Point {
this.z = z;
Object.freeze(this);
}

toString() {
return this.z || this.z === 0
? `Point{srid=${formatAsFloat(this.srid)}, x=${formatAsFloat(this.x)}, y=${formatAsFloat(this.y)}, z=${formatAsFloat(this.z)}}`
: `Point{srid=${formatAsFloat(this.srid)}, x=${formatAsFloat(this.x)}, y=${formatAsFloat(this.y)}}`;
}
}

function formatAsFloat(number) {
return Number.isInteger(number) ? number + '.0' : number.toString();
}

Object.defineProperty(Point.prototype, POINT_IDENTIFIER_PROPERTY, {
Expand Down
20 changes: 20 additions & 0 deletions test/v1/spatial-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ describe('spatial-types', () => {
testSendingAndReceivingOfPoints(done, new Point(CARTESIAN_3D_CRS_CODE.toNumber(), 12.87, 13.89, 14.901));
});

it('should convert points to string', () => {
const point1 = new Point(CARTESIAN_3D_CRS_CODE, 19.24, 100.29, 20.22222);
expect(point1.toString()).toEqual('Point{srid=9157, x=19.24, y=100.29, z=20.22222}');

const point2 = new Point(WGS_84_2D_CRS_CODE, 1.00005, 2.00006);
expect(point2.toString()).toEqual('Point{srid=4326, x=1.00005, y=2.00006}');

const point3 = new Point(WGS_84_3D_CRS_CODE, 1.111, 2.222, 0.0);
expect(point3.toString()).toEqual('Point{srid=4979, x=1.111, y=2.222, z=0.0}');

const point4 = new Point(CARTESIAN_2D_CRS_CODE, 78.15, 92.2, null);
expect(point4.toString()).toEqual('Point{srid=7203, x=78.15, y=92.2}');

const point5 = new Point(WGS_84_2D_CRS_CODE, 123.9, 64.5, undefined);
expect(point5.toString()).toEqual('Point{srid=4326, x=123.9, y=64.5}');

const point6 = new Point(CARTESIAN_2D_CRS_CODE, 23.9378123, 67.3891, Number.NaN);
expect(point6.toString()).toEqual('Point{srid=7203, x=23.9378123, y=67.3891}');
});

function testReceivingOfPoints(done, query, pointChecker) {
if (neo4jDoesNotSupportPoints(done)) {
return;
Expand Down