Skip to content

Add .pick() and .exclude() #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
37 changes: 37 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,40 @@ export function stringifyUrl(
object: UrlObject,
options?: StringifyOptions
): string;

/**
Filter query parameters from a url.

@param url The url containing the query parameters to filter.
@param includeParams The names of the query parameters to retain. All other query parameters will be removed from the url.
@param filter A filter predicate that will be provided the name of each query parameter and its value.

@example
```
queryString.filterElements('https://foo.bar?foo=1&bar=2#hello', ['foo']);
//=> 'https://foo.bar?foo=1#hello'

queryString.filterElements('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
//=> 'https://foo.bar?bar=2#hello'
```
*/
export function filterElements(
url: string,
includeParams: ReadonlyArray<string>,
options?: ParseOptions & StringifyOptions
): string
export function filterElements(
url: string,
filter: (name: string, value: string | boolean | number) => boolean,
options?: {parseBooleans: true, parseNumbers: true} & ParseOptions & StringifyOptions
): string
export function filterElements(
url: string,
filter: (name: string, value: string | boolean) => boolean,
options?: {parseBooleans: true} & ParseOptions & StringifyOptions
): string
export function filterElements(
url: string,
filter: (name: string, value: string | number) => boolean,
options?: {parseNumbers: true} & ParseOptions & StringifyOptions
): string
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const strictUriEncode = require('strict-uri-encode');
const decodeComponent = require('decode-uri-component');
const splitOnFirst = require('split-on-first');
const filterObject = require('filter-obj');

const isNullOrUndefined = value => value === null || value === undefined;

Expand Down Expand Up @@ -376,3 +377,16 @@ exports.stringifyUrl = (input, options) => {

return `${url}${queryString}${hash}`;
};

exports.filterElements = (input, filter, options) => {
options = Object.assign({
parseFragmentIdentifier: true
}, options);

const {url, query, fragmentIdentifier} = exports.parseUrl(input, options);
return exports.stringifyUrl({
url,
query: filterObject(query, filter),
fragmentIdentifier
}, options);
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
"stringify",
"encode",
"decode",
"searchparams"
"searchparams",
"filter"
],
"dependencies": {
"decode-uri-component": "^0.2.0",
"filter-obj": "^1.1.0",
"split-on-first": "^1.0.0",
"strict-uri-encode": "^2.0.0"
},
Expand Down
37 changes: 37 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,43 @@ Type: `object`

Query items to add to the URL.

### .filterElements(url, includeParams, options?)
### .filterElements(url, filter, options?)

Filter query parameters from a url.

```js
queryString.filterElements('https://foo.bar?foo=1&bar=2#hello', ['foo']);
//=> 'https://foo.bar?foo=1#hello'

queryString.filterElements('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
//=> 'https://foo.bar?bar=2#hello'
```

#### url

Type: `string`

The url containing the query parameters to filter.

#### includeParams

Type: `string[]`

The names of the query parameters to retain. All other query parameters will be removed from the url.

#### filter

Type: `(name, value) => boolean`

A filter predicate that will be provided the name of each query parameter and its value. The predicate will be provided with the `name` of the query parameter and its `value`. The `parseNumbers` and `parseBooleans` options affect `value`.

#### options

Type: `object`

[Parse options](#options) and [stringify options](#options-1).

## Nesting

This module intentionally doesn't support nesting as it's not spec'd and varies between implementations, which causes a lot of [edge cases](https://github.com/visionmedia/node-querystring/issues).
Expand Down
17 changes: 17 additions & 0 deletions test/filter-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test from 'ava';
import queryString from '..';

test('filters elements in a url with a filter array', t => {
t.is(queryString.filterElements('http://example.com/?a=1&b=2&c=3#a', ['a', 'b']), 'http://example.com/?a=1&b=2#a');
});

test('filters elements in a url with a filter predicate', t => {
t.is(queryString.filterElements('http://example.com/?a=1&b=2&c=3#a', (name, value) => {
t.is(typeof name, 'string');
t.is(typeof value, 'number');

return name === 'a';
}, {
parseNumbers: true
}), 'http://example.com/?a=1#a');
});