geo-coordinates-parser 1.6.1 → 1.6.3

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -9,7 +9,7 @@ A Javascript function for reading a variety of coordinate formats and converting
9
9
  npm install geo-coordinates-parser
10
10
  ```
11
11
 
12
- # NOTE THAT USAGE CHANGED IN V1.6.0 TO BETTER SUPPORT ES6 AND COMMONJS
12
+ ## NOTE THAT USAGE CHANGED IN V1.6.0 TO BETTER SUPPORT ES6 AND COMMONJS
13
13
  ### Usage
14
14
  ```js
15
15
  const { convert } = require('geo-coordinates-parser'); //CommonJS
@@ -12,4 +12,5 @@ declare namespace converter {
12
12
  declare const to: Readonly<{
13
13
  DMS: "DMS";
14
14
  DM: "DM";
15
+ DD: "DD";
15
16
  }>;
@@ -357,7 +357,8 @@ function coordsCloseEnough(coordsToTest) {
357
357
  }
358
358
  const to = Object.freeze({
359
359
  DMS: 'DMS',
360
- DM: 'DM'
360
+ DM: 'DM',
361
+ DD: 'DD'
361
362
  });
362
363
  converter.to = to;
363
364
  exports.default = converter;
@@ -3,9 +3,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const index_js_1 = __importDefault(require("../src/toCoordinateFormat.js/index.js"));
6
+ const toCoordinateFormat_js_1 = __importDefault(require("../toCoordinateFormat.js"));
7
7
  let test = {
8
- decimalCoordinates: '-234.3456, 28.92435',
9
- toCoordinateFormat: index_js_1.default
8
+ decimalCoordinates: '-24.3456, 28.92435',
9
+ toCoordinateFormat: toCoordinateFormat_js_1.default
10
10
  };
11
11
  console.log(test.toCoordinateFormat('DMS'));
12
+ test.decimalCoordinates = '-25.76887,28.26939';
13
+ console.log(test.toCoordinateFormat('DMS'));
14
+ console.log(test.toCoordinateFormat('DM'));
15
+ console.log(test.toCoordinateFormat('DD'));
16
+ test.decimalCoordinates = '-25.815928, 28.070318';
17
+ console.log(test.toCoordinateFormat('DM'));
18
+ test.decimalCoordinates = '-25.000, 28.000';
19
+ console.log(test.toCoordinateFormat('DMS'));
20
+ console.log(test.toCoordinateFormat('DM'));
@@ -3,4 +3,4 @@ export default toCoordinateFormat;
3
3
  * Converts decimalCoordinates to other formats commonly used
4
4
  * @param {*} format Either DMS or DM
5
5
  */
6
- declare function toCoordinateFormat(format: any): string;
6
+ declare function toCoordinateFormat(format: any): any;
@@ -6,37 +6,45 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  * @param {*} format Either DMS or DM
7
7
  */
8
8
  function toCoordinateFormat(format) {
9
- if (!['DMS', 'DM'].includes(format))
9
+ if (!['DMS', 'DM', 'DD'].includes(format))
10
10
  throw new Error('invalid format specified');
11
11
  if (this.decimalCoordinates && this.decimalCoordinates.trim()) {
12
- const parts = this.decimalCoordinates.split(',').map(x => x.trim());
13
- const convertedLat = convert(parts[0], format, true);
14
- const convertedLong = convert(parts[1], format, false);
15
- return `${convertedLat}, ${convertedLong}`;
12
+ if (format == 'DD') {
13
+ return this.decimalCoordinates;
14
+ }
15
+ const parts = this.decimalCoordinates.split(',').map(x => Number(x.trim()));
16
+ let convertedLat = convert(parts[0], format, true);
17
+ let convertedLong = convert(parts[1], format, false);
18
+ //some custom cleaning for DMS
19
+ if (convertedLat.endsWith('.0"') && convertedLong.endsWith('.0"')) {
20
+ convertedLat = convertedLat.replace(/\.0"$/, '"');
21
+ convertedLong = convertedLong.replace(/\.0"$/, '"');
22
+ }
23
+ const latDirection = parts[0] >= 0 ? " N" : " S";
24
+ const longDirection = parts[1] >= 0 ? " E" : " W";
25
+ return `${convertedLat + latDirection}, ${convertedLong + longDirection}`;
16
26
  }
17
27
  else {
18
28
  throw new Error('no decimal coordinates to convert');
19
29
  }
20
30
  }
21
31
  //assumes everything is valid...
22
- function convert(coordString, format, isLatitude) {
23
- const coord = Number(coordString);
24
- let direction;
25
- if (isLatitude) {
26
- direction = coord >= 0 ? "N" : "S";
27
- }
28
- else {
29
- direction = coord >= 0 ? "E" : "W";
30
- }
32
+ function convert(coord, format) {
31
33
  const absolute = Math.abs(coord);
32
34
  const degrees = Math.floor(absolute);
33
35
  const minutesNotTruncated = (absolute - degrees) * 60;
34
36
  if (format == 'DM') {
35
- return `${degrees}° ${minutesNotTruncated.toFixed(3).replace(/\.0+$/, '')}' ${direction}`;
37
+ let dmMins = round(minutesNotTruncated, 3).toFixed(3).padStart(6, '0');
38
+ return `${degrees}° ${dmMins}'`;
36
39
  }
37
40
  //else
38
- const minutes = Math.floor(minutesNotTruncated);
39
- const seconds = ((minutesNotTruncated - minutes) * 60).toFixed(1).replace(/\.0$/, '');
40
- return `${degrees}° ${minutes}' ${seconds}" ${direction}`;
41
+ let minutes = Math.floor(minutesNotTruncated);
42
+ let seconds = ((minutesNotTruncated - minutes) * 60).toFixed(1).padStart(4, '0');
43
+ minutes = minutes.toString().padStart(2, '0');
44
+ return `${degrees}° ${minutes}' ${seconds}"`;
45
+ }
46
+ function round(num, places) {
47
+ const d = Math.pow(10, places);
48
+ return Math.round((num + Number.EPSILON) * d) / d;
41
49
  }
42
50
  exports.default = toCoordinateFormat;
@@ -12,4 +12,5 @@ declare namespace converter {
12
12
  declare const to: Readonly<{
13
13
  DMS: "DMS";
14
14
  DM: "DM";
15
+ DD: "DD";
15
16
  }>;
@@ -352,7 +352,8 @@ function coordsCloseEnough(coordsToTest) {
352
352
  }
353
353
  const to = Object.freeze({
354
354
  DMS: 'DMS',
355
- DM: 'DM'
355
+ DM: 'DM',
356
+ DD: 'DD'
356
357
  });
357
358
  converter.to = to;
358
359
  export default converter;
@@ -1,6 +1,15 @@
1
- import toCoordinateFormat from '../src/toCoordinateFormat.js/index.js';
1
+ import toCoordinateFormat from '../toCoordinateFormat.js';
2
2
  let test = {
3
- decimalCoordinates: '-234.3456, 28.92435',
3
+ decimalCoordinates: '-24.3456, 28.92435',
4
4
  toCoordinateFormat
5
5
  };
6
6
  console.log(test.toCoordinateFormat('DMS'));
7
+ test.decimalCoordinates = '-25.76887,28.26939';
8
+ console.log(test.toCoordinateFormat('DMS'));
9
+ console.log(test.toCoordinateFormat('DM'));
10
+ console.log(test.toCoordinateFormat('DD'));
11
+ test.decimalCoordinates = '-25.815928, 28.070318';
12
+ console.log(test.toCoordinateFormat('DM'));
13
+ test.decimalCoordinates = '-25.000, 28.000';
14
+ console.log(test.toCoordinateFormat('DMS'));
15
+ console.log(test.toCoordinateFormat('DM'));
@@ -3,4 +3,4 @@ export default toCoordinateFormat;
3
3
  * Converts decimalCoordinates to other formats commonly used
4
4
  * @param {*} format Either DMS or DM
5
5
  */
6
- declare function toCoordinateFormat(format: any): string;
6
+ declare function toCoordinateFormat(format: any): any;
@@ -4,37 +4,45 @@
4
4
  * @param {*} format Either DMS or DM
5
5
  */
6
6
  function toCoordinateFormat(format) {
7
- if (!['DMS', 'DM'].includes(format))
7
+ if (!['DMS', 'DM', 'DD'].includes(format))
8
8
  throw new Error('invalid format specified');
9
9
  if (this.decimalCoordinates && this.decimalCoordinates.trim()) {
10
- const parts = this.decimalCoordinates.split(',').map(x => x.trim());
11
- const convertedLat = convert(parts[0], format, true);
12
- const convertedLong = convert(parts[1], format, false);
13
- return `${convertedLat}, ${convertedLong}`;
10
+ if (format == 'DD') {
11
+ return this.decimalCoordinates;
12
+ }
13
+ const parts = this.decimalCoordinates.split(',').map(x => Number(x.trim()));
14
+ let convertedLat = convert(parts[0], format, true);
15
+ let convertedLong = convert(parts[1], format, false);
16
+ //some custom cleaning for DMS
17
+ if (convertedLat.endsWith('.0"') && convertedLong.endsWith('.0"')) {
18
+ convertedLat = convertedLat.replace(/\.0"$/, '"');
19
+ convertedLong = convertedLong.replace(/\.0"$/, '"');
20
+ }
21
+ const latDirection = parts[0] >= 0 ? " N" : " S";
22
+ const longDirection = parts[1] >= 0 ? " E" : " W";
23
+ return `${convertedLat + latDirection}, ${convertedLong + longDirection}`;
14
24
  }
15
25
  else {
16
26
  throw new Error('no decimal coordinates to convert');
17
27
  }
18
28
  }
19
29
  //assumes everything is valid...
20
- function convert(coordString, format, isLatitude) {
21
- const coord = Number(coordString);
22
- let direction;
23
- if (isLatitude) {
24
- direction = coord >= 0 ? "N" : "S";
25
- }
26
- else {
27
- direction = coord >= 0 ? "E" : "W";
28
- }
30
+ function convert(coord, format) {
29
31
  const absolute = Math.abs(coord);
30
32
  const degrees = Math.floor(absolute);
31
33
  const minutesNotTruncated = (absolute - degrees) * 60;
32
34
  if (format == 'DM') {
33
- return `${degrees}° ${minutesNotTruncated.toFixed(3).replace(/\.0+$/, '')}' ${direction}`;
35
+ let dmMins = round(minutesNotTruncated, 3).toFixed(3).padStart(6, '0');
36
+ return `${degrees}° ${dmMins}'`;
34
37
  }
35
38
  //else
36
- const minutes = Math.floor(minutesNotTruncated);
37
- const seconds = ((minutesNotTruncated - minutes) * 60).toFixed(1).replace(/\.0$/, '');
38
- return `${degrees}° ${minutes}' ${seconds}" ${direction}`;
39
+ let minutes = Math.floor(minutesNotTruncated);
40
+ let seconds = ((minutesNotTruncated - minutes) * 60).toFixed(1).padStart(4, '0');
41
+ minutes = minutes.toString().padStart(2, '0');
42
+ return `${degrees}° ${minutes}' ${seconds}"`;
43
+ }
44
+ function round(num, places) {
45
+ const d = Math.pow(10, places);
46
+ return Math.round((num + Number.EPSILON) * d) / d;
39
47
  }
40
48
  export default toCoordinateFormat;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geo-coordinates-parser",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
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": "merge.js",
@@ -43,8 +43,5 @@
43
43
  "bugs": {
44
44
  "url": "https://github.com/ianengelbrecht/geo-coordinates-parser/issues"
45
45
  },
46
- "homepage": "https://github.com/ianengelbrecht/geo-coordinates-parser#readme",
47
- "devDependencies": {
48
- "tinyify": "^2.5.1"
49
- }
46
+ "homepage": "https://github.com/ianengelbrecht/geo-coordinates-parser#readme"
50
47
  }