@devp0nt/route0 1.0.0-next.6 → 1.0.0-next.61
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/dist/cjs/index.cjs +749 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +505 -92
- package/dist/esm/index.d.ts +505 -92
- package/dist/esm/index.js +668 -78
- package/dist/esm/index.js.map +1 -1
- package/package.json +43 -19
- package/src/index.test.ts +2204 -64
- package/src/index.ts +1666 -268
- package/dist/cjs/index.js +0 -158
- package/dist/cjs/index.js.map +0 -1
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var index_exports = {};
|
|
20
|
+
__export(index_exports, {
|
|
21
|
+
Route0: () => Route0,
|
|
22
|
+
Routes: () => Routes
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(index_exports);
|
|
25
|
+
class Route0 {
|
|
26
|
+
definition;
|
|
27
|
+
pathDefinition;
|
|
28
|
+
paramsDefinition;
|
|
29
|
+
searchDefinition;
|
|
30
|
+
hasLooseSearch;
|
|
31
|
+
_baseurl;
|
|
32
|
+
/** Base URL used when generating absolute URLs (`abs: true`). */
|
|
33
|
+
get baseurl() {
|
|
34
|
+
if (!this._baseurl) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
"baseurl for route " + this.definition + ' is not set, please provide it like Route0.create(route, {baseurl: "https://example.com"}) in config or set via overrides like routes._.override({baseurl: "https://example.com"})'
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return this._baseurl;
|
|
40
|
+
}
|
|
41
|
+
set baseurl(baseurl) {
|
|
42
|
+
this._baseurl = baseurl;
|
|
43
|
+
}
|
|
44
|
+
constructor(definition, config = {}) {
|
|
45
|
+
this.definition = definition;
|
|
46
|
+
this.pathDefinition = Route0._getPathDefinitionBydefinition(definition);
|
|
47
|
+
this.paramsDefinition = Route0._getParamsDefinitionBydefinition(definition);
|
|
48
|
+
this.searchDefinition = Route0._getSearchDefinitionBydefinition(definition);
|
|
49
|
+
this.hasLooseSearch = Route0._hasLooseSearch(definition);
|
|
50
|
+
const { baseurl } = config;
|
|
51
|
+
if (baseurl && typeof baseurl === "string" && baseurl.length) {
|
|
52
|
+
this._baseurl = baseurl;
|
|
53
|
+
} else {
|
|
54
|
+
const g = globalThis;
|
|
55
|
+
if (typeof g?.location?.origin === "string" && g.location.origin.length > 0) {
|
|
56
|
+
this._baseurl = g.location.origin;
|
|
57
|
+
} else {
|
|
58
|
+
this._baseurl = void 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a callable route instance.
|
|
64
|
+
*
|
|
65
|
+
* If an existing route/callable route is provided, it is cloned.
|
|
66
|
+
*/
|
|
67
|
+
static create(definition, config) {
|
|
68
|
+
if (typeof definition === "function") {
|
|
69
|
+
return definition.clone(config);
|
|
70
|
+
}
|
|
71
|
+
if (typeof definition === "object") {
|
|
72
|
+
return definition.clone(config);
|
|
73
|
+
}
|
|
74
|
+
const original = new Route0(definition, config);
|
|
75
|
+
const callable = original.get.bind(original);
|
|
76
|
+
Object.setPrototypeOf(callable, original);
|
|
77
|
+
Object.defineProperty(callable, Symbol.toStringTag, {
|
|
78
|
+
value: original.definition
|
|
79
|
+
});
|
|
80
|
+
return callable;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Normalizes a definition/route into a callable route.
|
|
84
|
+
*
|
|
85
|
+
* Unlike `create`, passing a callable route returns the same instance.
|
|
86
|
+
*/
|
|
87
|
+
static from(definition) {
|
|
88
|
+
if (typeof definition === "function") {
|
|
89
|
+
return definition;
|
|
90
|
+
}
|
|
91
|
+
const original = typeof definition === "object" ? definition : new Route0(definition);
|
|
92
|
+
const callable = original.get.bind(original);
|
|
93
|
+
Object.setPrototypeOf(callable, original);
|
|
94
|
+
Object.defineProperty(callable, Symbol.toStringTag, {
|
|
95
|
+
value: original.definition
|
|
96
|
+
});
|
|
97
|
+
return callable;
|
|
98
|
+
}
|
|
99
|
+
static _splitPathDefinitionAndSearchTailDefinition(definition) {
|
|
100
|
+
const i = definition.indexOf("&");
|
|
101
|
+
if (i === -1) return { pathDefinition: definition, searchTailDefinition: "" };
|
|
102
|
+
return {
|
|
103
|
+
pathDefinition: definition.slice(0, i),
|
|
104
|
+
searchTailDefinition: definition.slice(i)
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
static _getAbsPath(baseurl, pathWithSearch) {
|
|
108
|
+
return new URL(pathWithSearch, baseurl).toString().replace(/\/$/, "");
|
|
109
|
+
}
|
|
110
|
+
static _getPathDefinitionBydefinition(definition) {
|
|
111
|
+
const { pathDefinition } = Route0._splitPathDefinitionAndSearchTailDefinition(definition);
|
|
112
|
+
return pathDefinition;
|
|
113
|
+
}
|
|
114
|
+
static _getParamsDefinitionBydefinition(definition) {
|
|
115
|
+
const { pathDefinition } = Route0._splitPathDefinitionAndSearchTailDefinition(definition);
|
|
116
|
+
const matches = Array.from(pathDefinition.matchAll(/:([A-Za-z0-9_]+)/g));
|
|
117
|
+
const paramsDefinition = Object.fromEntries(matches.map((m) => [m[1], true]));
|
|
118
|
+
const keysCount = Object.keys(paramsDefinition).length;
|
|
119
|
+
if (keysCount === 0) {
|
|
120
|
+
return void 0;
|
|
121
|
+
}
|
|
122
|
+
return paramsDefinition;
|
|
123
|
+
}
|
|
124
|
+
static _getSearchDefinitionBydefinition(definition) {
|
|
125
|
+
const { searchTailDefinition } = Route0._splitPathDefinitionAndSearchTailDefinition(definition);
|
|
126
|
+
if (!searchTailDefinition) {
|
|
127
|
+
return void 0;
|
|
128
|
+
}
|
|
129
|
+
const keys = searchTailDefinition.split("&").filter(Boolean);
|
|
130
|
+
const searchDefinition = Object.fromEntries(keys.map((k) => [k, true]));
|
|
131
|
+
const keysCount = Object.keys(searchDefinition).length;
|
|
132
|
+
if (keysCount === 0) {
|
|
133
|
+
return void 0;
|
|
134
|
+
}
|
|
135
|
+
return searchDefinition;
|
|
136
|
+
}
|
|
137
|
+
static _hasLooseSearch(definition) {
|
|
138
|
+
return /&$/.test(definition);
|
|
139
|
+
}
|
|
140
|
+
/** Extends the current route definition by appending a suffix route. */
|
|
141
|
+
extend(suffixDefinition) {
|
|
142
|
+
const { pathDefinition: parentPathDefinition } = Route0._splitPathDefinitionAndSearchTailDefinition(this.definition);
|
|
143
|
+
const { pathDefinition: suffixPathDefinition, searchTailDefinition: suffixSearchTailDefinition } = Route0._splitPathDefinitionAndSearchTailDefinition(suffixDefinition);
|
|
144
|
+
const pathDefinition = `${parentPathDefinition}/${suffixPathDefinition}`.replace(/\/{2,}/g, "/");
|
|
145
|
+
const definition = `${pathDefinition}${suffixSearchTailDefinition}`;
|
|
146
|
+
return Route0.create(definition, { baseurl: this._baseurl });
|
|
147
|
+
}
|
|
148
|
+
// implementation
|
|
149
|
+
get(...args) {
|
|
150
|
+
const { searchInput, paramsInput, absInput, hashInput } = (() => {
|
|
151
|
+
if (args.length === 0) {
|
|
152
|
+
return {
|
|
153
|
+
searchInput: {},
|
|
154
|
+
paramsInput: {},
|
|
155
|
+
absInput: false,
|
|
156
|
+
hashInput: void 0
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
const input = args[0];
|
|
160
|
+
if (typeof input !== "object" || input === null) {
|
|
161
|
+
return {
|
|
162
|
+
searchInput: {},
|
|
163
|
+
paramsInput: {},
|
|
164
|
+
absInput: false,
|
|
165
|
+
hashInput: void 0
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const { search, abs, hash, ...params } = input;
|
|
169
|
+
return {
|
|
170
|
+
searchInput: search || {},
|
|
171
|
+
paramsInput: params,
|
|
172
|
+
absInput: abs ?? false,
|
|
173
|
+
hashInput: hash
|
|
174
|
+
};
|
|
175
|
+
})();
|
|
176
|
+
const neededParamsKeys = this.paramsDefinition ? Object.keys(this.paramsDefinition) : [];
|
|
177
|
+
const providedParamsKeys = Object.keys(paramsInput);
|
|
178
|
+
const notProvidedKeys = neededParamsKeys.filter((k) => !providedParamsKeys.includes(k));
|
|
179
|
+
if (notProvidedKeys.length) {
|
|
180
|
+
Object.assign(paramsInput, Object.fromEntries(notProvidedKeys.map((k) => [k, "undefined"])));
|
|
181
|
+
}
|
|
182
|
+
let url = this.pathDefinition;
|
|
183
|
+
url = url.replace(/:([A-Za-z0-9_]+)/g, (_m, k) => encodeURIComponent(String(paramsInput?.[k] ?? "")));
|
|
184
|
+
const searchInputStringified = Object.fromEntries(Object.entries(searchInput).map(([k, v]) => [k, String(v)]));
|
|
185
|
+
url = [url, new URLSearchParams(searchInputStringified).toString()].filter(Boolean).join("?");
|
|
186
|
+
url = url.replace(/\/{2,}/g, "/");
|
|
187
|
+
url = absInput ? Route0._getAbsPath(this.baseurl, url) : url;
|
|
188
|
+
if (hashInput !== void 0) {
|
|
189
|
+
url = `${url}#${hashInput}`;
|
|
190
|
+
}
|
|
191
|
+
return url;
|
|
192
|
+
}
|
|
193
|
+
// implementation
|
|
194
|
+
flat(...args) {
|
|
195
|
+
const { searchInput, paramsInput, absInput, hashInput } = (() => {
|
|
196
|
+
if (args.length === 0) {
|
|
197
|
+
return {
|
|
198
|
+
searchInput: {},
|
|
199
|
+
paramsInput: {},
|
|
200
|
+
absInput: false,
|
|
201
|
+
hashInput: void 0
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
const input = args[0];
|
|
205
|
+
if (typeof input !== "object" || input === null) {
|
|
206
|
+
return {
|
|
207
|
+
searchInput: {},
|
|
208
|
+
paramsInput: {},
|
|
209
|
+
absInput: args[1] ?? false,
|
|
210
|
+
hashInput: void 0
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
const loose = args[2] ?? this.hasLooseSearch;
|
|
214
|
+
const paramsKeys = this.getParamsKeys();
|
|
215
|
+
const paramsInput2 = paramsKeys.reduce((acc, key) => {
|
|
216
|
+
if (input[key] !== void 0) {
|
|
217
|
+
acc[key] = input[key];
|
|
218
|
+
}
|
|
219
|
+
return acc;
|
|
220
|
+
}, {});
|
|
221
|
+
const searchKeys = this.getSearchKeys();
|
|
222
|
+
const searchInput2 = Object.keys(input).filter((k) => {
|
|
223
|
+
if (k === "hash") {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
if (searchKeys.includes(k)) {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
if (paramsKeys.includes(k)) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
return !!loose;
|
|
233
|
+
}).reduce((acc, key) => {
|
|
234
|
+
acc[key] = input[key];
|
|
235
|
+
return acc;
|
|
236
|
+
}, {});
|
|
237
|
+
const hashInput2 = input.hash;
|
|
238
|
+
return {
|
|
239
|
+
searchInput: searchInput2,
|
|
240
|
+
paramsInput: paramsInput2,
|
|
241
|
+
absInput: args[1] ?? false,
|
|
242
|
+
hashInput: hashInput2
|
|
243
|
+
};
|
|
244
|
+
})();
|
|
245
|
+
return this.get({
|
|
246
|
+
...paramsInput,
|
|
247
|
+
search: searchInput,
|
|
248
|
+
abs: absInput,
|
|
249
|
+
hash: hashInput
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
flatLoose(...args) {
|
|
253
|
+
return this.flat(args[0], args[1], true);
|
|
254
|
+
}
|
|
255
|
+
flatStrict(...args) {
|
|
256
|
+
return this.flat(args[0], args[1], false);
|
|
257
|
+
}
|
|
258
|
+
/** Returns path param keys extracted from route definition. */
|
|
259
|
+
getParamsKeys() {
|
|
260
|
+
return Object.keys(this.paramsDefinition || {});
|
|
261
|
+
}
|
|
262
|
+
/** Returns named search keys extracted from route definition. */
|
|
263
|
+
getSearchKeys() {
|
|
264
|
+
return Object.keys(this.searchDefinition || {});
|
|
265
|
+
}
|
|
266
|
+
/** Returns all flat input keys (`search + params`). */
|
|
267
|
+
getFlatKeys() {
|
|
268
|
+
return [...this.getSearchKeys(), ...this.getParamsKeys()];
|
|
269
|
+
}
|
|
270
|
+
getDefinition() {
|
|
271
|
+
return this.pathDefinition;
|
|
272
|
+
}
|
|
273
|
+
/** Clones route with optional config override. */
|
|
274
|
+
clone(config) {
|
|
275
|
+
return Route0.create(this.definition, config);
|
|
276
|
+
}
|
|
277
|
+
getRegexBaseStrictString() {
|
|
278
|
+
return this.pathDefinition.replace(/:(\w+)/g, "___PARAM___").replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/___PARAM___/g, "([^/]+)");
|
|
279
|
+
}
|
|
280
|
+
getRegexBaseString() {
|
|
281
|
+
return this.getRegexBaseStrictString().replace(/\/+$/, "") + "/?";
|
|
282
|
+
}
|
|
283
|
+
getRegexStrictString() {
|
|
284
|
+
return `^${this.getRegexBaseStrictString()}$`;
|
|
285
|
+
}
|
|
286
|
+
getRegexString() {
|
|
287
|
+
return `^${this.getRegexBaseString()}$`;
|
|
288
|
+
}
|
|
289
|
+
getRegexStrict() {
|
|
290
|
+
return new RegExp(this.getRegexStrictString());
|
|
291
|
+
}
|
|
292
|
+
getRegex() {
|
|
293
|
+
return new RegExp(this.getRegexString());
|
|
294
|
+
}
|
|
295
|
+
/** Creates a grouped strict regex pattern string from many routes. */
|
|
296
|
+
static getRegexStrictStringGroup(routes) {
|
|
297
|
+
const patterns = routes.map((route) => route.getRegexStrictString()).join("|");
|
|
298
|
+
return `(${patterns})`;
|
|
299
|
+
}
|
|
300
|
+
/** Creates a strict grouped regex from many routes. */
|
|
301
|
+
static getRegexStrictGroup(routes) {
|
|
302
|
+
const patterns = Route0.getRegexStrictStringGroup(routes);
|
|
303
|
+
return new RegExp(`^(${patterns})$`);
|
|
304
|
+
}
|
|
305
|
+
/** Creates a grouped regex pattern string from many routes. */
|
|
306
|
+
static getRegexStringGroup(routes) {
|
|
307
|
+
const patterns = routes.map((route) => route.getRegexString()).join("|");
|
|
308
|
+
return `(${patterns})`;
|
|
309
|
+
}
|
|
310
|
+
/** Creates a grouped regex from many routes. */
|
|
311
|
+
static getRegexGroup(routes) {
|
|
312
|
+
const patterns = Route0.getRegexStringGroup(routes);
|
|
313
|
+
return new RegExp(`^(${patterns})$`);
|
|
314
|
+
}
|
|
315
|
+
/** Converts any location shape to relative form (removes host/origin fields). */
|
|
316
|
+
static toRelLocation(location) {
|
|
317
|
+
return {
|
|
318
|
+
...location,
|
|
319
|
+
abs: false,
|
|
320
|
+
origin: void 0,
|
|
321
|
+
href: void 0,
|
|
322
|
+
port: void 0,
|
|
323
|
+
host: void 0,
|
|
324
|
+
hostname: void 0
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
/** Converts a location to absolute form using provided base URL. */
|
|
328
|
+
static toAbsLocation(location, baseurl) {
|
|
329
|
+
const relLoc = Route0.toRelLocation(location);
|
|
330
|
+
const url = new URL(relLoc.hrefRel, baseurl);
|
|
331
|
+
return {
|
|
332
|
+
...location,
|
|
333
|
+
abs: true,
|
|
334
|
+
origin: url.origin,
|
|
335
|
+
href: url.href,
|
|
336
|
+
port: url.port,
|
|
337
|
+
host: url.host,
|
|
338
|
+
hostname: url.hostname
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
static getLocation(hrefOrHrefRelOrLocation) {
|
|
342
|
+
if (hrefOrHrefRelOrLocation instanceof URL) {
|
|
343
|
+
return Route0.getLocation(hrefOrHrefRelOrLocation.href);
|
|
344
|
+
}
|
|
345
|
+
if (typeof hrefOrHrefRelOrLocation !== "string") {
|
|
346
|
+
hrefOrHrefRelOrLocation = hrefOrHrefRelOrLocation.href || hrefOrHrefRelOrLocation.hrefRel;
|
|
347
|
+
}
|
|
348
|
+
const abs = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(hrefOrHrefRelOrLocation);
|
|
349
|
+
const base = abs ? void 0 : "http://example.com";
|
|
350
|
+
const url = new URL(hrefOrHrefRelOrLocation, base);
|
|
351
|
+
const searchParams = Object.fromEntries(url.searchParams.entries());
|
|
352
|
+
let pathname = url.pathname;
|
|
353
|
+
if (pathname.length > 1 && pathname.endsWith("/")) {
|
|
354
|
+
pathname = pathname.slice(0, -1);
|
|
355
|
+
}
|
|
356
|
+
const hrefRel = pathname + url.search + url.hash;
|
|
357
|
+
const location = {
|
|
358
|
+
pathname,
|
|
359
|
+
search: url.search,
|
|
360
|
+
hash: url.hash,
|
|
361
|
+
origin: abs ? url.origin : void 0,
|
|
362
|
+
href: abs ? url.href : void 0,
|
|
363
|
+
hrefRel,
|
|
364
|
+
abs,
|
|
365
|
+
// extra host-related fields (available even for relative with dummy base)
|
|
366
|
+
host: abs ? url.host : void 0,
|
|
367
|
+
hostname: abs ? url.hostname : void 0,
|
|
368
|
+
port: abs ? url.port || void 0 : void 0,
|
|
369
|
+
// specific to UnknownLocation
|
|
370
|
+
searchParams,
|
|
371
|
+
params: void 0,
|
|
372
|
+
route: void 0,
|
|
373
|
+
known: false,
|
|
374
|
+
exact: false,
|
|
375
|
+
ancestor: false,
|
|
376
|
+
descendant: false,
|
|
377
|
+
unmatched: false
|
|
378
|
+
};
|
|
379
|
+
return location;
|
|
380
|
+
}
|
|
381
|
+
getLocation(hrefOrHrefRelOrLocation) {
|
|
382
|
+
if (hrefOrHrefRelOrLocation instanceof URL) {
|
|
383
|
+
return this.getLocation(hrefOrHrefRelOrLocation.href);
|
|
384
|
+
}
|
|
385
|
+
if (typeof hrefOrHrefRelOrLocation !== "string") {
|
|
386
|
+
hrefOrHrefRelOrLocation = hrefOrHrefRelOrLocation.href || hrefOrHrefRelOrLocation.hrefRel;
|
|
387
|
+
}
|
|
388
|
+
const location = Route0.getLocation(hrefOrHrefRelOrLocation);
|
|
389
|
+
location.route = this.definition;
|
|
390
|
+
location.params = {};
|
|
391
|
+
const pathname = location.pathname.length > 1 && location.pathname.endsWith("/") ? location.pathname.slice(0, -1) : location.pathname;
|
|
392
|
+
const paramNames = [];
|
|
393
|
+
const def = this.pathDefinition.length > 1 && this.pathDefinition.endsWith("/") ? this.pathDefinition.slice(0, -1) : this.pathDefinition;
|
|
394
|
+
def.replace(/:([A-Za-z0-9_]+)/g, (_m, name) => {
|
|
395
|
+
paramNames.push(String(name));
|
|
396
|
+
return "";
|
|
397
|
+
});
|
|
398
|
+
const exactRe = new RegExp(`^${this.getRegexBaseString()}$`);
|
|
399
|
+
const ancestorRe = new RegExp(`^${this.getRegexBaseString()}(?:/.*)?$`);
|
|
400
|
+
const exactMatch = pathname.match(exactRe);
|
|
401
|
+
const ancestorMatch = pathname.match(ancestorRe);
|
|
402
|
+
const exact = !!exactMatch;
|
|
403
|
+
const ancestor = !exact && !!ancestorMatch;
|
|
404
|
+
const paramsMatch = exactMatch || (ancestor ? ancestorMatch : null);
|
|
405
|
+
if (paramsMatch) {
|
|
406
|
+
const values = paramsMatch.slice(1, 1 + paramNames.length);
|
|
407
|
+
const params = Object.fromEntries(paramNames.map((n, i) => [n, decodeURIComponent(values[i] ?? "")]));
|
|
408
|
+
location.params = params;
|
|
409
|
+
} else {
|
|
410
|
+
location.params = {};
|
|
411
|
+
}
|
|
412
|
+
const getParts = (path) => path === "/" ? ["/"] : path.split("/").filter(Boolean);
|
|
413
|
+
const defParts = getParts(def);
|
|
414
|
+
const pathParts = getParts(pathname);
|
|
415
|
+
let isPrefix = true;
|
|
416
|
+
if (pathParts.length > defParts.length) {
|
|
417
|
+
isPrefix = false;
|
|
418
|
+
} else {
|
|
419
|
+
for (let i = 0; i < pathParts.length; i++) {
|
|
420
|
+
const defPart = defParts[i];
|
|
421
|
+
const pathPart = pathParts[i];
|
|
422
|
+
if (!defPart) {
|
|
423
|
+
isPrefix = false;
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
if (defPart.startsWith(":")) continue;
|
|
427
|
+
if (defPart !== pathPart) {
|
|
428
|
+
isPrefix = false;
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const descendant = !exact && isPrefix;
|
|
434
|
+
const unmatched = !exact && !ancestor && !descendant;
|
|
435
|
+
if (descendant) {
|
|
436
|
+
const descendantParams = {};
|
|
437
|
+
for (let i = 0; i < pathParts.length; i++) {
|
|
438
|
+
const defPart = defParts[i];
|
|
439
|
+
const pathPart = pathParts[i];
|
|
440
|
+
if (!defPart || !pathPart) continue;
|
|
441
|
+
if (defPart.startsWith(":")) {
|
|
442
|
+
descendantParams[defPart.slice(1)] = decodeURIComponent(pathPart);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
location.params = descendantParams;
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
...location,
|
|
449
|
+
known: true,
|
|
450
|
+
exact,
|
|
451
|
+
ancestor,
|
|
452
|
+
descendant,
|
|
453
|
+
unmatched
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Safe parser for flat input objects.
|
|
458
|
+
*
|
|
459
|
+
* Returns structured success/error result instead of throwing.
|
|
460
|
+
*/
|
|
461
|
+
safeParseFlatInput(input, loose) {
|
|
462
|
+
loose ??= this.hasLooseSearch;
|
|
463
|
+
const paramsKeys = this.getParamsKeys();
|
|
464
|
+
if (input === void 0) {
|
|
465
|
+
if (paramsKeys.length) {
|
|
466
|
+
return {
|
|
467
|
+
success: false,
|
|
468
|
+
data: void 0,
|
|
469
|
+
error: new Error(`Missing params: ${paramsKeys.map((k) => `"${k}"`).join(", ")}`)
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
input = {};
|
|
473
|
+
}
|
|
474
|
+
if (typeof input !== "object" || input === null) {
|
|
475
|
+
return {
|
|
476
|
+
success: false,
|
|
477
|
+
data: void 0,
|
|
478
|
+
error: new Error("Invalid input: expected object")
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
const inputKeys = Object.keys(input);
|
|
482
|
+
const notDefinedKeys = paramsKeys.filter((k) => !inputKeys.includes(k));
|
|
483
|
+
if (notDefinedKeys.length) {
|
|
484
|
+
return {
|
|
485
|
+
success: false,
|
|
486
|
+
data: void 0,
|
|
487
|
+
error: new Error(`Missing params: ${notDefinedKeys.map((k) => `"${k}"`).join(", ")}`)
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
const data = {};
|
|
491
|
+
const filterKeys = !loose ? [...paramsKeys, ...this.getSearchKeys()] : false;
|
|
492
|
+
for (const [k, v] of Object.entries(input)) {
|
|
493
|
+
if (filterKeys && !filterKeys.includes(k)) {
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
if (typeof v === "string") {
|
|
497
|
+
data[k] = v;
|
|
498
|
+
} else if (typeof v === "number") {
|
|
499
|
+
data[k] = String(v);
|
|
500
|
+
} else {
|
|
501
|
+
const isParamKey = paramsKeys.includes(k);
|
|
502
|
+
return {
|
|
503
|
+
success: false,
|
|
504
|
+
data: void 0,
|
|
505
|
+
error: new Error(
|
|
506
|
+
`Invalid input: expected string, number,${!isParamKey ? " or undefined," : ""} got ${typeof v} for "${k}"`
|
|
507
|
+
)
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return {
|
|
512
|
+
success: true,
|
|
513
|
+
data,
|
|
514
|
+
error: void 0
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
/** Throwing variant of `safeParseFlatInput()`. */
|
|
518
|
+
parseFlatInput(input, loose) {
|
|
519
|
+
loose ??= this.hasLooseSearch;
|
|
520
|
+
const result = this.safeParseFlatInput(input, loose);
|
|
521
|
+
if (result.error) {
|
|
522
|
+
throw result.error;
|
|
523
|
+
}
|
|
524
|
+
return result.data;
|
|
525
|
+
}
|
|
526
|
+
/** True when path structure is equal (param names are ignored). */
|
|
527
|
+
isSame(other) {
|
|
528
|
+
return this.pathDefinition.replace(/:([A-Za-z0-9_]+)/g, "__PARAM__") === other.pathDefinition.replace(/:([A-Za-z0-9_]+)/g, "__PARAM__");
|
|
529
|
+
}
|
|
530
|
+
/** Static convenience wrapper for `isSame`. */
|
|
531
|
+
static isSame(a, b) {
|
|
532
|
+
if (!a) {
|
|
533
|
+
if (!b) return true;
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
if (!b) {
|
|
537
|
+
return false;
|
|
538
|
+
}
|
|
539
|
+
return Route0.create(a).isSame(Route0.create(b));
|
|
540
|
+
}
|
|
541
|
+
/** True when current route is more specific/deeper than `other`. */
|
|
542
|
+
isDescendant(other) {
|
|
543
|
+
if (!other) return false;
|
|
544
|
+
other = Route0.create(other);
|
|
545
|
+
const getParts = (path) => path === "/" ? ["/"] : path.split("/").filter(Boolean);
|
|
546
|
+
if (other.pathDefinition === "/" && this.pathDefinition !== "/") {
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
const thisParts = getParts(this.pathDefinition);
|
|
550
|
+
const otherParts = getParts(other.pathDefinition);
|
|
551
|
+
if (thisParts.length <= otherParts.length) return false;
|
|
552
|
+
for (let i = 0; i < otherParts.length; i++) {
|
|
553
|
+
const otherPart = otherParts[i];
|
|
554
|
+
const thisPart = thisParts[i];
|
|
555
|
+
if (otherPart.startsWith(":")) continue;
|
|
556
|
+
if (otherPart !== thisPart) return false;
|
|
557
|
+
}
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
/** True when current route is broader/shallower than `other`. */
|
|
561
|
+
isAncestor(other) {
|
|
562
|
+
if (!other) return false;
|
|
563
|
+
other = Route0.create(other);
|
|
564
|
+
const getParts = (path) => path === "/" ? ["/"] : path.split("/").filter(Boolean);
|
|
565
|
+
if (this.pathDefinition === "/" && other.pathDefinition !== "/") {
|
|
566
|
+
return true;
|
|
567
|
+
}
|
|
568
|
+
const thisParts = getParts(this.pathDefinition);
|
|
569
|
+
const otherParts = getParts(other.pathDefinition);
|
|
570
|
+
if (thisParts.length >= otherParts.length) return false;
|
|
571
|
+
for (let i = 0; i < thisParts.length; i++) {
|
|
572
|
+
const thisPart = thisParts[i];
|
|
573
|
+
const otherPart = otherParts[i];
|
|
574
|
+
if (thisPart.startsWith(":")) continue;
|
|
575
|
+
if (thisPart !== otherPart) return false;
|
|
576
|
+
}
|
|
577
|
+
return true;
|
|
578
|
+
}
|
|
579
|
+
/** True when two route patterns can match the same concrete URL. */
|
|
580
|
+
isConflict(other) {
|
|
581
|
+
if (!other) return false;
|
|
582
|
+
other = Route0.create(other);
|
|
583
|
+
const getParts = (path) => {
|
|
584
|
+
if (path === "/") return ["/"];
|
|
585
|
+
return path.split("/").filter(Boolean);
|
|
586
|
+
};
|
|
587
|
+
const thisParts = getParts(this.pathDefinition);
|
|
588
|
+
const otherParts = getParts(other.pathDefinition);
|
|
589
|
+
if (thisParts.length !== otherParts.length) {
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
for (let i = 0; i < thisParts.length; i++) {
|
|
593
|
+
const thisPart = thisParts[i];
|
|
594
|
+
const otherPart = otherParts[i];
|
|
595
|
+
if (thisPart.startsWith(":") && otherPart.startsWith(":")) {
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
if (thisPart.startsWith(":") || otherPart.startsWith(":")) {
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
if (thisPart !== otherPart) {
|
|
602
|
+
return false;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
return true;
|
|
606
|
+
}
|
|
607
|
+
/** Specificity comparator used for deterministic route ordering. */
|
|
608
|
+
isMoreSpecificThan(other) {
|
|
609
|
+
if (!other) return false;
|
|
610
|
+
other = Route0.create(other);
|
|
611
|
+
const getParts = (path) => {
|
|
612
|
+
if (path === "/") return ["/"];
|
|
613
|
+
return path.split("/").filter(Boolean);
|
|
614
|
+
};
|
|
615
|
+
const thisParts = getParts(this.pathDefinition);
|
|
616
|
+
const otherParts = getParts(other.pathDefinition);
|
|
617
|
+
for (let i = 0; i < Math.min(thisParts.length, otherParts.length); i++) {
|
|
618
|
+
const thisIsStatic = !thisParts[i].startsWith(":");
|
|
619
|
+
const otherIsStatic = !otherParts[i].startsWith(":");
|
|
620
|
+
if (thisIsStatic && !otherIsStatic) return true;
|
|
621
|
+
if (!thisIsStatic && otherIsStatic) return false;
|
|
622
|
+
}
|
|
623
|
+
return this.pathDefinition < other.pathDefinition;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
class Routes {
|
|
627
|
+
_routes;
|
|
628
|
+
_pathsOrdering;
|
|
629
|
+
_keysOrdering;
|
|
630
|
+
_ordered;
|
|
631
|
+
_;
|
|
632
|
+
constructor({
|
|
633
|
+
routes,
|
|
634
|
+
isHydrated = false,
|
|
635
|
+
pathsOrdering,
|
|
636
|
+
keysOrdering,
|
|
637
|
+
ordered
|
|
638
|
+
}) {
|
|
639
|
+
this._routes = isHydrated ? routes : Routes.hydrate(routes);
|
|
640
|
+
if (!pathsOrdering || !keysOrdering || !ordered) {
|
|
641
|
+
const ordering = Routes.makeOrdering(this._routes);
|
|
642
|
+
this._pathsOrdering = ordering.pathsOrdering;
|
|
643
|
+
this._keysOrdering = ordering.keysOrdering;
|
|
644
|
+
this._ordered = this._keysOrdering.map((key) => this._routes[key]);
|
|
645
|
+
} else {
|
|
646
|
+
this._pathsOrdering = pathsOrdering;
|
|
647
|
+
this._keysOrdering = keysOrdering;
|
|
648
|
+
this._ordered = ordered;
|
|
649
|
+
}
|
|
650
|
+
this._ = {
|
|
651
|
+
routes: this._routes,
|
|
652
|
+
getLocation: this._getLocation.bind(this),
|
|
653
|
+
override: this._override.bind(this),
|
|
654
|
+
pathsOrdering: this._pathsOrdering,
|
|
655
|
+
keysOrdering: this._keysOrdering,
|
|
656
|
+
ordered: this._ordered
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
/** Creates and hydrates a typed routes collection. */
|
|
660
|
+
static create(routes, override) {
|
|
661
|
+
const result = Routes.prettify(new Routes({ routes }));
|
|
662
|
+
if (!override) {
|
|
663
|
+
return result;
|
|
664
|
+
}
|
|
665
|
+
return result._.override(override);
|
|
666
|
+
}
|
|
667
|
+
static prettify(instance) {
|
|
668
|
+
Object.setPrototypeOf(instance, Routes.prototype);
|
|
669
|
+
Object.defineProperty(instance, Symbol.toStringTag, {
|
|
670
|
+
value: "Routes"
|
|
671
|
+
});
|
|
672
|
+
Object.assign(instance, {
|
|
673
|
+
override: instance._override.bind(instance)
|
|
674
|
+
});
|
|
675
|
+
Object.assign(instance, instance._routes);
|
|
676
|
+
return instance;
|
|
677
|
+
}
|
|
678
|
+
static hydrate(routes) {
|
|
679
|
+
const result = {};
|
|
680
|
+
for (const key in routes) {
|
|
681
|
+
if (Object.hasOwn(routes, key)) {
|
|
682
|
+
const value = routes[key];
|
|
683
|
+
result[key] = typeof value === "string" ? Route0.create(value) : value;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return result;
|
|
687
|
+
}
|
|
688
|
+
_getLocation(hrefOrHrefRelOrLocation) {
|
|
689
|
+
const input = hrefOrHrefRelOrLocation;
|
|
690
|
+
for (const route of this._ordered) {
|
|
691
|
+
const loc = route.getLocation(hrefOrHrefRelOrLocation);
|
|
692
|
+
if (loc.exact) {
|
|
693
|
+
return loc;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return typeof input === "string" ? Route0.getLocation(input) : Route0.getLocation(input);
|
|
697
|
+
}
|
|
698
|
+
static makeOrdering(routes) {
|
|
699
|
+
const hydrated = Routes.hydrate(routes);
|
|
700
|
+
const entries = Object.entries(hydrated);
|
|
701
|
+
const getParts = (path) => {
|
|
702
|
+
if (path === "/") return ["/"];
|
|
703
|
+
return path.split("/").filter(Boolean);
|
|
704
|
+
};
|
|
705
|
+
entries.sort(([_keyA, routeA], [_keyB, routeB]) => {
|
|
706
|
+
const partsA = getParts(routeA.pathDefinition);
|
|
707
|
+
const partsB = getParts(routeB.pathDefinition);
|
|
708
|
+
if (partsA.length !== partsB.length) {
|
|
709
|
+
return partsA.length - partsB.length;
|
|
710
|
+
}
|
|
711
|
+
if (routeA.isConflict(routeB)) {
|
|
712
|
+
if (routeA.isMoreSpecificThan(routeB)) return -1;
|
|
713
|
+
if (routeB.isMoreSpecificThan(routeA)) return 1;
|
|
714
|
+
}
|
|
715
|
+
return routeA.pathDefinition.localeCompare(routeB.pathDefinition);
|
|
716
|
+
});
|
|
717
|
+
const pathsOrdering = entries.map(([_key, route]) => route.definition);
|
|
718
|
+
const keysOrdering = entries.map(([_key]) => _key);
|
|
719
|
+
return { pathsOrdering, keysOrdering };
|
|
720
|
+
}
|
|
721
|
+
/** Returns a cloned routes collection with config applied to each route. */
|
|
722
|
+
_override(config) {
|
|
723
|
+
const newRoutes = {};
|
|
724
|
+
for (const key in this._routes) {
|
|
725
|
+
if (Object.hasOwn(this._routes, key)) {
|
|
726
|
+
newRoutes[key] = this._routes[key].clone(config);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
const instance = new Routes({
|
|
730
|
+
routes: newRoutes,
|
|
731
|
+
isHydrated: true,
|
|
732
|
+
pathsOrdering: this._pathsOrdering,
|
|
733
|
+
keysOrdering: this._keysOrdering,
|
|
734
|
+
ordered: this._keysOrdering.map((key) => newRoutes[key])
|
|
735
|
+
});
|
|
736
|
+
return Routes.prettify(instance);
|
|
737
|
+
}
|
|
738
|
+
static _ = {
|
|
739
|
+
prettify: Routes.prettify.bind(Routes),
|
|
740
|
+
hydrate: Routes.hydrate.bind(Routes),
|
|
741
|
+
makeOrdering: Routes.makeOrdering.bind(Routes)
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
745
|
+
0 && (module.exports = {
|
|
746
|
+
Route0,
|
|
747
|
+
Routes
|
|
748
|
+
});
|
|
749
|
+
//# sourceMappingURL=index.cjs.map
|