prexp 0.1.0 copy "prexp: ^0.1.0" to clipboard
prexp: ^0.1.0 copied to clipboard

discontinuedreplaced by: routingkit

Convert the path string to a regular expression, such as `/users/:name` and the like.

Prexp #

Prexp (Path to regular expression) is a Dart package that converts a path to a regular expression.

import 'package:prexp/prexp.dart';

void main() {
  final String route = r'/users/:name';

  final Prexp prexp = Prexp.fromString(route);
  print(prexp.hasMatch('/users/odroe')); // true

  final PathMatcher matcher = PathMatcher.fromPrexp(prexp);
  print(matcher('/users/odroe')); // (PrexpMatch(/users/Seven, {name: Seven}))

  final PathBuilder builder = PathBuilder.fromPath(route);
  print(builder({'name': 'odroe'})); // /users/odroe

  print(Prexp.parse(
      route)); // [StringPrexpToken(/users), MetadataPrexpToken({"name":"name","prefix":"/","suffix":"","pattern":"[^\\/#\\?]+?","modifier":""}]
}

Installation #

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

dependencies:
  prexp: latest

Or install it from the command line:

dart pub add prexp

Create a list of PrexpToken #

The Prexp.parse static utility to create an list of PrexpToken from a path.

final Iterable<PrexpToken> tokens = Prexp.parse('/users/:name');
Optional parameters
  • delimiter - The delimiter between segments. Defaults to /#?.
  • prefixes - The prefixes to use when parsing a path. Defaults to ./.

Prexp class #

Prexp implements the RegExp interface and is compatible with RegExp. The only difference between Prexp and RegExp is that Prexp contains the parameter source information of path.

final Prexp prexp = Prexp.fromString('/users/:name');

print(prexp.hasMatch('/users/odroe')); // true
print(prexp is RegExp); // true
print(prexp.metadata); // MetadataPrexpToken({"name":"name","prefix":"/","suffix":"","pattern":"[^\\/#\\?]+?","modifier":""})

Create a Prexp from a string

final Prexp prexp = Prexp.fromString('/users/:name');

Create a Prexp from a RegExp

final RegExp regexp = ...;
final Prexp prexp = Prexp.fromRegExp(regexp);

Create a Prexp from a list of PrexpToken

final Iterable<PrexpToken> tokens = Prexp.parse('/users/:name');
final Prexp prexp = Prexp.fromTokens(tokens);

Path builder #

The PathBuilder class is used to build a path from a map of parameters.

Create a PathBuilder from a path

final PathBuilder builder = PathBuilder.fromPath('/users/:name');

print(builder({'name': 'odroe'})); // /users/odroe

Create a PathBuilder from list of PrexpToken

final Iterable<PrexpToken> tokens = Prexp.parse('/users/:name');
final PathBuilder builder = PathBuilder.fromTokens(tokens);

print(builder({'name': 'odroe'})); // /users/odroe

Path matcher #

The PathMatcher class is used to match a path against a route.

Create a PathMatcher from a Prexp

final Prexp prexp = Prexp.fromString('/users/:name');
final PathMatcher matcher = PathMatcher.fromPrexp(prexp);

print(matcher('/users/odroe')); // (PrexpMatch(/users/odroe, {name: odroe}))

Create a PathMatcher from a RegExp

final RegExp regexp = ...;
final Iterable<MetadataPrexpToken> metadata = ...;
final PathMatcher matcher = PathMatcher.fromRegExp(regexp, metadata);

print(matcher('/users/odroe'));

Match a path against a route


final PathMatcher matcher = ...;
final Iterable<PrexpMatch> matches = matcher('/users/odroe');

print(matches); // (PrexpMatch(/users/odroe, {name: odroe}))

Note: If the path does not match the route, the PathMatcher returns an empty list.

1
likes
0
pub points
4%
popularity

Publisher

verified publisherodroe.com

Convert the path string to a regular expression, such as `/users/:name` and the like.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on prexp