@tanstack/router-core 0.0.1-beta.45 → 0.0.1-beta.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/actions.js +94 -0
- package/build/cjs/actions.js.map +1 -0
- package/build/cjs/history.js +163 -0
- package/build/cjs/history.js.map +1 -0
- package/build/cjs/index.js +18 -20
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/interop.js +175 -0
- package/build/cjs/interop.js.map +1 -0
- package/build/cjs/path.js +4 -5
- package/build/cjs/path.js.map +1 -1
- package/build/cjs/route.js +16 -138
- package/build/cjs/route.js.map +1 -1
- package/build/cjs/routeConfig.js +1 -7
- package/build/cjs/routeConfig.js.map +1 -1
- package/build/cjs/routeMatch.js +194 -199
- package/build/cjs/routeMatch.js.map +1 -1
- package/build/cjs/router.js +726 -703
- package/build/cjs/router.js.map +1 -1
- package/build/cjs/store.js +54 -0
- package/build/cjs/store.js.map +1 -0
- package/build/esm/index.js +1305 -1114
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +229 -192
- package/build/types/index.d.ts +172 -109
- package/build/umd/index.development.js +1381 -2331
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +3 -3
- package/src/actions.ts +157 -0
- package/src/history.ts +199 -0
- package/src/index.ts +4 -7
- package/src/interop.ts +169 -0
- package/src/link.ts +2 -2
- package/src/route.ts +34 -239
- package/src/routeConfig.ts +3 -34
- package/src/routeInfo.ts +6 -21
- package/src/routeMatch.ts +270 -285
- package/src/router.ts +967 -963
- package/src/store.ts +52 -0
- package/build/cjs/sharedClone.js +0 -122
- package/build/cjs/sharedClone.js.map +0 -1
- package/src/sharedClone.ts +0 -118
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* router-core
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var invariant = require('tiny-invariant');
|
|
16
|
+
var store = require('./store.js');
|
|
17
|
+
|
|
18
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
19
|
+
|
|
20
|
+
var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
|
|
21
|
+
|
|
22
|
+
// createRouterAction is a constrained identify function that takes options: key, action, onSuccess, onError, onSettled, etc
|
|
23
|
+
function createAction(options) {
|
|
24
|
+
const store$1 = store.createStore({
|
|
25
|
+
submissions: []
|
|
26
|
+
}, options.debug);
|
|
27
|
+
return {
|
|
28
|
+
options,
|
|
29
|
+
store: store$1,
|
|
30
|
+
reset: () => {
|
|
31
|
+
store$1.setState(s => {
|
|
32
|
+
s.submissions = [];
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
submit: async payload => {
|
|
36
|
+
const submission = {
|
|
37
|
+
submittedAt: Date.now(),
|
|
38
|
+
status: 'pending',
|
|
39
|
+
payload: payload,
|
|
40
|
+
invalidate: () => {
|
|
41
|
+
setSubmission(s => {
|
|
42
|
+
s.isInvalid = true;
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
getIsLatest: () => store$1.state.submissions[store$1.state.submissions.length - 1]?.submittedAt === submission.submittedAt
|
|
46
|
+
};
|
|
47
|
+
const setSubmission = updater => {
|
|
48
|
+
store$1.setState(s => {
|
|
49
|
+
const a = s.submissions.find(d => d.submittedAt === submission.submittedAt);
|
|
50
|
+
invariant__default["default"](a, 'Could not find submission in store');
|
|
51
|
+
updater(a);
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
store$1.setState(s => {
|
|
55
|
+
s.submissions.push(submission);
|
|
56
|
+
s.submissions.reverse();
|
|
57
|
+
s.submissions = s.submissions.slice(0, options.maxSubmissions ?? 10);
|
|
58
|
+
s.submissions.reverse();
|
|
59
|
+
});
|
|
60
|
+
const after = async () => {
|
|
61
|
+
options.onEachSettled?.(submission);
|
|
62
|
+
if (submission.getIsLatest()) await options.onLatestSettled?.(submission);
|
|
63
|
+
};
|
|
64
|
+
try {
|
|
65
|
+
const res = await options.action?.(submission.payload);
|
|
66
|
+
setSubmission(s => {
|
|
67
|
+
s.response = res;
|
|
68
|
+
});
|
|
69
|
+
await options.onEachSuccess?.(submission);
|
|
70
|
+
if (submission.getIsLatest()) await options.onLatestSuccess?.(submission);
|
|
71
|
+
await after();
|
|
72
|
+
setSubmission(s => {
|
|
73
|
+
s.status = 'success';
|
|
74
|
+
});
|
|
75
|
+
return res;
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.error(err);
|
|
78
|
+
setSubmission(s => {
|
|
79
|
+
s.error = err;
|
|
80
|
+
});
|
|
81
|
+
await options.onEachError?.(submission);
|
|
82
|
+
if (submission.getIsLatest()) await options.onLatestError?.(submission);
|
|
83
|
+
await after();
|
|
84
|
+
setSubmission(s => {
|
|
85
|
+
s.status = 'error';
|
|
86
|
+
});
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
exports.createAction = createAction;
|
|
94
|
+
//# sourceMappingURL=actions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actions.js","sources":["../../src/actions.ts"],"sourcesContent":["// createRouterAction is a constrained identify function that takes options: key, action, onSuccess, onError, onSettled, etc\n\nimport invariant from 'tiny-invariant'\nimport { batch, createStore, Store } from './store'\n\nexport interface ActionOptions<\n TKey extends string = string,\n TPayload = unknown,\n TResponse = unknown,\n TError = Error,\n> {\n key?: TKey\n action: (payload: TPayload) => TResponse | Promise<TResponse>\n onLatestSuccess?: ActionCallback<TPayload, TResponse, TError>\n onEachSuccess?: ActionCallback<TPayload, TResponse, TError>\n onLatestError?: ActionCallback<TPayload, TResponse, TError>\n onEachError?: ActionCallback<TPayload, TResponse, TError>\n onLatestSettled?: ActionCallback<TPayload, TResponse, TError>\n onEachSettled?: ActionCallback<TPayload, TResponse, TError>\n maxSubmissions?: number\n debug?: boolean\n}\n\ntype ActionCallback<TPayload, TResponse, TError> = (\n submission: ActionSubmission<TPayload, TResponse, TError>,\n) => void | Promise<void>\n\nexport interface Action<\n TKey extends string = string,\n TPayload = unknown,\n TResponse = unknown,\n TError = Error,\n> {\n options: ActionOptions<TKey, TPayload, TResponse, TError>\n submit: (payload?: TPayload) => Promise<TResponse>\n reset: () => void\n store: Store<ActionStore<TPayload, TResponse, TError>>\n}\n\nexport interface ActionStore<\n TPayload = unknown,\n TResponse = unknown,\n TError = Error,\n> {\n submissions: ActionSubmission<TPayload, TResponse, TError>[]\n}\n\nexport type ActionFn<TActionPayload = unknown, TActionResponse = unknown> = (\n submission: TActionPayload,\n) => TActionResponse | Promise<TActionResponse>\n\nexport interface ActionSubmission<\n TPayload = unknown,\n TResponse = unknown,\n TError = Error,\n> {\n submittedAt: number\n status: 'idle' | 'pending' | 'success' | 'error'\n payload: TPayload\n response?: TResponse\n error?: TError\n isInvalid?: boolean\n invalidate: () => void\n getIsLatest: () => boolean\n}\n\nexport function createAction<TKey extends string, TPayload, TResponse, TError>(\n options: ActionOptions<TKey, TPayload, TResponse, TError>,\n): Action<TKey, TPayload, TResponse, TError> {\n const store = createStore<ActionStore<TPayload, TResponse, TError>>(\n {\n submissions: [],\n },\n options.debug,\n )\n\n return {\n options,\n store,\n reset: () => {\n store.setState((s) => {\n s.submissions = []\n })\n },\n submit: async (payload) => {\n const submission: ActionSubmission<TPayload, TResponse, TError> = {\n submittedAt: Date.now(),\n status: 'pending',\n payload: payload as TPayload,\n invalidate: () => {\n setSubmission((s) => {\n s.isInvalid = true\n })\n },\n getIsLatest: () =>\n store.state.submissions[store.state.submissions.length - 1]\n ?.submittedAt === submission.submittedAt,\n }\n\n const setSubmission = (\n updater: (\n submission: ActionSubmission<TPayload, TResponse, TError>,\n ) => void,\n ) => {\n store.setState((s) => {\n const a = s.submissions.find(\n (d) => d.submittedAt === submission.submittedAt,\n )\n\n invariant(a, 'Could not find submission in store')\n\n updater(a)\n })\n }\n\n store.setState((s) => {\n s.submissions.push(submission)\n s.submissions.reverse()\n s.submissions = s.submissions.slice(0, options.maxSubmissions ?? 10)\n s.submissions.reverse()\n })\n\n const after = async () => {\n options.onEachSettled?.(submission)\n if (submission.getIsLatest())\n await options.onLatestSettled?.(submission)\n }\n\n try {\n const res = await options.action?.(submission.payload)\n setSubmission((s) => {\n s.response = res\n })\n await options.onEachSuccess?.(submission)\n if (submission.getIsLatest())\n await options.onLatestSuccess?.(submission)\n await after()\n setSubmission((s) => {\n s.status = 'success'\n })\n return res\n } catch (err: any) {\n console.error(err)\n setSubmission((s) => {\n s.error = err\n })\n await options.onEachError?.(submission)\n if (submission.getIsLatest()) await options.onLatestError?.(submission)\n await after()\n setSubmission((s) => {\n s.status = 'error'\n })\n throw err\n }\n },\n }\n}\n"],"names":["createAction","options","store","createStore","submissions","debug","reset","setState","s","submit","payload","submission","submittedAt","Date","now","status","invalidate","setSubmission","isInvalid","getIsLatest","state","length","updater","a","find","d","invariant","push","reverse","slice","maxSubmissions","after","onEachSettled","onLatestSettled","res","action","response","onEachSuccess","onLatestSuccess","err","console","error","onEachError","onLatestError"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAkEO,SAASA,YAAY,CAC1BC,OAAyD,EACd;EAC3C,MAAMC,OAAK,GAAGC,iBAAW,CACvB;AACEC,IAAAA,WAAW,EAAE,EAAA;AACf,GAAC,EACDH,OAAO,CAACI,KAAK,CACd,CAAA;EAED,OAAO;IACLJ,OAAO;WACPC,OAAK;AACLI,IAAAA,KAAK,EAAE,MAAM;AACXJ,MAA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,GAAGhC,UAAU,CAAC,CAAA;AACvE,QAAA,MAAMoB,KAAK,EAAE,CAAA;QACbd,aAAa,CAAET,CAAC,IAAK;UACnBA,CAAC,CAACO,MAAM,GAAG,OAAO,CAAA;AACpB,SAAC,CAAC,CAAA;AACF,QAAA,MAAMwB,GAAG,CAAA;AACX,OAAA;AACF,KAAA;GACD,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* router-core
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
// While the public API was clearly inspired by the "history" npm package,
|
|
16
|
+
// This implementation attempts to be more lightweight by
|
|
17
|
+
// making assumptions about the way TanStack Router works
|
|
18
|
+
|
|
19
|
+
const popStateEvent = 'popstate';
|
|
20
|
+
function createHistory(opts) {
|
|
21
|
+
let currentLocation = opts.getLocation();
|
|
22
|
+
let unsub = () => {};
|
|
23
|
+
let listeners = new Set();
|
|
24
|
+
const onUpdate = () => {
|
|
25
|
+
currentLocation = opts.getLocation();
|
|
26
|
+
listeners.forEach(listener => listener());
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
get location() {
|
|
30
|
+
return currentLocation;
|
|
31
|
+
},
|
|
32
|
+
listen: cb => {
|
|
33
|
+
if (listeners.size === 0) {
|
|
34
|
+
unsub = opts.listener(onUpdate);
|
|
35
|
+
}
|
|
36
|
+
listeners.add(cb);
|
|
37
|
+
return () => {
|
|
38
|
+
listeners.delete(cb);
|
|
39
|
+
if (listeners.size === 0) {
|
|
40
|
+
unsub();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
push: (path, state) => {
|
|
45
|
+
opts.pushState(path, state);
|
|
46
|
+
onUpdate();
|
|
47
|
+
},
|
|
48
|
+
replace: (path, state) => {
|
|
49
|
+
opts.replaceState(path, state);
|
|
50
|
+
onUpdate();
|
|
51
|
+
},
|
|
52
|
+
go: index => {
|
|
53
|
+
opts.go(index);
|
|
54
|
+
onUpdate();
|
|
55
|
+
},
|
|
56
|
+
back: () => {
|
|
57
|
+
opts.back();
|
|
58
|
+
onUpdate();
|
|
59
|
+
},
|
|
60
|
+
forward: () => {
|
|
61
|
+
opts.forward();
|
|
62
|
+
onUpdate();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function createBrowserHistory(opts) {
|
|
67
|
+
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.hash}${window.location.search}`);
|
|
68
|
+
const createHref = opts?.createHref ?? (path => path);
|
|
69
|
+
const getLocation = () => parseLocation(getHref(), history.state);
|
|
70
|
+
return createHistory({
|
|
71
|
+
getLocation,
|
|
72
|
+
listener: onUpdate => {
|
|
73
|
+
window.addEventListener(popStateEvent, onUpdate);
|
|
74
|
+
return () => {
|
|
75
|
+
window.removeEventListener(popStateEvent, onUpdate);
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
pushState: (path, state) => {
|
|
79
|
+
window.history.pushState({
|
|
80
|
+
...state,
|
|
81
|
+
key: createRandomKey()
|
|
82
|
+
}, '', createHref(path));
|
|
83
|
+
},
|
|
84
|
+
replaceState: (path, state) => {
|
|
85
|
+
window.history.replaceState({
|
|
86
|
+
...state,
|
|
87
|
+
key: createRandomKey()
|
|
88
|
+
}, '', createHref(path));
|
|
89
|
+
},
|
|
90
|
+
back: () => window.history.back(),
|
|
91
|
+
forward: () => window.history.forward(),
|
|
92
|
+
go: n => window.history.go(n)
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function createHashHistory() {
|
|
96
|
+
return createBrowserHistory({
|
|
97
|
+
getHref: () => window.location.hash.substring(1),
|
|
98
|
+
createHref: path => `#${path}`
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function createMemoryHistory(opts = {
|
|
102
|
+
initialEntries: ['/']
|
|
103
|
+
}) {
|
|
104
|
+
const entries = opts.initialEntries;
|
|
105
|
+
let index = opts.initialIndex ?? entries.length - 1;
|
|
106
|
+
let currentState = {};
|
|
107
|
+
const getLocation = () => parseLocation(entries[index], currentState);
|
|
108
|
+
return createHistory({
|
|
109
|
+
getLocation,
|
|
110
|
+
listener: onUpdate => {
|
|
111
|
+
window.addEventListener(popStateEvent, onUpdate);
|
|
112
|
+
// We might need to handle the hashchange event in the future
|
|
113
|
+
// window.addEventListener(hashChangeEvent, onUpdate)
|
|
114
|
+
return () => {
|
|
115
|
+
window.removeEventListener(popStateEvent, onUpdate);
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
pushState: (path, state) => {
|
|
119
|
+
currentState = {
|
|
120
|
+
...state,
|
|
121
|
+
key: createRandomKey()
|
|
122
|
+
};
|
|
123
|
+
entries.push(path);
|
|
124
|
+
index++;
|
|
125
|
+
},
|
|
126
|
+
replaceState: (path, state) => {
|
|
127
|
+
currentState = {
|
|
128
|
+
...state,
|
|
129
|
+
key: createRandomKey()
|
|
130
|
+
};
|
|
131
|
+
entries[index] = path;
|
|
132
|
+
},
|
|
133
|
+
back: () => {
|
|
134
|
+
index--;
|
|
135
|
+
},
|
|
136
|
+
forward: () => {
|
|
137
|
+
index = Math.min(index + 1, entries.length - 1);
|
|
138
|
+
},
|
|
139
|
+
go: n => window.history.go(n)
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
function parseLocation(href, state) {
|
|
143
|
+
let hashIndex = href.indexOf('#');
|
|
144
|
+
let searchIndex = href.indexOf('?');
|
|
145
|
+
const pathEnd = Math.min(hashIndex, searchIndex);
|
|
146
|
+
return {
|
|
147
|
+
href,
|
|
148
|
+
pathname: pathEnd > -1 ? href.substring(0, pathEnd) : href,
|
|
149
|
+
hash: hashIndex > -1 ? href.substring(hashIndex, searchIndex) : '',
|
|
150
|
+
search: searchIndex > -1 ? href.substring(searchIndex) : '',
|
|
151
|
+
state
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Thanks co-pilot!
|
|
156
|
+
function createRandomKey() {
|
|
157
|
+
return (Math.random() + 1).toString(36).substring(7);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
exports.createBrowserHistory = createBrowserHistory;
|
|
161
|
+
exports.createHashHistory = createHashHistory;
|
|
162
|
+
exports.createMemoryHistory = createMemoryHistory;
|
|
163
|
+
//# sourceMappingURL=history.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history.js","sources":["../../src/history.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: RouterLocation\n listen: (cb: () => void) => () => void\n push: (path: string, state: any) => void\n replace: (path: string, state: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface RouterLocation extends ParsedPath {\n state: any\n}\n\nconst popStateEvent = 'popstate'\n\nfunction createHistory(opts: {\n getLocation: () => RouterLocation\n listener: (onUpdate: () => void) => () => void\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n}): RouterHistory {\n let currentLocation = opts.getLocation()\n let unsub = () => {}\n let listeners = new Set<() => void>()\n\n const onUpdate = () => {\n currentLocation = opts.getLocation()\n\n listeners.forEach((listener) => listener())\n }\n\n return {\n get location() {\n return currentLocation\n },\n listen: (cb: () => void) => {\n if (listeners.size === 0) {\n unsub = opts.listener(onUpdate)\n }\n listeners.add(cb)\n\n return () => {\n listeners.delete(cb)\n if (listeners.size === 0) {\n unsub()\n }\n }\n },\n push: (path: string, state: any) => {\n opts.pushState(path, state)\n onUpdate()\n },\n replace: (path: string, state: any) => {\n opts.replaceState(path, state)\n onUpdate()\n },\n go: (index) => {\n opts.go(index)\n onUpdate()\n },\n back: () => {\n opts.back()\n onUpdate()\n },\n forward: () => {\n opts.forward()\n onUpdate()\n },\n }\n}\n\nexport function createBrowserHistory(opts?: {\n getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.hash}${window.location.search}`)\n const createHref = opts?.createHref ?? ((path) => path)\n const getLocation = () => parseLocation(getHref(), history.state)\n\n return createHistory({\n getLocation,\n listener: (onUpdate) => {\n window.addEventListener(popStateEvent, onUpdate)\n return () => {\n window.removeEventListener(popStateEvent, onUpdate)\n }\n },\n pushState: (path, state) => {\n window.history.pushState(\n { ...state, key: createRandomKey() },\n '',\n createHref(path),\n )\n },\n replaceState: (path, state) => {\n window.history.replaceState(\n { ...state, key: createRandomKey() },\n '',\n createHref(path),\n )\n },\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n })\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {}\n\n const getLocation = () => parseLocation(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n listener: (onUpdate) => {\n window.addEventListener(popStateEvent, onUpdate)\n // We might need to handle the hashchange event in the future\n // window.addEventListener(hashChangeEvent, onUpdate)\n return () => {\n window.removeEventListener(popStateEvent, onUpdate)\n }\n },\n pushState: (path, state) => {\n currentState = {\n ...state,\n key: createRandomKey(),\n }\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = {\n ...state,\n key: createRandomKey(),\n }\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => window.history.go(n),\n })\n}\n\nfunction parseLocation(href: string, state: any): RouterLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n const pathEnd = Math.min(hashIndex, searchIndex)\n\n return {\n href,\n pathname: pathEnd > -1 ? href.substring(0, pathEnd) : href,\n hash: hashIndex > -1 ? href.substring(hashIndex, searchIndex) : '',\n search: searchIndex > -1 ? href.substring(searchIndex) : '',\n state,\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["popStateEvent","createHistory","opts","currentLocation","getLocation","unsub","listeners","Set","onUpdate","forEach","listener","location","listen","cb","size","add","delete","push","path","state","pushState","replace","replaceState","go","index","back","forward","createBrowserHistory","getHref","window","pathname","hash","search","createHref","parseLocation","history","addEventListener","removeEventListener","key","createRandomKey","n","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","length","currentState","Math","min","href","hashIndex","indexOf","searchIndex","pathEnd","random","toString"],"mappings":";;;;;;;;;;;;;;AAAA;AACA;AACA;;AAuBA,MAAMA,aAAa,GAAG,UAAU,CAAA;AAEhC,SAASC,aAAa,CAACC,IAQtB,EAAiB;AAChB,EAAA,IAAIC,eAAe,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;AACxC,EAAA,IAAIC,KAAK,GAAG,MAAM,EAAE,CAAA;AACpB,EAAA,IAAIC,SAAS,GAAG,IAAIC,GAAG,EAAc,CAAA;EAErC,MAAMC,QAAQ,GAAG,MAAM;AACrBL,IAAAA,eAAe,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;AAEpCE,IAAAA,SAAS,CAACG,OAAO,CAAEC,QAAQ,IAAKA,QAAQ,EAAE,CAAC,CAAA;GAC5C,CAAA;EAED,OAAO;AACL,IAAA,IAAIC,QAAQ,GAAG;AACb,MAA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,SAAS,EAAEE,WAAW,CAAC,GAAG,EAAE;AAClEtB,IAAAA,MAAM,EAAEsB,WAAW,GAAG,CAAC,CAAC,GAAGH,IAAI,CAACT,SAAS,CAACY,WAAW,CAAC,GAAG,EAAE;AAC3DnC,IAAAA,KAAAA;GACD,CAAA;AACH,CAAA;;AAEA;AACA,SAASoB,eAAe,GAAG;AACzB,EAAA,OAAO,CAACU,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACf,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;;;"}
|
package/build/cjs/index.js
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
|
|
13
13
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
14
|
|
|
15
|
-
var history = require('history');
|
|
16
15
|
var invariant = require('tiny-invariant');
|
|
16
|
+
var history = require('./history.js');
|
|
17
17
|
var path = require('./path.js');
|
|
18
18
|
var qss = require('./qss.js');
|
|
19
19
|
var route = require('./route.js');
|
|
@@ -22,7 +22,9 @@ var routeMatch = require('./routeMatch.js');
|
|
|
22
22
|
var router = require('./router.js');
|
|
23
23
|
var searchParams = require('./searchParams.js');
|
|
24
24
|
var utils = require('./utils.js');
|
|
25
|
-
var
|
|
25
|
+
var interop = require('./interop.js');
|
|
26
|
+
var actions = require('./actions.js');
|
|
27
|
+
var store = require('./store.js');
|
|
26
28
|
|
|
27
29
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
28
30
|
|
|
@@ -30,22 +32,13 @@ var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
|
|
|
30
32
|
|
|
31
33
|
|
|
32
34
|
|
|
33
|
-
Object.defineProperty(exports, 'createBrowserHistory', {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
get: function () { return history.createBrowserHistory; }
|
|
36
|
-
});
|
|
37
|
-
Object.defineProperty(exports, 'createHashHistory', {
|
|
38
|
-
enumerable: true,
|
|
39
|
-
get: function () { return history.createHashHistory; }
|
|
40
|
-
});
|
|
41
|
-
Object.defineProperty(exports, 'createMemoryHistory', {
|
|
42
|
-
enumerable: true,
|
|
43
|
-
get: function () { return history.createMemoryHistory; }
|
|
44
|
-
});
|
|
45
35
|
Object.defineProperty(exports, 'invariant', {
|
|
46
|
-
|
|
47
|
-
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () { return invariant__default["default"]; }
|
|
48
38
|
});
|
|
39
|
+
exports.createBrowserHistory = history.createBrowserHistory;
|
|
40
|
+
exports.createHashHistory = history.createHashHistory;
|
|
41
|
+
exports.createMemoryHistory = history.createMemoryHistory;
|
|
49
42
|
exports.cleanPath = path.cleanPath;
|
|
50
43
|
exports.interpolatePath = path.interpolatePath;
|
|
51
44
|
exports.joinPaths = path.joinPaths;
|
|
@@ -58,11 +51,12 @@ exports.trimPathLeft = path.trimPathLeft;
|
|
|
58
51
|
exports.trimPathRight = path.trimPathRight;
|
|
59
52
|
exports.decode = qss.decode;
|
|
60
53
|
exports.encode = qss.encode;
|
|
61
|
-
exports.
|
|
54
|
+
exports.Route = route.Route;
|
|
62
55
|
exports.createRouteConfig = routeConfig.createRouteConfig;
|
|
63
56
|
exports.rootRouteId = routeConfig.rootRouteId;
|
|
64
|
-
exports.
|
|
65
|
-
exports.
|
|
57
|
+
exports.RouteMatch = routeMatch.RouteMatch;
|
|
58
|
+
exports.Router = router.Router;
|
|
59
|
+
exports.defaultFetchServerDataFn = router.defaultFetchServerDataFn;
|
|
66
60
|
exports.defaultParseSearch = searchParams.defaultParseSearch;
|
|
67
61
|
exports.defaultStringifySearch = searchParams.defaultStringifySearch;
|
|
68
62
|
exports.parseSearchWith = searchParams.parseSearchWith;
|
|
@@ -71,5 +65,9 @@ exports.functionalUpdate = utils.functionalUpdate;
|
|
|
71
65
|
exports.last = utils.last;
|
|
72
66
|
exports.pick = utils.pick;
|
|
73
67
|
exports.warning = utils.warning;
|
|
74
|
-
exports.
|
|
68
|
+
exports.replaceEqualDeep = interop.replaceEqualDeep;
|
|
69
|
+
exports.trackDeep = interop.trackDeep;
|
|
70
|
+
exports.createAction = actions.createAction;
|
|
71
|
+
exports.batch = store.batch;
|
|
72
|
+
exports.createStore = store.createStore;
|
|
75
73
|
//# sourceMappingURL=index.js.map
|
package/build/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* router-core
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
// /**
|
|
16
|
+
// * This function converts a store to an immutable value, which is
|
|
17
|
+
// * more complex than you think. On first read, (when prev is undefined)
|
|
18
|
+
// * every value must be recursively touched so tracking is "deep".
|
|
19
|
+
// * Every object/array structure must also be cloned to
|
|
20
|
+
// * have a new reference, otherwise it will get mutated by subsequent
|
|
21
|
+
// * store updates.
|
|
22
|
+
// *
|
|
23
|
+
// * In the case that prev is supplied, we have to do deep comparisons
|
|
24
|
+
// * between prev and next objects/array references and if they are deeply
|
|
25
|
+
// * equal, we can return the prev version for referential equality.
|
|
26
|
+
// */
|
|
27
|
+
// export function storeToImmutable<T>(prev: any, next: T): T {
|
|
28
|
+
// const cache = new Map()
|
|
29
|
+
|
|
30
|
+
// // Visit all nodes
|
|
31
|
+
// // clone all next structures
|
|
32
|
+
// // from bottom up, if prev === next, return prev
|
|
33
|
+
|
|
34
|
+
// function recurse(prev: any, next: any) {
|
|
35
|
+
// if (cache.has(next)) {
|
|
36
|
+
// return cache.get(next)
|
|
37
|
+
// }
|
|
38
|
+
|
|
39
|
+
// const prevIsArray = Array.isArray(prev)
|
|
40
|
+
// const nextIsArray = Array.isArray(next)
|
|
41
|
+
// const prevIsObj = isPlainObject(prev)
|
|
42
|
+
// const nextIsObj = isPlainObject(next)
|
|
43
|
+
// const nextIsComplex = nextIsArray || nextIsObj
|
|
44
|
+
|
|
45
|
+
// const isArray = prevIsArray && nextIsArray
|
|
46
|
+
// const isObj = prevIsObj && nextIsObj
|
|
47
|
+
|
|
48
|
+
// const isSameStructure = isArray || isObj
|
|
49
|
+
|
|
50
|
+
// if (nextIsComplex) {
|
|
51
|
+
// const prevSize = isArray
|
|
52
|
+
// ? prev.length
|
|
53
|
+
// : isObj
|
|
54
|
+
// ? Object.keys(prev).length
|
|
55
|
+
// : -1
|
|
56
|
+
// const nextKeys = isArray ? next : Object.keys(next)
|
|
57
|
+
// const nextSize = nextKeys.length
|
|
58
|
+
|
|
59
|
+
// let changed = false
|
|
60
|
+
// const copy: any = nextIsArray ? [] : {}
|
|
61
|
+
|
|
62
|
+
// for (let i = 0; i < nextSize; i++) {
|
|
63
|
+
// const key = isArray ? i : nextKeys[i]
|
|
64
|
+
// const prevValue = isSameStructure ? prev[key] : undefined
|
|
65
|
+
// const nextValue = next[key]
|
|
66
|
+
|
|
67
|
+
// // Recurse the new value
|
|
68
|
+
// try {
|
|
69
|
+
// console.count(key)
|
|
70
|
+
// copy[key] = recurse(prevValue, nextValue)
|
|
71
|
+
// } catch {}
|
|
72
|
+
|
|
73
|
+
// // If the new value has changed reference,
|
|
74
|
+
// // mark the obj/array as changed
|
|
75
|
+
// if (!changed && copy[key] !== prevValue) {
|
|
76
|
+
// changed = true
|
|
77
|
+
// }
|
|
78
|
+
// }
|
|
79
|
+
|
|
80
|
+
// // No items have changed!
|
|
81
|
+
// // If something has changed, return a clone of the next obj/array
|
|
82
|
+
// if (changed || prevSize !== nextSize) {
|
|
83
|
+
// cache.set(next, copy)
|
|
84
|
+
// return copy
|
|
85
|
+
// }
|
|
86
|
+
|
|
87
|
+
// // If they are exactly the same, return the prev obj/array
|
|
88
|
+
// cache.set(next, prev)
|
|
89
|
+
// return prev
|
|
90
|
+
// }
|
|
91
|
+
|
|
92
|
+
// cache.set(next, next)
|
|
93
|
+
// return next
|
|
94
|
+
// }
|
|
95
|
+
|
|
96
|
+
// return recurse(prev, next)
|
|
97
|
+
// }
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* This function returns `a` if `b` is deeply equal.
|
|
101
|
+
* If not, it will replace any deeply equal children of `b` with those of `a`.
|
|
102
|
+
* This can be used for structural sharing between immutable JSON values for example.
|
|
103
|
+
* Do not use this with signals
|
|
104
|
+
*/
|
|
105
|
+
function replaceEqualDeep(prev, _next) {
|
|
106
|
+
if (prev === _next) {
|
|
107
|
+
return prev;
|
|
108
|
+
}
|
|
109
|
+
const next = _next;
|
|
110
|
+
const array = Array.isArray(prev) && Array.isArray(next);
|
|
111
|
+
if (array || isPlainObject(prev) && isPlainObject(next)) {
|
|
112
|
+
const prevSize = array ? prev.length : Object.keys(prev).length;
|
|
113
|
+
const nextItems = array ? next : Object.keys(next);
|
|
114
|
+
const nextSize = nextItems.length;
|
|
115
|
+
const copy = array ? [] : {};
|
|
116
|
+
let equalItems = 0;
|
|
117
|
+
for (let i = 0; i < nextSize; i++) {
|
|
118
|
+
const key = array ? i : nextItems[i];
|
|
119
|
+
copy[key] = replaceEqualDeep(prev[key], next[key]);
|
|
120
|
+
if (copy[key] === prev[key]) {
|
|
121
|
+
equalItems++;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return prevSize === nextSize && equalItems === prevSize ? prev : copy;
|
|
125
|
+
}
|
|
126
|
+
return next;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Copied from: https://github.com/jonschlinkert/is-plain-object
|
|
130
|
+
function isPlainObject(o) {
|
|
131
|
+
if (!hasObjectPrototype(o)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// If has modified constructor
|
|
136
|
+
const ctor = o.constructor;
|
|
137
|
+
if (typeof ctor === 'undefined') {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// If has modified prototype
|
|
142
|
+
const prot = ctor.prototype;
|
|
143
|
+
if (!hasObjectPrototype(prot)) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// If constructor does not have an Object-specific method
|
|
148
|
+
if (!prot.hasOwnProperty('isPrototypeOf')) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Most likely a plain Object
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
function hasObjectPrototype(o) {
|
|
156
|
+
return Object.prototype.toString.call(o) === '[object Object]';
|
|
157
|
+
}
|
|
158
|
+
function trackDeep(obj) {
|
|
159
|
+
const seen = new Set();
|
|
160
|
+
JSON.stringify(obj, (_, value) => {
|
|
161
|
+
if (typeof value === 'function') {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
if (typeof value === 'object' && value !== null) {
|
|
165
|
+
if (seen.has(value)) return;
|
|
166
|
+
seen.add(value);
|
|
167
|
+
}
|
|
168
|
+
return value;
|
|
169
|
+
});
|
|
170
|
+
return obj;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
exports.replaceEqualDeep = replaceEqualDeep;
|
|
174
|
+
exports.trackDeep = trackDeep;
|
|
175
|
+
//# sourceMappingURL=interop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interop.js","sources":["../../src/interop.ts"],"sourcesContent":["// /**\n// * This function converts a store to an immutable value, which is\n// * more complex than you think. On first read, (when prev is undefined)\n// * every value must be recursively touched so tracking is \"deep\".\n// * Every object/array structure must also be cloned to\n// * have a new reference, otherwise it will get mutated by subsequent\n// * store updates.\n// *\n// * In the case that prev is supplied, we have to do deep comparisons\n// * between prev and next objects/array references and if they are deeply\n// * equal, we can return the prev version for referential equality.\n// */\n// export function storeToImmutable<T>(prev: any, next: T): T {\n// const cache = new Map()\n\n// // Visit all nodes\n// // clone all next structures\n// // from bottom up, if prev === next, return prev\n\n// function recurse(prev: any, next: any) {\n// if (cache.has(next)) {\n// return cache.get(next)\n// }\n\n// const prevIsArray = Array.isArray(prev)\n// const nextIsArray = Array.isArray(next)\n// const prevIsObj = isPlainObject(prev)\n// const nextIsObj = isPlainObject(next)\n// const nextIsComplex = nextIsArray || nextIsObj\n\n// const isArray = prevIsArray && nextIsArray\n// const isObj = prevIsObj && nextIsObj\n\n// const isSameStructure = isArray || isObj\n\n// if (nextIsComplex) {\n// const prevSize = isArray\n// ? prev.length\n// : isObj\n// ? Object.keys(prev).length\n// : -1\n// const nextKeys = isArray ? next : Object.keys(next)\n// const nextSize = nextKeys.length\n\n// let changed = false\n// const copy: any = nextIsArray ? [] : {}\n\n// for (let i = 0; i < nextSize; i++) {\n// const key = isArray ? i : nextKeys[i]\n// const prevValue = isSameStructure ? prev[key] : undefined\n// const nextValue = next[key]\n\n// // Recurse the new value\n// try {\n// console.count(key)\n// copy[key] = recurse(prevValue, nextValue)\n// } catch {}\n\n// // If the new value has changed reference,\n// // mark the obj/array as changed\n// if (!changed && copy[key] !== prevValue) {\n// changed = true\n// }\n// }\n\n// // No items have changed!\n// // If something has changed, return a clone of the next obj/array\n// if (changed || prevSize !== nextSize) {\n// cache.set(next, copy)\n// return copy\n// }\n\n// // If they are exactly the same, return the prev obj/array\n// cache.set(next, prev)\n// return prev\n// }\n\n// cache.set(next, next)\n// return next\n// }\n\n// return recurse(prev, next)\n// }\n\n/**\n * This function returns `a` if `b` is deeply equal.\n * If not, it will replace any deeply equal children of `b` with those of `a`.\n * This can be used for structural sharing between immutable JSON values for example.\n * Do not use this with signals\n */\nexport function replaceEqualDeep<T>(prev: any, _next: T): T {\n if (prev === _next) {\n return prev\n }\n\n const next = _next as any\n\n const array = Array.isArray(prev) && Array.isArray(next)\n\n if (array || (isPlainObject(prev) && isPlainObject(next))) {\n const prevSize = array ? prev.length : Object.keys(prev).length\n const nextItems = array ? next : Object.keys(next)\n const nextSize = nextItems.length\n const copy: any = array ? [] : {}\n\n let equalItems = 0\n\n for (let i = 0; i < nextSize; i++) {\n const key = array ? i : nextItems[i]\n copy[key] = replaceEqualDeep(prev[key], next[key])\n if (copy[key] === prev[key]) {\n equalItems++\n }\n }\n\n return prevSize === nextSize && equalItems === prevSize ? prev : copy\n }\n\n return next\n}\n\n// Copied from: https://github.com/jonschlinkert/is-plain-object\nfunction isPlainObject(o: any) {\n if (!hasObjectPrototype(o)) {\n return false\n }\n\n // If has modified constructor\n const ctor = o.constructor\n if (typeof ctor === 'undefined') {\n return true\n }\n\n // If has modified prototype\n const prot = ctor.prototype\n if (!hasObjectPrototype(prot)) {\n return false\n }\n\n // If constructor does not have an Object-specific method\n if (!prot.hasOwnProperty('isPrototypeOf')) {\n return false\n }\n\n // Most likely a plain Object\n return true\n}\n\nfunction hasObjectPrototype(o: any) {\n return Object.prototype.toString.call(o) === '[object Object]'\n}\n\nexport function trackDeep<T>(obj: T): T {\n const seen = new Set()\n\n JSON.stringify(obj, (_, value) => {\n if (typeof value === 'function') {\n return undefined\n }\n if (typeof value === 'object' && value !== null) {\n if (seen.has(value)) return\n seen.add(value)\n }\n\n return value\n })\n\n return obj\n}\n"],"names":["replaceEqualDeep","prev","_next","next","array","Array","isArray","isPlainObject","prevSize","length","Object","keys","nextItems","nextSize","copy","equalItems","i","key","o","hasObjectPrototype","ctor","constructor","prot","prototype","hasOwnProperty","toString","call","trackDeep","obj","seen","Set","JSON","stringify","_","value","undefined","has","add"],"mappings":";;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;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iB,CAAA;AAChE,CAAA;AAEO,SAASS,SAAS,CAAIC,GAAM,EAAK;AACtC,EAAA,MAAMC,IAAI,GAAG,IAAIC,GAAG,EAAE,CAAA;EAEtBC,IAAI,CAACC,SAAS,CAACJ,GAAG,EAAE,CAACK,CAAC,EAAEC,KAAK,KAAK;AAChC,IAAA,IAAI,OAAOA,KAAK,KAAK,UAAU,EAAE;AAC/B,MAAA,OAAOC,SAAS,CAAA;AAClB,KAAA;IACA,IAAI,OAAOD,KAAK,KAAK,QAAQ,IAAIA,KAAK,KAAK,IAAI,EAAE;AAC/C,MAAA,IAAIL,IAAI,CAACO,GAAG,CAACF,KAAK,CAAC,EAAE,OAAA;AACrBL,MAAAA,IAAI,CAACQ,GAAG,CAACH,KAAK,CAAC,CAAA;AACjB,KAAA;AAEA,IAAA,OAAOA,KAAK,CAAA;AACd,GAAC,CAAC,CAAA;AAEF,EAAA,OAAON,GAAG,CAAA;AACZ;;;;;"}
|
package/build/cjs/path.js
CHANGED
|
@@ -45,9 +45,8 @@ function resolvePath(basepath, base, to) {
|
|
|
45
45
|
baseSegments.push(toSegment);
|
|
46
46
|
} else ;
|
|
47
47
|
} else if (toSegment.value === '..') {
|
|
48
|
-
var _last;
|
|
49
48
|
// Extra trailing slash? pop it off
|
|
50
|
-
if (baseSegments.length > 1 &&
|
|
49
|
+
if (baseSegments.length > 1 && utils.last(baseSegments)?.value === '/') {
|
|
51
50
|
baseSegments.pop();
|
|
52
51
|
}
|
|
53
52
|
baseSegments.pop();
|
|
@@ -144,14 +143,14 @@ function matchByPath(basepath, from, matchLocation) {
|
|
|
144
143
|
const isLastBaseSegment = i === baseSegments.length - 1;
|
|
145
144
|
if (routeSegment) {
|
|
146
145
|
if (routeSegment.type === 'wildcard') {
|
|
147
|
-
if (baseSegment
|
|
146
|
+
if (baseSegment?.value) {
|
|
148
147
|
params['*'] = joinPaths(baseSegments.slice(i).map(d => d.value));
|
|
149
148
|
return true;
|
|
150
149
|
}
|
|
151
150
|
return false;
|
|
152
151
|
}
|
|
153
152
|
if (routeSegment.type === 'pathname') {
|
|
154
|
-
if (routeSegment.value === '/' && !
|
|
153
|
+
if (routeSegment.value === '/' && !baseSegment?.value) {
|
|
155
154
|
return true;
|
|
156
155
|
}
|
|
157
156
|
if (baseSegment) {
|
|
@@ -168,7 +167,7 @@ function matchByPath(basepath, from, matchLocation) {
|
|
|
168
167
|
return false;
|
|
169
168
|
}
|
|
170
169
|
if (routeSegment.type === 'param') {
|
|
171
|
-
if (
|
|
170
|
+
if (baseSegment?.value === '/') {
|
|
172
171
|
return false;
|
|
173
172
|
}
|
|
174
173
|
if (baseSegment.value.charAt(0) !== '$') {
|