-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 some new url helper functions #10885
Changes from 2 commits
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 |
---|---|---|
|
@@ -34,6 +34,51 @@ export function addQueryArgs( url, args ) { | |
return baseUrl + '?' + stringify( { ...query, ...args } ); | ||
} | ||
|
||
/** | ||
* Returns a single query argument of the url | ||
* | ||
* @param {string} url URL | ||
* @param {string} arg Query arg name | ||
* | ||
* @return {Array|string} Query arg value. | ||
*/ | ||
export function getQueryArg( url, arg ) { | ||
const queryStringIndex = url.indexOf( '?' ); | ||
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. Given repetition in the file, seems we could do for an internal utility function(s) which return the parsed query and/or return the query fragment from a 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. Yeah it's not really nice at the moment. I think we should address this in a following PR. 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. Or just use a proper URL parsers, like https://nodejs.org/docs/latest/api/url.html#url_url_search † I assume reason we haven't is due to size of the imported (stub) module. |
||
const query = queryStringIndex !== -1 ? parse( url.substr( queryStringIndex + 1 ) ) : {}; | ||
|
||
return query[ arg ]; | ||
} | ||
|
||
/** | ||
* Determines whether the URL contains a given query arg. | ||
* | ||
* @param {string} url 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. Per standards examples, we've avoided bothering to align the |
||
* @param {...string} arg Query arg name | ||
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. This is not used as a spread parameter. |
||
* | ||
* @return {boolean} Whether or not the URL contains the query aeg. | ||
*/ | ||
export function hasQueryArg( url, arg ) { | ||
return typeof getQueryArg( url, arg ) !== 'undefined'; | ||
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. FYI: Comparing to |
||
} | ||
|
||
/** | ||
* Removes arguments from the query string of the url | ||
* | ||
* @param {string} url URL | ||
* @param {string} args Query Args | ||
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. This is used as a spread parameter. |
||
* | ||
* @return {string} Updated URL | ||
*/ | ||
export function removeQueryArgs( url, ...args ) { | ||
const queryStringIndex = url.indexOf( '?' ); | ||
const query = queryStringIndex !== -1 ? parse( url.substr( queryStringIndex + 1 ) ) : {}; | ||
const baseUrl = queryStringIndex !== -1 ? url.substr( 0, queryStringIndex ) : url; | ||
|
||
args.forEach( ( arg ) => delete query[ arg ] ); | ||
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. Guess we've not brought in Lodash yet as a dependency of this one, but good use-case for 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. Yeah I looked into this one but since it's a one-liner it shouldn't be necessary to add a dependency just for this use case. |
||
|
||
return baseUrl + '?' + stringify( query ); | ||
} | ||
|
||
/** | ||
* Prepends "http://" to a url, if it looks like something that is meant to be a TLD. | ||
* | ||
|
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.
Example is wrong; needs second parameter.