UNPKG

4.75 kBMarkdownView Raw
1parse-domain
2============
3**Splits a URL into sub-domain, domain and the top-level domain.**
4
5[![](https://img.shields.io/npm/v/parse-domain.svg)](https://www.npmjs.com/package/parse-domain)
6[![](https://img.shields.io/npm/dm/parse-domain.svg)](https://www.npmjs.com/package/parse-domain)
7[![Dependency Status](https://david-dm.org/peerigon/parse-domain.svg)](https://david-dm.org/peerigon/parse-domain)
8[![Build Status](https://travis-ci.com/peerigon/parse-domain.svg?branch=master)](https://travis-ci.com/peerigon/parse-domain)
9[![Coverage Status](https://img.shields.io/coveralls/peerigon/parse-domain.svg)](https://coveralls.io/r/peerigon/parse-domain?branch=master)
10[![Known Vulnerabilities](https://snyk.io/test/github/peerigon/parse-domain/badge.svg)](https://snyk.io/test/github/peerigon/parse-domain)
11
12Since domains are handled differently across different countries and organizations, splitting a URL into sub-domain, domain and top-level-domain parts is not a simple regexp. **parse-domain** uses a [large list of known top-level domains](https://publicsuffix.org/list/public_suffix_list.dat) from publicsuffix.org to recognize different parts of the domain.
13
14This module uses a [trie](https://en.wikipedia.org/wiki/Trie) data structure under the hood to ensure the smallest possible library size and the fastest lookup. The library is roughly 30KB minified and gzipped. Since publicsuffix.org is frequently updated, the data structure is built on `npm install` as a `postinstall` hook. If something goes wrong during that step, the library falls back to a prebuilt list that has been built at the time of publishing.
15
16<br />
17
18Installation
19------------------------------------------------------------------------
20
21```sh
22npm install parse-domain
23```
24
25<br />
26
27Usage
28------------------------------------------------------------------------
29
30```javascript
31
32// import the module
33const parseDomain = require("parse-domain");
34
35// long subdomains can be handled
36expect(parseDomain("some.subdomain.example.co.uk")).to.eql({
37 subdomain: "some.subdomain",
38 domain: "example",
39 tld: "co.uk"
40});
41
42// protocols, usernames, passwords, ports, paths, queries and hashes are disregarded
43expect(parseDomain("https://user:password@example.co.uk:8080/some/path?and&query#hash")).to.eql({
44 subdomain: "",
45 domain: "example",
46 tld: "co.uk"
47});
48
49// unknown top-level domains are ignored
50expect(parseDomain("unknown.tld.kk")).to.equal(null);
51
52// invalid urls are also ignored
53expect(parseDomain("invalid url")).to.equal(null);
54expect(parseDomain({})).to.equal(null);
55```
56
57### Introducing custom tlds
58
59```javascript
60// custom top-level domains can optionally be specified
61expect(parseDomain("mymachine.local",{ customTlds: ["local"] })).to.eql({
62 subdomain: "",
63 domain: "mymachine",
64 tld: "local"
65});
66
67// custom regexps can optionally be specified (instead of customTlds)
68expect(parseDomain("localhost",{ customTlds:/localhost|\.local/ })).to.eql({
69 subdomain: "",
70 domain: "",
71 tld: "localhost"
72});
73```
74
75It can sometimes be helpful to apply the customTlds argument using a helper function
76
77```javascript
78function parseLocalDomains(url) {
79 return parseDomain(url, {
80 customTlds: /localhost|\.local/
81 });
82}
83
84expect(parseLocalDomains("localhost")).to.eql({
85 subdomain: "",
86 domain: "",
87 tld: "localhost"
88});
89expect(parseLocalDomains("mymachine.local")).to.eql({
90 subdomain: "",
91 domain: "mymachine",
92 tld: "local"
93});
94```
95
96<br />
97
98API
99------------------------------------------------------------------------
100
101### `parseDomain(url: string, options: ParseOptions): ParsedDomain|null`
102
103Returns `null` if `url` has an unknown tld or if it's not a valid url.
104
105#### `ParseOptions`
106
107```javascript
108{
109 // A list of custom tlds that are first matched against the url.
110 // Useful if you also need to split internal URLs like localhost.
111 customTlds: RegExp|Array<string>,
112
113 // There are lot of private domains that act like top-level domains,
114 // like blogspot.com, googleapis.com or s3.amazonaws.com.
115 // By default, these domains would be split into:
116 // { subdomain: ..., domain: "blogspot", tld: "com" }
117 // When this flag is set to true, the domain will be split into
118 // { subdomain: ..., domain: ..., tld: "blogspot.com" }
119 // See also https://github.com/peerigon/parse-domain/issues/4
120 privateTlds: boolean - default: false
121}
122```
123
124#### `ParsedDomain`
125
126```javascript
127{
128 tld: string,
129 domain: string,
130 subdomain: string
131}
132```
133
134<br />
135
136License
137------------------------------------------------------------------------
138
139Unlicense
140
141## Sponsors
142
143[<img src="https://assets.peerigon.com/peerigon/logo/peerigon-logo-flat-spinat.png" width="150" />](https://peerigon.com)