Skip to content

Commit

Permalink
esmodules: Update lib/url (Automattic#21140)
Browse files Browse the repository at this point in the history
@see: https://github.com/Automattic/wp-calypso/milestone/224

Previously we were exporting a default object of multiple methods and
importing those through the non-spec-compliant Babel destructuring
import statements.

This patch makes those methods proper named exports in the work of
turning off CommonJS compilation.
  • Loading branch information
dmsnell authored Jan 16, 2018
1 parent 4a7506d commit 817a8d6
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions client/lib/url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { has, isString, omit, startsWith } from 'lodash';
* Internal dependencies
*/
import config from 'config';
import { addQueryArgs } from 'lib/route';
import { isLegacyRoute } from 'lib/route/legacy-routes';

export { addQueryArgs } from 'lib/route';

/**
* Check if a URL is located outside of Calypso.
* Note that the check this function implements is incomplete --
Expand All @@ -21,11 +22,11 @@ import { isLegacyRoute } from 'lib/route/legacy-routes';
* @param {string} url - URL to check
* @return {bool} true if the given URL is located outside of Calypso
*/
function isOutsideCalypso( url ) {
export function isOutsideCalypso( url ) {
return url && ( startsWith( url, '//' ) || ! startsWith( url, '/' ) );
}

function isExternal( url ) {
export function isExternal( url ) {
// parseURL will return hostname = null if no protocol or double-slashes
// the url passed in might be of form `en.support.wordpress.com`
// so for this function we'll append double-slashes to fake it
Expand Down Expand Up @@ -59,7 +60,7 @@ function isExternal( url ) {
return hostname !== config( 'hostname' );
}

function isHttps( url ) {
export function isHttps( url ) {
return url && startsWith( url, 'https://' );
}

Expand All @@ -71,7 +72,7 @@ const urlWithoutHttpRegex = /^https?:\/\//;
* @param {String} url The URL to remove http(s) from
* @return {?String} URL without the initial http(s)
*/
function withoutHttp( url ) {
export function withoutHttp( url ) {
if ( url === '' ) {
return '';
}
Expand All @@ -83,14 +84,14 @@ function withoutHttp( url ) {
return url.replace( urlWithoutHttpRegex, '' );
}

function addSchemeIfMissing( url, scheme ) {
export function addSchemeIfMissing( url, scheme ) {
if ( false === schemeRegex.test( url ) ) {
return scheme + '://' + url;
}
return url;
}

function setUrlScheme( url, scheme ) {
export function setUrlScheme( url, scheme ) {
const schemeWithSlashes = scheme + '://';
if ( startsWith( url, schemeWithSlashes ) ) {
return url;
Expand All @@ -104,7 +105,7 @@ function setUrlScheme( url, scheme ) {
return url.replace( schemeRegex, schemeWithSlashes );
}

function urlToSlug( url ) {
export function urlToSlug( url ) {
if ( ! url ) {
return null;
}
Expand All @@ -120,7 +121,7 @@ function urlToSlug( url ) {
* @param {String} urlToConvert The URL to convert
* @return {String} The URL's domain and path
*/
function urlToDomainAndPath( urlToConvert ) {
export function urlToDomainAndPath( urlToConvert ) {
return withoutHttp( urlToConvert ).replace( /\/$/, '' );
}

Expand All @@ -133,7 +134,7 @@ function urlToDomainAndPath( urlToConvert ) {
* @param {String} query The string to check
* @return {Boolean} Does it appear to be a URL?
*/
function resemblesUrl( query ) {
export function resemblesUrl( query ) {
if ( ! query ) {
return false;
}
Expand Down Expand Up @@ -170,7 +171,7 @@ function resemblesUrl( query ) {
* @param {Array|String} paramsToOmit The collection of params or single param to reject
* @return {String} Url less the omitted params.
*/
function omitUrlParams( url, paramsToOmit ) {
export function omitUrlParams( url, paramsToOmit ) {
if ( ! url ) {
return null;
}
Expand All @@ -188,7 +189,7 @@ function omitUrlParams( url, paramsToOmit ) {
* @param {String} encodedURI URI to attempt to decode
* @return {String} Decoded URI (or passed in value on error)
*/
function decodeURIIfValid( encodedURI ) {
export function decodeURIIfValid( encodedURI ) {
if ( ! ( isString( encodedURI ) || has( encodedURI, 'toString' ) ) ) {
return '';
}
Expand All @@ -205,7 +206,7 @@ function decodeURIIfValid( encodedURI ) {
* @param {String} encodedURIComponent URI component to attempt to decode
* @return {String} Decoded URI component (or passed in value on error)
*/
function decodeURIComponentIfValid( encodedURIComponent ) {
export function decodeURIComponentIfValid( encodedURIComponent ) {
if ( ! ( isString( encodedURIComponent ) || has( encodedURIComponent, 'toString' ) ) ) {
return '';
}
Expand All @@ -215,20 +216,3 @@ function decodeURIComponentIfValid( encodedURIComponent ) {
return encodedURIComponent;
}
}

export default {
decodeURIIfValid,
decodeURIComponentIfValid,
isOutsideCalypso,
isExternal,
isHttps,
withoutHttp,
addSchemeIfMissing,
setUrlScheme,
urlToSlug,
urlToDomainAndPath,
// [TODO]: Move lib/route/add-query-args contents here
addQueryArgs,
resemblesUrl,
omitUrlParams,
};

0 comments on commit 817a8d6

Please sign in to comment.