Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
path-pattern
Advanced tools
A small library to match and compile paths
This is a a wrapper around path-to-regexp.
Even if we use this package in production at Realytics we can't garanty it won't break. If you want to use this, do it carefully and feel free to report issue so we can improve it 😉.
You need NodeJS and NPM or Yarn.
npm install path-pattern --save
or
yarn add path-pattern
This package was originally made to work with react-router-magic but can be used separatly.
Note : If you use Typescript, typings are include in the package !
import { PathPattern } from 'path-pattern';
const PathPattern = require('path-pattern').PathPattern;
import { PathPattern } from 'path-pattern';
// let's create a simple pattern
const homePattern = new PathPattern('/home');
// test if the path match with a location
homePattern.match({ pathname: '/home' })
// => { path: '/home', url: '/home', isExact: true, params: {} }
// match only the start of the path :
homePattern.match({ pathname: '/home/hello' })
// => { path: '/home', url: '/home', isExact: false, params: {} }
// you can use matchExact to match only exact path
homePattern.matchExact({ pathname: '/home' })
// => { path: '/home', url: '/home', isExact: true, params: {} }
homePattern.matchExact({ pathname: '/home/hello' })
// => false
// You can get a path from a pattern with compile
// this is more useful when parameters are envolved (see bellow)
homePattern.compile()
// => '/home'
import { PathPattern } from 'path-pattern';
// A pattern with params
const userPattern = new PathPattern('/user/:user');
userPattern.match({ pathname: '/user' })
// => false
userPattern.match({ pathname: '/user/john' })
// => { path: '/user/:user', url: '/user/john', isExact: true, params: { user: 'john' } }
// you can pass params value to compile it
userPattern.compile({ user: 'john' })
// => '/user/john'
import { InheritedPathPattern } from 'path-pattern';
// A pattern that inherite from another
const pagePattern = new InheritedPathPattern(homePattern, '/:page');
pagePattern.match({ pathname: '/home/hello' })
// => { path: '/home/:page', url: '/home/hello', isExact: true, params: { page: 'hello' } }
// compile works as expected
pagePattern.compile({ page: 'yolo' })
// => '/home/yolo'
Matcher
(type)
(location: Location) => Match;
A matcher is a function that take a location (a string or a [https://github.com/ReactTraining/history](Location object)) and return a Match object or false.
Match
(type)
false | { params, isExact, path, url }
An object that represent the result of a Matcher
. If the path doesn't match it's false, if it does it's an object.
PathPattern
constructor(pattern: string)
matchAdvanced(options): Matcher
options
: Object { exact?: boolean; strict?: boolean; }
returns
: A Matcher
functionWe use SemVer for versioning. For the versions available, see the releases on this repository.
This project is licensed under the MIT License - see the LICENSE.md file for details
FAQs
A url matching lib to go with Realytics/react-router-magic
The npm package path-pattern receives a total of 0 weekly downloads. As such, path-pattern popularity was classified as not popular.
We found that path-pattern demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.