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

A small library to match and compile paths

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

This package is not Production ready !

This package is under developement, do not use it in production.

Prerequisites

You need NodeJS and NPM or Yarn.

Installing

npm install path-pattern --save

or

yarn add path-pattern

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

const homePattern = new PathPattern('/home');
// same as
const homePattern = new PathPattern('/home', { exact: false, strict: false });

API

PathPattern

> new PathPattern<P>(path: string, options?: { strict?: boolean, exact?: boolean })
  • P: Type of params object (in Match resulr)
  • path: The path pattern to parse, see path-to-regexp docs
  • option.strict: (false by default) When true, a path that has a trailing slash will only match a location.pathname with a trailing slash. This has no effect when there are additional URL segments in the location.pathname
  • option.exact: (false by default) When true, the path will not match sub path (see below)
Examples
  • new PathPattern('/home') (any path that start with home)
    • Will match
      • /home
      • /home/
      • /home/user
      • /home/user/
      • /home/user/john
    • Will not match
      • /
      • /welcome
  • new PathPattern('/home', { exact: true }) (home path)
    • Will match
      • /home
      • /home/
    • Will not match
      • /home/user
      • /home/user/
      • /home/user/john
      • /
      • /welcome
  • new PathPattern('/home/', { exact: true, strict: true }) (home path with a trailing slash)
    • Will match
      • /home/
    • Will not match
      • /home
      • /home/user
      • /home/user/
      • /home/user/john
      • /
      • /welcome
> match(location: string): (Match<P> | false)
  • location: the location.pathname you want to match
  • Result : a Match object is the location match the path, false if not. See Match object bellow
> compile(params?: P): string
  • params: Params used to build the path (for example { user: 'john' } to build /home/john)
  • Result: the compiled path

> Match

Match is a plain object that contain the following keys:

  • path: string: The path that match, example: /user/:user
  • url: string: The url that match, example: /user/john,
  • isExact: boolean: Is the match an exact match example: false,
  • params: {[paramName: string]: any}: Params found, example: { 'user': 'john' }

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 30 May 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