@ooneex/url 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ooneex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,478 @@
1
+ # @ooneex/url
2
+
3
+ A comprehensive TypeScript/JavaScript library for working with URLs. This package provides powerful URL parsing, manipulation, and building capabilities with a clean, type-safe API for web applications.
4
+
5
+ ![Browser](https://img.shields.io/badge/Browser-Compatible-green?style=flat-square&logo=googlechrome)
6
+ ![Bun](https://img.shields.io/badge/Bun-Compatible-orange?style=flat-square&logo=bun)
7
+ ![Deno](https://img.shields.io/badge/Deno-Compatible-blue?style=flat-square&logo=deno)
8
+ ![Node.js](https://img.shields.io/badge/Node.js-Compatible-green?style=flat-square&logo=node.js)
9
+ ![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue?style=flat-square&logo=typescript)
10
+ ![MIT License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)
11
+
12
+ ## Features
13
+
14
+ ✅ **Complete URL Parsing** - Parse and extract all URL components (protocol, hostname, port, path, queries, fragment)
15
+
16
+ ✅ **Type-Safe** - Full TypeScript support with proper type definitions
17
+
18
+ ✅ **Immutable & Mutable** - Choose between ReadonlyUrl for parsing or Url for manipulation
19
+
20
+ ✅ **Cross-Platform** - Works in Browser, Node.js, Bun, and Deno
21
+
22
+ ✅ **Smart Query Parsing** - Automatically converts query parameters to appropriate types (string, number, boolean)
23
+
24
+ ✅ **Subdomain Detection** - Intelligently separates subdomains from domains
25
+
26
+ ✅ **Port Handling** - Proper default port detection and custom port support
27
+
28
+ ✅ **Path Normalization** - Clean path handling with proper slash management
29
+
30
+ ✅ **Zero Dependencies** - No external dependencies required (uses workspace utilities)
31
+
32
+ ## Installation
33
+
34
+ ### Bun
35
+ ```bash
36
+ bun add @ooneex/url
37
+ ```
38
+
39
+ ### pnpm
40
+ ```bash
41
+ pnpm add @ooneex/url
42
+ ```
43
+
44
+ ### Yarn
45
+ ```bash
46
+ yarn add @ooneex/url
47
+ ```
48
+
49
+ ### npm
50
+ ```bash
51
+ npm install @ooneex/url
52
+ ```
53
+
54
+ ## Usage
55
+
56
+ ### Basic URL Parsing
57
+
58
+ ```typescript
59
+ import { ReadonlyUrl } from '@ooneex/url';
60
+
61
+ const url = new ReadonlyUrl('https://blog.example.com:3000/posts/123?page=2&sort=desc#comments');
62
+
63
+ // Get URL components
64
+ console.log(url.getProtocol()); // "https"
65
+ console.log(url.getHostname()); // "blog.example.com"
66
+ console.log(url.getSubdomain()); // "blog"
67
+ console.log(url.getDomain()); // "example.com"
68
+ console.log(url.getPort()); // 3000
69
+ console.log(url.getPath()); // "/posts/123"
70
+ console.log(url.getQueries()); // { page: 2, sort: "desc" }
71
+ console.log(url.getFragment()); // "comments"
72
+ console.log(url.getOrigin()); // "https://blog.example.com:3000"
73
+ ```
74
+
75
+ ### URL Manipulation
76
+
77
+ ```typescript
78
+ import { Url } from '@ooneex/url';
79
+
80
+ const url = new Url('https://example.com');
81
+
82
+ // Chain modifications
83
+ url.setProtocol('http')
84
+ .setHostname('api.example.com')
85
+ .setPort(8080)
86
+ .setPath('/v1/users')
87
+ .addQuery('limit', 10)
88
+ .addQuery('offset', 0)
89
+ .setFragment('results');
90
+
91
+ console.log(url.toString()); // "http://api.example.com:8080/v1/users?limit=10&offset=0#results"
92
+ ```
93
+
94
+ ### Advanced Usage
95
+
96
+ ```typescript
97
+ import { Url, ReadonlyUrl } from '@ooneex/url';
98
+
99
+ // Parse complex URLs with multiple subdomains
100
+ const complexUrl = new ReadonlyUrl('https://dev.api.example.com/v2/users?active=true&count=100');
101
+ console.log(complexUrl.getSubdomain()); // "dev.api"
102
+ console.log(complexUrl.getDomain()); // "example.com"
103
+ console.log(complexUrl.getQueries()); // { active: true, count: 100 }
104
+
105
+ // Build URLs programmatically
106
+ const apiUrl = new Url('https://api.example.com')
107
+ .setPath('/users')
108
+ .setQueries({
109
+ page: 1,
110
+ limit: 20,
111
+ active: true,
112
+ search: 'john'
113
+ });
114
+
115
+ console.log(apiUrl.toString()); // "https://api.example.com/users?page=1&limit=20&active=true&search=john"
116
+
117
+ // Handle localhost and IP addresses
118
+ const localUrl = new ReadonlyUrl('http://localhost:3000/dashboard');
119
+ console.log(localUrl.getSubdomain()); // null
120
+ console.log(localUrl.getDomain()); // "localhost"
121
+
122
+ const ipUrl = new ReadonlyUrl('http://192.168.1.1:8080/status');
123
+ console.log(ipUrl.getSubdomain()); // null
124
+ console.log(ipUrl.getDomain()); // "192.168.1.1"
125
+ ```
126
+
127
+ ## API Reference
128
+
129
+ ### `ReadonlyUrl` Class
130
+
131
+ Immutable URL parsing class that provides read-only access to URL components.
132
+
133
+ #### Constructor
134
+
135
+ ##### `new ReadonlyUrl(url: string | URL)`
136
+ Creates a new ReadonlyUrl instance from a URL string or URL object.
137
+
138
+ **Parameters:**
139
+ - `url` - The URL string or URL object to parse
140
+
141
+ **Example:**
142
+ ```typescript
143
+ const url = new ReadonlyUrl('https://example.com/path?query=value#fragment');
144
+ const urlFromObject = new ReadonlyUrl(new URL('https://example.com'));
145
+ ```
146
+
147
+ #### Methods
148
+
149
+ ##### `getNative(): URL`
150
+ Returns the native JavaScript URL object.
151
+
152
+ **Returns:** Native URL object
153
+
154
+ **Example:**
155
+ ```typescript
156
+ const nativeUrl = url.getNative();
157
+ console.log(nativeUrl instanceof URL); // true
158
+ ```
159
+
160
+ ##### `getProtocol(): string`
161
+ Gets the protocol (without the colon).
162
+
163
+ **Returns:** Protocol string (e.g., "https", "http")
164
+
165
+ **Example:**
166
+ ```typescript
167
+ const url = new ReadonlyUrl('https://example.com');
168
+ console.log(url.getProtocol()); // "https"
169
+ ```
170
+
171
+ ##### `getSubdomain(): string | null`
172
+ Gets the subdomain portion of the hostname.
173
+
174
+ **Returns:** Subdomain string or null if no subdomain
175
+
176
+ **Example:**
177
+ ```typescript
178
+ const url = new ReadonlyUrl('https://blog.example.com');
179
+ console.log(url.getSubdomain()); // "blog"
180
+
181
+ const url2 = new ReadonlyUrl('https://example.com');
182
+ console.log(url2.getSubdomain()); // null
183
+ ```
184
+
185
+ ##### `getDomain(): string`
186
+ Gets the domain portion of the hostname.
187
+
188
+ **Returns:** Domain string
189
+
190
+ **Example:**
191
+ ```typescript
192
+ const url = new ReadonlyUrl('https://blog.example.com');
193
+ console.log(url.getDomain()); // "example.com"
194
+ ```
195
+
196
+ ##### `getHostname(): string`
197
+ Gets the full hostname.
198
+
199
+ **Returns:** Hostname string
200
+
201
+ **Example:**
202
+ ```typescript
203
+ const url = new ReadonlyUrl('https://blog.example.com:3000');
204
+ console.log(url.getHostname()); // "blog.example.com"
205
+ ```
206
+
207
+ ##### `getPort(): number`
208
+ Gets the port number.
209
+
210
+ **Returns:** Port number (defaults to 80 if not specified)
211
+
212
+ **Example:**
213
+ ```typescript
214
+ const url = new ReadonlyUrl('https://example.com:3000');
215
+ console.log(url.getPort()); // 3000
216
+
217
+ const url2 = new ReadonlyUrl('https://example.com');
218
+ console.log(url2.getPort()); // 80
219
+ ```
220
+
221
+ ##### `getPath(): string`
222
+ Gets the URL path.
223
+
224
+ **Returns:** Path string (always starts with "/")
225
+
226
+ **Example:**
227
+ ```typescript
228
+ const url = new ReadonlyUrl('https://example.com/users/123');
229
+ console.log(url.getPath()); // "/users/123"
230
+ ```
231
+
232
+ ##### `getQueries(): Record<string, ScalarType>`
233
+ Gets all query parameters as an object with automatically parsed types.
234
+
235
+ **Returns:** Object with query parameters (values can be string, number, or boolean)
236
+
237
+ **Example:**
238
+ ```typescript
239
+ const url = new ReadonlyUrl('https://example.com?page=1&active=true&name=john');
240
+ console.log(url.getQueries()); // { page: 1, active: true, name: "john" }
241
+ ```
242
+
243
+ ##### `getFragment(): string`
244
+ Gets the URL fragment (without the hash).
245
+
246
+ **Returns:** Fragment string
247
+
248
+ **Example:**
249
+ ```typescript
250
+ const url = new ReadonlyUrl('https://example.com#section');
251
+ console.log(url.getFragment()); // "section"
252
+ ```
253
+
254
+ ##### `getBase(): string`
255
+ Gets the base URL (protocol + hostname + port).
256
+
257
+ **Returns:** Base URL string
258
+
259
+ **Example:**
260
+ ```typescript
261
+ const url = new ReadonlyUrl('https://example.com:3000/path?query=value');
262
+ console.log(url.getBase()); // "https://example.com:3000"
263
+ ```
264
+
265
+ ##### `getOrigin(): string`
266
+ Gets the origin (protocol + hostname + port).
267
+
268
+ **Returns:** Origin string
269
+
270
+ **Example:**
271
+ ```typescript
272
+ const url = new ReadonlyUrl('https://example.com:3000/path');
273
+ console.log(url.getOrigin()); // "https://example.com:3000"
274
+ ```
275
+
276
+ ##### `toString(): string`
277
+ Converts the URL to its string representation.
278
+
279
+ **Returns:** Full URL string
280
+
281
+ **Example:**
282
+ ```typescript
283
+ const url = new ReadonlyUrl('https://example.com/path?query=value#fragment');
284
+ console.log(url.toString()); // "https://example.com/path?query=value#fragment"
285
+ ```
286
+
287
+ ### `Url` Class
288
+
289
+ Mutable URL class that extends ReadonlyUrl with modification capabilities.
290
+
291
+ #### Constructor
292
+
293
+ ##### `new Url(url: string | URL)`
294
+ Creates a new Url instance from a URL string or URL object.
295
+
296
+ **Parameters:**
297
+ - `url` - The URL string or URL object to parse
298
+
299
+ **Example:**
300
+ ```typescript
301
+ const url = new Url('https://example.com');
302
+ ```
303
+
304
+ #### Modification Methods
305
+
306
+ ##### `setProtocol(protocol: string): Url`
307
+ Sets the protocol.
308
+
309
+ **Parameters:**
310
+ - `protocol` - Protocol string (with or without colon)
311
+
312
+ **Returns:** Self for chaining
313
+
314
+ **Example:**
315
+ ```typescript
316
+ const url = new Url('https://example.com');
317
+ url.setProtocol('http');
318
+ console.log(url.getProtocol()); // "http"
319
+ ```
320
+
321
+ ##### `setHostname(hostname: string): Url`
322
+ Sets the hostname and automatically parses subdomain/domain.
323
+
324
+ **Parameters:**
325
+ - `hostname` - Hostname string
326
+
327
+ **Returns:** Self for chaining
328
+
329
+ **Example:**
330
+ ```typescript
331
+ const url = new Url('https://example.com');
332
+ url.setHostname('api.example.com');
333
+ console.log(url.getSubdomain()); // "api"
334
+ console.log(url.getDomain()); // "example.com"
335
+ ```
336
+
337
+ ##### `setPort(port: number): Url`
338
+ Sets the port number.
339
+
340
+ **Parameters:**
341
+ - `port` - Port number
342
+
343
+ **Returns:** Self for chaining
344
+
345
+ **Example:**
346
+ ```typescript
347
+ const url = new Url('https://example.com');
348
+ url.setPort(8080);
349
+ console.log(url.getPort()); // 8080
350
+ ```
351
+
352
+ ##### `setPath(path: string): Url`
353
+ Sets the URL path.
354
+
355
+ **Parameters:**
356
+ - `path` - Path string (leading/trailing slashes are normalized)
357
+
358
+ **Returns:** Self for chaining
359
+
360
+ **Example:**
361
+ ```typescript
362
+ const url = new Url('https://example.com');
363
+ url.setPath('/api/users');
364
+ console.log(url.getPath()); // "/api/users"
365
+ ```
366
+
367
+ ##### `addQuery(key: string, value: ScalarType): Url`
368
+ Adds or updates a query parameter.
369
+
370
+ **Parameters:**
371
+ - `key` - Query parameter name
372
+ - `value` - Query parameter value (string, number, or boolean)
373
+
374
+ **Returns:** Self for chaining
375
+
376
+ **Example:**
377
+ ```typescript
378
+ const url = new Url('https://example.com');
379
+ url.addQuery('page', 1)
380
+ .addQuery('active', true)
381
+ .addQuery('search', 'john');
382
+ console.log(url.getQueries()); // { page: 1, active: true, search: "john" }
383
+ ```
384
+
385
+ ##### `setQueries(queries: Record<string, ScalarType>): Url`
386
+ Sets all query parameters, replacing existing ones.
387
+
388
+ **Parameters:**
389
+ - `queries` - Object with query parameters
390
+
391
+ **Returns:** Self for chaining
392
+
393
+ **Example:**
394
+ ```typescript
395
+ const url = new Url('https://example.com?old=value');
396
+ url.setQueries({ page: 1, limit: 10 });
397
+ console.log(url.getQueries()); // { page: 1, limit: 10 }
398
+ ```
399
+
400
+ ##### `removeQuery(key: string): Url`
401
+ Removes a specific query parameter.
402
+
403
+ **Parameters:**
404
+ - `key` - Query parameter name to remove
405
+
406
+ **Returns:** Self for chaining
407
+
408
+ **Example:**
409
+ ```typescript
410
+ const url = new Url('https://example.com?page=1&limit=10');
411
+ url.removeQuery('page');
412
+ console.log(url.getQueries()); // { limit: 10 }
413
+ ```
414
+
415
+ ##### `clearQueries(): Url`
416
+ Removes all query parameters.
417
+
418
+ **Returns:** Self for chaining
419
+
420
+ **Example:**
421
+ ```typescript
422
+ const url = new Url('https://example.com?page=1&limit=10');
423
+ url.clearQueries();
424
+ console.log(url.getQueries()); // {}
425
+ ```
426
+
427
+ ##### `setFragment(fragment: string): Url`
428
+ Sets the URL fragment.
429
+
430
+ **Parameters:**
431
+ - `fragment` - Fragment string (with or without hash)
432
+
433
+ **Returns:** Self for chaining
434
+
435
+ **Example:**
436
+ ```typescript
437
+ const url = new Url('https://example.com');
438
+ url.setFragment('section');
439
+ console.log(url.getFragment()); // "section"
440
+ console.log(url.toString()); // "https://example.com#section"
441
+ ```
442
+
443
+ ### Types
444
+
445
+ #### `IReadonlyUrl`
446
+ Interface defining all available read-only URL methods.
447
+
448
+ #### `IUrl`
449
+ Interface defining all available URL manipulation methods (extends IReadonlyUrl).
450
+
451
+ #### `ScalarType`
452
+ Type representing valid query parameter values (string, number, or boolean).
453
+
454
+ ## License
455
+
456
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
457
+
458
+ ## Contributing
459
+
460
+ Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
461
+
462
+ ### Development Setup
463
+
464
+ 1. Clone the repository
465
+ 2. Install dependencies: `bun install`
466
+ 3. Run tests: `bun run test`
467
+ 4. Build the project: `bun run build`
468
+
469
+ ### Guidelines
470
+
471
+ - Write tests for new features
472
+ - Follow the existing code style
473
+ - Update documentation for API changes
474
+ - Ensure all tests pass before submitting PR
475
+
476
+ ---
477
+
478
+ Made with ❤️ by the Ooneex team
@@ -0,0 +1,71 @@
1
+ import { ScalarType as ScalarType2 } from "@ooneex/types";
2
+ import { ScalarType } from "@ooneex/types";
3
+ interface IReadonlyUrl {
4
+ getNative: () => URL;
5
+ getProtocol: () => string;
6
+ getSubdomain: () => string | null;
7
+ getDomain: () => string;
8
+ getHostname: () => string;
9
+ getPort: () => number;
10
+ getPath: () => string;
11
+ getQueries: () => Record<string, ScalarType>;
12
+ getFragment: () => string;
13
+ getBase: () => string;
14
+ getOrigin: () => string;
15
+ getQuery: (name: string) => ScalarType | null;
16
+ toString: () => string;
17
+ }
18
+ interface IUrl extends IReadonlyUrl {
19
+ setProtocol: (protocol: string) => IUrl;
20
+ setHostname: (hostname: string) => IUrl;
21
+ setPort: (port: number) => IUrl;
22
+ setPath: (path: string) => IUrl;
23
+ setFragment: (fragment: string) => IUrl;
24
+ addQuery: (key: string, value: ScalarType) => IUrl;
25
+ setQueries: (queries: Record<string, ScalarType>) => IUrl;
26
+ removeQuery: (key: string) => IUrl;
27
+ clearQueries: () => IUrl;
28
+ }
29
+ declare class ReadonlyUrl implements IReadonlyUrl {
30
+ protected native: URL;
31
+ protected protocol: string;
32
+ protected subdomain: string | null;
33
+ protected domain: string;
34
+ protected hostname: string;
35
+ protected port: number;
36
+ protected path: string;
37
+ protected queries: Record<string, ScalarType2>;
38
+ protected fragment: string;
39
+ protected base: string;
40
+ protected origin: string;
41
+ constructor(url: string | URL);
42
+ getNative(): URL;
43
+ getProtocol(): string;
44
+ getSubdomain(): string | null;
45
+ getDomain(): string;
46
+ getHostname(): string;
47
+ getPort(): number;
48
+ getPath(): string;
49
+ getQueries(): Record<string, ScalarType2>;
50
+ getQuery(name: string): ScalarType2 | null;
51
+ getFragment(): string;
52
+ getBase(): string;
53
+ getOrigin(): string;
54
+ toString(): string;
55
+ }
56
+ import { ScalarType as ScalarType3 } from "@ooneex/types";
57
+ declare class Url extends ReadonlyUrl implements IUrl {
58
+ setProtocol(protocol: string): this;
59
+ setHostname(hostname: string): this;
60
+ setPort(port: number): this;
61
+ setPath(path: string): this;
62
+ addQuery(key: string, value: ScalarType3): this;
63
+ removeQuery(key: string): this;
64
+ setQueries(queries: Record<string, ScalarType3>): this;
65
+ clearQueries(): this;
66
+ setFragment(fragment: string): this;
67
+ private updateNativeUrl;
68
+ private shouldShowPort;
69
+ private buildQueryString;
70
+ }
71
+ export { Url, ReadonlyUrl, IUrl, IReadonlyUrl };
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import{parseString as t,trim as x}from"@ooneex/utils";class i{native;protocol;subdomain;domain;hostname;port;path;queries={};fragment;base;origin;constructor(g){if(this.native=new URL(g),this.protocol=x(this.native.protocol,":"),this.subdomain=null,this.hostname=this.native.hostname,this.domain=this.hostname,!/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(this.hostname)&&this.hostname!=="localhost"){let e=/(?<subdomain>.+)\.(?<domain>[a-z0-9-_]+\.[a-z0-9]+)$/i.exec(this.domain);if(e){let{subdomain:r,domain:U}=e.groups;this.subdomain=r,this.domain=U}}if(this.native.port)this.port=t(this.native.port);else{let r=(typeof g==="string"?g:g.toString()).match(/:(\d+)/);if(r)this.port=t(r[1]);else this.port=80}if(this.native.pathname==="/")this.path="/";else this.path=this.native.pathname.replace(/\/+$/,"");this.fragment=x(this.native.hash,"#"),this.base=`${this.native.protocol}//${this.native.host}`,this.origin=this.native.origin;for(let[e,r]of this.native.searchParams)if(r==="true")this.queries[e]=!0;else if(r==="false")this.queries[e]=!1;else if(/^\d+$/.test(r)&&!r.startsWith("0"))this.queries[e]=Number.parseInt(r,10);else if(/^-?\d+(\.\d+)?$/.test(r)&&!r.startsWith("0"))this.queries[e]=Number.parseFloat(r);else this.queries[e]=r}getNative(){return this.native}getProtocol(){return this.protocol}getSubdomain(){return this.subdomain}getDomain(){return this.domain}getHostname(){return this.hostname}getPort(){return this.port}getPath(){return this.path}getQueries(){return{...this.queries}}getQuery(g){return this.queries[g]||null}getFragment(){return this.fragment}getBase(){return this.base}getOrigin(){return this.origin}toString(){return this.native.toString()}}import{trim as Q}from"@ooneex/utils";class s extends i{setProtocol(g){let I=this.protocol;if(this.protocol=Q(g,":"),I==="http"&&this.port===80&&this.protocol==="https")this.port=80;else if(I==="https"&&this.port===443&&this.protocol==="http")this.port=80;return this.updateNativeUrl(),this}setHostname(g){if(this.hostname=g,this.subdomain=null,this.domain=g,!/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(g)&&g!=="localhost"){let e=/(?<subdomain>.+)\.(?<domain>[a-z0-9-_]+\.[a-z0-9]+)$/i.exec(g);if(e){let{subdomain:r,domain:U}=e.groups;this.subdomain=r,this.domain=U}}return this.updateNativeUrl(),this}setPort(g){return this.port=g,this.updateNativeUrl(),this}setPath(g){if(g==="")this.path="/";else this.path=`/${Q(g,"/")}`;return this.updateNativeUrl(),this}addQuery(g,I){return this.queries[g]=I,this.updateNativeUrl(),this}removeQuery(g){return delete this.queries[g],this.updateNativeUrl(),this}setQueries(g){return this.queries={...g},this.updateNativeUrl(),this}clearQueries(){return this.queries={},this.updateNativeUrl(),this}setFragment(g){return this.fragment=Q(g,"#"),this.updateNativeUrl(),this}updateNativeUrl(){let g=this.protocol.includes(":")?this.protocol:`${this.protocol}:`,I=this.shouldShowPort()?`:${this.port}`:"",e=this.path,r=this.buildQueryString(),U=this.fragment?`#${this.fragment}`:"",T=`${g}//${this.hostname}${I}${e}${r}${U}`;this.native=new URL(T),this.base=this.shouldShowPort()?`${g}//${this.hostname}${I}`:`${g}//${this.hostname}`,this.origin=this.shouldShowPort()?`${g}//${this.hostname}${I}`:`${g}//${this.hostname}`}shouldShowPort(){if(this.protocol==="http"&&this.port===80)return!1;if(this.protocol==="https"&&this.port===443)return!1;if(this.port===80)return!1;return!0}buildQueryString(){let g=new URLSearchParams;for(let[e,r]of Object.entries(this.queries))g.set(e,String(r));let I=g.toString();return I?`?${I}`:""}}export{s as Url,i as ReadonlyUrl};
2
+
3
+ //# debugId=E5A2C3518A1FC26E64756E2164756E21
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["src/ReadonlyUrl.ts", "src/Url.ts"],
4
+ "sourcesContent": [
5
+ "import type { ScalarType } from \"@ooneex/types\";\nimport { parseString, trim } from \"@ooneex/utils\";\nimport type { IReadonlyUrl } from \"./types\";\n\nexport class ReadonlyUrl implements IReadonlyUrl {\n protected native: URL;\n protected protocol: string;\n protected subdomain: string | null;\n protected domain: string;\n protected hostname: string;\n protected port: number;\n protected path: string;\n protected queries: Record<string, ScalarType> = {};\n protected fragment: string;\n protected base: string;\n protected origin: string;\n\n constructor(url: string | URL) {\n this.native = new URL(url);\n\n this.protocol = trim(this.native.protocol, \":\");\n this.subdomain = null;\n this.hostname = this.native.hostname;\n this.domain = this.hostname;\n\n // Only parse domain/subdomain for actual domain names, not IP addresses\n const isIpAddress = /^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$/.test(this.hostname);\n if (!isIpAddress && this.hostname !== \"localhost\") {\n const match = /(?<subdomain>.+)\\.(?<domain>[a-z0-9-_]+\\.[a-z0-9]+)$/i.exec(this.domain);\n if (match) {\n const { subdomain, domain } = match.groups as {\n subdomain: string;\n domain: string;\n };\n this.subdomain = subdomain;\n this.domain = domain;\n }\n }\n\n // Handle port parsing - native URL omits default ports, but we need to detect them\n if (this.native.port) {\n this.port = parseString(this.native.port);\n } else {\n // Check if the original URL string had an explicit port\n const urlString = typeof url === \"string\" ? url : url.toString();\n const portMatch = urlString.match(/:(\\d+)/);\n if (portMatch) {\n this.port = parseString(portMatch[1] as string);\n } else {\n this.port = 80; // Default port\n }\n }\n // Handle path - preserve structure but handle trailing slashes correctly\n if (this.native.pathname === \"/\") {\n this.path = \"/\";\n } else {\n // Remove all trailing slashes if present, but preserve internal empty segments\n this.path = this.native.pathname.replace(/\\/+$/, \"\");\n }\n this.fragment = trim(this.native.hash, \"#\");\n this.base = `${this.native.protocol}//${this.native.host}`;\n this.origin = this.native.origin;\n\n for (const [key, value] of this.native.searchParams) {\n // Only parse as number/boolean if it's clearly intended to be\n if (value === \"true\") {\n this.queries[key] = true;\n } else if (value === \"false\") {\n this.queries[key] = false;\n } else if (/^\\d+$/.test(value) && !value.startsWith(\"0\")) {\n // Only parse as number if it's all digits and doesn't start with 0 (to preserve \"001\")\n this.queries[key] = Number.parseInt(value, 10);\n } else if (/^-?\\d+(\\.\\d+)?$/.test(value) && !value.startsWith(\"0\")) {\n // Parse as float if it's a valid number\n this.queries[key] = Number.parseFloat(value);\n } else {\n this.queries[key] = value;\n }\n }\n }\n\n public getNative(): URL {\n return this.native;\n }\n\n public getProtocol(): string {\n return this.protocol;\n }\n\n public getSubdomain(): string | null {\n return this.subdomain;\n }\n\n public getDomain(): string {\n return this.domain;\n }\n\n public getHostname(): string {\n return this.hostname;\n }\n\n public getPort(): number {\n return this.port;\n }\n\n public getPath(): string {\n return this.path;\n }\n\n public getQueries(): Record<string, ScalarType> {\n return { ...this.queries };\n }\n\n public getQuery(name: string): ScalarType | null {\n return this.queries[name] || null;\n }\n\n public getFragment(): string {\n return this.fragment;\n }\n\n public getBase(): string {\n return this.base;\n }\n\n public getOrigin(): string {\n return this.origin;\n }\n\n public toString(): string {\n return this.native.toString();\n }\n}\n",
6
+ "import type { ScalarType } from \"@ooneex/types\";\nimport { trim } from \"@ooneex/utils\";\nimport { ReadonlyUrl } from \"./ReadonlyUrl\";\nimport type { IUrl } from \"./types\";\n\nexport class Url extends ReadonlyUrl implements IUrl {\n public setProtocol(protocol: string): this {\n const oldProtocol = this.protocol;\n this.protocol = trim(protocol, \":\");\n\n // Update port based on protocol change if it was a default port\n if (oldProtocol === \"http\" && this.port === 80 && this.protocol === \"https\") {\n this.port = 80; // Keep 80 as default for all protocols as per tests\n } else if (oldProtocol === \"https\" && this.port === 443 && this.protocol === \"http\") {\n this.port = 80; // Keep 80 as default for all protocols as per tests\n }\n\n this.updateNativeUrl();\n return this;\n }\n\n public setHostname(hostname: string): this {\n this.hostname = hostname;\n\n this.subdomain = null;\n this.domain = hostname;\n\n // Only parse domain/subdomain for actual domain names, not IP addresses\n const isIpAddress = /^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$/.test(hostname);\n if (!isIpAddress && hostname !== \"localhost\") {\n const match = /(?<subdomain>.+)\\.(?<domain>[a-z0-9-_]+\\.[a-z0-9]+)$/i.exec(hostname);\n if (match) {\n const { subdomain, domain } = match.groups as {\n subdomain: string;\n domain: string;\n };\n this.subdomain = subdomain;\n this.domain = domain;\n }\n }\n\n this.updateNativeUrl();\n return this;\n }\n\n public setPort(port: number): this {\n this.port = port;\n this.updateNativeUrl();\n return this;\n }\n\n public setPath(path: string): this {\n if (path === \"\") {\n this.path = \"/\";\n } else {\n this.path = `/${trim(path, \"/\")}`;\n }\n this.updateNativeUrl();\n return this;\n }\n\n public addQuery(key: string, value: ScalarType): this {\n this.queries[key] = value;\n this.updateNativeUrl();\n return this;\n }\n\n public removeQuery(key: string): this {\n delete this.queries[key];\n this.updateNativeUrl();\n return this;\n }\n\n public setQueries(queries: Record<string, ScalarType>): this {\n this.queries = { ...queries };\n this.updateNativeUrl();\n return this;\n }\n\n public clearQueries(): this {\n this.queries = {};\n this.updateNativeUrl();\n return this;\n }\n\n public setFragment(fragment: string): this {\n this.fragment = trim(fragment, \"#\");\n this.updateNativeUrl();\n return this;\n }\n\n private updateNativeUrl() {\n const protocol = this.protocol.includes(\":\") ? this.protocol : `${this.protocol}:`;\n const port = this.shouldShowPort() ? `:${this.port}` : \"\";\n const path = this.path;\n const queryString = this.buildQueryString();\n const fragment = this.fragment ? `#${this.fragment}` : \"\";\n const urlString = `${protocol}//${this.hostname}${port}${path}${queryString}${fragment}`;\n this.native = new URL(urlString);\n this.base = this.shouldShowPort() ? `${protocol}//${this.hostname}${port}` : `${protocol}//${this.hostname}`;\n this.origin = this.shouldShowPort() ? `${protocol}//${this.hostname}${port}` : `${protocol}//${this.hostname}`;\n }\n\n private shouldShowPort(): boolean {\n if (this.protocol === \"http\" && this.port === 80) return false;\n if (this.protocol === \"https\" && this.port === 443) return false;\n if (this.port === 80) return false; // Don't show default port 80\n return true;\n }\n\n private buildQueryString(): string {\n const params = new URLSearchParams();\n for (const [key, value] of Object.entries(this.queries)) {\n params.set(key, String(value));\n }\n const queryString = params.toString();\n return queryString ? `?${queryString}` : \"\";\n }\n}\n"
7
+ ],
8
+ "mappings": "AACA,sBAAS,UAAa,sBAGf,MAAM,CAAoC,CACrC,OACA,SACA,UACA,OACA,SACA,KACA,KACA,QAAsC,CAAC,EACvC,SACA,KACA,OAEV,WAAW,CAAC,EAAmB,CAU7B,GATA,KAAK,OAAS,IAAI,IAAI,CAAG,EAEzB,KAAK,SAAW,EAAK,KAAK,OAAO,SAAU,GAAG,EAC9C,KAAK,UAAY,KACjB,KAAK,SAAW,KAAK,OAAO,SAC5B,KAAK,OAAS,KAAK,SAIf,CADgB,uCAAuC,KAAK,KAAK,QAAQ,GACzD,KAAK,WAAa,YAAa,CACjD,IAAM,EAAQ,wDAAwD,KAAK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
3
+ "description": "",
4
+ "version": "0.0.1",
5
+ "type": "module",
6
+ "files": [
7
+ "dist",
8
+ "LICENSE",
9
+ "README.md",
10
+ "package.json"
11
+ ],
12
+ "module": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "import": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ }
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "license": "MIT",
24
+ "scripts": {
25
+ "test": "bun test tests",
26
+ "build": "bunup",
27
+ "lint": "tsgo --noEmit && bunx biome lint",
28
+ "publish:prod": "bun publish --tolerate-republish --access public",
29
+ "publish:pack": "bun pm pack --destination ./dist",
30
+ "publish:dry": "bun publish --dry-run"
31
+ },
32
+ "dependencies": {
33
+ "@ooneex/utils": "0.0.8"
34
+ },
35
+ "devDependencies": {
36
+ "@ooneex/types": "0.0.1"
37
+ }
38
+ }