geo-coordinates-parser 1.7.1 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -46,6 +46,8 @@ converted.decimalLatitude; // 40.446195 ✓
46
46
  converted.decimalLongitude; // -79.948862 ✓
47
47
  converted.verbatimLatitude; // '40° 26.7717' ✓
48
48
  converted.verbatimLongitude; // '-79° 56.93172' ✓
49
+ converted.decimalCoordinates; // for convenience
50
+ convert.originalFormat; // 'DM' to indicate degrees and minutes
49
51
  ```
50
52
  The returned object includes properties verbatimCoordinates, verbatimLatitude, verbatimLongitude, decimalLatitude, decimalLatitude, and decimalCoordinates.
51
53
 
@@ -23,6 +23,7 @@ function converter(coordsString, decimalPlaces) {
23
23
  let ddLng = null;
24
24
  let latdir = "";
25
25
  let lngdir = "";
26
+ let originalFormat = null;
26
27
  let match = [];
27
28
  let matchSuccess = false;
28
29
  if (regex_js_1.dm_numbers.test(coordsString)) {
@@ -37,6 +38,7 @@ function converter(coordsString, decimalPlaces) {
37
38
  if (Number(match[3]) < 0) {
38
39
  ddLng *= -1;
39
40
  }
41
+ originalFormat = "DM";
40
42
  }
41
43
  else {
42
44
  throw new Error("invalid coordinate format");
@@ -55,6 +57,7 @@ function converter(coordsString, decimalPlaces) {
55
57
  if (ddLng.includes(',')) {
56
58
  ddLng = ddLng.replace(',', '.');
57
59
  }
60
+ originalFormat = "DD";
58
61
  //validation, we don't want things like 23.00000
59
62
  //some more validation: no zero coords or degrees only
60
63
  if (Number(Math.round(ddLat)) == Number(ddLat)) {
@@ -84,9 +87,11 @@ function converter(coordsString, decimalPlaces) {
84
87
  ddLat = Math.abs(parseInt(match[2]));
85
88
  if (match[4]) {
86
89
  ddLat += match[4] / 60;
90
+ originalFormat = "DM";
87
91
  }
88
92
  if (match[6]) {
89
93
  ddLat += match[6].replace(',', '.') / 3600;
94
+ originalFormat = "DMS";
90
95
  }
91
96
  if (parseInt(match[2]) < 0) {
92
97
  ddLat = -1 * ddLat;
@@ -122,9 +127,11 @@ function converter(coordsString, decimalPlaces) {
122
127
  ddLat = Math.abs(parseInt(match[2]));
123
128
  if (match[4]) {
124
129
  ddLat += match[4] / 60;
130
+ originalFormat = "DM";
125
131
  }
126
132
  if (match[6]) {
127
133
  ddLat += match[6] / 3600;
134
+ originalFormat = "DMS";
128
135
  }
129
136
  if (parseInt(match[2]) < 0) {
130
137
  ddLat = -1 * ddLat;
@@ -160,9 +167,11 @@ function converter(coordsString, decimalPlaces) {
160
167
  ddLat = Math.abs(parseInt(match[2]));
161
168
  if (match[4]) {
162
169
  ddLat += match[4].replace(',', '.') / 60;
170
+ originalFormat = "DM";
163
171
  }
164
172
  if (match[6]) {
165
173
  ddLat += match[6].replace(',', '.') / 3600;
174
+ originalFormat = "DMS";
166
175
  }
167
176
  if (parseInt(match[2]) < 0) {
168
177
  ddLat = -1 * ddLat;
@@ -209,6 +218,13 @@ function converter(coordsString, decimalPlaces) {
209
218
  if (latdir && latdir == lngdir) {
210
219
  throw new Error("invalid coordinates format");
211
220
  }
221
+ // a bit of tidying up...
222
+ if (ddLat.toString().includes(',')) {
223
+ ddLat = ddLat.replace(',', '.');
224
+ }
225
+ if (ddLng.toString().includes(',')) {
226
+ ddLng = ddLng.replace(',', '.');
227
+ }
212
228
  //make sure the signs and cardinal directions match
213
229
  let patt = /S|SOUTH/i;
214
230
  if (patt.test(latdir)) {
@@ -284,13 +300,6 @@ function converter(coordsString, decimalPlaces) {
284
300
  if (/^\d+$/.test(verbatimLat) || /^\d+$/.test(verbatimLng)) {
285
301
  throw new Error('degree only coordinate/s provided');
286
302
  }
287
- // last bit of tidying up...
288
- if (isNaN(ddLat) && ddLat.includes(',')) {
289
- ddLat = ddLat.replace(',', '.');
290
- }
291
- if (isNaN(ddLng) && ddLng.includes(',')) {
292
- ddLng = ddLng.replace(',', '.');
293
- }
294
303
  //all done!!
295
304
  //just truncate the decimals appropriately
296
305
  ddLat = Number(Number(ddLat).toFixed(decimalPlaces));
@@ -302,6 +311,7 @@ function converter(coordsString, decimalPlaces) {
302
311
  decimalLatitude: ddLat,
303
312
  decimalLongitude: ddLng,
304
313
  decimalCoordinates: `${ddLat},${ddLng}`,
314
+ originalFormat,
305
315
  closeEnough: coordsCloseEnough,
306
316
  toCoordinateFormat: toCoordinateFormat_js_1.default
307
317
  });
@@ -28,8 +28,12 @@ function toCoordinateFormat(format) {
28
28
  const minutesLatitudeNotTruncated = (absoluteLatitude - degreesLatitude) * 60;
29
29
  const minutesLongitudeNotTruncated = (absoluteLongitude - degreesLongitude) * 60;
30
30
  if (format == 'DM') {
31
- const dmMinsLatitude = round(minutesLatitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
32
- const dmMinsLongitude = round(minutesLongitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
31
+ let dmMinsLatitude = round(minutesLatitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
32
+ let dmMinsLongitude = round(minutesLongitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
33
+ if (dmMinsLatitude.endsWith('.000') && dmMinsLongitude.endsWith('.000')) {
34
+ dmMinsLatitude = dmMinsLatitude.replace(/\.000$/, '');
35
+ dmMinsLongitude = dmMinsLongitude.replace(/\.000$/, '');
36
+ }
33
37
  result = `${degreesLatitude}° ${dmMinsLatitude}' ${latDir}, ${degreesLongitude}° ${dmMinsLongitude}' ${longDir}`;
34
38
  }
35
39
  if (format == "DMS") {
@@ -40,9 +44,9 @@ function toCoordinateFormat(format) {
40
44
  const latMinutesString = latMinutes.toString().padStart(2, '0');
41
45
  const longMinutesString = longMinutes.toString().padStart(2, '0');
42
46
  // if they both end in .0 we drop the .0
43
- if (latSeconds.endsWith('.0"') && longSeconds.endsWith('.0"')) {
44
- latSeconds = latSeconds.replace(/\.0"$/, '"');
45
- longSeconds = longSeconds.replace(/\.0"$/, '"');
47
+ if (latSeconds.endsWith('.0') && longSeconds.endsWith('.0')) {
48
+ latSeconds = latSeconds.replace(/\.0$/, '');
49
+ longSeconds = longSeconds.replace(/\.0$/, '');
46
50
  }
47
51
  result = `${degreesLatitude}° ${latMinutesString}' ${latSeconds}" ${latDir}, ${degreesLongitude}° ${longMinutesString}' ${longSeconds}" ${longDir}`;
