Socket
Socket
Sign inDemoInstall

path-pattern

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

path-pattern

A url matching lib to go with Realytics/react-router-magic


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 11 Oct 2017

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