Skip to content

Commit

Permalink
Merge pull request #83 from Workiva/removeQueryParam
Browse files Browse the repository at this point in the history
CPLAT-11688: Add a `removeQueryParam` method.
  • Loading branch information
rmconsole4-wk authored Jul 22, 2020
2 parents d26a23b + 9441572 commit e8bc30a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ cache:

jobs:
include:
- dart: 2.2.0
name: "SDK: 2.2.0"
- dart: 2.3.0
name: "SDK: 2.3.0"
script:
- dartanalyzer .
- pub run test
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.3.0

- Add a `removeQueryParam()` method.

## 1.2.8

- Readme updates.
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM google/dart:2.5
FROM google/dart:2
WORKDIR /build/
ADD pubspec.yaml .
RUN pub get --no-precompile
RUN pub get
FROM scratch
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2015 Workiva Inc.
Copyright 2015-2020 Workiva Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fluri
Copyright 2015 Workiva Inc.
Copyright 2015-2020 Workiva Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ If you wanted to build a long URI from the individual pieces, you would do
something like this:

```dart
Uri uri = new Uri(
var uri = Uri(
scheme: 'https',
host: 'example.com',
path: 'path/to/resource'
Expand All @@ -34,7 +34,7 @@ uri = uri.replace(
Now let's say you want update the query without losing what you already have:

```dart
Map query = new Map.from(uri.queryParameters);
var query = Map.from(uri.queryParameters);
query['bar'] = '10';
uri = uri.replace(queryParameters: query);
```
Expand All @@ -47,7 +47,7 @@ With fluri, the above interactions are easy:
```dart
import 'package:fluri/fluri.dart';
Fluri fluri = new Fluri()
var fluri = Fluri()
..scheme = 'https'
..host = 'example.com'
..path = 'path/to/resource';
Expand All @@ -65,9 +65,9 @@ build on top of a base URL:
```dart
import 'package:fluri/fluri.dart';
Fluri base = new Fluri('https://example.com/base/');
var base = Fluri('https://example.com/base/');
Fluri fluri = new Fluri.from(base)
var fluri = Fluri.from(base)
..appendToPath('path/to/resource')
..setQueryParam('count', '10');
```
Expand All @@ -77,23 +77,23 @@ Fluri also supports multi-value parameters. To access the query parameters as
class):

```dart
var fluri = new Fluri('/resource?format=json&format=text');
var fluri = Fluri('/resource?format=json&format=text');
print(fluri.queryParameters); // {'format': 'text'}
print(fluri.queryParametersAll); // {'format': ['json', 'text']}
```

To set a single query parameter to multiple values:

```dart
var fluri = new Fluri('/resource');
var fluri = Fluri('/resource');
fluri.setQueryParam('format', ['json', 'text']);
print(fluri.queryParametersAll); // {'format': ['json', 'text']}
```

Using `setQueryParam` will always replace existing values:

