path_to_regexp 0.2.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 83

path_to_regexp

Pub Travis

Converts a path such as /user/:id into a regular expression.

Matching #

pathToRegExp() converts a path specification into a regular expression that matches conforming paths.

final regExp = pathToRegExp('/user/:id');
regExp.hasMatch('/user/12'); // => true
regExp.hasMatch('/user/alice'); // => true

Custom Parameters #

By default, parameters match anything until the next delimiter. This behavior can be customized by specifying a regular expression in parentheses following a parameter name.

final regExp = pathToRegExp(r'/user/:id(\d+)');
regExp.hasMatch('/user/12'); // => true
regExp.hasMatch('/user/alice'); // => false

Extracting Parameters #

Parameters can be extracted from a path specification during conversion into a regular expression.

final parameters = <String>[];
final regExp = pathToRegExp('/user/:id', parameters: parameters);
parameters; // => ['id']

Extracting Arguments #

extract() maps the parameters of a path specification to their corresponding arguments in a match.

final parameters = <String>[];
final regExp = pathToRegExp('/user/:id', parameters: parameters);
final match = regExp.matchAsPrefix('/user/12');
extract(parameters, match); // => {'id': '12'}

Generating #

pathToFunction() converts a path specification into a function that generates matching paths.

final toPath = pathToFunction('/user/:id');
toPath({'id': '12'}); // => '/user/12'

Tokens #

parse() converts a path specification into a list of tokens, which can be used to create a regular expression or path generating function.

final tokens = parse('/users/:id');
final regExp = tokensToRegExp(tokens);
final toPath = tokensToFunction(tokens);

Similar to pathToRegExp(), parameters can also be extracted during parsing.

final parameters = <String>[];
final tokens = parse('/users/:id', parameters: parameters);

If you intend to match and generate paths from the same path specification, parse() and the token-based functions should be preferred to their path-based counterparts. This is because the token-based functions can reuse the same tokens, whereas each path-based function must parse the path specification anew.

Options #

Prefix Matching #

By default, a regular expression created by pathToRegExp or tokensToRegExp matches the entire input. However, if the optional prefix argument is true, it may also match as a prefix until a delimiter.

final regExp = pathToRegExp('/user/:id', prefix: true);
regExp.hasMatch('/user/12/details'); // => true

Demo #

Try the path_to_regexp_demo to experiment with this library.

Credit #

This package is heavily inspired by its JavaScript namesake path-to-regexp.

0.2.1 #

  • Removed unnecessary code.

  • Added an example.

  • Included package:pedantic/analysis_options.yaml.

0.2.0 #

  • Removed support for optional parameters.

0.1.2 #

  • Updated SDK constraints to support Dart 2 stable.

0.1.1 #

  • Lowered SDK constraint so that Pub can analyze the package and generate documentation.

example/main.dart

import 'package:path_to_regexp/path_to_regexp.dart';

void main() {
  // Parse a path into tokens, and extract parameters names.
  final parameters = <String>[];
  final tokens = parse(r'/user/:id(\d+)', parameters: parameters);
  print(parameters); // [id]

  // Create a regular expression from tokens.
  final regExp = tokensToRegExp(tokens);
  print(regExp.hasMatch('/user/12')); // true
  print(regExp.hasMatch('/user/alice')); // false

  // Extract parameter arguments from a match.
  final match = regExp.matchAsPrefix('/user/12');
  print(extract(parameters, match)); // {id: 12}

  // Create a path function from tokens.
  final toPath = tokensToFunction(tokens);
  print(toPath({'id': '12'})); // /user/12
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  path_to_regexp: ^0.2.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:path_to_regexp/path_to_regexp.dart';
  
Version Uploaded Documentation Archive
0.2.1 Mar 9, 2019 Go to the documentation of path_to_regexp 0.2.1 Download path_to_regexp 0.2.1 archive
0.2.0 Oct 31, 2018 Go to the documentation of path_to_regexp 0.2.0 Download path_to_regexp 0.2.0 archive
0.1.2 Jul 27, 2018 Go to the documentation of path_to_regexp 0.1.2 Download path_to_regexp 0.1.2 archive
0.1.1 Jul 4, 2018 Go to the documentation of path_to_regexp 0.1.1 Download path_to_regexp 0.1.1 archive
0.1.0 Jul 4, 2018 Go to the documentation of path_to_regexp 0.1.0 Download path_to_regexp 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
66
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

We analyzed this package on Jun 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:path_to_regexp/path_to_regexp.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
pedantic ^1.5.0
test ^1.3.0