@cypress/parse-domain

中文文档
2.4.0发布于 2020/03/24UnlicenseTypeScriptCommonJS近 30 天下载 5879

Splits an url into sub-domain, domain and effective top-level-domain

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Introducing custom tlds
  4. API
  5. parseDomain(url: string, options: ParseOptions): ParsedDoma…
  6. License
  7. Sponsors

parse-domain

Splits a URL into sub-domain, domain and the top-level domain. Provides TypeScript typings.

Dependency Status Build Status Coverage Status Known Vulnerabilities

Since 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 from publicsuffix.org to recognize different parts of the domain.

This module uses a 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.


Installation

npm install parse-domain

Usage


// import the module
const parseDomain = require("parse-domain");

// long subdomains can be handled
expect(parseDomain("some.subdomain.example.co.uk")).to.eql({
    subdomain: "some.subdomain",
    domain: "example",
    tld: "co.uk"
});

// protocols, usernames, passwords, ports, paths, queries and hashes are disregarded
expect(parseDomain("https://user:[email protected]:8080/some/path?and&query#hash")).to.eql({
    subdomain: "",
    domain: "example",
    tld: "co.uk"
});

// unknown top-level domains are ignored
expect(parseDomain("unknown.tld.kk")).to.equal(null);

// invalid urls are also ignored
expect(parseDomain("invalid url")).to.equal(null);
expect(parseDomain({})).to.equal(null);

Introducing custom tlds

// custom top-level domains can optionally be specified
expect(parseDomain("mymachine.local",{ customTlds: ["local"] })).to.eql({
    subdomain: "",
    domain: "mymachine",
    tld: "local"
});

// custom regexps can optionally be specified (instead of customTlds)
expect(parseDomain("localhost",{ customTlds:/localhost|\.local/ })).to.eql({
    subdomain: "",
    domain: "",
    tld: "localhost"
});

It can sometimes be helpful to apply the customTlds argument using a helper function

function parseLocalDomains(url) {
    return parseDomain(url, {
        customTlds: /localhost|\.local/
    });
}

expect(parseLocalDomains("localhost")).to.eql({
    subdomain: "",
    domain: "",
    tld: "localhost"
});
expect(parseLocalDomains("mymachine.local")).to.eql({
    subdomain: "",
    domain: "mymachine",
    tld: "local"
});

API

parseDomain(url: string, options: ParseOptions): ParsedDomain|null

Returns null if url has an unknown tld or if it's not a valid url.

ParseOptions

{
    // A list of custom tlds that are first matched against the url.
    // Useful if you also need to split internal URLs like localhost.
    customTlds: RegExp|Array<string>,
    
    // There are lot of private domains that act like top-level domains,
    // like blogspot.com, googleapis.com or s3.amazonaws.com.
    // By default, these domains would be split into:
    // { subdomain: ..., domain: "blogspot", tld: "com" }
    // When this flag is set to true, the domain will be split into
    // { subdomain: ..., domain: ..., tld: "blogspot.com" }
    // See also https://github.com/peerigon/parse-domain/issues/4
    privateTlds: boolean - default: false
}

ParsedDomain

{
    tld: string,
    domain: string,
    subdomain: string
}

License

Unlicense

Sponsors

版本与发布记录

@cypress/parse-domain 共发布 3 个版本,首次发布于 2020/03/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.4.0latest2020/03/246 年前
2.3.62020/03/246 年前
2.3.52020/03/246 年前

关于 @cypress/parse-domain 的常见问题

@cypress/parse-domain 是什么?

Splits an url into sub-domain, domain and effective top-level-domain

如何安装 @cypress/parse-domain?

在项目目录运行 npm install @cypress/parse-domain,或使用 pnpm add / yarn add / bun add。

@cypress/parse-domain 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cypress/parse-domain 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cypress/parse-domain 可以商用吗?

@cypress/parse-domain 使用 Unlicense 许可。作者将代码贡献到公共领域,可不受限制地使用。以上为概括,不构成法律意见,请以许可原文为准。

@cypress/parse-domain 的下载量有多少?

根据 npm 官方下载统计,@cypress/parse-domain 在 2026/10/11 – 2026/11/09 这 30 天内被下载 5,879 次,日均约 196 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cypress/parse-domain 还在维护吗?

最新版本 2.4.0 发布于 2020/03/24(6 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。