-
-
Notifications
You must be signed in to change notification settings - Fork 452
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
sindresorhus
merged 19 commits into
sindresorhus:master
from
komkanit:stringify-url-method
Jan 16, 2020
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8939d39
add stringifyUrl method
komkanit 21a8477
add more test
komkanit b96de12
add type
komkanit e287069
add document of stringifyUrl method
komkanit c217c58
add stringifyUrl example
komkanit 3ebf1e6
remove acronyms word
komkanit 54e4780
update stringifyUrl document
komkanit e76bd7b
add test about the URL already has a query string
komkanit ad0f653
add test stringify URL from result of parseUrl
komkanit a0de598
copy stringifyUrl description from readme
komkanit b7907b0
Update stringify-url.js
sindresorhus e661440
change param name
komkanit 1a187e1
fix test case query should be replace instead
komkanit c424ee5
update test result of parseUrl
komkanit 83449af
sync document with readme
komkanit 0a93fdb
add document about the merging behavior
komkanit 00f4233
Update index.d.ts
sindresorhus 564d97c
Update readme.md
sindresorhus 75b42ca
Update index.js
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,3 +206,22 @@ 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. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options) | ||
|
||
Query items in the `query` property overrides queries in the `url` property. | ||
|
||
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. You need to document the merging behavior here. |
||
@example | ||
``` | ||
queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}}); | ||
//=> 'https://foo.bar?foo=bar' | ||
|
||
queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}}); | ||
//=> 'https://foo.bar?foo=bar' | ||
``` | ||
*/ | ||
export function stringifyUrl( | ||
object: ParsedUrl, | ||
options?: StringifyOptions | ||
): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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?a=b', query: {foo: ['bar', 'baz']}}), 'https://foo.bar?a=b&foo=bar&foo=baz'); | ||
t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}}), 'https://foo.bar?foo=bar'); | ||
}); | ||
|
||
test('stringify URL from the result of `parseUrl` without query string', t => { | ||
const url = 'https://foo.bar'; | ||
const parsedUrl = queryString.parseUrl(url); | ||
t.deepEqual(queryString.stringifyUrl(parsedUrl), url); | ||
}); | ||
|
||
test('stringify URL from the result of `parseUrl` with query string', t => { | ||
const url = 'https://foo.bar?foo=bar&foo=baz'; | ||
const parsedUrl = queryString.parseUrl(url); | ||
t.deepEqual(queryString.stringifyUrl(parsedUrl), url); | ||
}); | ||
|
||
test('stringify URL from the result of `parseUrl` with query string that contains `=`', t => { | ||
const url = 'https://foo.bar?foo=bar=&foo=baz='; | ||
const parsedUrl = queryString.parseUrl(url); | ||
t.deepEqual(queryString.stringifyUrl(parsedUrl, {encode: false}), url); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
No many empty lines