Skip to content

suzulabo/path-matcher

Repository files navigation

@suzulabo/path-matcher

Install

pnpm add @suzulabo/path-matcher

Example

type RouteMatch = Match & {
  tag: string;
};

const matches: RouteMatch[] = [
  {
    pattern: '',
    tag: 'index',
  },
  {
    pattern: 'about',
    tag: 'about',
  },
  {
    pattern: /^[0-9A-Z]{5}$/,
    tag: 'post',
    name: 'postID',
    nexts: [
      {
        pattern: /^[0-9]{4}$/,
        tag: 'comment',
        name: 'commentID',
      },
    ],
  },
];

const path = '/ABC12/0001';
const m = pathMatcher(matches, path);
if (m) {
  assert(m.match.tag == 'comment');
  assert(m.params['postID'] == 'ABC12');
  assert(m.params['commentID'] == '0001');
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published