Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

791 B
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import {
  type Match,
  type MatchFunction,
  match,
  type ParamData,
} from "path-to-regexp";

export class TinyRouter {
  private routes: {
    template: string;
    pathFunction: MatchFunction<ParamData>;
  }[];

  public constructor(templates: string[]) {
    this.routes = templates.map((template) => {
      return {
        template,
        pathFunction: match(template),
      };
    });
  }

  public match(
    location: Location,
  ): [Match<ParamData>, template: string] | false {
    for (const { pathFunction, template } of this.routes) {
      const match =
        pathFunction(location.hash) || pathFunction(location.pathname);
      if (match) {
        return [match, template];
      }
    }

    return false;
  }
}