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');
new URL('apple');
isURL('/apple');
new URL('/apple');
isURL('//apple');
new URL('//apple');
isURL('://apple');
new URL('://apple');
isURL('a://apple');
new URL('a://apple');
isURL('a://apple://banana');
new URL('a://apple://banana');
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