is-whatwg-url

1.0.0 • Public • Published

is-whatwg-url

Can be parsed or not by whatwg url module.

Why

whatwg-url constructor throw error for normal string. is-whatwg-url is just validater for whatwg-url constructor.

Usage

const isURL = require('is-whatwg-url');
const { URL } = require('url');
 
isURL('apple'); // false
new URL('apple'); // throw Error Invalid URL
 
isURL('/apple'); // false
new URL('/apple'); // throw Error Invalid URL
 
isURL('//apple'); // false
new URL('//apple'); // throw Error Invalid URL
 
isURL('://apple'); // false
new URL('://apple'); // throw Error Invalid URL
 
isURL('a://apple'); // true
new URL('a://apple'); // success
 
isURL('a://apple://banana'); // true
new URL('a://apple://banana'); // success

How it works

check input string have protocol:// prefix. protocol should not be empty string.

Caveats

This module does not guarantee that input is URL or not. It just check can be parsed or not.

Alt.

consider using try-catch instead of this is-whatwg-url module.

let url;
try {
  url = new URL('apple');
} catch (err) {
  url = null;
}

Related

Package Sidebar

Install

npm i is-whatwg-url

Weekly Downloads

1

Version

1.0.0

License

ISC

Unpacked Size

3.34 kB

Total Files

5

Last publish

Collaborators

  • zeakd