@tmlmobilidade/utils 20250822.1829.15 → 20250822.1847.54
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.
|
@@ -17,14 +17,21 @@ export declare function isValidLongitude(value: number): boolean;
|
|
|
17
17
|
* @returns True if the coordinate pair is valid, false otherwise.
|
|
18
18
|
*/
|
|
19
19
|
export declare function isValidCoordinatePair(lat: number, lng: number): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Clamps a coordinate value to 6 decimal places.
|
|
22
|
+
* @param value The coordinate value to clamp.
|
|
23
|
+
* @returns The clamped coordinate value.
|
|
24
|
+
*/
|
|
25
|
+
export declare function clampCoordinate(value: number): number;
|
|
20
26
|
/**
|
|
21
27
|
* Parses a coordinate string in the following formats:
|
|
22
28
|
* - `lat, lng`
|
|
23
29
|
* - `lat lng` (with a space or a tab)
|
|
24
30
|
* @param input The coordinate string to parse.
|
|
31
|
+
* @param clamp Whether to clamp the latitude and longitude values to 6 decimal places.
|
|
25
32
|
* @returns The parsed coordinates as an object, or null if the input is invalid.
|
|
26
33
|
*/
|
|
27
|
-
export declare const parseCoordinateString: (input: string) => null | {
|
|
34
|
+
export declare const parseCoordinateString: (input: string, clamp?: boolean) => null | {
|
|
28
35
|
lat: number;
|
|
29
36
|
lng: number;
|
|
30
37
|
};
|
|
@@ -24,19 +24,31 @@ export function isValidLongitude(value) {
|
|
|
24
24
|
export function isValidCoordinatePair(lat, lng) {
|
|
25
25
|
return isValidLatitude(lat) && isValidLongitude(lng);
|
|
26
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Clamps a coordinate value to 6 decimal places.
|
|
29
|
+
* @param value The coordinate value to clamp.
|
|
30
|
+
* @returns The clamped coordinate value.
|
|
31
|
+
*/
|
|
32
|
+
export function clampCoordinate(value) {
|
|
33
|
+
return parseFloat(value.toFixed(6));
|
|
34
|
+
}
|
|
27
35
|
/**
|
|
28
36
|
* Parses a coordinate string in the following formats:
|
|
29
37
|
* - `lat, lng`
|
|
30
38
|
* - `lat lng` (with a space or a tab)
|
|
31
39
|
* @param input The coordinate string to parse.
|
|
40
|
+
* @param clamp Whether to clamp the latitude and longitude values to 6 decimal places.
|
|
32
41
|
* @returns The parsed coordinates as an object, or null if the input is invalid.
|
|
33
42
|
*/
|
|
34
|
-
export const parseCoordinateString = (input) => {
|
|
43
|
+
export const parseCoordinateString = (input, clamp = true) => {
|
|
35
44
|
const regex = /^\s*([+-]?\d+(?:\.\d+)?)\s*(?:,|\s)\s*([+-]?\d+(?:\.\d+)?)\s*$/;
|
|
36
45
|
const match = input.match(regex);
|
|
37
46
|
if (!match)
|
|
38
47
|
return null;
|
|
39
48
|
const lat = parseFloat(match[1]);
|
|
40
49
|
const lng = parseFloat(match[2]);
|
|
41
|
-
|
|
50
|
+
if (clamp)
|
|
51
|
+
return isValidCoordinatePair(lat, lng) ? { lat: clampCoordinate(lat), lng: clampCoordinate(lng) } : null;
|
|
52
|
+
else
|
|
53
|
+
return isValidCoordinatePair(lat, lng) ? { lat, lng } : null;
|
|
42
54
|
};
|
package/package.json
CHANGED