Skip to content
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 getQueryArg function to url package #10879

Closed
swissspidy opened this issue Oct 21, 2018 · 1 comment
Closed

Add getQueryArg function to url package #10879

swissspidy opened this issue Oct 21, 2018 · 1 comment
Labels
[Package] Url /packages/url [Type] Enhancement A suggestion for improvement.

Comments

@swissspidy
Copy link
Member

While working on #10862 I noticed that there's no easy way to retrieve a single query argument from a variable when using the @wordpress/url package.

Since that package already uses qs, it should be trivial to add such a function.

A similar PHP function is also proposed for WordPress core (https://core.trac.wordpress.org/ticket/34699), so we could get feature parity here.

The function could work like this:

import { getQueryArg } from '@wordpress/url';

const url = 'http://wp.org/wp-json/wp/v2/posts?_locale=foo';

const locale = getQueryArg( url, '_locale' ); // 'foo'
const unknown = getQueryArg( url, 'foo' ); // undefined

This way one doesn't have to import qs themselves or use some indexOf or regex trickery to get a query arg or to check for its sole presence.

Happy to work on a PR for that if such a function makes sense.

@swissspidy swissspidy added [Type] Enhancement A suggestion for improvement. [Package] Url /packages/url labels Oct 21, 2018
@swissspidy
Copy link
Member Author

As mentioned in #10862 (comment), this would make it very simple to add a hasQueryArg helper function:

function hasQueryArg( url, var ) {
  return typeof getQueryArg( url, var ) !== 'undefined';
}

swissspidy added a commit that referenced this issue Oct 21, 2018
gziolo pushed a commit that referenced this issue Oct 23, 2018
* Add some new url helper functions

See #10879.

* Document new functions in readme

* Improve docs

* Add another test for removeQueryArgs()
antpb pushed a commit to antpb/gutenberg that referenced this issue Oct 26, 2018
* Add some new url helper functions

See WordPress#10879.

* Document new functions in readme

* Improve docs

* Add another test for removeQueryArgs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Url /packages/url [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

1 participant