jsdomain-parser 1.0.0
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.
- package/README.md +151 -0
- package/dist/jsdomain-parser.cjs.js +9885 -0
- package/dist/jsdomain-parser.cjs.min.js +1 -0
- package/dist/jsdomain-parser.esm.js +9880 -0
- package/dist/jsdomain-parser.esm.min.js +1 -0
- package/dist/jsdomain-parser.js +9890 -0
- package/dist/jsdomain-parser.min.js +1 -0
- package/package.json +33 -0
- package/rollup.config.js +40 -0
- package/src/index.js +46 -0
- package/src/parseTld.js +56 -0
- package/src/tlds.json +9781 -0
- package/test/test.html +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# JS URL Parser
|
|
2
|
+
|
|
3
|
+
JS URL Parser is a lightweight JavaScript library designed to parse and extract detailed information from URLs. It supports detecting TLDs (Top-Level Domains), handling private and unknown TLDs, and extending the TLD list with custom values.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Extracts detailed URL components (protocol, domain, hostname, path, etc.).
|
|
8
|
+
- Detects TLDs using a comprehensive list of ICANN and private TLDs.
|
|
9
|
+
- Supports custom TLDs via configuration.
|
|
10
|
+
- Allows toggling support for private and unknown TLDs.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install jsdomain-parser
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Or include it directly in your project:
|
|
19
|
+
|
|
20
|
+
```html
|
|
21
|
+
<script src="./dist/jsdomain-parser.min.js"></script>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Example 1: Basic Parsing
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
import { parse } from "jsdomain-parser";
|
|
30
|
+
|
|
31
|
+
const url = "https://www.example.com/path?query=test#hash";
|
|
32
|
+
const result = parse(url);
|
|
33
|
+
|
|
34
|
+
console.log(result);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**Output:**
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"tld": {
|
|
42
|
+
"name": "com",
|
|
43
|
+
"length": 1,
|
|
44
|
+
"parts": ["com"]
|
|
45
|
+
},
|
|
46
|
+
"url": {
|
|
47
|
+
"domain": "example.com",
|
|
48
|
+
"origin": "https://www.example.com",
|
|
49
|
+
"protocol": "https:",
|
|
50
|
+
"host": "www.example.com",
|
|
51
|
+
"hostname": "www.example.com",
|
|
52
|
+
"port": "",
|
|
53
|
+
"pathname": "/path",
|
|
54
|
+
"search": "?query=test",
|
|
55
|
+
"hash": "#hash",
|
|
56
|
+
"query": {
|
|
57
|
+
"query": "test"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Example 2: Using Custom Options
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
const url = "my.custom.tld";
|
|
67
|
+
|
|
68
|
+
const result = parse(url, {
|
|
69
|
+
allowPrivate: true, // Enable private TLDs
|
|
70
|
+
allowUnknown: false, // Disallow unknown TLDs
|
|
71
|
+
extendedTlds: ["custom.tld", "tld"], // Add custom TLDs
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
console.log(result);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Output:**
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"tld": {
|
|
82
|
+
"name": "custom.tld",
|
|
83
|
+
"length": 2,
|
|
84
|
+
"parts": ["custom", "tld"]
|
|
85
|
+
},
|
|
86
|
+
"url": {
|
|
87
|
+
"domain": "my.custom.tld",
|
|
88
|
+
"origin": "http://my.custom.tld",
|
|
89
|
+
"protocol": "http:",
|
|
90
|
+
"host": "my.custom.tld",
|
|
91
|
+
"hostname": "my.custom.tld",
|
|
92
|
+
"port": "",
|
|
93
|
+
"pathname": "/",
|
|
94
|
+
"search": "",
|
|
95
|
+
"hash": "",
|
|
96
|
+
"query": {}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Example 3: Error Handling
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
try {
|
|
105
|
+
const result = parse("invalid-url");
|
|
106
|
+
console.log(result);
|
|
107
|
+
} catch (e) {
|
|
108
|
+
console.error("Error:", e.message);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**Output:**
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
Error: Invalid URL: Invalid URL: Error: Could not detect TLD. You can set allowUnknown to true for allowing unknown TLDs.
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## API Reference
|
|
119
|
+
|
|
120
|
+
### `parse(url, options = {})`
|
|
121
|
+
|
|
122
|
+
Parses a URL string and returns an object containing TLD and URL details.
|
|
123
|
+
|
|
124
|
+
- **`url`**: The URL to parse (string).
|
|
125
|
+
- **`options`** (object): Optional configurations:
|
|
126
|
+
- `allowPrivate` (boolean): Allow private TLDs (default: `true`).
|
|
127
|
+
- `allowUnknown` (boolean): Allow unknown TLDs (default: `false`).
|
|
128
|
+
- `extendedTlds` (array): Array of custom TLDs to extend the list.
|
|
129
|
+
|
|
130
|
+
**Returns:**
|
|
131
|
+
An object with the following structure:
|
|
132
|
+
|
|
133
|
+
- `tld`: Details of the detected TLD.
|
|
134
|
+
- `name`: The TLD name.
|
|
135
|
+
- `length`: The number of parts in the TLD.
|
|
136
|
+
- `parts`: An array of the TLD parts.
|
|
137
|
+
- `url`: Details of the parsed URL.
|
|
138
|
+
- `domain`: The domain name.
|
|
139
|
+
- `origin`: The full URL origin.
|
|
140
|
+
- `protocol`: The protocol (e.g., `http:`).
|
|
141
|
+
- `host`: The host.
|
|
142
|
+
- `hostname`: The hostname.
|
|
143
|
+
- `port`: The port (if any).
|
|
144
|
+
- `pathname`: The path.
|
|
145
|
+
- `search`: The query string.
|
|
146
|
+
- `hash`: The hash fragment.
|
|
147
|
+
- `query`: An object representation of query parameters.
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT
|