geo-coordinates-parser 1.5.7 → 1.5.8

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -71,9 +71,10 @@ Formats used for testing can be be accessed with:
71
71
  convert.formats
72
72
  ```
73
73
 
74
- **<span style="color:red">Please add coordinate formats that throw an error in the Github Issues.</span>**
74
+ **Please add coordinate formats that throw an error in the Github Issues.**
75
75
 
76
- **<span style="color:red">Note that formats like 24.56S 26.48E are treated as degrees and minutes! And 24.0, 26.0 is treated as an error. If you don't want this behaviour you need to catch these cases with your own code before you use convert.</span>**
76
+ **CAUTION!!!**
77
+ **Coordinates like '24.56S 26.48E' are treated as degrees and minutes and '24, 26' or '24.0, 26.0' will throw an error. If you don't want this behaviour you need to catch these cases with your own code before you use convert.* *
77
78
 
78
79
  ### Want to use it in the browser?
79
80
  Add ```<script src="https://cdn.jsdelivr.net/npm/geo-coordinates-parser/bundle/geocoordsparser.js"></script>```
package/converter.js CHANGED
@@ -1,6 +1,8 @@
1
1
  //function for converting coordinates from a string to decimal and verbatim
2
2
  //this is just a comment
3
3
 
4
+ const { dd_re, dms_periods, dms_abbr, coords_other } = require('./regex.js')
5
+
4
6
  const toCoordinateFormat = require('./toCoordinateFormat.js')
5
7
 
6
8
  /**
@@ -44,11 +46,11 @@ function converter(coordsString, decimalPlaces) {
44
46
 
45
47
  //some more validation: no zero coords or degrees only
46
48
  if (Number(Math.round(ddLat)) == Number(ddLat)) {
47
- throw new Error('degree only coordinate provided')
49
+ throw new Error('integer only coordinate provided')
48
50
  }
49
51
 
50
52
  if (Number(Math.round(ddLng)) == Number(ddLng)) {
51
- throw new Error('degree only coordinate provided')
53
+ throw new Error('integer only coordinate provided')
52
54
  }
53
55
 
54
56
  //get directions
@@ -198,7 +200,7 @@ function converter(coordsString, decimalPlaces) {
198
200
  latdir = match[8];
199
201
  lngdir = match[16];
200
202
  }
201
-
203
+
202
204
  }
203
205
  else {
204
206
  throw new Error("invalid coordinates format")
@@ -296,7 +298,32 @@ function converter(coordsString, decimalPlaces) {
296
298
  }
297
299
 
298
300
  }
301
+
302
+ //validation again...
303
+
304
+ //we only allow zeros after the period if its DM
305
+ const splitLat = verbatimLat.split('.')
306
+ if(splitLat.length == 2) {
307
+ if(splitLat[1] == 0 && splitLat[1].length != 2){
308
+ throw new Error('invalid coordinates format')
309
+ }
310
+ }
311
+
312
+ const splitLon = verbatimLng.split('.')
313
+ if(splitLon.length == 2) {
314
+ if(splitLon[1] == 0 && splitLon[1].length != 2){
315
+ throw new Error('invalid coordinates format')
316
+ }
317
+ }
318
+
319
+ //no integer coords allowed
320
+ //validation -- no integer coords
321
+ if(/^\d+$/.test(verbatimLat) || /^\d+$/.test(verbatimLng)) {
322
+ throw new Error('degree only coordinate/s provided')
323
+ }
299
324
 
325
+
326
+ //some tidying up...
300
327
  if(isNaN(ddLat) && ddLat.includes(',')) {
301
328
  ddLat = ddLat.replace(',', '.')
302
329
  }
@@ -335,7 +362,8 @@ function checkMatch(match) { //test if the matched groups arrays are 'balanced'.
335
362
  }
336
363
 
337
364
  //first remove the empty values from the array
338
- var filteredMatch = match.filter(x=>x);
365
+ //var filteredMatch = match.filter(x=>x);
366
+ var filteredMatch = [...match]
339
367
 
340
368
  //we need to shift the array because it contains the whole coordinates string in the first item
341
369
  filteredMatch.shift();
@@ -354,7 +382,16 @@ function checkMatch(match) { //test if the matched groups arrays are 'balanced'.
354
382
  for (var i = 0; i < halflen; i++) {
355
383
  const leftside = filteredMatch[i]
356
384
  const rightside = filteredMatch[i + halflen]
357
- if ((numerictest.test(leftside) && numerictest.test(rightside)) || (stringtest.test(leftside) && stringtest.test(rightside)) || leftside == rightside) {
385
+ const bothAreNumbers = numerictest.test(leftside) && numerictest.test(rightside)
386
+ const bothAreStrings = stringtest.test(leftside) && stringtest.test(rightside)
387
+ const valuesAreEqual = leftside == rightside
388
+ if (leftside == undefined && rightside == undefined) { //we have to handle undefined because regex converts it to string 'undefined'!!
389
+ continue
390
+ }
391
+ else if (leftside == undefined || rightside == undefined) { //no we need to handle the case where one is and the other not...
392
+ return false
393
+ }
394
+ else if (bothAreNumbers || bothAreStrings || valuesAreEqual) {
358
395
  continue;
359
396
  }
360
397
  else {
@@ -395,19 +432,6 @@ function coordsCloseEnough(coordsToTest) {
395
432
  }
396
433
  }
397
434
 
398
- //Coordinates pattern matching regex
399
-
400
- //decimal degrees
401
- var dd_re = /(NORTH|SOUTH|[NS])?[\s]*([+-]?[0-8]?[0-9](?:[\.,]\d{3,}))[\s]*([•º°]?)[\s]*(NORTH|SOUTH|[NS])?[\s]*[,/;]?[\s]*(EAST|WEST|[EW])?[\s]*([+-]?[0-1]?[0-9]?[0-9](?:[\.,]\d{3,}))[\s]*([•º°]?)[\s]*(EAST|WEST|[EW])?/i;
402
-
403
- //degrees minutes seconds with '.' as separator - gives array with 15 values
404
- var dms_periods = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*(\.)\s*([0-5]?[0-9])\s*(\.)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*(\.)\s*([0-5]?[0-9])\s*(\.)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(EAST|WEST|[EW])?/i;
405
-
406
- //degrees minutes seconds with words 'degrees, minutes, seconds' as separators (needed because the s of seconds messes with the S of SOUTH) - gives array of 17 values
407
- var dms_abbr = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*(D(?:EG)?(?:REES)?)\s*([0-5]?[0-9])\s*(M(?:IN)?(?:UTES)?)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(S(?:EC)?(?:ONDS)?)?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*(D(?:EG)?(?:REES)?)\s*([0-5]?[0-9])\s*(M(?:IN)?(?:UTES)?)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(S(?:EC)?(?:ONDS)?)\s*(EAST|WEST|[EW])?/i;
408
-
409
- //everything else - gives array of 17 values
410
- var coords_other = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*([•º°\.:]|D(?:EG)?(?:REES)?)?\s*,?([0-5]?[0-9](?:[\.,]\d{1,})?)?\s*(['′´’\.:]|M(?:IN)?(?:UTES)?)?\s*,?((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(''|′′|’’|´´|["″”\.])?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*([•º°\.:]|D(?:EG)?(?:REES)?)?\s*,?([0-5]?[0-9](?:[\.,]\d{1,})?)?\s*(['′´’\.:]|M(?:IN)?(?:UTES)?)?\s*,?((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(''|′′|´´|’’|["″”\.])?\s*(EAST|WEST|[EW])?/i;
411
435
 
412
436
  const to = Object.freeze({
413
437
  DMS: 'DMS',
package/failFormats.js CHANGED
@@ -1,6 +1,13 @@
1
1
  //TODO These formats should throw...
2
2
 
3
3
  const failingFormats = [
4
+ '10,10',
5
+ '46,8',
6
+ '12.12323, 123',
7
+ '24.0, 26.0',
8
+ '27.0 23.0', //same as above but different values
9
+ '10.00000S 10.000000E', //integer values only
10
+ '00.00 01.00', //DM, but no directions
4
11
  '50°4\'17.698"south, 24.34532', //different formats on each side
5
12
  '90°4\'17.698"south, 23°4\'17.698"east', //latitude out of bounds
6
13
  '89°4\'17.698"south, 183°4\'17.698"east', //longitude out of bounds
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geo-coordinates-parser",
3
- "version": "1.5.7",
3
+ "version": "1.5.8",
4
4
  "description": "A Javascript function for reading a variety of coordinate formats and converting to decimal numbers. Builds on other efforts by returning the verbatim coordinates and the decimal coordinates all in one object.",
5
5
  "main": "merge.js",
6
6
  "scripts": {
package/regex.js ADDED
@@ -0,0 +1,20 @@
1
+ //Coordinates pattern matching regex
2
+
3
+ //decimal degrees
4
+ var dd_re = /(NORTH|SOUTH|[NS])?[\s]*([+-]?[0-8]?[0-9](?:[\.,]\d{3,}))[\s]*([•º°]?)[\s]*(NORTH|SOUTH|[NS])?[\s]*[,/;]?[\s]*(EAST|WEST|[EW])?[\s]*([+-]?[0-1]?[0-9]?[0-9](?:[\.,]\d{3,}))[\s]*([•º°]?)[\s]*(EAST|WEST|[EW])?/i;
5
+
6
+ //degrees minutes seconds with '.' as separator - gives array with 15 values
7
+ var dms_periods = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*(\.)\s*([0-5]?[0-9])\s*(\.)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*(\.)\s*([0-5]?[0-9])\s*(\.)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(EAST|WEST|[EW])?/i;
8
+
9
+ //degrees minutes seconds with words 'degrees, minutes, seconds' as separators (needed because the s of seconds messes with the S of SOUTH) - gives array of 17 values
10
+ var dms_abbr = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*(D(?:EG)?(?:REES)?)\s*([0-5]?[0-9])\s*(M(?:IN)?(?:UTES)?)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(S(?:EC)?(?:ONDS)?)?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*(D(?:EG)?(?:REES)?)\s*([0-5]?[0-9])\s*(M(?:IN)?(?:UTES)?)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(S(?:EC)?(?:ONDS)?)\s*(EAST|WEST|[EW])?/i;
11
+
12
+ //everything else - gives array of 17 values
13
+ var coords_other = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*([•º°\.:]|D(?:EG)?(?:REES)?)?\s*,?([0-5]?[0-9](?:[\.,]\d{1,})?)?\s*(['′´’\.:]|M(?:IN)?(?:UTES)?)?\s*,?((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(''|′′|’’|´´|["″”\.])?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*([•º°\.:]|D(?:EG)?(?:REES)?)?\s*,?([0-5]?[0-9](?:[\.,]\d{1,})?)?\s*(['′´’\.:]|M(?:IN)?(?:UTES)?)?\s*,?((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(''|′′|´´|’’|["″”\.])?\s*(EAST|WEST|[EW])?/i;
14
+
15
+ module.exports = {
16
+ dd_re,
17
+ dms_periods,
18
+ dms_abbr,
19
+ coords_other
20
+ }
package/testIndividual.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const convert = require('./converter')
2
- const test = '27.45.34 S S 23.23.23'
2
+ const test = '10,11'
3
+ //const test = '00.00, 01.00'
3
4
 
4
5
  try{
5
6
  let converted = convert(test)
package/testformats.js CHANGED
@@ -244,6 +244,27 @@ var coordsRegexFormats = [
244
244
 
245
245
  var otherFormats = [
246
246
  // additional formats we've encountered
247
+ {
248
+ verbatimCoordinates: '10.432342S 10.6345345E', //this is read as degrees and minutes
249
+ verbatimLatitude: '10.432342S',
250
+ verbatimLongitude: '10.6345345E',
251
+ decimalLatitude: -10.432342,
252
+ decimalLongitude: 10.6345345
253
+ },
254
+ {
255
+ verbatimCoordinates: '10.00S 10.00E', //this is read as degrees and minutes
256
+ verbatimLatitude: '10.00S',
257
+ verbatimLongitude: '10.00E',
258
+ decimalLatitude: -10.00000,
259
+ decimalLongitude: 10.00000
260
+ },
261
+ {
262
+ verbatimCoordinates: '00.00S 01.00E', //this is read as degrees and minutes
263
+ verbatimLatitude: '00.00S',
264
+ verbatimLongitude: '01.00E',
265
+ decimalLatitude: 0.00000,
266
+ decimalLongitude: 1.00000
267
+ },
247
268
  {
248
269
  verbatimCoordinates: '18.24S 22.45E', //this is read as degrees and minutes
249
270
  verbatimLatitude: '18.24S',