Giter VIP home page Giter VIP logo

extract-domain's Introduction

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, and unless the TLD option is used, anything other than the domain and second level domain (so www.example.co.uk would return co.uk)

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:[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' ]

const urlError = [
    "ftp://example.org/resource.txt",
    "http://example.co.uk/",
    "exampledotcom"
];

extractDomains(urlError, { verbose: true }); // The given URL exampledotcom is not a valid URL. Please verify your string|array.'

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.

extract-domain's People

Contributors

bjarneo avatar dependabot[bot] avatar madeindjs avatar peternewman avatar shaynem avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.