bjarneo / extract-domain Goto Github PK
View Code? Open in Web Editor NEWExtract domain name from an URL
Home Page: https://www.npmjs.com/package/extract-domain
License: MIT License
Extract domain name from an URL
Home Page: https://www.npmjs.com/package/extract-domain
License: MIT License
This array of URLS
["https://www.independent.co.uk/travel/news-and-advi…terdam-rome-venice-warsaw-dubrovnik-a8108096.html", "https://www.ipsf.org.il/?section=20", "https://timesofindia.indiatimes.com/blogs/voices/g…up-for-sustainable-tourism-in-a-post-covid-world/"]
returns this array of strings ["co.uk", "org.il", "indiatimes.com"]
The first two aren't working properly for some reason.
I tried to import as per the library docs
import extractDomain from 'extract-domain';
And then use it like
const urls = await extractDomain("https://www.mydomain", {
tld: true,
});
but it throws
extractDomain.default is not a function
Also tried
import * as extractDomain from 'extract-domain';
And then use it like
const urls = await extractDomain.default("https://www.mydomain", {
tld: true,
});
But got the same error
The only way I was able to make it work was by doing
const extractDomain = require('extract-domain');
And then use like in the first case
I am using typescript 5.1.6
I'm currently using this library to extract the domain of a website and I'm running into issues.
https://www.test.com returns test.com
https://test.com returns null
Expected result:
https://test.com returns test.com
EDIT: The TLD option is set to TRUE
EDIT2: When the TLD option is set to FALSE, everything works accordingly
Example:
extractDomain('https://www.argos.co.uk/');
Expected result:
argos.co.uk
Current result:
co.uk
Possible solution:
Check against some good TLD list. For example: https://publicsuffix.org/list/public_suffix_list.dat
extractDomain(["mother.com/q/s"])
Output should be: mother.com
but instead it is mother.com/q/s
.
- info Linting and checking validity of types ../node_modules/.pnpm/extract-domain@4.1.0/node_modules/extract-domain/index.ts:85:9
Type error: Type 'Promise<string | null>' is not assignable to type 'string | Promise<string>'.
Type 'Promise<string | null>' is not assignable to type 'Promise<string>'.
Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.
83 | }
84 |
> 85 | return import('psl')
| ^
86 | .then((psl) => Promise.resolve(psl.get(url.slice(offsetStart, offsetPath))))
87 | .catch((error) => {
88 | console.error(error);
I tried to extract the domain from this username '[email protected]' and got a blank string but when i tried with '[email protected]' i got the domain part 'something.com'.
please make this one work for less than 5 chars too.
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 protected]
=> 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 protected]
=> 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
}
}
Thanks for the work - currently this can't be used with multi-part domain endings such as .co.uk or .com.au (and countless others).
Hi,
There is a little bug: if the domain is an IP address (like http://127.0.0.1:port), it returns only 0.1 instead of the full IP address.
Best,
Xavier
i looks like it doesn't work on this .co.ma
not recognize http://localhost:8080
I'm using this library in a React Native project, but when upgrading to react-native: 0.69.1
and trying to compile android app (npx react-native run-android
) I get the following error:
error Failed to load configuration of your project.
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /my-project/node_modules/extract-domain/package.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:440:9)
at packageExportsResolve (node:internal/modules/esm/resolve:692:3)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.resolve (node:internal/modules/cjs/helpers:108:19)
at resolveNodeModuleDir (/my-project/node_modules/@react-native-community/cli-tools/build/resolveNodeModuleDir.js:24:42)
at /my-project/node_modules/@react-native-community/cli-config/build/loadConfig.js:93:76
at Array.reduce (<anonymous>)
I could fix it by modifying the node_modules/extract-domain/package.json
and replacing the exports
property with this:
"exports": {
".": {
"import": "./dist/extract-domain.modern.js"
},
"./package.json": "./package.json"
},
But I didn't want to create a PR with this change because I guess I'm missing some things here.
I'm getting a require is not defined
error. (Actually getting Error: You must install psl library
, but the real error is just swallowed by the catch
…).
This is probably because, this package defines an ESM entry point:
"exports": {
"require": "./dist/extract-domain.js",
// here =>
"default": "./dist/extract-domain.modern.mjs"
},
But the file extract-domain.modern.mjs
is using a dynamic require
to get psl
on demand here.
which ends up in the .mjs
-file.
The thing is, require
does not exist in Node ESM.
In other words: The file is stating I'm ESM
, while using a CJS
-only feature.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.