jsdomain-parser 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,21 +15,21 @@
15
15
  font-family: monospace;
16
16
  }
17
17
  </style>
18
+
19
+ <script src="../dist//jsdomain-parser.umd.min.js"></script>
18
20
  </head>
19
21
  <body>
20
22
  <input type="text" placeholder="Enter URL" />
21
23
  <button id="parse">Parse</button>
22
24
  <div id="result"></div>
23
25
  <script type="module">
24
- import { parse } from "../dist/jsdomain-parser.esm.js";
25
-
26
26
  document.querySelector("#parse").addEventListener("click", () => {
27
27
  const input = document.querySelector("input").value;
28
28
 
29
29
  let parsed;
30
30
 
31
31
  try {
32
- parsed = parse(input, {
32
+ parsed = jsDomainParser.parse(input, {
33
33
  allowPrivate: true, // allow private TLDs
34
34
  allowUnknown: false, // restrict to known TLDs only
35
35
  extendedTlds: ["my.tld", "tld"], // add custom TLDs to extend the list
package/index.js CHANGED
@@ -1,4 +1,11 @@
1
+ /**
2
+ * @typedef {import('./index').ParseResult} ParseResult
3
+ * @typedef {import('./index').ParseOptions} ParseOptions
4
+ * @typedef {import('./index').TldInfo} TldInfo
5
+ * @typedef {import('./index').ParsedUrl} ParsedUrl
6
+ */
7
+
1
8
  const parseTld = require("./src/parseTld.js");
2
9
  const parse = require("./src/parse.js");
3
10
 
4
- module.exports = { parse, parseTld };
11
+ module.exports = { parse, parseTld };
package/package.json CHANGED
@@ -1,34 +1,43 @@
1
- {
2
- "name": "jsdomain-parser",
3
- "version": "1.0.7",
4
- "description": "Parse URLs to extract TLDs, domain, protocols and more",
5
- "main": "index.js",
6
- "scripts": {
7
- "build": "rollup -c",
8
- "test": "jest"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "https://github.com/ToolsOverflow/jsdomain-parser"
13
- },
14
- "keywords": [
15
- "url",
16
- "parser",
17
- "domain",
18
- "parser",
19
- "tld",
20
- "extractor",
21
- "extract",
22
- "tld"
23
- ],
24
- "author": "ToolsOverflow",
25
- "license": "MIT",
26
- "devDependencies": {
27
- "@rollup/plugin-json": "^6.1.0",
28
- "jest": "^29.7.0",
29
- "rollup": "^2.79.2",
30
- "rollup-plugin-commonjs": "^10.1.0",
31
- "rollup-plugin-node-resolve": "^5.2.0",
32
- "rollup-plugin-terser": "^7.0.2"
33
- }
34
- }
1
+ {
2
+ "name": "jsdomain-parser",
3
+ "version": "1.0.9",
4
+ "description": "Parse URLs to extract TLDs, domain, protocols and more",
5
+ "main": "index.js",
6
+ "types": "./types/index.d.ts",
7
+ "type": "commonjs",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types/index.d.ts",
11
+ "require": "./index.js",
12
+ "default": "./index.js"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "rollup -c",
17
+ "test": "jest"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/ToolsOverflow/jsdomain-parser"
22
+ },
23
+ "keywords": [
24
+ "url",
25
+ "parser",
26
+ "domain",
27
+ "parser",
28
+ "tld",
29
+ "extractor",
30
+ "extract",
31
+ "tld"
32
+ ],
33
+ "author": "ToolsOverflow",
34
+ "license": "MIT",
35
+ "devDependencies": {
36
+ "@rollup/plugin-json": "^6.1.0",
37
+ "jest": "^29.7.0",
38
+ "rollup": "^2.79.2",
39
+ "rollup-plugin-commonjs": "^10.1.0",
40
+ "rollup-plugin-node-resolve": "^5.2.0",
41
+ "rollup-plugin-terser": "^7.0.2"
42
+ }
43
+ }
package/rollup.config.js CHANGED
@@ -25,15 +25,15 @@ export default {
25
25
  plugins: [terser()],
26
26
  },
27
27
  {
28
- file: "dist/jsdomain-parser.min.js",
29
- format: "iife",
30
- name: "DomainParser",
31
- plugins: [terser()],
28
+ file: "dist/jsdomain-parser.umd.js",
29
+ format: "umd",
30
+ name: "jsDomainParser",
32
31
  },
33
32
  {
34
- file: "dist/jsdomain-parser.js",
35
- format: "iife",
36
- name: "DomainParser",
33
+ file: "dist/jsdomain-parser.umd.min.js",
34
+ format: "umd",
35
+ name: "jsDomainParser",
36
+ plugins: [terser()],
37
37
  },
38
38
  ],
39
39
  plugins: [resolve(), commonjs(), json()],
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Parsed TLD information
3
+ */
4
+ export interface TldInfo {
5
+ /** The full public suffix / effective TLD (e.g. "co.uk", "com") */
6
+ name: string;
7
+ /** Number of labels/parts in the TLD (e.g. 2 for co.uk) */
8
+ length: number;
9
+ /** Individual labels of the TLD */
10
+ parts: string[];
11
+ }
12
+
13
+ /**
14
+ * Parsed URL components (similar to WHATWG URL but with query as object)
15
+ */
16
+ export interface ParsedUrl {
17
+ /** Second-level domain + TLD (e.g. "example.com") */
18
+ domain: string;
19
+ /** Full origin including protocol (e.g. "https://www.example.com") */
20
+ origin: string;
21
+ /** Protocol with colon (e.g. "https:") */
22
+ protocol: string;
23
+ /** Host including subdomains and port if present (e.g. "www.example.com:8080") */
24
+ host: string;
25
+ /** Hostname without port (e.g. "www.example.com") */
26
+ hostname: string;
27
+ /** Port number as string (empty string if none) */
28
+ port: string;
29
+ /** Path including leading slash (e.g. "/blog/post") */
30
+ pathname: string;
31
+ /** Query string including ? (empty string if none) */
32
+ search: string;
33
+ /** Fragment including # (empty string if none) */
34
+ hash: string;
35
+ /** Parsed query parameters as key-value object */
36
+ query: Record<string, string>;
37
+ }
38
+
39
+ /**
40
+ * Result returned by parse()
41
+ */
42
+ export interface ParseResult {
43
+ /** Information about the detected top-level domain / public suffix */
44
+ tld: TldInfo;
45
+ /** Parsed components of the URL */
46
+ url: ParsedUrl;
47
+ }
48
+
49
+ /**
50
+ * Parsing options
51
+ */
52
+ export interface ParseOptions {
53
+ /**
54
+ * Whether to consider private (non-ICANN) TLDs
55
+ * @default true
56
+ */
57
+ allowPrivate?: boolean;
58
+
59
+ /**
60
+ * Whether to allow parsing when no known TLD is found
61
+ * @default false
62
+ */
63
+ allowUnknown?: boolean;
64
+
65
+ /**
66
+ * Additional custom TLDs to recognize (each item can be "tld" or "sub.tld")
67
+ */
68
+ extendedTlds?: string[];
69
+ }
70
+
71
+ /**
72
+ * Parses a URL string and extracts domain/TLD information using the public suffix list.
73
+ *
74
+ * @param url - The URL or hostname to parse (string)
75
+ * @param options - Optional configuration
76
+ * @throws Error when URL is invalid or TLD cannot be determined (unless allowUnknown=true)
77
+ */
78
+ export function parse(url: string, options?: ParseOptions): ParseResult;