Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

matchit

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

matchit

Quickly parse & match URLs

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
348K
increased by0.15%
Maintainers
1
Weekly downloads
 
Created

What is matchit?

The 'matchit' npm package is a tiny, fast, and efficient routing library for matching URL patterns. It is designed to be minimalistic and highly performant, making it suitable for use in both client-side and server-side applications.

What are matchit's main functionalities?

Basic Route Matching

This feature allows you to define and match URL patterns. The `parse` function is used to define routes, and the `match` function is used to match a URL against these routes, returning the matched route and any parameters.

const { match, parse } = require('matchit');

const routes = [
  parse('/users'),
  parse('/users/:id'),
  parse('/posts/:postId/comments/:commentId')
];

const matchRoute = (url) => {
  for (const route of routes) {
    const params = match(url, route);
    if (params) return { route, params };
  }
  return null;
};

console.log(matchRoute('/users/123')); // { route: { ... }, params: { id: '123' } }

Nested Route Matching

This feature allows for nested route matching, where you can define routes with multiple parameters and match URLs that include these nested parameters.

const { match, parse } = require('matchit');

const routes = [
  parse('/users'),
  parse('/users/:id'),
  parse('/users/:id/posts/:postId')
];

const matchRoute = (url) => {
  for (const route of routes) {
    const params = match(url, route);
    if (params) return { route, params };
  }
  return null;
};

console.log(matchRoute('/users/123/posts/456')); // { route: { ... }, params: { id: '123', postId: '456' } }

Wildcard Route Matching

This feature allows for wildcard route matching, where you can define routes that match any sub-path. The wildcard parameter captures the rest of the URL.

const { match, parse } = require('matchit');

const routes = [
  parse('/files/*'),
  parse('/files/images/*'),
  parse('/files/videos/*')
];

const matchRoute = (url) => {
  for (const route of routes) {
    const params = match(url, route);
    if (params) return { route, params };
  }
  return null;
};

console.log(matchRoute('/files/images/photo.jpg')); // { route: { ... }, params: { wildcard: 'photo.jpg' } }

Other packages similar to matchit

Keywords

FAQs

Package last updated on 05 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc