-
-
Notifications
You must be signed in to change notification settings - Fork 453
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
Changes from 4 commits
8939d39
21a8477
b96de12
e287069
c217c58
3ebf1e6
54e4780
e76bd7b
ad0f653
a0de598
b7907b0
e661440
1a187e1
c424ee5
83449af
0a93fdb
00f4233
564d97c
75b42ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -206,3 +206,13 @@ export function stringify( | |||||
Extract a query string from a URL that can be passed into `.parse()`. | ||||||
*/ | ||||||
export function extract(url: string): string; | ||||||
|
||||||
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No many empty lines |
||||||
/** | ||||||
Stringify an object of URL and the query into a URL. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be useful with an example here. |
||||||
*/ | ||||||
export function stringifyUrl( | ||||||
input: ParsedUrl, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
options?: StringifyOptions | ||||||
): string; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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('?'); | ||
|
@@ -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(str => str).join('&'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use acronyms. |
||
if (queryString) { | ||
queryString = `?${queryString}`; | ||
} | ||
|
||
return `${url}${queryString}${hash}`; | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -221,6 +221,21 @@ queryString.parseUrl('https://foo.bar?foo=bar'); | |||||
//=> {url: 'https://foo.bar', query: {foo: 'bar'}} | ||||||
``` | ||||||
|
||||||
### .stringifyUrl(object, options?) | ||||||
|
||||||
The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before this, it should describe what it does. |
||||||
|
||||||
The `object` are the same as result of `.parseUrl` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's easier to just describe what it accepts specifically here. |
||||||
|
||||||
The `options` are the same as for `.stringify()`. | ||||||
|
||||||
Returns a URL with query string. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```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 | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||
import test from 'ava'; | ||||||
import queryString from '..'; | ||||||
|
||||||
test('stringify url not containing query string', t => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 query string', t => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // @komcal |
||||||
}); |
There was a problem hiding this comment.
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.