-
Notifications
You must be signed in to change notification settings - Fork 80
/
where-point.ts
192 lines (163 loc) · 4.08 KB
/
where-point.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {Entity, Point} from "../entity"
import { Search } from "./search"
import { WhereField } from "./where-field"
type Units = 'm' | 'km' | 'ft' | 'mi'
/** A function that defines a circle for `.inCircle` searches. */
export type CircleFunction = (circle: Circle) => Circle
/** A builder that defines a circle. */
export class Circle {
/** @internal */
longitudeOfOrigin: number = 0
/** @internal */
latitudeOfOrigin: number = 0
/** @internal */
size: number = 1
/** @internal */
units: Units = 'm'
/**
* Sets the longitude. If not set, defaults to 0.0.
*
* @param value The longitude.
* @returns This instance.
*/
longitude(value: number) {
this.longitudeOfOrigin = value
return this
}
/**
* Sets the latitude. If not set, defaults to 0.0.
*
* @param value The latitude.
* @returns This instance.
*/
latitude(value: number) {
this.latitudeOfOrigin = value
return this
}
/**
* Sets the origin of the circle using a {@link Point}. If not
* set, defaults to [Null Island](https://en.wikipedia.org/wiki/Null_Island).
*
* @param point A {@link Point} containing the longitude and latitude of the origin.
* @returns This instance.
*/
origin(point: Point): Circle
/**
* Sets the origin of the circle. If not set, defaults to
* [Null Island](https://en.wikipedia.org/wiki/Null_Island).
*
* @param longitude The longitude.
* @param latitude The latitude.
* @returns This instance.
*/
origin(longitude: number, latitude: number): Circle
/** @internal */
origin(pointOrLongitude: number | Point, latitude?: number): Circle {
if (typeof pointOrLongitude === 'number' && latitude !== undefined) {
this.longitudeOfOrigin = pointOrLongitude
this.latitudeOfOrigin = latitude
} else {
const point = pointOrLongitude as Point
this.longitudeOfOrigin = point.longitude
this.latitudeOfOrigin = point.latitude
}
return this
}
/**
* Sets the radius of the {@link Circle}. Defaults to 1. If units are
* not specified, defaults to meters.
*
* @param size The radius of the circle.
* @returns This instance.
*/
radius(size: number) {
this.size = size
return this
}
/**
* Sets the units to meters.
* @returns This instance.
*/
get m() { return this.meters }
/**
* Sets the units to meters.
* @returns This instance.
*/
get meter() { return this.meters }
/**
* Sets the units to meters.
* @returns This instance.
*/
get meters() {
this.units = 'm'
return this
}
/**
* Sets the units to kilometers.
* @returns This instance.
*/
get km() { return this.kilometers }
/**
* Sets the units to kilometers.
* @returns This instance.
*/
get kilometer() { return this.kilometers }
/**
* Sets the units to kilometers.
* @returns This instance.
*/
get kilometers() {
this.units = 'km'
return this
}
/**
* Sets the units to feet.
* @returns This instance.
*/
get ft() { return this.feet }
/**
* Sets the units to feet.
* @returns This instance.
*/
get foot() { return this.feet }
/**
* Sets the units to feet.
* @returns This instance.
*/
get feet() {
this.units = 'ft'
return this
}
/**
* Sets the units to miles.
* @returns This instance.
*/
get mi() { return this.miles }
/**
* Sets the units to miles.
* @returns This instance.
*/
get mile() { return this.miles }
/**
* Sets the units to miles.
* @returns This instance.
*/
get miles() {
this.units = 'mi'
return this
}
}
export class WherePoint<T extends Entity> extends WhereField<T> {
private circle: Circle = new Circle()
inRadius(circleFn: CircleFunction): Search<T> {
return this.inCircle(circleFn)
}
inCircle(circleFn: CircleFunction): Search<T> {
this.circle = circleFn(this.circle)
return this.search
}
toString(): string {
const { longitudeOfOrigin, latitudeOfOrigin, size, units } = this.circle
return this.buildQuery(`[${longitudeOfOrigin} ${latitudeOfOrigin} ${size} ${units}]`)
}
}