(ids: Pick<RouteID, 'id' | 'params'>[], chain: RouteChain)
| 35 | }; |
| 36 | |
| 37 | export const matchesIDs = (ids: Pick<RouteID, 'id' | 'params'>[], chain: RouteChain): number => { |
| 38 | const len = Math.min(ids.length, chain.length); |
| 39 | |
| 40 | let score = 0; |
| 41 | |
| 42 | for (let i = 0; i < len; i++) { |
| 43 | const routeId = ids[i]; |
| 44 | const routeChain = chain[i]; |
| 45 | // Skip results where the route id does not match the chain at the same index |
| 46 | if (routeId.id.toLowerCase() !== routeChain.id) { |
| 47 | break; |
| 48 | } |
| 49 | if (routeId.params) { |
| 50 | const routeIdParams = Object.keys(routeId.params); |
| 51 | // Only compare routes with the chain that have the same number of parameters. |
| 52 | if (routeIdParams.length === routeChain.segments.length) { |
| 53 | // Maps the route's params into a path based on the path variable names, |
| 54 | // to compare against the route chain format. |
| 55 | // |
| 56 | // Before: |
| 57 | // ```ts |
| 58 | // { |
| 59 | // params: { |
| 60 | // s1: 'a', |
| 61 | // s2: 'b' |
| 62 | // } |
| 63 | // } |
| 64 | // ``` |
| 65 | // |
| 66 | // After: |
| 67 | // ```ts |
| 68 | // [':s1',':s2'] |
| 69 | // ``` |
| 70 | // |
| 71 | const pathWithParams = routeIdParams.map((key) => `:${key}`); |
| 72 | for (let j = 0; j < pathWithParams.length; j++) { |
| 73 | // Skip results where the path variable is not a match |
| 74 | if (pathWithParams[j].toLowerCase() !== routeChain.segments[j]) { |
| 75 | break; |
| 76 | } |
| 77 | // Weight path matches for the same index higher. |
| 78 | score++; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | // Weight id matches |
| 83 | score++; |
| 84 | } |
| 85 | return score; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * Matches the segments against the chain. |
no outgoing calls
no test coverage detected