```dart
var fluri = new Fluri('/resource');
var fluri = Fluri('/resource');
fluri.setQueryParam('format', ['json', 'text']);
fluri.setQueryParam('format', ['binary', 'text']);
print(fluri.queryParametersAll); // {'format': ['binary', 'text']}
Expand All @@ -103,7 +103,7 @@ You can use the `queryParametersAll` setter to set the entire query with
multi-value param support:

```dart
var fluri = new Fluri('/resource');
var fluri = Fluri('/resource');
fluri.queryParametersAll = {'format': ['json', 'text'], 'count': ['5']}
print(fluri.queryParametersAll); // {'format': ['json', 'text'], 'count': ['5']}
```
Expand All @@ -113,7 +113,7 @@ Again, if you need to preserve existing query parameters, you can use the
provide will be merged with existing values:

```dart
var fluri = new Fluri('/resource?format=json');
var fluri = Fluri('/resource?format=json');
fluri.updateQuery({'format': ['binary', 'text'], 'count': '5'}, mergeValues: true);
print(fluri.queryParametersAll); // {'format': ['binary', 'json', 'text'], 'count': ['5']}
```
24 changes: 17 additions & 7 deletions lib/fluri.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 Workiva Inc.
// Copyright 2015-2020 Workiva Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,7 +32,7 @@
/// import 'package:fluri/fluri.dart';
///
/// void main() {
/// Fluri fluri = new Fluri()
/// var fluri = Fluri()
/// ..host = 'example.com'
/// ..scheme = 'https'
/// ..path = 'path/to/resource'
Expand All @@ -53,7 +53,7 @@
/// class Request extends Object with FluriMixin {}
///
/// void main() {
/// Request req = new Request()
/// var req = Request()
/// ..host = 'example.com'
/// ..scheme = 'https'
/// ..path = 'path/to/resource'
Expand All @@ -73,7 +73,7 @@ library fluri;
/// import 'package:fluri/fluri.dart';
///
/// void main() {
/// Fluri fluri = new Fluri()
/// var fluri = Fluri()
/// ..host = 'example.com'
/// ..scheme = 'https'
/// ..path = 'path/to/resource'
Expand All @@ -95,8 +95,7 @@ library fluri;
class Fluri extends FluriMixin {
/// Construct a new [Fluri] instance.
///
/// A starting [uri] may be supplied which will be parsed
/// by [Uri].[Uri.parse].
/// A starting [uri] may be supplied which will be parsed by [Uri.parse].
Fluri([String uri]) {
this.uri = Uri.parse(uri != null ? uri : '');
}
Expand Down Expand Up @@ -128,7 +127,7 @@ class Fluri extends FluriMixin {
/// class Request extends Object with FluriMixin {}
///
/// void main() {
/// Request req = new Request()
/// var req = Request()
/// ..host = 'example.com'
/// ..scheme = 'https'
/// ..path = 'path/to/resource'
Expand Down Expand Up @@ -228,6 +227,17 @@ class FluriMixin {
updateQuery({param: value});
}

/// Remove [param] from the URI query parameters.
///
/// If there are multiple values with the key of [param], all will be removed.
/// If there is no query param with the key of [param], this does nothing.
void removeQueryParam(String param) {
_uri = _uri.replace(queryParameters: {
for (final key in queryParametersAll.keys)
if (key != param) key: List.of(queryParametersAll[key]),
});
}

/// Update the URI query parameters, merging the given map with the
/// current query parameters map instead of overwriting it.
///
Expand Down
11 changes: 2 additions & 9 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
name: fluri
version: 1.2.8
version: 1.3.0
description: Fluri is a fluent URI library built to make URI mutation easy.
authors:
- Workiva Client Platform Team <[email protected]>
- Dustin Lessard <[email protected]>
- Evan Weible <[email protected]>
- Jay Udey <[email protected]>
- Max Peterson <[email protected]>
- Trent Grover <[email protected]>
homepage: https://github.com/Workiva/fluri
environment:
sdk: ">=2.2.0 <3.0.0"
sdk: ">=2.3.0 <3.0.0"
dev_dependencies:
dependency_validator: ^1.4.0
test: ^1.9.2
22 changes: 21 additions & 1 deletion test/fluri_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 Workiva Inc.
// Copyright 2015-2020 Workiva Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -148,6 +148,26 @@ void commonFluriTests(FluriMixin getFluri()) {
unorderedEquals(['binary', 'text']));
});

test('should allow removing a query parameter', () {
final fluri = getFluri()
..updateQuery({
'single': 'true',
'multi': ['one', 'two']
});
expect(fluri.queryParametersAll.keys,
allOf(contains('single'), contains('multi')));
fluri.removeQueryParam('multi');
expect(fluri.queryParametersAll['single'], ['true']);
expect(fluri.queryParametersAll.keys, isNot(contains('multi')));
fluri.removeQueryParam('single');
expect(fluri.queryParametersAll.keys, isNot(contains('single')));
});

test('should do nothing if removing a non-existant query parameter', () {
expect(
(getFluri()..removeQueryParam('none')).queryParameters, isNotEmpty);
});

test('should allow updating the query parameters ', () {
getFluri().updateQuery({'limit': '10', 'format': 'json'});
getFluri().updateQuery({
Expand Down

0 comments on commit e8bc30a

Please sign in to comment.