extract-domain v5.0.2
Extract domain name from URL
This package provides a performant way to extract domain names from URLs without using regular expressions or array manipulations.
Learn more about What is a URL
Supports
- ESM
- Node.js
- Browser
Usage
Installation
$ npm i --save extract-domainDevelopment
# Install bun https://bun.sh/
curl -fsSL https://bun.sh/install | bash
# tests
bun test:watchAPI
- @param {Urls} urls "https://www.google.com", "https://www.github.com" or "https://www.google.com"
- @param {GetDomainOptions} opts
{ tld: true }permit to get Top Level Domain like*.co.uk - @returns {Urls | Promise} Returns URL(s) or a promise of URL(s) if the PSL lib is being used
ESM import
import extractDomain from 'extract-domain';Examples
const urls = [
'https://www.npmjs.com/package/extract-domain',
'http://www.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
'http://user:password@example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
'https://npmjs.com/package/extract-domain',
'ftp://example.org/resource.txt',
'http://example.co.uk/',
'this.is.my@email.com',
];
extractDomain(urls[0]); // npmjs.com
extractDomain(urls); // [ 'npmjs.com', 'example.com', 'example.com', 'npmjs.com', 'example.org', 'co.uk', 'email.com' ]TLD support
TLD support requires the optional dependency of the psl library.
Examples
npm i --save-optional pslconst url =
'http://www.example.co.uk:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument';
async function extract(url) {
console.log(await extractDomain(url, { tld: true }));
// example.co.uk
}
// Or
extractDomain(url, { tld: true }).then(console.log);
// example.co.ukPlease note that using the tld flag may significantly slow down the process. Benchmark (old) results:
# extract domain 10,000 times
end ~14 ms (0 s + 13572914 ns)
# extract domain with tld 10,000 times
end ~4.29 s (4 s + 288108681 ns)Tests
$ bun testCoding style
$ bun prettyBenchmark
$ bun benchmarkContribution
Contributions are appreciated.
License
MIT-licensed. See LICENSE.
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago