@mpen/rerouter 0.1.9 → 0.3.1

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.
Files changed (89) hide show
  1. package/README.md +80 -18
  2. package/cli/bin.test.ts +221 -0
  3. package/cli/bin.ts +342 -0
  4. package/cli/fixtures/bin/kitchen-sink.tsx +15 -0
  5. package/cli/fixtures/bin/optional.tsx +3 -0
  6. package/cli/fixtures/bin/pages/Home.tsx +3 -0
  7. package/cli/fixtures/bin/pages/KitchenSink.tsx +3 -0
  8. package/cli/fixtures/bin/pages/Login.tsx +3 -0
  9. package/cli/fixtures/bin/pages/Match.tsx +3 -0
  10. package/cli/fixtures/bin/pages/NotFound.tsx +3 -0
  11. package/cli/fixtures/bin/pages/Optional.tsx +3 -0
  12. package/cli/fixtures/bin/regexp-groups.tsx +11 -0
  13. package/cli/fixtures/bin/simple.tsx +1 -0
  14. package/cli/fixtures/bin/unnamed.tsx +4 -0
  15. package/cli/tsconfig.json +9 -0
  16. package/dist/acorn-k7ED_tOl.js +4968 -0
  17. package/dist/angular--Iqdw9UJ.js +4057 -0
  18. package/dist/babel-hfWAujRY.js +9878 -0
  19. package/dist/bin.d.ts +29 -0
  20. package/dist/bin.js +233 -0
  21. package/dist/estree-C1Zjnvlw.js +7266 -0
  22. package/dist/flow-BaD9LyIP.js +52912 -0
  23. package/dist/glimmer-CvCjW_1V.js +7541 -0
  24. package/dist/graphql-BdtzBuWh.js +1945 -0
  25. package/dist/html-DkZtUVbo.js +7137 -0
  26. package/dist/index.d.ts +278 -0
  27. package/dist/index.js +247 -0
  28. package/dist/markdown-Z8Vrc69e.js +6876 -0
  29. package/dist/meriyah-DeO4stuH.js +7590 -0
  30. package/dist/postcss-BmgGJ0E5.js +6777 -0
  31. package/dist/prettier-BT_F8kIx.js +15629 -0
  32. package/dist/routes-PW-bNm8e.js +135 -0
  33. package/dist/typescript-DtIxStjy.js +22936 -0
  34. package/dist/yaml-CWOPBY0q.js +5281 -0
  35. package/examples/App.tsx +80 -0
  36. package/examples/dist/BlogPost-c10d9w2p.js +1 -0
  37. package/examples/dist/FetchLoading-534mdrgz.js +1 -0
  38. package/examples/dist/FetchLoading-sbxbdkre.js +1 -0
  39. package/examples/dist/Home-a1258p25.js +1 -0
  40. package/examples/dist/KitchenSink-821mjg0h.js +1 -0
  41. package/examples/dist/Login-wywx6bp7.js +1 -0
  42. package/examples/dist/Match-1e72jm5w.js +1 -0
  43. package/examples/dist/NotFound-smxj24jw.js +1 -0
  44. package/examples/dist/SlowLoading-59xxmbfk.js +1 -0
  45. package/examples/dist/index-0d4kj0rv.js +2 -0
  46. package/examples/dist/index-3x197sbt.js +9 -0
  47. package/examples/dist/index-a2hkfx1n.js +9 -0
  48. package/examples/dist/index-d21me1mc.js +9 -0
  49. package/examples/dist/index-ktqdknsn.js +2 -0
  50. package/examples/dist/index-p53qxxzd.js +2 -0
  51. package/examples/dist/index.html +67 -0
  52. package/examples/index.html +67 -0
  53. package/examples/pages/BlogPost.tsx +17 -0
  54. package/examples/pages/FetchLoading.tsx +53 -0
  55. package/examples/pages/FetchLoadingItem.tsx +45 -0
  56. package/examples/pages/Home.tsx +3 -0
  57. package/examples/pages/KitchenSink.tsx +23 -0
  58. package/examples/pages/Login.tsx +3 -0
  59. package/examples/pages/Match.tsx +5 -0
  60. package/examples/pages/NotFound.tsx +3 -0
  61. package/examples/pages/SlowLoading.tsx +8 -0
  62. package/examples/routes.gen.ts +105 -0
  63. package/examples/routes.ts +40 -0
  64. package/examples/server/serve-dist.ts +33 -0
  65. package/examples/server/tsconfig.json +9 -0
  66. package/package.json +41 -31
  67. package/src/components/Link.test.tsx +139 -0
  68. package/src/components/Link.tsx +89 -0
  69. package/src/components/NavLink.test.tsx +119 -0
  70. package/src/components/NavLink.tsx +71 -0
  71. package/src/components/Router.test.tsx +183 -0
  72. package/src/components/Router.tsx +207 -0
  73. package/src/hooks/index.ts +1 -0
  74. package/src/hooks/useUrl.ts +22 -0
  75. package/src/index.ts +6 -0
  76. package/src/lib/mergeSearch.test.ts +37 -0
  77. package/src/lib/mergeSearch.ts +21 -0
  78. package/src/lib/routes.test.ts +67 -0
  79. package/src/lib/routes.ts +247 -0
  80. package/src/lib/url.ts +9 -0
  81. package/tsconfig.json +10 -0
  82. package/tsdown.config.ts +21 -0
  83. package/LICENSE +0 -21
  84. package/dist/bundle.cjs +0 -422
  85. package/dist/bundle.d.ts +0 -2
  86. package/dist/bundle.mjs +0 -420
  87. package/dist/dev.d.ts +0 -1
  88. package/dist/log.d.ts +0 -1
  89. package/dist/uri-template.d.ts +0 -56
