-
Notifications
You must be signed in to change notification settings - Fork 56
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
Parse undefined / null => isValid:true ? #131
Comments
Hi @lqzhgood, thanks for taking the time to open an issue. Unfortunately this is "expected" behavior. For backward compatibility reasons any value passed to If the values you are manipulated are already hostnames, you can override this step completely using a custom const tldjs = require('tldjs');
tldjs.parse(undefined);
// { hostname: 'undefined',
// isValid: true,
// isIp: false,
// tldExists: false,
// publicSuffix: null,
// domain: null,
// subdomain: null }
const custom = tldjs.fromUserSettings({ extractHostname: (hostname) => hostname });
custom.parse(undefined)
// { hostname: undefined,
// isValid: false,
// isIp: false,
// tldExists: false,
// publicSuffix: null,
// domain: null,
// subdomain: null } Note that you could also use this customization to implement your own URL parsing (e.g.: using the const URL = require('url').URL;
const tldjs = require('tldjs').fromUserSettings({ extractHostname: (url) => new URL(url).hostname });
tldjs.parse(undefined);
// Thrown:
// { TypeError [ERR_INVALID_URL]: Invalid URL: undefined
// at onParseError (internal/url.js:241:17)
// at new URL (internal/url.js:319:5)
// at extractHostname (repl:1:60)
// at Object.parse (/home/remi/dev/repositories/public/tld.js/index.js:52:17) input: 'undefined' }
tldjs.parse('https://foo.com/bar')
// { hostname: 'foo.com',
// isValid: true,
// isIp: false,
// tldExists: true,
// publicSuffix: 'com',
// domain: 'foo.com',
// subdomain: '' } I hope this helps! |
in https://github.com/oncletom/tld.js/blob/master/lib/clean-host.js#L68
module.exports = function extractHostname(value) {
if (!isNaN(value)) {
return null;
}
if (typeof value !== 'string') {
return null;
}
// First check if `value` is already a valid hostname.
if (isValid(value)) {
return trimTrailingDots(value);
}
var url = value;
var needsTrimming = checkTrimmingNeeded(url);
...... tld.parse(null)
tld.parse(undefined)
tld.parse(() => {})
{
"hostname": null,
"isValid": false,
"isIp": false,
"tldExists": false,
"publicSuffix": null,
"domain": null,
"subdomain": null
} That looks fine. And there's nothing wrong with that run And I have one more question that I'm not sure about。 module.exports = function isValid(hostname) {
if (!isNaN(hostname)) {
return false;
}
if (typeof hostname !== 'string') {
return false;
}
... |
@lqzhgood All good suggestions. Feel free to make a PR to the repository with improvements, but there was not much activity around lately. I currently maintain my own fork of this project. |
tldjs.parse(undefined)
isValid:true
tldjs.parse(null)
isValid:true
The text was updated successfully, but these errors were encountered: