Skip to content

Commit

Permalink
Add prependHTTP() to @wordpress/url (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
pento authored Apr 25, 2018
1 parent aac5139 commit 510cd3e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ npm install @wordpress/url --save
## Usage

```JS
importaddQueryArgs } from '@wordpress/url';
importaddQueryArgs, prependHTTP } from '@wordpress/url';

// Appends arguments to the query string of a given url
const newUrl = addQueryArgs( 'https://google.com', { q: 'test' } ); // https://google.com/?q=test

// Prepends 'http://' to URLs that are probably mean to have them
const actualUrl = prependHTTP( 'wordpress.org' ); // http://wordpress.org
```

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
18 changes: 18 additions & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import { parse, format } from 'url';
import { parse as parseQueryString, stringify } from 'querystring';

const EMAIL_REGEXP = /^(mailto:)?[a-z0-9._%+-]+@[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}$/i;
const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i;

/**
* Appends arguments to the query string of the url
*
Expand All @@ -19,3 +22,18 @@ export function addQueryArgs( url, args ) {

return format( { ...parsedURL, query } );
}

/**
* Prepends "http://" to a url, if it looks like something that is meant to be a TLD.
*
* @param {String} url The URL to test
*
* @return {String} The updated URL
*/
export function prependHTTP( url ) {
if ( ! USABLE_HREF_REGEXP.test( url ) && ! EMAIL_REGEXP.test( url ) ) {
return 'http://' + url;
}

return url;
}
58 changes: 57 additions & 1 deletion packages/url/src/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal Dependencies
*/
import { addQueryArgs } from '../';
import { addQueryArgs, prependHTTP } from '../';

describe( 'addQueryArgs', () => {
test( 'should append args to an URL without query string', () => {
Expand All @@ -25,3 +25,59 @@ describe( 'addQueryArgs', () => {
expect( addQueryArgs( url, args ) ).toBe( 'https://andalouses.example/beach?night=false&sun=true&sand=false' );
} );
} );

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

expect ( prependHTTP( url ) ).toBe( 'http://' + url );
} );

test( "shouldn't prepend http to an email", () => {
const url = 'foo@wordpress.org';

expect ( prependHTTP( url ) ).toBe( url );
} );

test( "shouldn't prepend http to an absolute URL", () => {
const url = '/wordpress';

expect ( prependHTTP( url ) ).toBe( url );
} );

test( "shouldn't prepend http to a relative URL", () => {
const url = './wordpress';

expect ( prependHTTP( url ) ).toBe( url );
} );

test( "shouldn't prepend http to an anchor URL", () => {
const url = '#wordpress';

expect ( prependHTTP( url ) ).toBe( url );
} );

test( "shouldn't prepend http to a URL that already has http", () => {
const url = 'http://wordpress.org';

expect ( prependHTTP( url ) ).toBe( url );
} );

test( "shouldn't prepend http to a URL that already has https", () => {
const url = 'https://wordpress.org';

expect ( prependHTTP( url ) ).toBe( url );
} );

test( "shouldn't prepend http to a URL that already has ftp", () => {
const url = 'ftp://wordpress.org';

expect ( prependHTTP( url ) ).toBe( url );
} );

test( "shouldn't prepend http to a URL that already has mailto", () => {
const url = 'mailto:foo@wordpress.org';

expect ( prependHTTP( url ) ).toBe( url );
} );
} );

0 comments on commit 510cd3e

Please sign in to comment.