package/dist/bundle.cjs DELETED
@@ -1,422 +0,0 @@
1
- "use strict";
2
-
3
- const PREFIX_RE = /^[+#./;?&]/;
4
-
5
- const MODIFIER_RE = /(?<repeat>\*)?(?::(?<func>[a-zA-Z][a-zA-Z0-9_]*))?(?::(?:(?<length>\d+)))?$/;
6
-
7
- const UNRESERVED = /[^a-zA-Z0-9\-._~]+/gsu;
8
-
9
- const UR_SET = /[^a-zA-Z0-9\-._~:\/?#[\]@!$&'()*+,;=]+/gsu;
10
-
11
- const PERCENT_RE = /^%[0-9a-fA-F]{2}$/;
12
-
13
- const STR = Symbol("string");
14
-
15
- const VAR = Symbol("var");
16
-
17
- const SeparatorMap = {
18
- "": ",",
19
- "+": ",",
20
- ".": ".",
21
- "/": "/",
22
- ";": ";",
23
- "?": "&",
24
- "&": "&",
25
- "#": ","
26
- };
27
-
28
- const FirstMap = {
29
- "": "",
30
- "+": "",
31
- ".": ".",
32
- "/": "/",
33
- ";": ";",
34
- "?": "?",
35
- "&": "&",
36
- "#": "#"
37
- };
38
-
39
- const Named = {
40
- "": false,
41
- "+": false,
42
- ".": false,
43
- "/": false,
44
- ";": true,
45
- "?": true,
46
- "&": true,
47
- "#": false
48
- };
49
-
50
- const IfEmp = {
51
- "": "",
52
- "+": "",
53
- ".": "",
54
- "/": "",
55
- ";": "",
56
- "?": "=",
57
- "&": "=",
58
- "#": ""
59
- };
60
-
61
- const ReservedExpansion = {
62
- "": false,
63
- "+": true,
64
- ".": false,
65
- "/": false,
66
- ";": false,
67
- "?": false,
68
- "&": false,
69
- "#": true
70
- };
71
-
72
- var VarType;
73
-
74
- (function(VarType) {
75
- VarType[VarType["NAMED"] = 0] = "NAMED";
76
- VarType[VarType["STATIC"] = 1] = "STATIC";
77
- VarType[VarType["DYNAMIC"] = 2] = "DYNAMIC";
78
- })(VarType || (VarType = {}));
79
-
80
- class UriTemplate {
81
- expandParts;
82
- matchRegex;
83
- matchMap;
84
- constructor(template) {
85
- this.expandParts = [];
86
- const re = [ "^" ];
87
- this.matchMap = new Map;
88
- let pos = 0;
89
- const matches = Array.from(template.matchAll(/\{(.+?)\}/g));
90
- let matchCounter = 0;
91
- for (const m of matches) {
92
- if (m.index > pos) {
93
- const lit = template.slice(pos, m.index);
94
- this.expandParts.push({
95
- type: STR,
96
- value: lit
97
- });
98
- const groupName = `static${matchCounter++}`;
99
- re.push(`(?<${groupName}>${escapeRegExp(lit)})`);
100
- this.matchMap.set(groupName, {
101
- type: VarType.STATIC
102
- });
103
- }
104
- pos = m.index + m[0].length;
105
- const ph = {
106
- type: VAR,
107
- prefix: "",
108
- vars: []
109
- };
110
- this.expandParts.push(ph);
111
- let varStr = m[1];
112
- const prefix = varStr.match(PREFIX_RE);
113
- if (prefix) {
114
- varStr = varStr.slice(1);
115
- ph.prefix = prefix[0];
116
- }
117
- const vars = varStr.split(",");
118
- const isNamed = Named[ph.prefix];
119
- let isFirst = true;
120
- if (!isNamed) {
121
- re.push(escapeRegExp(FirstMap[ph.prefix]));
122
- }
123
- for (const v of vars) {
124
- const item = {
125
- name: v,
126
- length: null,
127
- func: null,
128
- repeat: false
129
- };
130
- const mod = v.match(MODIFIER_RE);
131
- if (mod) {
132
- if (mod.groups.repeat !== undefined) {
133
- item.repeat = true;
134
- }
135
- if (mod.groups.length !== undefined) {
136
- item.length = Number(mod.groups.length);
137
- }
138
- if (mod.groups.func !== undefined) {
139
- item.func = mod.groups.func;
140
- }
141
- item.name = item.name.slice(0, mod.index);
142
- }
143
- ph.vars.push(item);
144
- if (!isNamed) {
145
- if (!isFirst) {
146
- re.push(escapeRegExp(SeparatorMap[ph.prefix]));
147
- }
148
- let groupName = item.name;
149
- if (!/^[a-z][a-z0-9_]*$/i.test(groupName)) {
150
- groupName = "param";
151
- }
152
- groupName += String(matchCounter++);
153
- re.push(`(?<${groupName}>`);
154
- if (item.func != null) {
155
- switch (item.func) {
156
- case "int":
157
- if (item.length != null) {
158
- re.push(`\\d{${item.length}}`);
159
- } else {
160
- re.push("-?\\d+");
161
- }
162
- break;
163
-
164
- default:
165
- throw new Error(`Unrecognized UriTemplate func: ${item.func}`);
166
- }
167
- } else {
168
- let ch;
169
- if (ph.prefix === "+") {
170
- ch = "[^?#]";
171
- } else if (ph.prefix === "#") {
172
- ch = ".";
173
- } else if (ph.prefix === "/" && item.repeat) {
174
- ch = "[^?#]";
175
- } else {
176
- ch = "[^/?#]";
177
- }
178
- if (item.length != null) {
179
- ch = `(?:%[0-9a-fA-F]{2}|${ch})`;
180
- re.push(ch, `{${item.length}}`);
181
- } else {
182
- re.push(ch, "+?");
183
- }
184
- }
185
- re.push(")");
186
- this.matchMap.set(groupName, {
187
- var: item,
188
- type: VarType.DYNAMIC,
189
- prefix: ph.prefix
190
- });
191
- }
192
- isFirst = false;
193
- }
194
- if (isNamed) {
195
- re.push("(?:", escapeRegExp(FirstMap[ph.prefix]));
196
- const groupName = `kwargs${matchCounter++}`;
197
- re.push(`(?<${groupName}>[^/?#]*)`);
198
- re.push(")?");
199
- this.matchMap.set(groupName, {
200
- vars: ph.vars,
201
- type: VarType.NAMED,
202
- prefix: ph.prefix
203
- });
204
- }
205
- }
206
- if (pos < template.length) {
207
- const lit = template.slice(pos);
208
- this.expandParts.push({
209
- type: STR,
210
- value: lit
211
- });
212
- const groupName = `static${matchCounter++}`;
213
- re.push(`(?<${groupName}>${escapeRegExp(lit)})`);
214
- this.matchMap.set(groupName, {
215
- type: VarType.STATIC
216
- });
217
- }
218
- re.push("$");
219
- this.matchRegex = new RegExp(re.join(""));
220
- }
221
- expand(variables) {
222
- const out = [];
223
- for (const p of this.expandParts) {
224
- switch (p.type) {
225
- case VAR:
226
- const vs = [];
227
- for (const v of p.vars) {
228
- if (Object.hasOwn(variables, v.name)) {
229
- const x = variables[v.name];
230
- if (x == null) continue;
231
- const sep = v.repeat ? SeparatorMap[p.prefix] : ",";
232
- const esc = x => percentEncodeRegExp(x, ReservedExpansion[p.prefix], v.length, v.repeat ? "=" : ",");
233
- let pre = "";
234
- if (Named[p.prefix]) {
235
- pre = v.name + (isEmpty(x) ? IfEmp[p.prefix] : "=");
236
- }
237
- if (Array.isArray(x)) {
238
- if (x.length) {
239
- vs.push((v.repeat ? "" : pre) + x.map((z => (v.repeat ? pre : "") + esc(z))).join(sep));
240
- }
241
- } else if (typeof x === "object") {
242
- if (Object.keys(x).length) {
243
- vs.push((v.repeat ? "" : pre) + Object.entries(x).map((([ok, ov]) => `${esc(ok)}${v.repeat ? "=" : ","}${esc(ov)}`)).join(sep));
244
- }
245
- } else {
246
- vs.push(pre + esc(x));
247
- }
248
- }
249
- }
250
- if (vs.length) {
251
- out.push(FirstMap[p.prefix], vs.join(SeparatorMap[p.prefix]));
252
- }
253
- break;
254
-
255
- case STR:
256
- out.push(p.value);
257
- break;
258
- }
259
- }
260
- return out.join("");
261
- }
262
- match(url) {
263
- let score = 0;
264
- const m = url.match(this.matchRegex);
265
- if (m !== null) {
266
- let params = [];
267
- if (m.groups != null) {
268
- for (const [k, v] of Object.entries(m.groups)) {
269
- const itemSpec = this.matchMap.get(k);
270
- let value;
271
- switch (itemSpec.type) {
272
- case VarType.NAMED:
273
- {
274
- const parsed = v == null ? Object.create(null) : parseParams(v, SeparatorMap[itemSpec.prefix]);
275
- for (const vs of itemSpec.vars) {
276
- if (vs.repeat) {
277
- score -= 1;
278
- if (isEmpty(parsed)) {
279
- params.push([ vs.name, EMPTY_OBJ ]);
280
- } else {
281
- params.push([ vs.name, mapValues(parsed, (x => formatElement(x, vs))) ]);
282
- }
283
- break;
284
- } else {
285
- if (parsed[vs.name] !== undefined) {
286
- score += 2;
287
- params.push([ vs.name, formatElement(parsed[vs.name], vs) ]);
288
- delete parsed[vs.name];
289
- } else {
290
- params.push([ vs.name, null ]);
291
- }
292
- }
293
- }
294
- }
295
- break;
296
-
297
- case VarType.DYNAMIC:
298
- {
299
- score += 2;
300
- if (v == null) {
301
- if (itemSpec.var.repeat) {
302
- if (Named[itemSpec.prefix]) {
303
- value = EMPTY_OBJ;
304
- } else {
305
- value = EMPTY_ARR;
306
- }
307
- } else {
308
- value = null;
309
- }
310
- } else {
311
- value = v;
312
- if (itemSpec.var.repeat) {
313
- value = value.split(SeparatorMap[itemSpec.prefix]).map((x => formatElement(x, itemSpec.var)));
314
- } else {
315
- value = formatElement(value, itemSpec.var);
316
- }
317
- }
318
- params.push([ itemSpec.var.name, value ]);
319
- }
320
- break;
321
-
322
- case VarType.STATIC:
323
- {
324
- score += 3;
325
- }
326
- break;
327
- }
328
- }
329
- }
330
- return {
331
- score,
332
- params: Object.fromEntries(params)
333
- };
334
- }
335
- return null;
336
- }
337
- }
338
-
339
- function formatElement(str, varSpec) {
340
- if (str == null) return null;
341
- let out = decodeURIComponent(str);
342
- if (varSpec.length != null) {
343
- out = out.slice(0, varSpec.length);
344
- }
345
- if (varSpec.func === "int") {
346
- out = Number(out);
347
- }
348
- return out;
349
- }
350
-
351
- function mapValues(obj, callback) {
352
- return Object.fromEntries(Object.entries(obj).map((([key, value]) => [ key, callback(value) ])));
353
- }
354
-
355
- function parseParams(queryString, separator) {
356
- const pairs = queryString.split(separator);
357
- return Object.fromEntries(pairs.map((pair => {
358
- const idx = pair.indexOf("=");
359
- if (idx === -1) {
360
- return [ pair, null ];
361
- }
362
- const key = pair.slice(0, idx);
363
- const value = pair.slice(idx + 1);
364
- return [ key, value ];
365
- })));
366
- }
367
-
368
- const EMPTY_OBJ = Object.freeze(Object.create(null));
369
-
370
- const EMPTY_ARR = Object.freeze([]);
371
-
372
- function isEmpty(x) {
373
- return x == null || x === "" || (Array.isArray(x) ? !x.length : typeof x === "object" && !Object.keys(x).length);
374
- }
375
-
376
- function escapeRegExp(string) {
377
- return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
378
- }
379
-
380
- function fullWide(n) {
381
- try {
382
- return n.toLocaleString("en-US", {
383
- useGrouping: false,
384
- maximumFractionDigits: 20
385
- });
386
- } catch {
387
- return n.toFixed(14).replace(/\.?0+$/, "");
388
- }
389
- }
390
-
391
- function percentEncodeRegExp(value, reserved, length, separator) {
392
- if (typeof value === "number") {
393
- return fullWide(value);
394
- }
395
- if (value === true) return "1";
396
- if (value === false) return "0";
397
- if (value == null) return "";
398
- if (Array.isArray(value)) {
399
- return value.map((v => percentEncodeRegExp(v, reserved, length, separator))).join(separator);
400
- }
401
- if (length != null) {
402
- value = value.slice(0, length);
403
- }
404
- if (reserved) {
405
- return value.replace(/%[0-9a-fA-F]{2}|./gsu, (m => {
406
- let v = PERCENT_RE.test(m) ? decodeURIComponent(m) : m;
407
- if (UR_SET.test(v)) {
408
- return percentEncode(v);
409
- }
410
- return m;
411
- }));
412
- }
413
- return value.replace(UNRESERVED, percentEncode);
414
- }
415
-
416
- const UTF8_ENCODER = new TextEncoder;
417
-
418
- function percentEncode(str) {
419
- return Array.from(UTF8_ENCODER.encode(str)).map((i => "%" + i.toString(16).toUpperCase().padStart(2, "0"))).join("");
420
- }
421
-
422
- exports.UriTemplate = UriTemplate;
package/dist/bundle.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export { UriTemplate } from './uri-template';
2
- export type { UrlParamValue, UriMatch, UriParams } from './uri-template';