パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.4.0

path_to_regexp

/user/:id のようなパスを正規表現に変換します。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
7.3万
いいね数
46
Pub ポイント
160

パッケージをインストール

flutter pub add path_to_regexp

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

パスパターン(例:'/user/:id')をルートマッチング用の正規表現に変換するDartパッケージです。パラメータのキャプチャ、ワイルドカード、オプションセグメントをサポートすることで、動的なパス解析が可能になります。Flutterアプリにおけるルーティングシステムの構築に最適で、定義されたルートに対してURLを照合するための軽量なソリューションを提供します。このパッケージはすべての主要プラットフォームと互換性があり、既存のナビゲーションロジックとシームレスに統合できます。

用途

  • ダイナミックルートマッチング
  • URL パターンの解析
  • ナビゲーションシステムの実装
  • ルートパラメータの抽出
  • カスタムルーター開発
  • APIエンドポイントの一致

主な機能

  • 名前付きパラメータ(例: :id)をサポートしています
  • オプションセグメントを処理する
  • ワイルドカード一致を含む
  • 再利用可能な RegExp オブジェクトを生成します
  • 軽量で高速
  • クロスプラットフォーム互換性

適している対象

  • カスタムルーターを構築する開発者
  • 柔軟なURLルーティングを必要とするアプリ
  • 動的パスを使用したフロントエンドナビゲーション
  • 複雑なルートパターンの解析
  • 既存のルーティングライブラリとの統合

注意事項

  • クエリパラメータを処理しない
  • 組み込みのミドルウェアサポートなし
  • 手動でのルート解決が必要です
  • パスレベルの一致に限定
  • 完全なHTTPサーバー用途には設計されていません

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

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
Case Sensitivity

By default, a regular expression created by pathToRegExp or tokensToRegExp is case sensitive. To create a case insensitive regular expression, set caseSensitive to false.

final regExp = pathToRegExp('/user/:id', caseSensitive: false);
regExp.hasMatch('/USER/12'); // => 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.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト