path-to-regexp

  • Version 6.2.2
  • Published
  • 108 kB
  • No dependencies
  • MIT license

Install

npm i path-to-regexp
yarn add path-to-regexp
pnpm add path-to-regexp

Overview

Express style path to RegExp utility

Index

Functions

function compile

compile: <P extends object = object>(
str: string,
options?: ParseOptions & TokensToFunctionOptions
) => PathFunction<P>;
  • Compile a string to a template function for the path.

function match

match: <P extends object = object>(
str: Path,
options?: ParseOptions & TokensToRegexpOptions & RegexpToFunctionOptions
) => MatchFunction<P>;
  • Create path match function from path-to-regexp spec.

function parse

parse: (str: string, options?: ParseOptions) => Token[];
  • Parse a string for the raw tokens.

function pathToRegexp

pathToRegexp: (
path: Path,
keys?: Key[],
options?: TokensToRegexpOptions & ParseOptions
) => RegExp;
  • Normalize the given path string, returning a regular expression.

    An empty array can be passed in for the keys, which will hold the placeholder key descriptions. For example, using /user/:id, keys will contain [{ name: 'id', delimiter: '/', optional: false, repeat: false }].

function regexpToFunction

regexpToFunction: <P extends object = object>(
re: RegExp,
keys: Key[],
options?: RegexpToFunctionOptions
) => MatchFunction<P>;
  • Create a path match function from path-to-regexp output.

function tokensToFunction

tokensToFunction: <P extends object = object>(
tokens: Token[],
options?: TokensToFunctionOptions
) => PathFunction<P>;
  • Expose a method for transforming tokens into the path function.

function tokensToRegexp

tokensToRegexp: (
tokens: Token[],
keys?: Key[],
options?: TokensToRegexpOptions
) => RegExp;
  • Expose a function for taking tokens and returning a RegExp.

Interfaces

interface Key

interface Key {}
  • Metadata about a key.

property modifier

modifier: string;

    property name

    name: string | number;

      property pattern

      pattern: string;

        property prefix

        prefix: string;

          property suffix

          suffix: string;

            interface MatchResult

            interface MatchResult<P extends object = object> {}
            • A match result contains data about the path match.

            property index

            index: number;

              property params

              params: P;

                property path

                path: string;

                  interface ParseOptions

                  interface ParseOptions {}

                    property delimiter

                    delimiter?: string;
                    • Set the default delimiter for repeat parameters. (default: '/')

                    property prefixes

                    prefixes?: string;
                    • List of characters to automatically consider prefixes when parsing.

                    interface RegexpToFunctionOptions

                    interface RegexpToFunctionOptions {}

                      property decode

                      decode?: (value: string, token: Key) => string;
                      • Function for decoding strings for params.

                      interface TokensToFunctionOptions

                      interface TokensToFunctionOptions {}

                        property encode

                        encode?: (value: string, token: Key) => string;
                        • Function for encoding input strings for output.

                        property sensitive

                        sensitive?: boolean;
                        • When true the regexp will be case sensitive. (default: false)

                        property validate

                        validate?: boolean;
                        • When false the function can produce an invalid (unmatched) path. (default: true)

                        interface TokensToRegexpOptions

                        interface TokensToRegexpOptions {}

                          property delimiter

                          delimiter?: string;
                          • Sets the final character for non-ending optimistic matches. (default: /)

                          property encode

                          encode?: (value: string) => string;
                          • Encode path tokens for use in the RegExp.

                          property end

                          end?: boolean;
                          • When true the regexp will match to the end of the string. (default: true)

                          property endsWith

                          endsWith?: string;
                          • List of characters that can also be "end" characters.

                          property sensitive

                          sensitive?: boolean;
                          • When true the regexp will be case sensitive. (default: false)

                          property start

                          start?: boolean;
                          • When true the regexp will match from the beginning of the string. (default: true)

                          property strict

                          strict?: boolean;
                          • When true the regexp won't allow an optional trailing delimiter to match. (default: false)

                          Type Aliases

                          type Match

                          type Match<P extends object = object> = false | MatchResult<P>;
                          • A match is either false (no match) or a match result.

                          type MatchFunction

                          type MatchFunction<P extends object = object> = (path: string) => Match<P>;
                          • The match function takes a string and returns whether it matched the path.

                          type Path

                          type Path = string | RegExp | Array<string | RegExp>;
                          • Supported path-to-regexp input types.

                          type PathFunction

                          type PathFunction<P extends object = object> = (data?: P) => string;

                            type Token

                            type Token = string | Key;
                            • A token is a string (nothing special) or key metadata (capture group).

                            Package Files (1)

                            Dependencies (0)

                            No dependencies.

                            Dev Dependencies (8)

                            Peer Dependencies (0)

                            No peer dependencies.

                            Badge

                            To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                            You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/path-to-regexp.

                            • Markdown
                              [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/path-to-regexp)
                            • HTML
                              <a href="https://www.jsdocs.io/package/path-to-regexp"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>