@blyou/reurl 0.0.1 → 0.0.3

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 CHANGED
@@ -1,132 +1,132 @@
1
- # reurl
2
-
3
- 超轻量的 URL 解析器:仅做必要的字符串切分与拼接,不做任何编码 / 解码 / 转义,不做规范化,不解析相对路径。与浏览器原生 `URL` / `URLSearchParams` 的 API 对齐,但运行时代码体积极小(构建产物约 1 KB gzip)。
4
-
5
- ## 特性
6
-
7
- - 🪶 **极小体积**:仅做必要的字符串切分与拼接,零依赖
8
- - 🔤 **原样保留**:中文、空格、`%20` 等字符一律不处理,查询串不经 percent 编解码
9
- - 🌐 **支持 IPv6**:`[2001:db8::1]:8080` 这类主机正确拆分 host / hostname / port
10
- - 🧩 **TypeScript 类型对齐**:导出 `ReURL` 与 `ReSearchParams`,签名对齐原生接口
11
-
12
- ## 安装
13
-
14
- ```bash
15
- pnpm i @blyou/reurl
16
- ```
17
-
18
- ## 使用
19
-
20
- ### ReURL
21
-
22
- ```ts
23
- import { ReURL } from '@blyou/reurl'
24
-
25
- const u = new ReURL('https://example.com:8080/a/b?x=1&y=2#frag')
26
-
27
- u.protocol // 'https:'
28
- u.host // 'example.com:8080'
29
- u.hostname // 'example.com'
30
- u.port // '8080'
31
- u.pathname // '/a/b'
32
- u.search // '?x=1&y=2'
33
- u.hash // '#frag'
34
- u.origin // 'https://example.com:8080'
35
- u.href // 'https://example.com:8080/a/b?x=1&y=2#frag'
36
- ```
37
-
38
- 所有 getter 都有对应的 setter,修改任一字段都会即时反映到 `href`:
39
-
40
- ```ts
41
- u.hostname = 'b.com' // host → 'b.com:8080'
42
- u.port = '9090' // host → 'b.com:9090'
43
- u.pathname = '/new' // href 同步更新
44
- ```
45
-
46
- #### userinfo(username / password)
47
-
48
- ```ts
49
- const u2 = new ReURL('https://user:pass@a.com/')
50
-
51
- u2.username // 'user'
52
- u2.password // 'pass'
53
- u2.password = 'new' // href → 'https://user:new@a.com/'
54
- u2.username = ''
55
- u2.password = '' // href → 'https://a.com/'(userinfo 整体移除)
56
- ```
57
-
58
- #### searchParams 双向联动
59
-
60
- ```ts
61
- const u = new ReURL('https://a.com/p?a=1&b=2')
62
- const sp = u.searchParams // 同一对象会被缓存
63
-
64
- sp.append('c', '3')
65
- u.href // 'https://a.com/p?a=1&b=2&c=3'
66
-
67
- u.search = 'k=v'
68
- sp.get('k') // 'v'
69
- ```
70
-
71
- ### ReSearchParams
72
-
73
- 查询串仅按 `&` / `=` 切分与拼接,不做编解码。
74
-
75
- ```ts
76
- import { ReSearchParams } from '@blyou/reurl'
77
-
78
- const sp = new ReSearchParams('?a=1&a=2&b=2')
79
-
80
- sp.get('a') // '1'
81
- sp.getAll('a') // ['1', '2']
82
- sp.has('b') // true
83
- sp.append('c', '3')
84
- sp.set('b', '9') // 先删后插,不保留键的原始位置
85
- sp.delete('a')
86
- sp.sort() // 按键稳定排序
87
- sp.toString() // 'b=9&c=3'
88
-
89
- // 迭代器
90
- for (const [key, value] of sp) {
91
- /* ... */
92
- }
93
- sp.keys()
94
- sp.values()
95
- sp.entries()
96
- ```
97
-
98
- 支持字符串 / 二维数组 / 对象 / `ReSearchParams` 多种构造方式:
99
-
100
- ```ts
101
- new ReSearchParams([
102
- ['a', '1'],
103
- ['b', '2'],
104
- ])
105
- new ReSearchParams({ a: '1', b: '2' })
106
- new ReSearchParams(new ReSearchParams('a=1'))
107
- ```
108
-
109
- ## 与原生 API 的有意差异
110
-
111
- 为了在最小体积下工作,`reurl` 刻意省略了一些原生行为:
112
-
113
- | 项目 | 原生行为 | reurl 行为 |
114
- | -------------------- | ---------------------------- | ---------------------------------------------- |
115
- | 非分层协议 | `data:` / `mailto:` 等可解析 | **不支持**,直接抛出 `TypeError` |
116
- | 相对路径 | `new URL('/p', base)` 可解析 | **不支持** `base` 参数(相对路径视为不可解析) |
117
- | `searchParams.set()` | 保留键的原始位置 | 先删后插,位置移到末尾 |
118
- | userinfo | 拆分且 percent 编解码 | 拆分 username / password,但原样保留不编解码 |
119
-
120
- ## 开发
121
-
122
- ```bash
123
- pnpm install # 安装依赖
124
- pnpm test # 运行单元测试
125
- pnpm typecheck # 类型检查
126
- pnpm lint # 代码检查
127
- pnpm build # 构建产物到 dist/
128
- ```
129
-
130
- ## License
131
-
132
- [MIT](./LICENSE)
1
+ # reurl
2
+
3
+ 超轻量的 URL 解析器:仅做必要的字符串切分与拼接,不做任何编码 / 解码 / 转义,不做规范化,不解析相对路径。与浏览器原生 `URL` / `URLSearchParams` 的 API 对齐,但运行时代码体积极小(构建产物约 1 KB gzip)。
4
+
5
+ ## 特性
6
+
7
+ - 🪶 **极小体积**:仅做必要的字符串切分与拼接,零依赖
8
+ - 🔤 **原样保留**:中文、空格、`%20` 等字符一律不处理,查询串不经 percent 编解码
9
+ - 🌐 **支持 IPv6**:`[2001:db8::1]:8080` 这类主机正确拆分 host / hostname / port
10
+ - 🧩 **TypeScript 类型对齐**:导出 `ReURL` 与 `ReSearchParams`,签名对齐原生接口
11
+
12
+ ## 安装
13
+
14
+ ```bash
15
+ pnpm i @blyou/reurl
16
+ ```
17
+
18
+ ## 使用
19
+
20
+ ### ReURL
21
+
22
+ ```ts
23
+ import { ReURL } from '@blyou/reurl'
24
+
25
+ const u = new ReURL('https://example.com:8080/a/b?x=1&y=2#frag')
26
+
27
+ u.protocol // 'https:'
28
+ u.host // 'example.com:8080'
29
+ u.hostname // 'example.com'
30
+ u.port // '8080'
31
+ u.pathname // '/a/b'
32
+ u.search // '?x=1&y=2'
33
+ u.hash // '#frag'
34
+ u.origin // 'https://example.com:8080'
35
+ u.href // 'https://example.com:8080/a/b?x=1&y=2#frag'
36
+ ```
37
+
38
+ 所有 getter 都有对应的 setter,修改任一字段都会即时反映到 `href`:
39
+
40
+ ```ts
41
+ u.hostname = 'b.com' // host → 'b.com:8080'
42
+ u.port = '9090' // host → 'b.com:9090'
43
+ u.pathname = '/new' // href 同步更新
44
+ ```
45
+
46
+ #### userinfo(username / password)
47
+
48
+ ```ts
49
+ const u2 = new ReURL('https://user:pass@a.com/')
50
+
51
+ u2.username // 'user'
52
+ u2.password // 'pass'
53
+ u2.password = 'new' // href → 'https://user:new@a.com/'
54
+ u2.username = ''
55
+ u2.password = '' // href → 'https://a.com/'(userinfo 整体移除)
56
+ ```
57
+
58
+ #### searchParams 双向联动
59
+
60
+ ```ts
61
+ const u = new ReURL('https://a.com/p?a=1&b=2')
62
+ const sp = u.searchParams // 同一对象会被缓存
63
+
64
+ sp.append('c', '3')
65
+ u.href // 'https://a.com/p?a=1&b=2&c=3'
66
+
67
+ u.search = 'k=v'
68
+ sp.get('k') // 'v'
69
+ ```
70
+
71
+ ### ReSearchParams
72
+
73
+ 查询串仅按 `&` / `=` 切分与拼接,不做编解码。
74
+
75
+ ```ts
76
+ import { ReSearchParams } from '@blyou/reurl'
77
+
78
+ const sp = new ReSearchParams('?a=1&a=2&b=2')
79
+
80
+ sp.get('a') // '1'
81
+ sp.getAll('a') // ['1', '2']
82
+ sp.has('b') // true
83
+ sp.append('c', '3')
84
+ sp.set('b', '9') // 先删后插,不保留键的原始位置
85
+ sp.delete('a')
86
+ sp.sort() // 按键稳定排序
87
+ sp.toString() // 'b=9&c=3'
88
+
89
+ // 迭代器
90
+ for (const [key, value] of sp) {
91
+ /* ... */
92
+ }
93
+ sp.keys()
94
+ sp.values()
95
+ sp.entries()
96
+ ```
97
+
98
+ 支持字符串 / 二维数组 / 对象 / `ReSearchParams` 多种构造方式:
99
+
100
+ ```ts
101
+ new ReSearchParams([
102
+ ['a', '1'],
103
+ ['b', '2'],
104
+ ])
105
+ new ReSearchParams({ a: '1', b: '2' })
106
+ new ReSearchParams(new ReSearchParams('a=1'))
107
+ ```
108
+
109
+ ## 与原生 API 的有意差异
110
+
111
+ 为了在最小体积下工作,`reurl` 刻意省略了一些原生行为:
112
+
113
+ | 项目 | 原生行为 | reurl 行为 |
114
+ | -------------------- | ---------------------------- | ---------------------------------------------- |
115
+ | 非分层协议 | `data:` / `mailto:` 等可解析 | **不支持**,直接抛出 `TypeError` |
116
+ | 相对路径 | `new URL('/p', base)` 可解析 | **不支持** `base` 参数(相对路径视为不可解析) |
117
+ | `searchParams.set()` | 保留键的原始位置 | 先删后插,位置移到末尾 |
118
+ | userinfo | 拆分且 percent 编解码 | 拆分 username / password,但原样保留不编解码 |
119
+
120
+ ## 开发
121
+
122
+ ```bash
123
+ pnpm install # 安装依赖
124
+ pnpm test # 运行单元测试
125
+ pnpm typecheck # 类型检查
126
+ pnpm lint # 代码检查
127
+ pnpm build # 构建产物到 dist/
128
+ ```
129
+
130
+ ## License
131
+
132
+ [MIT](./LICENSE)
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{constructor(e){this.href=e}get href(){let{username:e,password:t}=this;return this.t+`//`+(e||t?e+r(`:`,t)+`@`:``)+this.i+this.h+this.o+this.l}set href(e){let t=``+e,r=TypeError(`Invalid URL: `+e);if(!n.test(t))throw r;let i=t.indexOf(`#`);if(~i?(this.l=t.slice(i),t=t.slice(0,i)):this.l=``,i=t.indexOf(`?`),~i?(this.o=t.slice(i),t=t.slice(0,i)):this.o=``,this.t=n.exec(t)[0],t=t.slice(this.t.length),this.i=``,!t.startsWith(`//`))throw r;i=t.indexOf(`/`,2),~i?(this.host=t.slice(2,i),t=t.slice(i)):(this.host=t.slice(2),t=``),this.pathname=t,this.p?.u(this.o)}get protocol(){return this.t}set protocol(e){this.t=n.test(e)?e:e+`:`}get host(){return this.i}set host(e){let t=e.lastIndexOf(`@`);if(~t){let[n,r=``]=e.slice(0,t).split(`:`);this.username=n,this.password=r,e=e.slice(t+1)}else this.username=this.password=``;this.i=e;let n=e.indexOf(`:`,e.lastIndexOf(`]`)+1);~n?(this.R=e.slice(0,n),this.S=e.slice(n+1)):(this.R=e,this.S=``)}get hostname(){return this.R}set hostname(e){this.R=e,this.i=e+r(`:`,this.S)}get port(){return this.S}set port(e){this.i=this.R+r(`:`,this.S=e)}get pathname(){return this.h}set pathname(e){this.h=r(`/`,e)||`/`}get search(){return this.o}set search(e){this.o=r(`?`,e),this.p?.u(this.o)}get hash(){return this.l}set hash(e){this.l=r(`#`,e)}get origin(){return this.i?this.t+`//`+this.i:`null`}get searchParams(){return this.p??=new t(this)}toString(){return this.href}toJSON(){return this.href}},t=class t{m=[];constructor(n){if(n instanceof e&&(this._=n,n=n.o),n instanceof t&&(n=[...n]),typeof n==`string`)this.u(n);else if(n!=null)for(let[e,t=``]of Array.isArray(n)?n:Object.entries(n))this.m.push([e,t])}u(e){if(this.m=[],e=e[0]==`?`?e.slice(1):e)for(let t of e.split(`&`)){let[e,n=``]=t.split(`=`);this.m.push([e,n])}}M(){this._&&(this._.o=`?`+this)}append(e,t){this.M(this.m.push([e,t]))}delete(e,t){this.M(this.m=this.m.filter(n=>n[0]!=e||t!=null&&n[1]!=t))}get(e){return this.m.find(t=>t[0]==e)?.[1]??null}getAll(e){return this.m.filter(t=>t[0]==e).map(e=>e[1])}has(e,t){return this.m.some(n=>n[0]==e&&(t==null||n[1]==t))}set(e,t){this.delete(e),this.append(e,t)}sort(){this.M(this.m.sort(([e],[t])=>e<t?-1:+(e>t)))}toString(){return this.m.map(e=>e.join(`=`)).join(`&`)}*entries(){for(let e of this.m)yield e}*keys(){for(let e of this.m)yield e[0]}*values(){for(let e of this.m)yield e[1]}[Symbol.iterator](){return this.entries()}};const n=/^[a-zA-Z][a-zA-Z\d+.-]*:/,r=(e,t)=>t&&t[0]!=e?e+t:t;exports.ReSearchParams=t,exports.ReURL=e;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=/^[a-zA-Z][a-zA-Z\d+.-]*:/,t=(e,t)=>e&&t&&t[0]!=e?e+t:t;var n=class{constructor(e){this.href=e}get href(){let{username:e,password:n}=this;return this.t+`//`+(e||n?e+t(`:`,n)+`@`:``)+this.h+this.i+this.o+this.u}set href(t){let n=``+t,r=TypeError(`Invalid URL: `+t);if(!e.test(n))throw r;let i=n.indexOf(`#`);if(~i?(this.u=n.slice(i),n=n.slice(0,i)):this.u=``,i=n.indexOf(`?`),~i?(this.o=n.slice(i),n=n.slice(0,i)):this.o=``,this.t=e.exec(n)[0],n=n.slice(this.t.length),this.h=``,!n.startsWith(`//`))throw r;i=n.indexOf(`/`,2),~i?(this.host=n.slice(2,i),n=n.slice(i)):(this.host=n.slice(2),n=``),this.pathname=n,this.p?.l(this.o)}get protocol(){return this.t}set protocol(t){this.t=e.test(t)?t:t+`:`}get host(){return this.h}set host(e){let t=e.lastIndexOf(`@`);if(~t){let[n,r=``]=e.slice(0,t).split(`:`);this.username=n,this.password=r,e=e.slice(t+1)}else this.username=this.password=``;this.h=e;let n=e.indexOf(`:`,e.lastIndexOf(`]`)+1);~n?(this.m=e.slice(0,n),this.R=e.slice(n+1)):(this.m=e,this.R=``)}get hostname(){return this.m}set hostname(e){this.m=e,this.h=e+t(`:`,this.R)}get port(){return this.R}set port(e){this.h=this.m+t(`:`,this.R=e)}get pathname(){return this.i}set pathname(e){this.i=t(`/`,e)||`/`}get search(){return this.o}set search(e){this.o=t(`?`,e),this.p?.l(this.o)}get hash(){return this.u}set hash(e){this.u=t(`#`,e)}get origin(){return this.h?this.t+`//`+this.h:`null`}get searchParams(){return this.p||(this.p=new r(this.o),this.p.S=this),this.p}toString(){return this.href}toJSON(){return this.href}},r=class e{P=[];constructor(t){if(t instanceof e&&(t=[...t]),typeof t==`string`)this.l(t);else if(t!=null)for(let[e,n=``]of Array.isArray(t)?t:Object.entries(t))this.P.push([e,n])}l(e){if(this.P=[],e=e[0]==`?`?e.slice(1):e)for(let t of e.split(`&`)){let[e,n=``]=t.split(`=`);this.P.push([e,n])}}_(){this.S&&(this.S.o=`?`+this)}append(e,t){this._(this.P.push([e,t]))}delete(e,t){this._(this.P=this.P.filter(n=>n[0]!=e||t!=null&&n[1]!=t))}get(e){return this.P.find(t=>t[0]==e)?.[1]??null}getAll(e){return this.P.filter(t=>t[0]==e).map(e=>e[1])}has(e,t){return this.P.some(n=>n[0]==e&&(t==null||n[1]==t))}set(e,t){this.delete(e),this.append(e,t)}sort(){this._(this.P.sort(([e],[t])=>e<t?-1:+(e>t)))}toString(e){return t(e,this.P.map(e=>e.join(`=`)).join(`&`))}*entries(){for(let e of this.P)yield e}*keys(){for(let e of this.P)yield e[0]}*values(){for(let e of this.P)yield e[1]}[Symbol.iterator](){return this.entries()}},i=class{constructor(e){this.href=e}get href(){return this.i+this.o+this.u}set href(e){let t=``+e,n=t.indexOf(`#`);~n?(this.u=t.slice(n),t=t.slice(0,n)):this.u=``,n=t.indexOf(`?`),~n?(this.o=t.slice(n),t=t.slice(0,n)):this.o=``,this.pathname=t,this.p?.l(this.o)}get pathname(){return this.i}set pathname(e){this.i=t(`/`,e)||`/`}get search(){return this.o}set search(e){this.o=t(`?`,e),this.p?.l(this.o)}get hash(){return this.u}set hash(e){this.u=t(`#`,e)}get searchParams(){return this.p||(this.p=new r(this.o),this.p.S=this),this.p}toString(){return this.href}toJSON(){return this.href}};exports.RePath=i,exports.ReSearchParams=r,exports.ReURL=n,exports.prefix=t;
package/dist/index.d.cts CHANGED
@@ -11,6 +11,8 @@
11
11
  * - `searchParams.set()` 不保留键的原始位置(先删后插)
