/
geo-point.js
141 lines (121 loc) · 3.76 KB
/
geo-point.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
const isObject = require('lodash.isobject');
const isNil = require('lodash.isnil');
const { checkType } = require('./util');
/**
* A `GeoPoint` object that can be used in queries and filters that
* take a `GeoPoint`. `GeoPoint` supports various input formats.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html)
*/
class GeoPoint {
// eslint-disable-next-line require-jsdoc
constructor() {
// Take optional parameter and call appropriate method?
// Will have to check for string, object and array.
// this will be set depending on subsequent method called
this._point = null;
}
/**
* Print warning message to console namespaced by class name.
*
* @param {string} msg
* @private
*/
_warn(msg) {
console.warn(`[GeoPoint] ${msg}`);
}
/**
* Print warning messages to not mix Geo Point representations
* @private
*/
_warnMixedRepr() {
this._warn('Do not mix with other representation!');
this._warn('Overwriting.');
}
/**
* Check the instance for object representation of Geo Point.
* If representation is null, new object is initialised.
* If it is not null, warning is logged and point is overwritten.
* @private
*/
_checkObjRepr() {
if (isNil(this._point)) this._point = {};
else if (!isObject(this._point)) {
this._warnMixedRepr();
this._point = {};
}
}
/**
* Sets the latitude for the object representation.
*
* @param {number} lat Latitude
* @returns {GeoPoint} returns `this` so that calls can be chained
*/
lat(lat) {
this._checkObjRepr();
this._point.lat = lat;
return this;
}
/**
* Sets the longitude for the object representation.
*
* @param {number} lon Longitude
* @returns {GeoPoint} returns `this` so that calls can be chained
*/
lon(lon) {
this._checkObjRepr();
this._point.lon = lon;
return this;
}
/**
* Sets the Geo Point value expressed as an object,
* with `lat` and `lon` keys.
*
* @param {Object} point
* @returns {GeoPoint} returns `this` so that calls can be chained
* @throws {TypeError} If `point` is not an instance of object
*/
object(point) {
checkType(point, Object);
!isNil(this._point) && this._warnMixedRepr();
this._point = point;
return this; // This doesn't make much sense. What else are you gonna call?
}
/**
* Sets the Geo Point value expressed as an array
* with the format: `[ lon, lat ]`.
*
* @param {Array<number>} point Array in format `[ lon, lat ]`(`GeoJson` standard)
* @returns {GeoPoint} returns `this` so that calls can be chained
* @throws {TypeError} If `point` is not an instance of Array
*/
array(point) {
checkType(point, Array);
!isNil(this._point) && this._warnMixedRepr();
this._point = point;
return this; // This doesn't make much sense. What else are you gonna call?
}
/**
* Sets Geo-point expressed as a string with the format: `"lat,lon"`
* or as a geo hash
*
* @param {string} point
* @returns {GeoPoint} returns `this` so that calls can be chained
*/
string(point) {
!isNil(this._point) && this._warnMixedRepr();
this._point = point;
return this;
}
/**
* Override default `toJSON` to return DSL representation for the `GeoPoint`
*
* @override
* @returns {Object} returns an Object which maps to the elasticsearch query DSL
*/
toJSON() {
return this._point;
}
}
module.exports = GeoPoint;