ed

extract-domain

Extract domain name from an URL

Showing:

Popularity

Downloads/wk

4.9K

GitHub Stars

12

Maintenance

Last Commit

25d ago

Contributors

5

Package

Dependencies

1

License

MIT

Type Definitions

Tree-Shakeable

Yes?

Categories

Readme

Extract domain name from URL

Donate Travis

Performant domain name extraction. No regex or array magic.

What is an URL

However. This package will also remove the sub domain.

Supports

Browser and Node.

Usage

$ npm i --save extract-domain
  • urls = string|array
  • returns string|array
extractDomain(urls);

ES6

import extractDomain from "extract-domain";
const extractDomain = require("extract-domain");
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",
    "this.is.my@email.com"
];

extractDomain(urls[0]); // npmjs.com

extractDomain(urls); // [ 'npmjs.com', 'example.com', 'example.com', 'npmjs.com', 'example.org', 'email.com' ]

TLD support

TLD support require optional dependency to psl library.

const url =
    "http://www.example.co.uk:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument";

extractDomain(url, { tld: true });
// example.co.uk

However, using the tld flag will slow down the process by many seconds. Benchmark:

# 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

$ npm test

Coding style

$ npm run pretty

Benchmark

$ npm run benchmark

Contribution

Contributions are appreciated.

License

MIT-licensed. See LICENSE.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100