12
12
  * - userinfo 原样保留(原生 URL 会对 username / password 做 percent 编解码,reurl 不做)
13
13
  */
14
+ /** 前缀拼接,确保前缀不重复出现 */
15
+ declare const prefix: (prefix: string | undefined | null, value: string) => string;
14
16
  declare class ReURL {
15
17
  private _protocol;
16
18
  username: string;
@@ -62,11 +64,32 @@ declare class ReSearchParams {
62
64
  /** 按键稳定排序 */
63
65
  sort(): void;
64
66
  /** 仅拼接,不做任何编码 */
65
- toString(): string;
67
+ toString(pre?: string): string;
66
68
  entries(): IterableIterator<[string, string]>;
67
69
  keys(): IterableIterator<string>;
68
70
  values(): IterableIterator<string>;
69
71
  [Symbol.iterator](): IterableIterator<[string, string]>;
70
72
  }
73
+ declare class RePath {
74
+ private _pathname;
75
+ private _search;
76
+ private _hash;
77
+ private _searchParams?;
78
+ /**
79
+ * @param path 合法的路径查询串,如 `/a/b/c?x=1&y=2#frag`
80
+ */
81
+ constructor(path: string | RePath);
82
+ get href(): string;
83
+ set href(value: string);
84
+ get pathname(): string;
85
+ set pathname(value: string);
86
+ get search(): string;
87
+ set search(value: string);
88
+ get hash(): string;
89
+ set hash(value: string);
90
+ get searchParams(): ReSearchParams;
91
+ toString(): string;
92
+ toJSON(): string;
93
+ }
71
94
  //#endregion
72
- export { ReSearchParams, ReURL };
95
+ export { RePath, ReSearchParams, ReURL, prefix };
package/dist/index.d.mts CHANGED
@@ -11,6 +11,8 @@
11
11
  * - `searchParams.set()` 不保留键的原始位置(先删后插)
12
12
  * - userinfo 原样保留(原生 URL 会对 username / password 做 percent 编解码,reurl 不做)
13
13
  */
14
+ /** 前缀拼接,确保前缀不重复出现 */
15
+ declare const prefix: (prefix: string | undefined | null, value: string) => string;
14
16
  declare class ReURL {
15
17
  private _protocol;
16
18
  username: string;
@@ -62,11 +64,32 @@ declare class ReSearchParams {
62
64
  /** 按键稳定排序 */
63
65
  sort(): void;
64
66
  /** 仅拼接,不做任何编码 */
65
- toString(): string;
67
+ toString(pre?: string): string;
66
68
  entries(): IterableIterator<[string, string]>;
67
69
  keys(): IterableIterator<string>;
68
70
  values(): IterableIterator<string>;
69
71
  [Symbol.iterator](): IterableIterator<[string, string]>;
70
72
  }
73
+ declare class RePath {
74
+ private _pathname;
75
+ private _search;
76
+ private _hash;
77
+ private _searchParams?;
78
+ /**
79
+ * @param path 合法的路径查询串,如 `/a/b/c?x=1&y=2#frag`
80
+ */
81
+ constructor(path: string | RePath);
82
+ get href(): string;
83
+ set href(value: string);
84
+ get pathname(): string;
85
+ set pathname(value: string);
86
+ get search(): string;
87
+ set search(value: string);
88
+ get hash(): string;
89
+ set hash(value: string);
90
+ get searchParams(): ReSearchParams;
91
+ toString(): string;
92
+ toJSON(): string;
93
+ }
71
94
  //#endregion
72
- export { ReSearchParams, ReURL };
95
+ export { RePath, ReSearchParams, ReURL, prefix };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var e=class{constructor(e){this.href=e}get href(){let{username:e,password:t}=this;return this.t+`//`+(e||t?e+r(`:`,t)+`@`:``)+this.i+this.h+this.o+this.l}set href(e){let t=``+e,r=TypeError(`Invalid URL: `+e);if(!n.test(t))throw r;let i=t.indexOf(`#`);if(~i?(this.l=t.slice(i),t=t.slice(0,i)):this.l=``,i=t.indexOf(`?`),~i?(this.o=t.slice(i),t=t.slice(0,i)):this.o=``,this.t=n.exec(t)[0],t=t.slice(this.t.length),this.i=``,!t.startsWith(`//`))throw r;i=t.indexOf(`/`,2),~i?(this.host=t.slice(2,i),t=t.slice(i)):(this.host=t.slice(2),t=``),this.pathname=t,this.p?.u(this.o)}get protocol(){return this.t}set protocol(e){this.t=n.test(e)?e:e+`:`}get host(){return this.i}set host(e){let t=e.lastIndexOf(`@`);if(~t){let[n,r=``]=e.slice(0,t).split(`:`);this.username=n,this.password=r,e=e.slice(t+1)}else this.username=this.password=``;this.i=e;let n=e.indexOf(`:`,e.lastIndexOf(`]`)+1);~n?(this.m=e.slice(0,n),this.A=e.slice(n+1)):(this.m=e,this.A=``)}get hostname(){return this.m}set hostname(e){this.m=e,this.i=e+r(`:`,this.A)}get port(){return this.A}set port(e){this.i=this.m+r(`:`,this.A=e)}get pathname(){return this.h}set pathname(e){this.h=r(`/`,e)||`/`}get search(){return this.o}set search(e){this.o=r(`?`,e),this.p?.u(this.o)}get hash(){return this.l}set hash(e){this.l=r(`#`,e)}get origin(){return this.i?this.t+`//`+this.i:`null`}get searchParams(){return this.p??=new t(this)}toString(){return this.href}toJSON(){return this.href}},t=class t{S=[];constructor(n){if(n instanceof e&&(this.v=n,n=n.o),n instanceof t&&(n=[...n]),typeof n==`string`)this.u(n);else if(n!=null)for(let[e,t=``]of Array.isArray(n)?n:Object.entries(n))this.S.push([e,t])}u(e){if(this.S=[],e=e[0]==`?`?e.slice(1):e)for(let t of e.split(`&`)){let[e,n=``]=t.split(`=`);this.S.push([e,n])}}_(){this.v&&(this.v.o=`?`+this)}append(e,t){this._(this.S.push([e,t]))}delete(e,t){this._(this.S=this.S.filter(n=>n[0]!=e||t!=null&&n[1]!=t))}get(e){return this.S.find(t=>t[0]==e)?.[1]??null}getAll(e){return this.S.filter(t=>t[0]==e).map(e=>e[1])}has(e,t){return this.S.some(n=>n[0]==e&&(t==null||n[1]==t))}set(e,t){this.delete(e),this.append(e,t)}sort(){this._(this.S.sort(([e],[t])=>e<t?-1:+(e>t)))}toString(){return this.S.map(e=>e.join(`=`)).join(`&`)}*entries(){for(let e of this.S)yield e}*keys(){for(let e of this.S)yield e[0]}*values(){for(let e of this.S)yield e[1]}[Symbol.iterator](){return this.entries()}};const n=/^[a-zA-Z][a-zA-Z\d+.-]*:/,r=(e,t)=>t&&t[0]!=e?e+t:t;export{t as ReSearchParams,e as ReURL};
1
+ const e=/^[a-zA-Z][a-zA-Z\d+.-]*:/,t=(e,t)=>e&&t&&t[0]!=e?e+t:t;var n=class{constructor(e){this.href=e}get href(){let{username:e,password:n}=this;return this.t+`//`+(e||n?e+t(`:`,n)+`@`:``)+this.h+this.i+this.o+this.u}set href(t){let n=``+t,r=TypeError(`Invalid URL: `+t);if(!e.test(n))throw r;let i=n.indexOf(`#`);if(~i?(this.u=n.slice(i),n=n.slice(0,i)):this.u=``,i=n.indexOf(`?`),~i?(this.o=n.slice(i),n=n.slice(0,i)):this.o=``,this.t=e.exec(n)[0],n=n.slice(this.t.length),this.h=``,!n.startsWith(`//`))throw r;i=n.indexOf(`/`,2),~i?(this.host=n.slice(2,i),n=n.slice(i)):(this.host=n.slice(2),n=``),this.pathname=n,this.p?.l(this.o)}get protocol(){return this.t}set protocol(t){this.t=e.test(t)?t:t+`:`}get host(){return this.h}set host(e){let t=e.lastIndexOf(`@`);if(~t){let[n,r=``]=e.slice(0,t).split(`:`);this.username=n,this.password=r,e=e.slice(t+1)}else this.username=this.password=``;this.h=e;let n=e.indexOf(`:`,e.lastIndexOf(`]`)+1);~n?(this.m=e.slice(0,n),this.S=e.slice(n+1)):(this.m=e,this.S=``)}get hostname(){return this.m}set hostname(e){this.m=e,this.h=e+t(`:`,this.S)}get port(){return this.S}set port(e){this.h=this.m+t(`:`,this.S=e)}get pathname(){return this.i}set pathname(e){this.i=t(`/`,e)||`/`}get search(){return this.o}set search(e){this.o=t(`?`,e),this.p?.l(this.o)}get hash(){return this.u}set hash(e){this.u=t(`#`,e)}get origin(){return this.h?this.t+`//`+this.h:`null`}get searchParams(){return this.p||(this.p=new r(this.o),this.p.A=this),this.p}toString(){return this.href}toJSON(){return this.href}},r=class e{v=[];constructor(t){if(t instanceof e&&(t=[...t]),typeof t==`string`)this.l(t);else if(t!=null)for(let[e,n=``]of Array.isArray(t)?t:Object.entries(t))this.v.push([e,n])}l(e){if(this.v=[],e=e[0]==`?`?e.slice(1):e)for(let t of e.split(`&`)){let[e,n=``]=t.split(`=`);this.v.push([e,n])}}O(){this.A&&(this.A.o=`?`+this)}append(e,t){this.O(this.v.push([e,t]))}delete(e,t){this.O(this.v=this.v.filter(n=>n[0]!=e||t!=null&&n[1]!=t))}get(e){return this.v.find(t=>t[0]==e)?.[1]??null}getAll(e){return this.v.filter(t=>t[0]==e).map(e=>e[1])}has(e,t){return this.v.some(n=>n[0]==e&&(t==null||n[1]==t))}set(e,t){this.delete(e),this.append(e,t)}sort(){this.O(this.v.sort(([e],[t])=>e<t?-1:+(e>t)))}toString(e){return t(e,this.v.map(e=>e.join(`=`)).join(`&`))}*entries(){for(let e of this.v)yield e}*keys(){for(let e of this.v)yield e[0]}*values(){for(let e of this.v)yield e[1]}[Symbol.iterator](){return this.entries()}},i=class{constructor(e){this.href=e}get href(){return this.i+this.o+this.u}set href(e){let t=``+e,n=t.indexOf(`#`);~n?(this.u=t.slice(n),t=t.slice(0,n)):this.u=``,n=t.indexOf(`?`),~n?(this.o=t.slice(n),t=t.slice(0,n)):this.o=``,this.pathname=t,this.p?.l(this.o)}get pathname(){return this.i}set pathname(e){this.i=t(`/`,e)||`/`}get search(){return this.o}set search(e){this.o=t(`?`,e),this.p?.l(this.o)}get hash(){return this.u}set hash(e){this.u=t(`#`,e)}get searchParams(){return this.p||(this.p=new r(this.o),this.p.A=this),this.p}toString(){return this.href}toJSON(){return this.href}};export{i as RePath,r as ReSearchParams,n as ReURL,t as prefix};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blyou/reurl",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "A tiny zero-dependency URL parser.",
5
5
  "keywords": [
6
6
  "url",
@@ -20,6 +20,7 @@
20
20
  "dist"
21
21
  ],
22
22
  "type": "module",
23
+ "sideEffects": false,
23
24
  "main": "./dist/index.cjs",
24
25
  "module": "./dist/index.mjs",
25
26
  "types": "./dist/index.d.cts",