Skip to content

Commit

Permalink
Split @wordpress/url into modules. (#18689)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomes authored Dec 2, 2019
1 parent 50f556f commit b59ef1e
Show file tree
Hide file tree
Showing 21 changed files with 457 additions and 434 deletions.
44 changes: 44 additions & 0 deletions packages/url/src/add-query-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* External dependencies
*/
import { parse, stringify } from 'qs';

/**
* Appends arguments as querystring to the provided URL. If the URL already
* includes query arguments, the arguments are merged with (and take precedent
* over) the existing set.
*
* @param {string} [url=''] URL to which arguments should be appended. If omitted,
* only the resulting querystring is returned.
* @param {Object} args Query arguments to apply to URL.
*
* @example
* ```js
* const newURL = addQueryArgs( 'https://google.com', { q: 'test' } ); // https://google.com/?q=test
* ```
*
* @return {string} URL with arguments applied.
*/
export function addQueryArgs( url = '', args ) {
// If no arguments are to be appended, return original URL.
if ( ! args || ! Object.keys( args ).length ) {
return url;
}

let baseUrl = url;

// Determine whether URL already had query arguments.
const queryStringIndex = url.indexOf( '?' );
if ( queryStringIndex !== -1 ) {
// Merge into existing query arguments.
args = Object.assign(
parse( url.substr( queryStringIndex + 1 ) ),
args
);

// Change working base URL to omit previous query arguments.
baseUrl = baseUrl.substr( 0, queryStringIndex );
}

return baseUrl + '?' + stringify( args );
}
23 changes: 23 additions & 0 deletions packages/url/src/filter-url-for-display.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Returns a URL for display.
*
* @param {string} url Original URL.
*
* @example
* ```js
* const displayUrl = filterURLForDisplay( 'https://www.wordpress.org/gutenberg/' ); // wordpress.org/gutenberg
* ```
*
* @return {string} Displayed URL.
*/
export function filterURLForDisplay( url ) {
// Remove protocol and www prefixes.
const filteredURL = url.replace( /^(?:https?:)\/\/(?:www\.)?/, '' );

// Ends with / and only has that single slash, strip it.
if ( filteredURL.match( /^[^\/]+\/$/ ) ) {
return filteredURL.replace( '/', '' );
}

return filteredURL;
}
19 changes: 19 additions & 0 deletions packages/url/src/get-authority.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Returns the authority part of the URL.
*
* @param {string} url The full URL.
*
* @example
* ```js
* const authority1 = getAuthority( 'https://wordpress.org/help/' ); // 'wordpress.org'
* const authority2 = getAuthority( 'https://localhost:8080/test/' ); // 'localhost:8080'
* ```
*
* @return {string|void} The authority part of the URL.
*/
export function getAuthority( url ) {
const matches = /^[^\/\s:]+:(?:\/\/)?\/?([^\/\s#?]+)[\/#?]{0,1}\S*$/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
19 changes: 19 additions & 0 deletions packages/url/src/get-fragment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Returns the fragment part of the URL.
*
* @param {string} url The full URL
*
* @example
* ```js
* const fragment1 = getFragment( 'http://localhost:8080/this/is/a/test?query=true#fragment' ); // '#fragment'
* const fragment2 = getFragment( 'https://wordpress.org#another-fragment?query=true' ); // '#another-fragment'
* ```
*
* @return {string|void} The fragment part of the URL.
*/
export function getFragment( url ) {
const matches = /^\S+?(#[^\s\?]*)/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
19 changes: 19 additions & 0 deletions packages/url/src/get-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Returns the path part of the URL.
*
* @param {string} url The full URL.
*
* @example
* ```js
* const path1 = getPath( 'http://localhost:8080/this/is/a/test?query=true' ); // 'this/is/a/test'
* const path2 = getPath( 'https://wordpress.org/help/faq/' ); // 'help/faq'
* ```
*
* @return {string|void} The path part of the URL.
*/
export function getPath( url ) {
const matches = /^[^\/\s:]+:(?:\/\/)?[^\/\s#?]+[\/]([^\s#?]+)[#?]{0,1}\S*$/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
19 changes: 19 additions & 0 deletions packages/url/src/get-protocol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Returns the protocol part of the URL.
*
* @param {string} url The full URL.
*
* @example
* ```js
* const protocol1 = getProtocol( 'tel:012345678' ); // 'tel:'
* const protocol2 = getProtocol( 'https://wordpress.org' ); // 'https:'
* ```
*
* @return {string|void} The protocol part of the URL.
*/
export function getProtocol( url ) {
const matches = /^([^\s:]+:)/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
32 changes: 32 additions & 0 deletions packages/url/src/get-query-arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* External dependencies
*/
import { parse } from 'qs';

/**
* @typedef {{[key: string]: QueryArgParsed}} QueryArgObject
*/

/**
* @typedef {string|string[]|QueryArgObject} QueryArgParsed
*/

/**
* Returns a single query argument of the url
*
* @param {string} url URL.
* @param {string} arg Query arg name.
*
* @example
* ```js
* const foo = getQueryArg( 'https://wordpress.org?foo=bar&bar=baz', 'foo' ); // bar
* ```
*
* @return {QueryArgParsed|undefined} Query arg value.
*/
export function getQueryArg( url, arg ) {
const queryStringIndex = url.indexOf( '?' );
const query = queryStringIndex !== -1 ? parse( url.substr( queryStringIndex + 1 ) ) : {};

return query[ arg ];
}
19 changes: 19 additions & 0 deletions packages/url/src/get-query-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Returns the query string part of the URL.
*
* @param {string} url The full URL.
*
* @example
* ```js
* const queryString1 = getQueryString( 'http://localhost:8080/this/is/a/test?query=true#fragment' ); // 'query=true'
* const queryString2 = getQueryString( 'https://wordpress.org#fragment?query=false&search=hello' ); // 'query=false&search=hello'
* ```
*
* @return {string|void} The query string part of the URL.
*/
export function getQueryString( url ) {
const matches = /^\S+?\?([^\s#]+)/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}
21 changes: 21 additions & 0 deletions packages/url/src/has-query-arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Internal dependencies
*/
import { getQueryArg } from './get-query-arg';

/**
* Determines whether the URL contains a given query arg.
*
* @param {string} url URL.
* @param {string} arg Query arg name.
*
* @example
* ```js
* const hasBar = hasQueryArg( 'https://wordpress.org?foo=bar&bar=baz', 'bar' ); // true
* ```
*
* @return {boolean} Whether or not the URL contains the query arg.
*/
export function hasQueryArg( url, arg ) {
return getQueryArg( url, arg ) !== undefined;
}
Loading

0 comments on commit b59ef1e

Please sign in to comment.