| 7 | * @param {string} id |
| 8 | */ |
| 9 | export function parse_route_id(id) { |
| 10 | /** @type {import('types').RouteParam[]} */ |
| 11 | const params = []; |
| 12 | |
| 13 | const pattern = |
| 14 | id === '/' |
| 15 | ? /^\/$/ |
| 16 | : new RegExp( |
| 17 | `^${get_route_segments(id) |
| 18 | .map((segment) => { |
| 19 | // special case — /[...rest]/ could contain zero segments |
| 20 | const rest_match = /^\[\.\.\.(\w+)(?:=(\w+))?\]$/.exec(segment); |
| 21 | if (rest_match) { |
| 22 | params.push({ |
| 23 | name: rest_match[1], |
| 24 | matcher: rest_match[2], |
| 25 | optional: false, |
| 26 | rest: true, |
| 27 | chained: true |
| 28 | }); |
| 29 | return '(?:/([^]*))?'; |
| 30 | } |
| 31 | // special case — /[[optional]]/ could contain zero segments |
| 32 | const optional_match = /^\[\[(\w+)(?:=(\w+))?\]\]$/.exec(segment); |
| 33 | if (optional_match) { |
| 34 | params.push({ |
| 35 | name: optional_match[1], |
| 36 | matcher: optional_match[2], |
| 37 | optional: true, |
| 38 | rest: false, |
| 39 | chained: true |
| 40 | }); |
| 41 | return '(?:/([^/]+))?'; |
| 42 | } |
| 43 | |
| 44 | if (!segment) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | const parts = segment.split(/\[(.+?)\](?!\])/); |
| 49 | const result = parts |
| 50 | .map((content, i) => { |
| 51 | if (i % 2) { |
| 52 | if (content.startsWith('x+')) { |
| 53 | return escape(String.fromCharCode(parseInt(content.slice(2), 16))); |
| 54 | } |
| 55 | |
| 56 | if (content.startsWith('u+')) { |
| 57 | return escape( |
| 58 | String.fromCharCode( |
| 59 | ...content |
| 60 | .slice(2) |
| 61 | .split('-') |
| 62 | .map((code) => parseInt(code, 16)) |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | |