-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split @wordpress/url into modules. (#18689)
- Loading branch information
Showing
21 changed files
with
457 additions
and
434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.