-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utils: adding a
addQueryArgs
URL utils and using the built-in URL m…
…odule for that
- Loading branch information
1 parent
298fe0d
commit 0006673
Showing
6 changed files
with
55 additions
and
22 deletions.
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
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,25 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { addQueryArgs } from '../url'; | ||
|
||
describe( 'addQueryArgs', () => { | ||
it( 'should append args to an URL without query string', () => { | ||
const url = 'https://andalouses.com/beach'; | ||
const args = { sun: 'true', sand: 'false' }; | ||
|
||
expect( addQueryArgs( url, args ) ).to.eql( 'https://andalouses.com/beach?sun=true&sand=false' ); | ||
} ); | ||
|
||
it( 'should append args to an URL with query string', () => { | ||
const url = 'https://andalouses.com/beach?night=false'; | ||
const args = { sun: 'true', sand: 'false' }; | ||
|
||
expect( addQueryArgs( url, args ) ).to.eql( 'https://andalouses.com/beach?night=false&sun=true&sand=false' ); | ||
} ); | ||
} ); |
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,20 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { parse, format } from 'url'; | ||
|
||
/** | ||
* Appends arguments to the query string of the url | ||
* | ||
* @param {String} url URL | ||
* @param {Object} args Query Args | ||
* | ||
* @return {String} Updated URL | ||
*/ | ||
export function addQueryArgs( url, args ) { | ||
const parsedUrl = parse( url, true ); | ||
const query = { ...parsedUrl.query, ...args }; | ||
delete parsedUrl.search; | ||
|
||
return format( { ...parsedUrl, query } ); | ||
} |
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