-
Notifications
You must be signed in to change notification settings - Fork 42
/
utils.js
267 lines (225 loc) · 6.53 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Returns true if object has any nested routes empty
* @param routeObject
**/
const anyEmptyNestedRoutes = (routeObject) => {
let result = false;
if (Object.keys(routeObject).length === 0) {
return true;
}
if (routeObject.childRoute && Object.keys(routeObject.childRoute).length === 0) {
result = true;
} else if (routeObject.childRoute) {
result = anyEmptyNestedRoutes(routeObject.childRoute);
}
return result;
};
/**
* Compare two routes ignoring named params
* @param pathName string
* @param routeName string
**/
const compareRoutes = (pathName, routeName) => {
routeName = removeSlash(routeName);
if (routeName.includes(':')) {
return routeName.includes(pathName);
} else {
return routeName.startsWith(pathName);
}
};
/**
* Returns a boolean indicating if the name of path exists in the route based on the language parameter
* @param pathName string
* @param route object
* @param language string
**/
const findLocalisedRoute = (pathName, route, language) => {
let exists = false;
if (language) {
return { exists: route.lang && route.lang[language] && route.lang[language].includes(pathName), language };
}
exists = compareRoutes(pathName, route.name);
if (!exists && route.lang && typeof route.lang === 'object') {
for (const [key, value] of Object.entries(route.lang)) {
if (compareRoutes(pathName, value)) {
exists = true;
language = key;
}
}
}
return { exists, language };
};
/**
* Return all the consecutive named param (placeholders) of a pathname
* @param pathname
**/
const getNamedParams = (pathName = '') => {
if (pathName.trim().length === 0) return [];
const namedUrlParams = getPathNames(pathName);
return namedUrlParams.reduce((validParams, param) => {
if (param[0] === ':') {
validParams.push(param.slice(1));
}
return validParams;
}, []);
};
/**
* Split a pathname based on /
* @param pathName
* Private method
**/
const getPathNames = (pathName) => {
if (pathName === '/' || pathName.trim().length === 0) return [pathName];
pathName = removeSlash(pathName, 'both');
return pathName.split('/');
};
/**
* Return the first part of a pathname until the first named param is found
* @param name
**/
const nameToPath = (name = '') => {
let routeName;
if (name === '/' || name.trim().length === 0) return name;
name = removeSlash(name, 'lead');
routeName = name.split(':')[0];
routeName = removeSlash(routeName, 'trail');
return routeName.toLowerCase();
};
/**
* Return the path name excluding query params
* @param name
**/
const pathWithoutQueryParams = (currentRoute) => {
const path = currentRoute.path.split('?');
return path[0];
};
/**
* Return the path name including query params
* @param name
**/
const pathWithQueryParams = (currentRoute) => {
let queryParams = [];
if (currentRoute.queryParams) {
for (let [key, value] of Object.entries(currentRoute.queryParams)) {
queryParams.push(`${key}=${value}`);
}
}
const hash = currentRoute.hash ? currentRoute.hash : '';
if (queryParams.length > 0) {
return `${currentRoute.path}?${queryParams.join('&')}${hash}`;
} else {
return currentRoute.path + hash;
}
};
/**
* Returns a string with trailing or leading slash character removed
* @param pathName string
* @param position string - lead, trail, both
**/
const removeExtraPaths = (pathNames, basePathNames) => {
const names = basePathNames.split('/');
if (names.length > 1) {
names.forEach(function(name, index) {
if (name.length > 0 && index > 0) {
pathNames.shift();
}
});
}
return pathNames;
};
/**
* Returns a string with trailing or leading slash character removed
* @param pathName string
* @param position string - lead, trail, both
**/
const removeSlash = (pathName, position = 'lead') => {
if (position === 'trail' || position === 'both') {
pathName = pathName.replace(/\/$/, '');
}
if (position === 'lead' || position === 'both') {
pathName = pathName.replace(/^\//, '');
}
return pathName;
};
/**
* Returns the name of the route based on the language parameter
* @param route object
* @param language string
**/
const routeNameLocalised = (route, language = null) => {
if (!language || !route.lang || !route.lang[language]) {
return route.name;
} else {
return route.lang[language];
}
};
/**
* Return the path name excluding query params
* @param name
**/
const startsWithNamedParam = (currentRoute) => {
const routeName = removeSlash(currentRoute);
return routeName.startsWith(':');
};
/**
* Updates the base route path.
* Route objects can have nested routes (childRoutes) or just a long name like "admin/employees/show/:id"
*
* @param basePath string
* @param pathNames array
* @param route object
* @param language string
**/
const updateRoutePath = (basePath, pathNames, route, language, convert = false) => {
if (basePath === '/' || basePath.trim().length === 0) return { result: basePath, language: null };
let basePathResult = basePath;
let routeName = route.name;
let currentLanguage = language;
if (convert) {
currentLanguage = '';
}
routeName = removeSlash(routeName);
basePathResult = removeSlash(basePathResult);
if (!route.childRoute) {
let localisedRoute = findLocalisedRoute(basePathResult, route, currentLanguage);
if (localisedRoute.exists && convert) {
basePathResult = routeNameLocalised(route, language);
}
let routeNames = routeName.split(':')[0];
routeNames = removeSlash(routeNames, 'trail');
routeNames = routeNames.split('/');
routeNames.shift();
routeNames.forEach(() => {
const currentPathName = pathNames[0];
localisedRoute = findLocalisedRoute(`${basePathResult}/${currentPathName}`, route, currentLanguage);
if (currentPathName && localisedRoute.exists) {
if (convert) {
basePathResult = routeNameLocalised(route, language);
} else {
basePathResult = `${basePathResult}/${currentPathName}`;
}
pathNames.shift();
} else {
return { result: basePathResult, language: localisedRoute.language };
}
});
return { result: basePathResult, language: localisedRoute.language };
} else {
return { result: basePath, language: currentLanguage };
}
};
export {
anyEmptyNestedRoutes,
compareRoutes,
findLocalisedRoute,
getNamedParams,
getPathNames,
nameToPath,
pathWithQueryParams,
pathWithoutQueryParams,
removeExtraPaths,
removeSlash,
routeNameLocalised,
startsWithNamedParam,
updateRoutePath,
};