Skip to content

Commit ded72f8

Browse files
committed
Freeze Point objects to make them immutable
1 parent d41dda6 commit ded72f8

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/v1/spatial-types.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const POINT_IDENTIFIER_PROPERTY = '__isPoint__';
2323

2424
/**
2525
* Represents a single two or three-dimensional point in a particular coordinate reference system.
26+
* Created <code>Point</code> objects are frozen with {@link Object#freeze()} in constructor and thus immutable.
2627
*/
2728
export class Point {
2829

@@ -31,13 +32,14 @@ export class Point {
3132
* @param {number|Integer} srid the coordinate reference system identifier.
3233
* @param {number} x the <code>x</code> coordinate of the point.
3334
* @param {number} y the <code>y</code> coordinate of the point.
34-
* @param {number|undefined} z the <code>y</code> coordinate of the point or <code>undefined</code> if point has 2 dimensions.
35+
* @param {number} [z=undefined] the <code>y</code> coordinate of the point or <code>undefined</code> if point has 2 dimensions.
3536
*/
3637
constructor(srid, x, y, z) {
3738
this.srid = int(srid);
3839
this.x = x;
3940
this.y = y;
4041
this.z = z;
42+
Object.freeze(this);
4143
}
4244
}
4345

test/v1/spatial-types.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,34 @@ describe('spatial-types', () => {
6565
}
6666
});
6767

68+
it('should expose frozen immutable points', () => {
69+
const point = new Point(CARTESIAN_2D_CRS_CODE, 1.2, 3.4);
70+
71+
expect(Object.isFrozen(point)).toBeTruthy();
72+
73+
expect(point.srid).toEqual(CARTESIAN_2D_CRS_CODE);
74+
expect(point.x).toEqual(1.2);
75+
expect(point.y).toEqual(3.4);
76+
expect(point.z).toBeUndefined();
77+
78+
try {
79+
point.x = 5.6;
80+
} catch (e) {
81+
}
82+
try {
83+
point.y = 7.8;
84+
} catch (e) {
85+
}
86+
try {
87+
point.z = 9.0;
88+
} catch (e) {
89+
}
90+
91+
expect(point.x).toEqual(1.2);
92+
expect(point.y).toEqual(3.4);
93+
expect(point.z).toBeUndefined();
94+
});
95+
6896
it('should receive 2D points', done => {
6997
testReceivingOfPoints(done, 'RETURN point({x: 169.99, y: 12.1718})', point => {
7098
expect(isPoint(point)).toBeTruthy();

0 commit comments

Comments
 (0)