Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 15 additions & 56 deletions packages/go_router_builder/lib/src/path_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,29 @@

final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?');

/// Converts a [pattern] such as `/user/:id` into [RegExp].
///
/// The path parameters can be specified by prefixing them with `:`. The
/// `parameters` are used for storing path parameter names.
/// Extracts the path parameters from a [pattern] such as `/user/:id`.
///
/// The path parameters can be specified by prefixing them with `:`.
///
/// For example:
///
/// `pattern` = `/user/:id/book/:bookId`
///
/// The `parameters` would contain `['id', 'bookId']` as a result of calling
/// this method.
///
/// To extract the path parameter values from a [RegExpMatch], pass the
/// [RegExpMatch] into [extractPathParameters] with the `parameters` that are
/// used for generating the [RegExp].
RegExp patternToRegExp(String pattern, List<String> parameters) {
final StringBuffer buffer = StringBuffer('^');
int start = 0;
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern)) {
if (match.start > start) {
buffer.write(RegExp.escape(pattern.substring(start, match.start)));
}
final String name = match[1]!;
final String? optionalPattern = match[2];
final String regex = optionalPattern != null
? _escapeGroup(optionalPattern, name)
: '(?<$name>[^/]+)';
buffer.write(regex);
parameters.add(name);
start = match.end;
}

if (start < pattern.length) {
buffer.write(RegExp.escape(pattern.substring(start)));
}

if (!pattern.endsWith('/')) {
buffer.write(r'(?=/|$)');
}
return RegExp(buffer.toString(), caseSensitive: false);
}
Copy link
Contributor Author

@ValentinVignal ValentinVignal Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this commit (refactor: Simplify path utils methods to only keep what is used),

I'm only deleting and simplifying patternToRegExp. We don't need to use the returned RegExp. So instead I'm returning the parameters (and I don't need the parameter parameters anymore)


String _escapeGroup(String group, [String? name]) {
final String escapedGroup = group.replaceFirstMapped(
RegExp(r'[:=!]'), (Match match) => '\\${match[0]}');
if (name != null) {
return '(?<$name>$escapedGroup)';
}
return escapedGroup;
}
/// ```dart
/// final pattern = '/user/:id/book/:bookId';
/// final pathParameters = pathParametersFromPattern(pattern); // {'id', 'bookId'}
/// ```
Set<String> pathParametersFromPattern(String pattern) => <String>{
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern))
match[1]!,
};

/// Reconstructs the full path from a [pattern] and path parameters.
///
/// This is useful for restoring the original path from a [RegExpMatch].
///
/// For example, A path matched a [RegExp] returned from [patternToRegExp] and
/// produced a [RegExpMatch]. To reconstruct the path from the match, one
/// can follow these steps:
/// For example:
///
/// 1. Get the `pathParameters` by calling [extractPathParameters] with the
/// [RegExpMatch] and the parameters used for generating the [RegExp].
/// 2. Call [patternToPath] with the `pathParameters` from the first step and
/// the original `pattern` used for generating the [RegExp].
/// ```dart
/// final pattern = '/family/:id';
/// final path = patternToPath(pattern, {'id': 'family-id'}); // '/family/family-id'
/// ```
String patternToPath(String pattern, Map<String, String> pathParameters) {
final StringBuffer buffer = StringBuffer();
int start = 0;
Expand Down