项目作者: mswjs

项目描述 :
Matches a URL against a path. Parameters, wildcards, RegExp.
高级语言: TypeScript
项目地址: git://github.com/mswjs/node-match-path.git
创建时间: 2019-07-24T13:23:22Z
项目社区:https://github.com/mswjs/node-match-path

开源协议:

下载


Package version

node-match-path

Matches a URL against the given path.

Getting started

Install

  1. npm install node-match-path

Usage

  1. const { match } = require('node-match-path')
  2. match('/user/:userId', '/user/5')
  3. /*
  4. {
  5. matches: true,
  6. params: {
  7. userId: '5'
  8. }
  9. }
  10. */

API

match(path: RegExp | string, url: string): Match

Returns a match data, if any, between a url and a path.

String path

  1. match('/admin', '/admin')
  2. /*
  3. {
  4. matches: true,
  5. params: null
  6. }
  7. */

Path parameters

  1. match('/admin/:messageId', '/admin/abc-123')
  2. /*
  3. {
  4. matches: true,
  5. params: {
  6. messageId: 'abc-123'
  7. }
  8. }
  9. */

Wildcard

  1. match('/user/*/inbox', '/user/abc-123/inbox')
  2. /*
  3. {
  4. matches: true,
  5. params: null
  6. }
  7. */

Regular expression

  1. match(/\/messages\/.+?\/participants/, '/messages/5/participants')
  2. /*
  3. {
  4. matches: true,
  5. params: null
  6. }
  7. */

Honorable mentions