Skip to content

Commit

Permalink
Add some new url helper functions
Browse files Browse the repository at this point in the history
See #10879.
  • Loading branch information
swissspidy committed Oct 21, 2018
1 parent dee3dcf commit f136bfb
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/url/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.2.0 (Unreleased)

### Features

- Added `getQueryArg`.
- Added `hasQueryArg`.
- Added `removeQueryArgs`.

## 2.1.0 (2018-10-16)

### Features
Expand Down
45 changes: 45 additions & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( '?' );
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
* @param {...string} arg Query arg name
*
* @return {boolean} Whether or not the URL contains the query aeg.
*/
export function hasQueryArg( url, arg ) {
return typeof getQueryArg( url, arg ) !== 'undefined';
}

/**
* Removes arguments from the query string of the url
*
* @param {string} url URL
* @param {string} args Query Args
*
* @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 ] );

return baseUrl + '?' + stringify( query );
}

/**
* Prepends "http://" to a url, if it looks like something that is meant to be a TLD.
*
Expand Down
57 changes: 57 additions & 0 deletions packages/url/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { every } from 'lodash';
import {
isURL,
addQueryArgs,
getQueryArg,
hasQueryArg,
removeQueryArgs,
prependHTTP,
safeDecodeURI,
} from '../';
Expand Down Expand Up @@ -69,6 +72,60 @@ describe( 'addQueryArgs', () => {
} );
} );

describe( 'getQueryArg', () => {
it( 'should get the value of an existing query arg', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';

expect( getQueryArg( url, 'foo' ) ).toBe( 'bar' );
} );

it( 'should not return a value of an unknown query arg', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';

expect( getQueryArg( url, 'baz' ) ).toBeUndefined();
} );

it( 'should get the value of an arry query arg', () => {
const url = 'https://andalouses.example/beach?foo[]=bar&foo[]=baz';

expect( getQueryArg( url, 'foo' ) ).toEqual( [ 'bar', 'baz' ] );
} );
} );

describe( 'hasQueryArg', () => {
it( 'should return true for an existing query arg', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';

expect( hasQueryArg( url, 'foo' ) ).toBeTruthy();
} );

it( 'should return false for an unknown query arg', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';

expect( hasQueryArg( url, 'baz' ) ).toBeFalsy();
} );

it( 'should return true for an arry query arg', () => {
const url = 'https://andalouses.example/beach?foo[]=bar&foo[]=baz';

expect( hasQueryArg( url, 'foo' ) ).toBeTruthy();
} );
} );

describe( 'removeQueryArgs', () => {
it( 'should not change URL not containing query args', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';

expect( removeQueryArgs( url, 'baz', 'test' ) ).toEqual( url );
} );

it( 'should remove existing query args', () => {
const url = 'https://andalouses.example/beach?foo=bar&baz=foo&bar=baz';

expect( removeQueryArgs( url, 'foo', 'bar' ) ).toEqual( 'https://andalouses.example/beach?baz=foo' );
} );
} );

describe( 'prependHTTP', () => {
it( 'should prepend http to a domain', () => {
const url = 'wordpress.org';
Expand Down

0 comments on commit f136bfb

Please sign in to comment.