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 .stringifyUrl() method #217

Merged
merged 19 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 11 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
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,17 @@ export function stringify(
Extract a query string from a URL that can be passed into `.parse()`.
*/
export function extract(url: string): string;

/**
Stringify an object into a URL with a query string and sorting the keys.

Copy link
Owner

Choose a reason for hiding this comment

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

You need to document the merging behavior here.

@example
```
queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
// => 'https://foo.bar?foo=bar'
```
*/
export function stringifyUrl(
input: ParsedUrl,
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
input: ParsedUrl,
object: ParsedUrl,

options?: StringifyOptions
): string;
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ function removeHash(input) {
return input;
}

function getHash(url) {
let hash = '';
const hashStart = url.indexOf('#');
if (hashStart !== -1) {
hash = url.slice(hashStart);
}

return hash;
}

function extract(input) {
input = removeHash(input);
const queryStart = input.indexOf('?');
Expand Down Expand Up @@ -290,3 +300,16 @@ exports.parseUrl = (input, options) => {
query: parse(extract(input), options)
};
};

exports.stringifyUrl = (input, options) => {
const url = removeHash(input.url).split('?')[0] || '';
const queryFromUrl = this.extract(input.url);
const hash = getHash(input.url);
const stringifyQuery = this.stringify(input.query, options);
let queryString = [queryFromUrl, stringifyQuery].filter(string => string).join('&');
if (queryString) {
queryString = `?${queryString}`;
}

return `${url}${queryString}${hash}`;
};
29 changes: 29 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,35 @@ queryString.parseUrl('https://foo.bar?foo=bar');
//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
```

### .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)

#### object

Type: `object`

##### url

Type: `string`

A URL that will be merge with query object

##### query

Type: `object`

A query object that will be merge with URL

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

Returns a URL with a query string.

```js
queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
// => 'https://foo.bar?foo=bar'
```
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved


## Nesting

Expand Down
35 changes: 35 additions & 0 deletions test/stringify-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import test from 'ava';
import queryString from '..';

test('stringify URL without a query string', t => {
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/'}), 'https://foo.bar/');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/', query: {}}), 'https://foo.bar/');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/#top', query: {}}), 'https://foo.bar/#top');
t.deepEqual(queryString.stringifyUrl({url: '', query: {}}), '');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?', query: {}}), 'https://foo.bar');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?foo=bar', query: {}}), 'https://foo.bar?foo=bar');
});

test('stringify URL with a query string', t => {
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}}), 'https://foo.bar?foo=bar');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?', query: {foo: 'bar'}}), 'https://foo.bar?foo=bar');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/#top', query: {foo: 'bar'}}), 'https://foo.bar/?foo=bar#top');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar', a: 'b'}}), 'https://foo.bar?a=b&foo=bar');
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?', query: {foo: ['bar', 'baz']}}), 'https://foo.bar?foo=bar&foo=baz');
Copy link
Owner

Choose a reason for hiding this comment

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

Needs a test to show what happens if the URL already has a query string and a query string is specified.

Also add some tests that does parseUrl and then pass the result to stringifyUrl to make sure they are compatible.

Copy link
Owner

Choose a reason for hiding this comment

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

// @komcal

t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}}), 'https://foo.bar?foo=baz&foo=bar');
Copy link
Owner

Choose a reason for hiding this comment

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

This would be surprising behavior for me. Adding another query item here with the same name actually changes the semantics as now the result would be an array instead of a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sindresorhus you think the result would be https://foo.bar?foo[]=bas&foo[]=bar? I'm not sure what the answer would be.

Copy link
Owner

Choose a reason for hiding this comment

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

No, that would be completely wrong as it's a different arrayFormat format.

I'm saying that it should replace instead: 'https://foo.bar?foo=bar'. The behavior should be clearly documented.

});

test('stringify URL from the result of `parseUrl` without query string', t => {
const parsedUrl = queryString.parseUrl('https://foo.bar#top');
t.deepEqual(queryString.stringifyUrl(parsedUrl), 'https://foo.bar');
});

test('stringify URL from the result of `parseUrl` with query string', t => {
const parsedUrl = queryString.parseUrl('https://foo.bar?foo=bar&foo=baz#top');
t.deepEqual(queryString.stringifyUrl(parsedUrl), 'https://foo.bar?foo=bar&foo=baz');
});

test('stringify URL from the result of `parseUrl` with query string that contains `=`', t => {
const parsedUrl = queryString.parseUrl('https://foo.bar?foo=bar=&foo=baz=');
t.deepEqual(queryString.stringifyUrl(parsedUrl, {encode: false}), 'https://foo.bar?foo=bar=&foo=baz=');
});