UNPKG

1.45 kBJavaScriptView Raw
1import { addLeadingSlash } from '@tarojs/runtime';
2
3class RoutesAlias {
4 constructor() {
5 this.conf = [];
6 this.getConfig = (url = '') => {
7 const customRoute = this.conf.filter((arr) => {
8 return arr.includes(url);
9 });
10 return customRoute[0];
11 };
12 this.getOrigin = (url = '') => {
13 var _a;
14 return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
15 };
16 this.getAlias = (url = '') => {
17 var _a;
18 return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
19 };
20 this.getAll = (url = '') => {
21 return this.conf
22 .filter((arr) => arr.includes(url))
23 .reduceRight((p, a) => {
24 p.unshift(a[1]);
25 return p;
26 }, []);
27 };
28 }
29 set(customRoutes = {}) {
30 for (let key in customRoutes) {
31 const path = customRoutes[key];
32 key = addLeadingSlash(key);
33 if (typeof path === 'string') {
34 this.conf.push([key, addLeadingSlash(path)]);
35 }
36 else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
37 this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
38 }
39 }
40 }
41}
42const routesAlias = new RoutesAlias();
43
44export { routesAlias };