Skip to content

Realytics/path-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Path Pattern

Build Status npm

A small library to match and compile paths

This is a a wrapper around path-to-regexp.

This package is not almost production ready!

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 😉.

Prerequisites

You need NodeJS and NPM or Yarn.

Installing

npm install path-pattern --save

or

yarn add path-pattern

Motivations

This package was originally made to work with react-router-magic but can be used separatly.

Import in your project

ES6 or Typescript

Note : If you use Typescript, typings are include in the package !

import { PathPattern } from 'path-pattern';

Node

const PathPattern = require('path-pattern').PathPattern;

Usage

Basic example

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'

Pattern with parameters

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'

Inherited pattern

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'

API

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 function

- ``

Versioning

We use SemVer for versioning. For the versions available, see the releases on this repository.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

About

A small library to match and compile paths

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published