-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·304 lines (214 loc) · 7.63 KB
/
cli.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env node
/* eslint no-var: "off", no-console: "off" */
var program = require('commander');
var chalk = require('chalk');
var pkg = require('./package.json');
var Geo = require('./dist/geo'),
defaultGeo = new Geo();
function distanceUnitsRegex() {
var distanceUnits = Object.keys(defaultGeo.distanceUnits).map(function(v) {
return v.toLowerCase();
}).join('|');
return new RegExp('^('+distanceUnits+')$', 'i');
}
function formatCoords(coords) {
coords = coords.split(',');
return {
lat: coords[0],
lng: coords[1]
};
}
function geo(options) {
var opts = {};
var keys = [
'distanceUnits',
'distancePrecision',
'coordsFormat',
'coordsPrecision'
];
var i;
for (i = 0; i !== keys.length; i++) {
opts[keys[i]] = options.parent[keys[i]];
}
return new Geo(opts);
}
program
.version(pkg.version)
.option('-u, --distance-units <units>', chalk.dim('output distance units: "meters", "feet", etc (eg: '+chalk.bold('-u miles')+')'), distanceUnitsRegex())
.option('-p, --distance-precision <format>', chalk.dim('round distance to these digits (eg: '+chalk.bold('-p 3')+')'), /\d+/)
.option('-c, --coords-format <digits>', chalk.dim('output format of coordinates (eg: '+chalk.bold('-c dms')+')'), /^(dms|dm|dd|d)$/i)
.option('-d, --coords-precision <digits>', chalk.dim('round coordinates to these digits (eg: '+chalk.bold('-d 6')+')'), /\d+/);
/* convertDistance */
program
.command('convert-distance <distance> <from-units> <to-units>')
.description(chalk.dim('convert distance description'))
.action(function(distance, fromUnits, toUnits, options) {
console.log(geo(options).convertDistance(distance, fromUnits, toUnits));
});
/* formatBounds */
program
.command('format-bounds <coords1> <coords2>')
.description(chalk.dim('returns 4 bounds coordinates (southwest, northeast, etc) from two specified points'))
.option('-f, --format [mode]', 'Output format mode: json, list', /^(json|list)$/i)
.action(function(coords1, coords2, options) {
var bounds = geo(options).formatBounds([
formatCoords(coords1),
formatCoords(coords2)
]);
var format = options.format || 'json';
if (format === 'list') {
bounds = bounds.sw.lat+','+bounds.sw.lng+'|'+
bounds.nw.lat+','+bounds.nw.lng+'|'+
bounds.ne.lat+','+bounds.ne.lng+'|'+
bounds.se.lat+','+bounds.se.lng;
}
console.log(bounds);
});
/* formatCoord */
program
.command('format-coord <coord> [is-lat]')
.description(chalk.dim('formats coordinate according to ')+'--coords-format'+chalk.dim(' option'))
.action(function(coord, isLat, options) {
if (isLat === 'false') {
isLat = false;
} else {
isLat = !!isLat;
}
console.log(geo(options).formatCoord(coord, isLat));
});
/* formatCoords */
program
.command('format-coords <coords>')
.description(chalk.dim('formats coordinates according to ')+'--coords-format'+chalk.dim(' option'))
.option('-f, --format [mode]', 'Output format mode: json, list', /^(json|list)$/i)
.action(function(coords, options) {
coords = formatCoords(coords);
coords = geo(options).formatCoords(coords);
var format = options.format || 'json';
if (format === 'list') {
coords = coords.lat+','+coords.lng;
}
console.log(coords);
});
/* formatDistance */
program
.command('format-distance <meters>')
.description(chalk.dim('formats distance according to ')+'--distance-units'+chalk.dim(' option'))
.action(function(meters, options) {
console.log(geo(options).formatDistance(meters));
});
/* getCenter */
program
.command('get-center [coords...]')
.description(chalk.dim('calculates center of provided coordinates'))
.option('-f, --format [mode]', 'Output format mode: json, list', /^(json|list)$/i)
.action(function(coords, options) {
var coordsArray = coords.map(function(c) {
return formatCoords(c);
});
var center = geo(options).getCenter(coordsArray);
var format = options.format || 'json';
if (format === 'list') {
center = center.lat+','+center.lng;
}
console.log(center);
});
/* getClosest */
program
.command('get-closest <coords> [coords...]')
.description(chalk.dim('returns closest coordinate to the ')+'coords'+chalk.dim(' point'))
.option('-f, --format [mode]', 'Output format mode: json, list', /^(json|list)$/i)
.action(function(center, coords, options) {
center = formatCoords(center);
var coordsArray = coords.map(function(c) {
return formatCoords(c);
});
var closest = geo(options).getClosest(center, coordsArray);
var format = options.format || 'json';
if (format === 'list') {
closest = closest.lat+','+closest.lng;
}
console.log(closest);
});
/* getDestinationPoint */
program
.command('get-destination-point <coords> <distance> <bearing> [radius]')
.description(chalk.dim('calculates destination point given initial coordinate, distance in meters and bearing'))
.option('-f, --format [mode]', 'Output format mode: json, list', /^(json|list)$/i)
.action(function(coords, distance, bearing, radius, options) {
coords = formatCoords(coords);
distance = parseInt(distance, 10);
bearing = parseInt(bearing, 10);
var destinationPoint = geo(options).getDestinationPoint(coords, distance, bearing, radius);
var format = options.format || 'json';
if (format === 'list') {
destinationPoint = destinationPoint.lat+','+destinationPoint.lng;
}
console.log(destinationPoint);
});
/* getDistance */
program
.command('get-distance <coords1> <coords2>')
.description(chalk.dim('calculates distance (according to ')+'--distance-units'+chalk.dim(') between two points'))
.action(function(coords1, coords2, options) {
coords1 = formatCoords(coords1);
coords2 = formatCoords(coords2);
console.log(geo(options).getDistance(coords1, coords2));
});
/* isPointInBounds */
program
.command('is-point-in-bounds <coords> <bounds1> <bounds2>')
.description(chalk.dim('returns ')+'true'+chalk.dim(' if coodinate is between provided bounds'))
.action(function(coords, bounds1, bounds2, options) {
coords = formatCoords(coords);
bounds1 = formatCoords(bounds1);
bounds2 = formatCoords(bounds2);
console.log(geo(options).isPointInBounds(coords, [
bounds1,
bounds2
]));
});
/* isPointInCircle */
program
.command('is-point-in-circle <coords> <center> <meters>')
.description(chalk.dim('returns ')+'true'+chalk.dim(' if coordinate is in the circle'))
.action(function(coords, center, meters, options) {
coords = formatCoords(coords);
center = formatCoords(center);
meters = parseInt(meters, 10);
console.log(geo(options).isPointInCircle(coords, center, meters));
});
/* isValidLat */
program
.command('is-valid-lat <lat>')
.description(chalk.dim('returns ')+'true'+chalk.dim(' if coordinate is a validate latitude'))
.action(function(lat, options) {
console.log(geo(options).isValidLat(lat));
});
/* isValidLng */
program
.command('is-valid-lng <lng>')
.description(chalk.dim('returns ')+'true'+chalk.dim(' if coordinate is a validate longitude'))
.action(function(lng, options) {
console.log(geo(options).isValidLng(lng));
});
/* orderByDistance */
program
.command('order-by-distance <center> [coords...]')
.option('-f, --format [mode]', 'Output format mode: json, list', /^(json|list)$/i)
.description(chalk.dim('orders points by distance, first being closest to ')+'center')
.action(function(center, coords, options) {
center = formatCoords(center);
var coordsArray = coords.map(function(c) {
return formatCoords(c);
});
var ordered = geo(options).orderByDistance(center, coordsArray);
var format = options.format || 'json';
if (format === 'list') {
ordered = ordered.map(function(coord) {
return coord.lat+','+coord.lng;
}).join('|');
}
console.log(ordered);
});
program.parse(process.argv);