UNPKG

2.06 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function find(name, children) {
6 return children.some(function (node) {
7 if (node.name === name) {
8 return true;
9 }
10 else if (node.children) {
11 return find(name, node.children);
12 }
13 return false;
14 });
15}
16function active(route, response, options) {
17 if (options === void 0) { options = {}; }
18 if (response.name !== route.name &&
19 (!options.partial || !find(response.name, route.children))) {
20 return false;
21 }
22 var keys = route.keys;
23 if (keys.length) {
24 if (!options.params) {
25 return false;
26 }
27 for (var r = 0, length = keys.length; r < length; r++) {
28 var key = keys[r];
29 var param = options.params[key];
30 if (!param || param !== response.params[key]) {
31 return false;
32 }
33 }
34 }
35 if (options.components) {
36 return options.components(response.location);
37 }
38 return true;
39}
40
41function ancestors(route) {
42 var ancestors = [];
43 var parent = route.parent;
44 while (parent !== undefined) {
45 ancestors.unshift(parent);
46 parent = parent.parent;
47 }
48 return ancestors;
49}
50
51function pathname(route, params) {
52 return route.methods.pathname(params);
53}
54
55function prefetch(route, options) {
56 if (options === void 0) { options = {}; }
57 if (!route.methods.resolve) {
58 return Promise.resolve({
59 resolved: null,
60 error: "Could not prefetch data for " + route.name + " because it does not have a resolve function."
61 });
62 }
63 return route.methods
64 .resolve(options.match, options.external)
65 .then(function (resolved) { return ({ resolved: resolved, error: null }); }, function (error) { return ({ error: error, resolved: null }); });
66}
67
68exports.active = active;
69exports.ancestors = ancestors;
70exports.pathname = pathname;
71exports.prefetch = prefetch;