1
1
import { formatLocation , formatParam } from './helpers' ;
2
2
import { StaticMapsPath } from './types' ;
3
3
4
+ // Style properties that can be applied to paths in the Static Maps API
5
+ const PATH_STYLE_KEYS = [ 'color' , 'weight' , 'fillcolor' , 'geodesic' ] as const ;
6
+
4
7
/**
5
- * Assembles path parameters for the Static Maps Api from an array of paths.
8
+ * Builds the style portion of a path parameter string.
9
+ * @param path - The path object containing style properties
10
+ * @returns A string with style parameters in the format "|key:value"
11
+ */
12
+ function buildStyleParams ( path : StaticMapsPath ) : string {
13
+ let styleParams = '' ;
14
+
15
+ PATH_STYLE_KEYS . forEach ( key => {
16
+ if ( path [ key ] !== undefined ) {
17
+ styleParams += `|${ key } :${ path [ key ] } ` ;
18
+ }
19
+ } ) ;
20
+
21
+ return styleParams ;
22
+ }
23
+
24
+ /**
25
+ * Builds the coordinates portion of a path parameter string.
26
+ * @param coordinates - Either a string or array of location objects
27
+ * @returns A string with coordinates in the format "|lat,lng|lat,lng"
28
+ */
29
+ function buildCoordinateParams (
30
+ coordinates : StaticMapsPath [ 'coordinates' ]
31
+ ) : string {
32
+ if ( typeof coordinates === 'string' ) {
33
+ return `|${ decodeURIComponent ( coordinates ) } ` ;
34
+ }
35
+
36
+ return coordinates . map ( location => `|${ formatLocation ( location ) } ` ) . join ( '' ) ;
37
+ }
38
+
39
+ /**
40
+ * Assembles path parameters for the Static Maps API from an array of paths.
6
41
*
7
- * This function groups paths by their style properties (color, weight, fillcolor, geodesic)
8
- * and then constructs a string of path parameters for each group. Each path parameter string
42
+ * This function constructs a string of path parameters for each path. Each path parameter string
9
43
* includes the style properties and the coordinates of the paths.
10
44
*
11
45
* @param {Array<StaticMapsPath> } [paths=[]] - An array of paths to be assembled into path parameters.
12
46
* @returns {Array<string> } An array of path parameter strings.
13
47
*
14
48
* @example
49
+ * ```typescript
15
50
* const paths = [
16
51
* {
17
52
* color: 'red',
@@ -24,56 +59,18 @@ import {StaticMapsPath} from './types';
24
59
* ];
25
60
*
26
61
* const pathParams = assemblePathParams(paths);
27
- * Output: [
28
- * 'color:red|weight:5|40.714728,-73.998672|40.718217,-73.998284'
29
- * ]
62
+ * // Output: ['color:red|weight:5|40.714728,-73.998672|40.718217,-73.998284']
63
+ * ```
30
64
*/
31
- export function assemblePathParams ( paths : Array < StaticMapsPath > = [ ] ) {
32
- const pathParams : Array < string > = [ ] ;
33
-
34
- // Group paths by their style properties (color, weight, fillcolor, geodesic)
35
- // to combine paths with identical styles into single parameter strings
36
- const pathsByStyle = paths ?. reduce (
37
- ( styles , path ) => {
38
- const { color = 'default' , weight, fillcolor, geodesic} = path ;
39
-
40
- // Create unique key for this style combination
41
- const key = [ color , weight , fillcolor , geodesic ]
42
- . filter ( Boolean )
43
- . join ( '-' ) ;
65
+ export function assemblePathParams (
66
+ paths : Array < StaticMapsPath > = [ ]
67
+ ) : string [ ] {
68
+ return paths . map ( path => {
69
+ const styleParams = buildStyleParams ( path ) ;
70
+ const coordinateParams = buildCoordinateParams ( path . coordinates ) ;
44
71
45
- styles [ key ] = styles [ key ] || [ ] ;
46
- styles [ key ] . push ( path ) ;
47
- return styles ;
48
- } ,
49
- { } as Record < string , Array < StaticMapsPath > >
50
- ) ;
72
+ const pathParam = styleParams + coordinateParams ;
51
73
52
- // Process each group of paths with identical styles
53
- Object . values ( pathsByStyle ?? { } ) . forEach ( paths => {
54
- let pathParam = '' ;
55
-
56
- // Build style parameter string using properties from first path in group
57
- // since all paths in this group share the same style
58
- Object . entries ( paths [ 0 ] ) . forEach ( ( [ key , value ] ) => {
59
- if ( [ 'color' , 'weight' , 'fillcolor' , 'geodesic' ] . includes ( key ) ) {
60
- pathParam += `|${ key } :${ value } ` ;
61
- }
62
- } ) ;
63
-
64
- // Add location for all marker in style group
65
- for ( const path of paths ) {
66
- if ( typeof path . coordinates === 'string' ) {
67
- pathParam += `|${ decodeURIComponent ( path . coordinates ) } ` ;
68
- } else {
69
- for ( const location of path . coordinates ) {
70
- pathParam += `|${ formatLocation ( location ) } ` ;
71
- }
72
- }
73
- }
74
-
75
- pathParams . push ( pathParam ) ;
74
+ return formatParam ( pathParam ) ;
76
75
} ) ;
77
-
78
- return pathParams . map ( formatParam ) ;
79
76
}
0 commit comments