bjarneo / extract-domain

Extract domain name from an URL
https://www.npmjs.com/package/extract-domain
MIT License
31 stars 4 forks source link

It would be nice if a naked root domain was simply returned rather than returning null #13

Closed hayksaakian closed 2 years ago

hayksaakian commented 2 years ago

It pretty much works as you would expect except for the simplest case where you already have a domain as the input.

Obviously this is trivial to check against, but it'd be nice if this library "just worked"

How it currently works:

example.com
  =>
https://example.com
  => example.com
www.example.com
  => example.com
//example.com/protocol-relative
  => example.com
email@example.com
  => example.com
weird-protocol://example.com
  => example.com

How it would be nice if it worked:

example.com
  => example.com
https://example.com
  => example.com
www.example.com
  => example.com
//example.com/protocol-relative
  => example.com
email@example.com
  => example.com
weird-protocol://example.com
  => example.com

This is how i'm currently working around this, it's ugly but it works:


const extractDomain = require('extract-domain');
function domain_from_url(url) {
  try {
    return extractDomain(url) ? extractDomain(url) : url
  } catch (error){
    console.log(error)
    return url
  }
}
bjarneo commented 2 years ago

Hi @hayksaakian,

I totally agree with you. Of course it should have the behavior you describe. Will look into this. Thanks for the feedback :)

Question is; how to solve this the correct way. :eyes:

bjarneo commented 2 years ago

@hayksaakian try the new version 2.4.0 :)