Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bjarneo/extract-domain
Extract domain name from an URL
https://github.com/bjarneo/extract-domain
domain domain-name extract-domain parse
Last synced: 19 days ago
JSON representation
Extract domain name from an URL
- Host: GitHub
- URL: https://github.com/bjarneo/extract-domain
- Owner: bjarneo
- License: mit
- Created: 2017-01-16T09:44:13.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-16T09:52:50.000Z (7 months ago)
- Last Synced: 2024-12-08T11:41:03.185Z (26 days ago)
- Topics: domain, domain-name, extract-domain, parse
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/extract-domain
- Size: 1.14 MB
- Stars: 31
- Watchers: 4
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Extract domain name from URL
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4JDQMB6MRJXQE&source=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](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL)
## Supports
- ESM
- Node.js
- Browser## Usage
### Installation
```bash
$ npm i --save extract-domain
```### Development
```
# Install bun https://bun.sh/
curl -fsSL https://bun.sh/install | bash# tests
bun test:watch
```### API
- @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 usedESM import
```js
import extractDomain from 'extract-domain';
```Examples
```js
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:[email protected]: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/',
'[email protected]',
];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](https://www.npmjs.com/package/psl).
Examples
```bash
npm i --save-optional psl
``````js
const 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.uk
```Please 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
```bash
$ bun test
```## Coding style
```bash
$ bun pretty
```## Benchmark
```bash
$ bun benchmark
```## Contribution
Contributions are appreciated.
## License
MIT-licensed. See LICENSE.