-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
132 lines (112 loc) · 2.98 KB
/
index.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
// @ts-check
export default (config = {}) => {
const _routes = new Map();
const _config = Object.assign({
baseUrl: '',
}, config);
const _getUrlParams = url => {
let mappedParams = url.match(/{\s*[\w\.]+\s*}/g); // eslint-disable-line
// if we have mappedParams, strip off the {}
if (mappedParams !== null) {
return mappedParams.map(x => x.match(/[\w\.]+/)[0]); // eslint-disable-line
}
return [];
};
const _stringifyParams = params => Object.keys(params)
.reduce((acc, k) => `${acc}${k}=${encodeURIComponent(params[k])}&`, '')
.replace(/&$/, '');
/**
* This replaces any interpolated params with items passed in via the routeParams object
*
* @param {string} url
* @param {Object} urlParams
* @param {Object} queryParams
* @return {string}
*/
const _replaceURLParams = (url, urlParams, queryParams) => {
const routeParams = _getUrlParams(url);
let mappedUrl = url;
if (Object.keys(urlParams).length !== 0) {
// only do this if we have route params
if (routeParams.length) {
// replace each occurrence of the param with the value passed in
routeParams.forEach(param => {
mappedUrl = mappedUrl.replace(`{${param}}`, urlParams[param]);
});
} else {
mappedUrl = `${mappedUrl}?${_stringifyParams(urlParams)}`;
}
}
if (Object.keys(queryParams).length !== 0) {
mappedUrl = `${mappedUrl}?${_stringifyParams(queryParams)}`;
}
return mappedUrl;
};
const _get = k => `${_config.baseUrl}${_routes.get(k)}`;
/**
* Generate a route from a key and url params to interpolate
*
* @param {string} k
* @param {Object} params
* @param {Object} queryParams
* @return {string}
*/
const generate = (k, params = {}, queryParams = {}) => _replaceURLParams(_get(k), params, queryParams);
/**
* Set a new route with a key and value
*
* @param {string} k
* @param {string} v
*/
const set = (k, v) => {
_routes.set(k, v);
};
/**
* Prefix a set of routes
*
* @param {Object} prefixes
* @param {string} prefixes.path
* @param {string} prefixes.name
* @param {Object} newRoutes
*/
const prefix = (prefixes = { path: '', name: '' }, newRoutes) => {
const name = prefixes.name ? `${prefixes.name}_` : '';
const path = prefixes.path ? `${prefixes.path}` : '';
Object.keys(newRoutes).forEach(k => {
set(`${name}${k}`, `${path}${newRoutes[k]}`);
});
};
/**
* Retrieve all the routes
*
* @return {array}
*/
const all = () => {
const routes = [];
_routes.forEach((value, key) => {
routes.push({ key, value });
});
return routes;
};
/**
* Lock the routes so there's no abililty call set
*
* @return {Object}
*/
const lock = () => ({
generate,
all,
});
return {
generate,
set,
prefix,
all,
lock,
// for testing purposes
_get,
_getUrlParams,
_replaceURLParams,
_stringifyParams,
};
};