-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
Are there any places in the existing codebase which could benefit from using those new methods? Can we document those newly introduced methods in |
@gziolo Good input, updated the readme accordingly!
Perhaps there are more potential usages in Gutenberg, but it's tough to say after a quick glance. Looking at export function sortQueryArgs( url ) {
const queryStringIndex = url.indexOf( '?' );
const query = queryStringIndex !== -1 ? parse( url.substr( queryStringIndex + 1 ) ) : {};
const baseUrl = queryStringIndex !== -1 ? url.substr( 0, queryStringIndex ) : url;
return baseUrl + '?' + stringify( query, { sort: (a, b) => a.localeCompare( b ) } );
} |
I wonder if |
Indeed const requestContainsUnboundedQuery = ( options ) => {
const pathIsUnbounded = options.path && getQueryArg( options.path, 'per_page' ) === '-1';
const urlIsUnbounded = options.url && getQueryArg( options.url, 'per_page' ) === '-1';
return pathIsUnbounded || urlIsUnbounded;
}; It's actually safer than just using |
Let's make sure we open follow-up issue when this one lands. It should be labeled with |
packages/url/src/index.js
Outdated
* @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 comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: Comparing to undefined
directly should work just as well, and saves some characters. typeof
would be useful only if trying to reference a variable which hasn't been initialized, or if we were targeting pre-ES5 browsers (IE7) where undefined
could be overwritten.
* @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 comment
The 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 comment
The 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.
* @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 comment
The reason will be displayed to describe this comment to others. Learn more.
Or just use a proper URL parsers, like require( 'url' ).parse( url ).search
†
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.
packages/url/src/index.js
Outdated
* Determines whether the URL contains a given query arg. | ||
* | ||
* @param {string} url URL | ||
* @param {...string} arg Query arg name |
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.
This is not used as a spread parameter.
packages/url/src/index.js
Outdated
/** | ||
* 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Per standards examples, we've avoided bothering to align the @param
type with @return
.
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 comment
The 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 omit
.
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.
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.
packages/url/src/index.js
Outdated
* 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 comment
The reason will be displayed to describe this comment to others. Learn more.
This is used as a spread parameter.
packages/url/README.md
Outdated
@@ -25,6 +25,15 @@ const newURL = addQueryArgs( 'https://google.com', { q: 'test' } ); // https:// | |||
|
|||
// Prepends 'http://' to URLs that are probably mean to have them | |||
const actualURL = prependHTTP( 'wordpress.org' ); // http://wordpress.org | |||
|
|||
// Gets a single query arg from the given URL. | |||
const foo = getQueryArg( 'https://wordpress.org?foo=bar&bar=baz' ); // bar |
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.
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.
Nice utilities 👍
Meta-note: As best I can tell, this closes #10879, but we've not included magic keywords to automate the closure.
|
Oh, I missed it there at the bottom 👍 |
Oh, you changed it! 🤦♂️ |
It worked out 👍 Thanks for addressing my feedback 💯 |
…rnmobile/fix-validation-errorrs-after-block-splitting * 'master' of https://github.com/WordPress/gutenberg: Add some new url helper functions (#10885) Components: Avoid SlotFill props destructuring (#10921) Update Package Changelogs (#10887) chore(release): publish Update plugin version to 4.1 RC2. (#10893)
* Add user locale api-fetch middleware See #10811 * Don't modify existing _locale query arg * Add changelog entry * Update changelog * Use new hasQueryArg helper function See #10885. * Match on simply the route path, instead of malformed query param * Fully mock embedding test * Skip the embedding test until we can make the travis env mock the requests * Skip demo test too, for now
* Add some new url helper functions See WordPress#10879. * Document new functions in readme * Improve docs * Add another test for removeQueryArgs()
* Add user locale api-fetch middleware See WordPress#10811 * Don't modify existing _locale query arg * Add changelog entry * Update changelog * Use new hasQueryArg helper function See WordPress#10885. * Match on simply the route path, instead of malformed query param * Fully mock embedding test * Skip the embedding test until we can make the travis env mock the requests * Skip demo test too, for now
Description
As discussed in #10862 and outlined in #10879, some more URL modification helpers might make sense. I figured when I add
getQueryArg
andhasQueryArg
I might just as well complete the group withremoveQueryArgs
.How has this been tested?
I included a few unit tests for each of the new functions.
Screenshots
Types of changes
Extends
@wordpress/url
package with new functionsgetQueryArg
,hasQueryArg
, andremoveQueryArgs
,Checklist:
Fixes #10879.