Skip to content

ParamIndexMap

reference
1 min readUpdated

Kind: Type

Source: src/router.ts

Type representing a map of parameter indices.

ParamIndexMap stores the position associated with each route parameter name. The router uses it to locate parameter values after matching a route path.

Definition

ts
Record<string, number>

Diagram

mermaid
graph LR
  RoutePattern[Route pattern parameters] --> ParamIndexMap[ParamIndexMap]
  MatchedValues[Matched parameter values] --> Lookup[Parameter lookup]
  ParamIndexMap --> Lookup

Usage

ts
import type { ParamIndexMap } from "./router";

const parameterNames = ["userId", "postSlug"];

const parameterIndices = Object.fromEntries(
  parameterNames.map((name, index) => [name, index]),
) as ParamIndexMap;

function getParameter(
  values: readonly string[],
  name: string,
): string | undefined {
  const index = parameterIndices[name];

  return index === undefined ? undefined : values[index];
}

AI Coding Instructions

  • Keep parameter names aligned with the names declared in route patterns.
  • Store indices that refer to positions in the matched parameter value list.
  • Check for an undefined index before reading from parameter values.
  • Update the map when route parsing changes the order of captured parameters.

Was this page helpful?

Download as PDF
ParamIndexMap — Hono (narrator proof)