forked from nullstack/nullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameterizable.js
More file actions
32 lines (28 loc) · 882 Bytes
/
parameterizable.js
File metadata and controls
32 lines (28 loc) · 882 Bytes
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
import serializeParam from '../shared/serializeParam';
import serializeSearch from '../shared/serializeSearch';
export default class Parameterizable {
match({node}) {
return (
node &&
node.attributes &&
(node.attributes.params || node.attributes.path)
)
}
transform({node, scope}) {
const {router, params} = scope.context;
let serializedParams;
if(node.attributes.params) {
serializedParams = {};
for(const key in node.attributes.params) {
serializedParams[key] = serializeParam(node.attributes.params[key]);
}
} else {
serializedParams = params;
}
const search = serializeSearch(serializedParams);
const path = node.attributes.path || router.path;
node.attributes.href = path + (search ? '?' : '') + search;
delete node.attributes.path;
delete node.attributes.params;
}
}