Skip to content
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

Add support for parsing/stringifying fragment identifier #222

Merged
merged 24 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
41 changes: 41 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ export interface ParseOptions {
```
*/
readonly parseBooleans?: boolean;

/**
Parse the fragment identifier from the URL.

@default false

@example
```
import queryString = require('query-string');

queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
```
*/
readonly parseFragmentIdentifier?: boolean;
}

export interface ParsedQuery<T = string> {
Expand All @@ -142,6 +157,11 @@ export function parse(query: string, options?: ParseOptions): ParsedQuery;
export interface ParsedUrl {
readonly url: string;
readonly query: ParsedQuery;

/**
The fragment identifier of the URL.
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
*/
readonly fragmentIdentifier?: string;
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -310,6 +330,27 @@ export interface StringifyOptions {
```
*/
readonly skipEmptyString?: boolean;

/**
Adds the fragment identifier to the URL.
Mark1626 marked this conversation as resolved.
Show resolved Hide resolved

@default false

@example
```
import queryString = require('query-string');

queryString.stringifyUrl({
url: 'https://foo.bar',
query: {top: 'foo'},
fragmentIdentifier: 'bar'
}, {
parseFragmentIdentifier: true
Mark1626 marked this conversation as resolved.
Show resolved Hide resolved
});
//=> 'https://foo.bar?top=foo#bar'
```
*/
readonly parseFragmentIdentifier?: boolean;
}

/**
Expand Down
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,22 +338,30 @@ exports.stringify = (object, options) => {
};

exports.parseUrl = (input, options) => {
return {
url: removeHash(input).split('?')[0] || '',
query: parse(extract(input), options)
};
const [url, hash] = splitOnFirst(input, '#');
return Object.assign(
{
url: url.split('?')[0] || '',
query: parse(extract(input), options)
},
options && options.parseFragmentIdentifier && hash ? {fragmentIdentifier: hash} : {}
);
};

exports.stringifyUrl = (input, options) => {
const url = removeHash(input.url).split('?')[0] || '';
const queryFromUrl = exports.extract(input.url);
const parsedQueryFromUrl = exports.parse(queryFromUrl);
const hash = getHash(input.url);
let hash = getHash(input.url);
const query = Object.assign(parsedQueryFromUrl, input.query);
let queryString = exports.stringify(query, options);
if (queryString) {
queryString = `?${queryString}`;
}

if (options && options.parseFragmentIdentifier && input.fragmentIdentifier) {
hash = `#${input.fragmentIdentifier}`;
Mark1626 marked this conversation as resolved.
Show resolved Hide resolved
}

return `${url}${queryString}${hash}`;
};
3 changes: 3 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ expectType<queryString.ParsedUrl>(
expectType<queryString.ParsedUrl>(
queryString.parseUrl('?foo=true', {parseBooleans: true})
);
expectType<queryString.ParsedUrl>(
queryString.parseUrl('?foo=true#bar', {parseFragmentIdentifier: true})
);

// Extract
expectType<string>(queryString.extract('http://foo.bar/?abc=def&hij=klm'));
44 changes: 41 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,6 @@ Note: This behaviour can be changed with the `skipNull` option.

Extract the URL and the query string as an object.

The `options` are the same as for `.parse()`.

Returns an object with a `url` and `query` property.

```js
Expand All @@ -350,6 +348,28 @@ queryString.parseUrl('https://foo.bar?foo=bar');
//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
```

#### options

Type: `object`

The `options` are the same as for `.parse()`.

Extra options are as below
Mark1626 marked this conversation as resolved.
Show resolved Hide resolved

##### parseFragmentIdentifier
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

Parse the fragment identifier from the URL.

Type: `boolean`\
Default: `false`

```js
const queryString = require('query-string');

queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
```

### .stringifyUrl(object, options?)

Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
Expand All @@ -368,7 +388,7 @@ queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
//=> 'https://foo.bar?foo=bar'
```

#### object
#### options

Type: `object`

Expand All @@ -384,6 +404,24 @@ Type: `object`

Query items to add to the URL.

##### parseFragmentIdentifier

Adds the fragment identifier to the URL.

Type: `boolean`\
Default: `false`

```js
queryString.stringifyUrl({
url: 'https://foo.bar',
query: {top: 'foo'},
fragmentIdentifier: 'bar'
}, {
parseFragmentIdentifier: true
});
//=> 'https://foo.bar?top=foo#bar'
```

## 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
6 changes: 6 additions & 0 deletions test/parse-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ test('handles strings with query string that contain =', t => {
t.deepEqual(queryString.parseUrl('https://foo.bar?foo=bar=&foo=baz='), {url: 'https://foo.bar', query: {foo: ['bar=', 'baz=']}});
});

test('handles strings with fragment identifier', t => {
t.deepEqual(queryString.parseUrl('https://foo.bar?top=foo#bar', {parseFragmentIdentifier: true}), {url: 'https://foo.bar', query: {top: 'foo'}, fragmentIdentifier: 'bar'});
t.deepEqual(queryString.parseUrl('https://foo.bar?foo=bar&foo=baz#top', {parseFragmentIdentifier: true}), {url: 'https://foo.bar', query: {foo: ['bar', 'baz']}, fragmentIdentifier: 'top'});
t.deepEqual(queryString.parseUrl('https://foo.bar/#top', {parseFragmentIdentifier: true}), {url: 'https://foo.bar/', query: {}, fragmentIdentifier: 'top'});
});

test('throws for invalid values', t => {
t.throws(() => {
queryString.parseUrl(null);
Expand Down
9 changes: 9 additions & 0 deletions test/stringify-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ test('stringify URL with a query string', t => {
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}}), 'https://foo.bar?foo=bar');
});

test('stringify URL with fragment identifier', t => {
const config = {parseFragmentIdentifier: true};
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {top: 'foo'}, fragmentIdentifier: 'bar'}, config), 'https://foo.bar?top=foo#bar');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: ['bar', 'baz']}, fragmentIdentifier: 'top'}, config), 'https://foo.bar?foo=bar&foo=baz#top');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/', query: {}, fragmentIdentifier: 'top'}, config), 'https://foo.bar/#top');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/#abc', query: {}, fragmentIdentifier: 'top'}, config), 'https://foo.bar/#top');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {}}, config), 'https://foo.bar');
});

test('skipEmptyString:: stringify URL with a query string', t => {
const config = {skipEmptyString: true};

Expand Down