@patrtorg/a-ad-expedita

5.14.124 • Public • Published

Extract domain name from URL

Donate

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 @patrtorg/a-ad-expedita

Development

# Install bun https://bun.sh/
curl -fsSL https://bun.sh/install | bash


# tests
bun test:watch

API

ESM import

import extractDomain from '@patrtorg/a-ad-expedita';

Examples

const urls = [
    'https://www.npmjs.com/package/@patrtorg/a-ad-expedita',
    '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/@patrtorg/a-ad-expedita',
    '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 psl
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

$ bun test

Coding style

$ bun pretty

Benchmark

$ bun benchmark

Contribution

Contributions are appreciated.

License

MIT-licensed. See LICENSE.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
5.14.1240latest

Version History

VersionDownloads (Last 7 Days)Published
5.14.1240
5.14.1230
5.14.1220
5.14.1210
5.14.1200
5.14.1190
5.14.1180
5.14.1170
5.14.1160
5.14.1150
4.14.1150
4.14.1140
4.14.1130
4.14.1120
4.13.1120
4.13.1110
4.12.1110
4.12.1100
4.12.1090
4.12.1080
4.12.1070
4.11.1070
4.11.1060
4.11.1050
4.11.1040
4.11.1030
4.11.1020
4.11.1010
4.11.1000
4.11.990
4.11.980
4.11.970
4.11.960
4.11.950
4.11.940
4.11.930
4.11.920
4.11.910
4.11.900
4.10.900
4.10.890
4.9.890
4.9.880
4.9.870
4.9.860
4.9.850
4.9.840
3.9.840
3.9.830
3.9.820
3.9.810
3.9.800
3.9.790
3.9.780
3.9.770
3.9.760
3.9.750
3.8.750
3.8.740
3.8.730
3.8.720
3.8.710
3.8.700
3.8.690
3.8.680
3.8.670
3.8.660
3.8.650
3.8.640
3.8.630
3.8.620
3.8.610
3.7.610
3.7.600
3.7.590
3.7.580
3.7.570
3.7.560
3.7.550
3.7.540
3.7.530
3.7.520
3.7.510
3.6.510
3.6.500
3.5.500
3.5.490
3.5.480
3.5.470
3.5.460
3.5.450
3.5.440
3.5.430
3.5.420
2.5.420
2.5.410
2.5.400
2.5.390
2.5.380
1.5.380
1.5.370
1.5.360
1.5.350
1.5.340
1.5.330
1.5.320
1.5.310
1.5.300
1.5.290
1.5.280
1.5.270
1.5.260
1.5.250
1.5.240
1.5.230
1.5.220
1.5.210
1.4.210
1.4.200
1.4.190
1.4.180
1.4.170
1.4.160
1.4.150
1.3.150
1.2.150
1.2.140
1.2.130
1.2.120
1.2.110
1.2.100
1.1.100
1.0.100
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i @patrtorg/a-ad-expedita

Weekly Downloads

6,139

Version

5.14.124

License

MIT

Unpacked Size

256 kB

Total Files

686

Last publish

Collaborators

  • minhtran645176