Repository navigation
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 |
| * @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.
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.
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.
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.
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.
| * 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.
This is not used as a spread parameter.
| /** | ||
| * Determines whether the URL contains a given query arg. | ||
| * | ||
| * @param {string} url URL |
There was a problem hiding this comment.
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.
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.
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.
| * 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.
This is used as a spread parameter.
| 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.
Example is wrong; needs second parameter.
aduth
left a comment
There was a problem hiding this comment.
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
getQueryArgandhasQueryArgI 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/urlpackage with new functionsgetQueryArg,hasQueryArg, andremoveQueryArgs,Checklist:
Fixes #10879.