Skip to content

Commit

Permalink
Enable null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
leonsenft committed Mar 6, 2021
1 parent ff10fa2 commit 169c725
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ dart_task:
# Run tests on lowest supported SDK version.
matrix:
include:
- dart: 2.3.0
- dart: 2.12.0
dart_task: test
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.0

* Migrated to null safety.

## 0.3.0

* Added an option to create case insensitive regular expressions.
Expand Down
2 changes: 1 addition & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {

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

// Create a path function from tokens.
final toPath = tokensToFunction(tokens);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/extract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ Map<String, String> extract(List<String> parameters, Match match) {
final length = parameters.length;
return {
// Offset the group index by one since the first group is the entire match.
for (var i = 0; i < length; ++i) parameters[i]: match.group(i + 1)
for (var i = 0; i < length; ++i) parameters[i]: match.group(i + 1)!
};
}
9 changes: 6 additions & 3 deletions lib/src/parse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ final _parameterRegExp = RegExp(
/// Parses a [path] specification.
///
/// Parameter names are added, in order, to [parameters] if provided.
List<Token> parse(String path, {List<String> parameters}) {
List<Token> parse(String path, {List<String>? parameters}) {
final matches = _parameterRegExp.allMatches(path);
final tokens = <Token>[];
var start = 0;
for (final match in matches) {
if (match.start > start) {
tokens.add(PathToken(path.substring(start, match.start)));
}
final name = match[1];
final pattern = match[2] != null ? escapeGroup(match[2]) : _defaultPattern;
final name = match[1]!;
final optionalPattern = match[2];
final pattern = optionalPattern != null
? escapeGroup(optionalPattern)
: _defaultPattern;
tokens.add(ParameterToken(name, pattern: pattern));
parameters?.add(name);
start = match.end;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/regexp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'token.dart';
/// [caseSensitive] parameters and the return value.
RegExp pathToRegExp(
String path, {
List<String> parameters,
List<String>? parameters,
bool prefix = false,
bool caseSensitive = true,
}) =>
Expand All @@ -33,7 +33,7 @@ RegExp tokensToRegExp(
bool caseSensitive = true,
}) {
final buffer = StringBuffer('^');
String lastPattern;
String? lastPattern;
for (final token in tokens) {
lastPattern = token.toPattern();
buffer.write(lastPattern);
Expand Down
7 changes: 3 additions & 4 deletions lib/src/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ class ParameterToken implements Token {
final String pattern;

/// The regular expression compiled from [pattern].
RegExp get regExp => _regExp ??= RegExp('^$pattern\$');
RegExp _regExp;
late final regExp = RegExp('^$pattern\$');

@override
String toPath(Map<String, String> args) {
if (args.containsKey(name)) {
final value = args[name];
final value = args[name];
if (value != null) {
if (!regExp.hasMatch(value)) {
throw ArgumentError.value('$args', 'args',
'Expected "$name" to match "$pattern", but got "$value"');
Expand Down
9 changes: 4 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
name: path_to_regexp
version: 0.3.0
version: 0.4.0
description: Converts a path such as '/user/:id' into a regular expression.
author: Leon Senft <[email protected]>
homepage: https://github.com/leonsenft/path_to_regexp

environment:
sdk: ">=2.3.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dev_dependencies:
# For `package:pedantic/analysis_options.yaml`.
pedantic: 1.8.0
test: ^1.3.0
pedantic: 1.11.0
test: ^1.16.5
13 changes: 6 additions & 7 deletions test/path_to_regexp_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,8 @@ void tests(
final match = parsedRegExp.matchAsPrefix(path);
if (matchCase.matches) {
test('should match "$path"', () {
expect(_groupsOf(match), matchCase.groups);
});
test('should extract arguments', () {
expect(match, isNotNull);
expect(_groupsOf(match!), matchCase.groups);
expect(extract(parameters, match), matchCase.args);
});
} else {
Expand Down Expand Up @@ -449,7 +448,7 @@ void tests(
List<String> _groupsOf(Match match) {
return List<String>.generate(
match.groupCount + 1,
(i) => match.group(i),
(i) => match.group(i)!,
growable: false,
);
}
Expand Down Expand Up @@ -480,8 +479,8 @@ ToPathCase throws({Map<String, String> given = const {}}) =>
class RegExpCase {
RegExpCase(this.path, this.groups, this.args);

final Map<String, String> args;
final List<String> groups;
final Map<String, String>? args;
final List<String>? groups;
final String path;

bool get matches => groups != null;
Expand All @@ -491,5 +490,5 @@ class ToPathCase {
ToPathCase(this.args, this.path);

final Map<String, String> args;
final String path;
final String? path;
}

0 comments on commit 169c725

Please sign in to comment.