UNPKG

4.62 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.org/peerigon/parse-domain.svg?branch=master)](https://travis-ci.org/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
11Since 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.
12
13This 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.
14
15<br />
16
17Installation
18------------------------------------------------------------------------
19
20```sh
21npm install parse-domain
22```
23
24<br />
25
26Usage
27------------------------------------------------------------------------
28
29```javascript
30
31// import the module
32const parseDomain = require("parse-domain");
33
34// long subdomains can be handled
35expect(parseDomain("some.subdomain.example.co.uk")).to.eql({
36 subdomain: "some.subdomain",
37 domain: "example",
38 tld: "co.uk"
39});
40
41// protocols, usernames, passwords, ports, paths, queries and hashes are disregarded
42expect(parseDomain("https://user:password@example.co.uk:8080/some/path?and&query#hash")).to.eql({
43 subdomain: "",
44 domain: "example",
45 tld: "co.uk"
46});
47
48// unknown top-level domains are ignored
49expect(parseDomain("unknown.tld.kk")).to.equal(null);
50
51// invalid urls are also ignored
52expect(parseDomain("invalid url")).to.equal(null);
53expect(parseDomain({})).to.equal(null);
54```
55
56### Introducing custom tlds
57
58```javascript
59// custom top-level domains can optionally be specified
60expect(parseDomain("mymachine.local",{ customTlds: ["local"] })).to.eql({
61 subdomain: "",
62 domain: "mymachine",
63 tld: "local"
64});
65
66// custom regexps can optionally be specified (instead of customTlds)
67expect(parseDomain("localhost",{ customTlds:/localhost|\.local/ })).to.eql({
68 subdomain: "",
69 domain: "",
70 tld: "localhost"
71});
72```
73
74It can sometimes be helpful to apply the customTlds argument using a helper function
75
76```javascript
77function parseLocalDomains(url) {
78 return parseDomain(url, {
79 customTlds: /localhost|\.local/
80 });
81}
82
83expect(parseLocalDomains("localhost")).to.eql({
84 subdomain: "",
85 domain: "",
86 tld: "localhost"
87});
88expect(parseLocalDomains("mymachine.local")).to.eql({
89 subdomain: "",
90 domain: "mymachine",
91 tld: "local"
92});
93```
94
95<br />
96
97API
98------------------------------------------------------------------------
99
100### `parseDomain(url: string, options: ParseOptions): ParsedDomain|null`
101
102Returns `null` if `url` has an unknown tld or if it's not a valid url.
103
104#### `ParseOptions`
105
106```javascript
107{
108 // A list of custom tlds that are first matched against the url.
109 // Useful if you also need to split internal URLs like localhost.
110 customTlds: RegExp|Array<string>,
111
112 // There are lot of private domains that act like top-level domains,
113 // like blogspot.com, googleapis.com or s3.amazonaws.com.
114 // By default, these domains would be split into:
115 // { subdomain: ..., domain: "blogspot", tld: "com" }
116 // When this flag is set to true, the domain will be split into
117 // { subdomain: ..., domain: ..., tld: "blogspot.com" }
118 // See also https://github.com/peerigon/parse-domain/issues/4
119 privateTlds: boolean - default: false
120}
121```
122
123#### `ParsedDomain`
124
125```javascript
126{
127 tld: string,
128 domain: string,
129 subdomain: string
130}
131```
132
133<br />
134
135License
136------------------------------------------------------------------------
137
138Unlicense
139
140## Sponsors
141
142[<img src="https://assets.peerigon.com/peerigon/logo/peerigon-logo-flat-spinat.png" width="150" />](https://peerigon.com)