1
+ import allTags from '../config/all-tags.json' ;
2
+
3
+ import automatedTools from '../config/tools-automated.json' ;
4
+
5
+ import manualTools from '../config/tools-manual.json' ;
6
+
7
+ import type { ToolData , ToolsListData } from '../types/components/tools/ToolDataType' ;
8
+
9
+
10
+
11
+
12
+ // Caching the processed results to avoid unnecessary reprocessing of the data
13
+
14
+ let cachedToolsData : ToolsListData | null = null ;
15
+
16
+
17
+
18
+
19
+ //Interface for tool object data
20
+
21
+ interface RawToolData {
22
+
23
+ title : string ;
24
+
25
+ description : string | null ;
26
+
27
+ links : any ;
28
+
29
+ filters : {
30
+
31
+ categories ?: string [ ] ;
32
+
33
+ hasCommercial : boolean ;
34
+
35
+ isAsyncAPIOwner : boolean ;
36
+
37
+ language ?: string ;
38
+
39
+ technology ?: string [ ] ;
40
+
41
+ } ;
42
+
43
+ }
44
+
45
+
46
+
47
+
48
+ export const processToolsData = ( ) : ToolsListData => {
49
+
50
+ // Returning the cached data if available in the memory
51
+
52
+ if ( cachedToolsData ) return cachedToolsData ;
53
+
54
+
55
+
56
+
57
+ // Creating a tag color lookup mapping from the allTags data
58
+
59
+ const tagColorMap : Record < string , { color : string ; borderColor : string } > = { } ;
60
+
61
+
62
+
63
+
64
+ // Processing allTags to build the color map from the allTags data
65
+
66
+ if ( Array . isArray ( allTags ) ) {
67
+
68
+ allTags . forEach ( ( tag : any ) => {
69
+
70
+ tagColorMap [ tag . name ] = {
71
+
72
+ color : tag . color ,
73
+
74
+ borderColor : tag . borderColor
75
+
76
+ } ;
77
+
78
+ } ) ;
79
+
80
+ } else if ( allTags . languages && allTags . technologies ) {
81
+
82
+ // Handling the structure with separate languages and technologies arrays for each language and technology array separately in allTags
83
+
84
+ allTags . languages . forEach ( ( tag : any ) => {
85
+
86
+ tagColorMap [ tag . name ] = {
87
+
88
+ color : tag . color ,
89
+
90
+ borderColor : tag . borderColor
91
+
92
+ } ;
93
+
94
+ } ) ;
95
+
96
+
97
+
98
+
99
+ allTags . technologies . forEach ( ( tag : any ) => {
100
+
101
+ tagColorMap [ tag . name ] = {
102
+
103
+ color : tag . color ,
104
+
105
+ borderColor : tag . borderColor
106
+
107
+ } ;
108
+
109
+ } ) ;
110
+
111
+ }
112
+
113
+
114
+
115
+
116
+ // Initializing the result object to store the processed tools data in the required format
117
+
118
+ const result : ToolsListData = { } ;
119
+
120
+
121
+
122
+
123
+ // Helper function to process a tool object and return the processed tool object
124
+
125
+ const processTool = ( tool : RawToolData ) : ToolData => {
126
+
127
+ // Processing technology tags with color information if available in the tool data and convert null descriptions to undefined
128
+
129
+ const processedTechnology = Array . isArray ( tool . filters ?. technology )
130
+
131
+ ? tool . filters . technology . map ( ( tech : string ) => ( {
132
+
133
+ name : tech ,
134
+
135
+ color : tagColorMap [ tech ] ?. color || '#cccccc' ,
136
+
137
+ borderColor : tagColorMap [ tech ] ?. borderColor || '#999999'
138
+
139
+ } ) )
140
+
141
+ : [ ] ;
142
+
143
+
144
+
145
+
146
+ // Processing language tags with color information if available in the tool data and convert null descriptions to undefined
147
+
148
+ const processedLanguage = tool . filters ?. language
149
+
150
+ ? [
151
+
152
+ {
153
+
154
+ name : tool . filters . language ,
155
+
156
+ color : tagColorMap [ tool . filters . language ] ?. color || '#cccccc' ,
157
+
158
+ borderColor : tagColorMap [ tool . filters . language ] ?. borderColor || '#999999'
159
+
160
+ }
161
+
162
+ ]
163
+
164
+ : undefined ;
165
+
166
+
167
+
168
+
169
+ // Returning the processed tool object with null descriptions converted to undefined values
170
+
171
+ return {
172
+
173
+ title : tool . title ,
174
+
175
+ description : tool . description === null ? undefined : tool . description ,
176
+
177
+ links : tool . links ,
178
+
179
+ filters : {
180
+
181
+ categories : tool . filters . categories || [ ] ,
182
+
183
+ hasCommercial : tool . filters . hasCommercial ,
184
+
185
+ isAsyncAPIOwner : tool . filters . isAsyncAPIOwner ,
186
+
187
+ language : processedLanguage ,
188
+
189
+ technology : processedTechnology
190
+
191
+ }
192
+
193
+ } ;
194
+
195
+ } ;
196
+
197
+
198
+
199
+
200
+ // Processing tools from both sources and adding them to the result object
201
+
202
+ [ automatedTools , manualTools ] . forEach ( ( source : any ) => {
203
+
204
+ Object . entries ( source ) . forEach ( ( [ category , data ] : [ string , any ] ) => {
205
+
206
+ if ( ! result [ category ] ) {
207
+
208
+ result [ category ] = {
209
+
210
+ description : data . description ,
211
+
212
+ toolsList : [ ]
213
+
214
+ } ;
215
+
216
+ }
217
+
218
+
219
+
220
+
221
+ if ( Array . isArray ( data . toolsList ) ) {
222
+
223
+ data . toolsList . forEach ( ( tool : any ) => {
224
+
225
+ result [ category ] . toolsList . push ( processTool ( tool ) ) ;
226
+
227
+ } ) ;
228
+
229
+ }
230
+
231
+ } ) ;
232
+
233
+ } ) ;
234
+
235
+
236
+
237
+
238
+ // Caching and return the result data from the result object
239
+
240
+ cachedToolsData = result ;
241
+
242
+
243
+
244
+
245
+ return result ;
246
+
247
+ } ;
0 commit comments