48
52
  }
@@ -18,6 +18,7 @@ function converter(coordsString, decimalPlaces) {
18
18
  let ddLng = null;
19
19
  let latdir = "";
20
20
  let lngdir = "";
21
+ let originalFormat = null;
21
22
  let match = [];
22
23
  let matchSuccess = false;
23
24
  if (dm_numbers.test(coordsString)) {
@@ -32,6 +33,7 @@ function converter(coordsString, decimalPlaces) {
32
33
  if (Number(match[3]) < 0) {
33
34
  ddLng *= -1;
34
35
  }
36
+ originalFormat = "DM";
35
37
  }
36
38
  else {
37
39
  throw new Error("invalid coordinate format");
@@ -50,6 +52,7 @@ function converter(coordsString, decimalPlaces) {
50
52
  if (ddLng.includes(',')) {
51
53
  ddLng = ddLng.replace(',', '.');
52
54
  }
55
+ originalFormat = "DD";
53
56
  //validation, we don't want things like 23.00000
54
57
  //some more validation: no zero coords or degrees only
55
58
  if (Number(Math.round(ddLat)) == Number(ddLat)) {
@@ -79,9 +82,11 @@ function converter(coordsString, decimalPlaces) {
79
82
  ddLat = Math.abs(parseInt(match[2]));
80
83
  if (match[4]) {
81
84
  ddLat += match[4] / 60;
85
+ originalFormat = "DM";
82
86
  }
83
87
  if (match[6]) {
84
88
  ddLat += match[6].replace(',', '.') / 3600;
89
+ originalFormat = "DMS";
85
90
  }
86
91
  if (parseInt(match[2]) < 0) {
87
92
  ddLat = -1 * ddLat;
@@ -117,9 +122,11 @@ function converter(coordsString, decimalPlaces) {
117
122
  ddLat = Math.abs(parseInt(match[2]));
118
123
  if (match[4]) {
119
124
  ddLat += match[4] / 60;
125
+ originalFormat = "DM";
120
126
  }
121
127
  if (match[6]) {
122
128
  ddLat += match[6] / 3600;
129
+ originalFormat = "DMS";
123
130
  }
124
131
  if (parseInt(match[2]) < 0) {
125
132
  ddLat = -1 * ddLat;
@@ -155,9 +162,11 @@ function converter(coordsString, decimalPlaces) {
155
162
  ddLat = Math.abs(parseInt(match[2]));
156
163
  if (match[4]) {
157
164
  ddLat += match[4].replace(',', '.') / 60;
165
+ originalFormat = "DM";
158
166
  }
159
167
  if (match[6]) {
160
168
  ddLat += match[6].replace(',', '.') / 3600;
169
+ originalFormat = "DMS";
161
170
  }
162
171
  if (parseInt(match[2]) < 0) {
163
172
  ddLat = -1 * ddLat;
@@ -204,6 +213,13 @@ function converter(coordsString, decimalPlaces) {
204
213
  if (latdir && latdir == lngdir) {
205
214
  throw new Error("invalid coordinates format");
206
215
  }
216
+ // a bit of tidying up...
217
+ if (ddLat.toString().includes(',')) {
218
+ ddLat = ddLat.replace(',', '.');
219
+ }
220
+ if (ddLng.toString().includes(',')) {
221
+ ddLng = ddLng.replace(',', '.');
222
+ }
207
223
  //make sure the signs and cardinal directions match
208
224
  let patt = /S|SOUTH/i;
209
225
  if (patt.test(latdir)) {
@@ -279,13 +295,6 @@ function converter(coordsString, decimalPlaces) {
279
295
  if (/^\d+$/.test(verbatimLat) || /^\d+$/.test(verbatimLng)) {
280
296
  throw new Error('degree only coordinate/s provided');
281
297
  }
282
- // last bit of tidying up...
283
- if (isNaN(ddLat) && ddLat.includes(',')) {
284
- ddLat = ddLat.replace(',', '.');
285
- }
286
- if (isNaN(ddLng) && ddLng.includes(',')) {
287
- ddLng = ddLng.replace(',', '.');
288
- }
289
298
  //all done!!
290
299
  //just truncate the decimals appropriately
291
300
  ddLat = Number(Number(ddLat).toFixed(decimalPlaces));
@@ -297,6 +306,7 @@ function converter(coordsString, decimalPlaces) {
297
306
  decimalLatitude: ddLat,
298
307
  decimalLongitude: ddLng,
299
308
  decimalCoordinates: `${ddLat},${ddLng}`,
309
+ originalFormat,
300
310
  closeEnough: coordsCloseEnough,
301
311
  toCoordinateFormat
302
312
  });
@@ -26,8 +26,12 @@ function toCoordinateFormat(format) {
26
26
  const minutesLatitudeNotTruncated = (absoluteLatitude - degreesLatitude) * 60;
27
27
  const minutesLongitudeNotTruncated = (absoluteLongitude - degreesLongitude) * 60;
28
28
  if (format == 'DM') {
29
- const dmMinsLatitude = round(minutesLatitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
30
- const dmMinsLongitude = round(minutesLongitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
29
+ let dmMinsLatitude = round(minutesLatitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
30
+ let dmMinsLongitude = round(minutesLongitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
31
+ if (dmMinsLatitude.endsWith('.000') && dmMinsLongitude.endsWith('.000')) {
32
+ dmMinsLatitude = dmMinsLatitude.replace(/\.000$/, '');
33
+ dmMinsLongitude = dmMinsLongitude.replace(/\.000$/, '');
34
+ }
31
35
  result = `${degreesLatitude}° ${dmMinsLatitude}' ${latDir}, ${degreesLongitude}° ${dmMinsLongitude}' ${longDir}`;
32
36
  }
33
37
  if (format == "DMS") {
@@ -38,9 +42,9 @@ function toCoordinateFormat(format) {
38
42
  const latMinutesString = latMinutes.toString().padStart(2, '0');
39
43
  const longMinutesString = longMinutes.toString().padStart(2, '0');
40
44
  // if they both end in .0 we drop the .0
41
- if (latSeconds.endsWith('.0"') && longSeconds.endsWith('.0"')) {
42
- latSeconds = latSeconds.replace(/\.0"$/, '"');
43
- longSeconds = longSeconds.replace(/\.0"$/, '"');
45
+ if (latSeconds.endsWith('.0') && longSeconds.endsWith('.0')) {
46
+ latSeconds = latSeconds.replace(/\.0$/, '');
47
+ longSeconds = longSeconds.replace(/\.0$/, '');
44
48
  }
45
49
  result = `${degreesLatitude}° ${latMinutesString}' ${latSeconds}" ${latDir}, ${degreesLongitude}° ${longMinutesString}' ${longSeconds}" ${longDir}`;
46
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geo-coordinates-parser",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
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
  "type": "module",
6
6
  "main": "./dist/cjs/merge.js",