Skip to content

Commit 9a7f2b2

Browse files
authored
Merge pull request #356 from lutovich/1.6-point-toString
Added Point.toString()
2 parents 23f2d17 + 595a776 commit 9a7f2b2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/v1/spatial-types.js

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ export class Point {
3939
this.z = z;
4040
Object.freeze(this);
4141
}
42+
43+
toString() {
44+
return this.z || this.z === 0
45+
? `Point{srid=${formatAsFloat(this.srid)}, x=${formatAsFloat(this.x)}, y=${formatAsFloat(this.y)}, z=${formatAsFloat(this.z)}}`
46+
: `Point{srid=${formatAsFloat(this.srid)}, x=${formatAsFloat(this.x)}, y=${formatAsFloat(this.y)}}`;
47+
}
48+
}
49+
50+
function formatAsFloat(number) {
51+
return Number.isInteger(number) ? number + '.0' : number.toString();
4252
}
4353

4454
Object.defineProperty(Point.prototype, POINT_IDENTIFIER_PROPERTY, {

test/v1/spatial-types.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,26 @@ describe('spatial-types', () => {
190190
testSendingAndReceivingOfPoints(done, new Point(CARTESIAN_3D_CRS_CODE.toNumber(), 12.87, 13.89, 14.901));
191191
});
192192

193+
it('should convert points to string', () => {
194+
const point1 = new Point(CARTESIAN_3D_CRS_CODE, 19.24, 100.29, 20.22222);
195+
expect(point1.toString()).toEqual('Point{srid=9157, x=19.24, y=100.29, z=20.22222}');
196+
197+
const point2 = new Point(WGS_84_2D_CRS_CODE, 1.00005, 2.00006);
198+
expect(point2.toString()).toEqual('Point{srid=4326, x=1.00005, y=2.00006}');
199+
200+
const point3 = new Point(WGS_84_3D_CRS_CODE, 1.111, 2.222, 0.0);
201+
expect(point3.toString()).toEqual('Point{srid=4979, x=1.111, y=2.222, z=0.0}');
202+
203+
const point4 = new Point(CARTESIAN_2D_CRS_CODE, 78.15, 92.2, null);
204+
expect(point4.toString()).toEqual('Point{srid=7203, x=78.15, y=92.2}');
205+
206+
const point5 = new Point(WGS_84_2D_CRS_CODE, 123.9, 64.5, undefined);
207+
expect(point5.toString()).toEqual('Point{srid=4326, x=123.9, y=64.5}');
208+
209+
const point6 = new Point(CARTESIAN_2D_CRS_CODE, 23.9378123, 67.3891, Number.NaN);
210+
expect(point6.toString()).toEqual('Point{srid=7203, x=23.9378123, y=67.3891}');
211+
});
212+
193213
function testReceivingOfPoints(done, query, pointChecker) {
194214
if (neo4jDoesNotSupportPoints(done)) {
195215
return;

0 commit comments

Comments
 (0)