Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Fix isURL #20172

Merged
merged 6 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/url/src/is-url.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const URL_REGEXP = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i;

/**
* Determines whether the given string looks like a URL.
*
* @param {string} url The string to scrutinise.
*
* @example
* ```js
* const isURL = isURL( 'https://wordpress.org' ); // true
* ```
*
* @return {boolean} Whether or not it looks like a URL.
*/
export function isURL( url ) {
const match = url.match( URL_REGEXP );
return match !== null && match.length >= 1 && match[ 0 ] === url;
etoledom marked this conversation as resolved.
Show resolved Hide resolved
}
33 changes: 33 additions & 0 deletions packages/url/src/test/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
etoledom marked this conversation as resolved.
Show resolved Hide resolved
* Internal dependencies
*/
import { isURL } from '../';

describe( 'isURL', () => {
it.each( [
[ 'http://wordpress.org' ],
[ 'https://wordpress.org' ],
[ 'HTTPS://WORDPRESS.ORG' ],
[ 'https://wordpress.org/./foo' ],
[ 'https://wordpress.org/path?query#fragment' ],
[ 'https://localhost/foo#bar' ],
[ 'mailto:[email protected]' ],
[ 'ssh://user:[email protected]:8080' ],
] )( 'valid (true): %s', ( url ) => {
expect( isURL( url ) ).toBe( true );
} );

it.each( [
[ 'http://word press.org' ],
[ 'http://wordpress.org:port' ],
[ 'http://[wordpress.org]/' ],
[ 'HTTP: HyperText Transfer Protocol' ],
[ 'URLs begin with a http:// prefix' ],
[ 'Go here: http://wordpress.org' ],
[ 'http://' ],
[ 'hello' ],
[ '' ],
] )( 'invalid (false): %s', ( url ) => {
expect( isURL( url ) ).toBe( false );
} );
} );