geo-coordinates-parser 1.6.2 → 1.6.3
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/dist/cjs/converter.d.ts +1 -0
- package/dist/cjs/converter.js +2 -1
- package/dist/cjs/tests/testFormatConverter.js +12 -3
- package/dist/cjs/toCoordinateFormat.d.ts +1 -1
- package/dist/cjs/toCoordinateFormat.js +26 -18
- package/dist/mjs/converter.d.ts +1 -0
- package/dist/mjs/converter.js +2 -1
- package/dist/mjs/tests/testFormatConverter.js +11 -2
- package/dist/mjs/toCoordinateFormat.d.ts +1 -1
- package/dist/mjs/toCoordinateFormat.js +26 -18
- package/package.json +1 -1
package/dist/cjs/converter.d.ts
CHANGED
package/dist/cjs/converter.js
CHANGED
@@ -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
|
6
|
+
const toCoordinateFormat_js_1 = __importDefault(require("../toCoordinateFormat.js"));
|
7
7
|
let test = {
|
8
|
-
decimalCoordinates: '-
|
9
|
-
toCoordinateFormat:
|
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'));
|
@@ -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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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(
|
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
|
-
|
37
|
+
let dmMins = round(minutesNotTruncated, 3).toFixed(3).padStart(6, '0');
|
38
|
+
return `${degrees}° ${dmMins}'`;
|
36
39
|
}
|
37
40
|
//else
|
38
|
-
|
39
|
-
|
40
|
-
|
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;
|
package/dist/mjs/converter.d.ts
CHANGED
package/dist/mjs/converter.js
CHANGED
@@ -1,6 +1,15 @@
|
|
1
|
-
import toCoordinateFormat from '../
|
1
|
+
import toCoordinateFormat from '../toCoordinateFormat.js';
|
2
2
|
let test = {
|
3
|
-
decimalCoordinates: '-
|
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'));
|
@@ -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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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(
|
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
|
-
|
35
|
+
let dmMins = round(minutesNotTruncated, 3).toFixed(3).padStart(6, '0');
|
36
|
+
return `${degrees}° ${dmMins}'`;
|
34
37
|
}
|
35
38
|
//else
|
36
|
-
|
37
|
-
|
38
|
-
|
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.
|
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",
|