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 --saveor
yarn add path-patternThis 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'
(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.
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.
options: Object{ exact?: boolean; strict?: boolean; }returns: AMatcherfunction
We 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