react-util-tools 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/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "react-util-tools",
3
+ "version": "1.0.0",
4
+ "description": "A collection of useful utilities: throttle, debounce, date formatting, device detection, money formatting, decimal calculations and more",
5
+ "type": "module",
6
+ "main": "dist/index.cjs",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "sideEffects": false,
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "scripts": {
18
+ "build": "tsup src/index.ts --format esm,cjs --dts",
19
+ "dev": "tsup src/index.ts --watch"
20
+ },
21
+ "keywords": [
22
+ "react",
23
+ "utils",
24
+ "tools",
25
+ "throttle",
26
+ "debounce",
27
+ "date-fns",
28
+ "decimal",
29
+ "format",
30
+ "device",
31
+ "browser-detection",
32
+ "money-format",
33
+ "url-params",
34
+ "typescript"
35
+ ],
36
+ "author": "alice & bobby",
37
+ "license": "MIT",
38
+ "devDependencies": {
39
+ "tsup": "^8.0.0",
40
+ "typescript": "^5.9.3"
41
+ },
42
+ "peerDependencies": {
43
+ "react": ">=19.2.0"
44
+ },
45
+ "dependencies": {
46
+ "date-fns": "^4.1.0",
47
+ "date-fns-tz": "^3.2.0",
48
+ "decimal.js": "^10.6.0"
49
+ }
50
+ }
@@ -0,0 +1,196 @@
1
+ # Address 工具
2
+
3
+ 提供 URL 地址栏参数获取的工具函数。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install your-package-name
9
+ ```
10
+
11
+ ## 使用方法
12
+
13
+ ### 导入
14
+
15
+ ```typescript
16
+ import { getQueryParam, getAllQueryParams, getQueryParamAll } from 'your-package-name'
17
+ ```
18
+
19
+ ## API
20
+
21
+ ### getQueryParam - 获取单个查询参数
22
+
23
+ 获取 URL 中指定的查询参数值。
24
+
25
+ **参数:**
26
+ - `key`: 参数名
27
+ - `url`: 可选,指定 URL,默认使用当前页面 URL
28
+
29
+ **返回:** 参数值(string)或 null
30
+
31
+ **示例:**
32
+
33
+ ```typescript
34
+ // URL: https://example.com?name=张三&age=25
35
+
36
+ const name = getQueryParam('name')
37
+ console.log(name) // "张三"
38
+
39
+ const city = getQueryParam('city')
40
+ console.log(city) // null
41
+
42
+ // 解析指定 URL
43
+ const value = getQueryParam('id', 'https://example.com?id=123')
44
+ console.log(value) // "123"
45
+ ```
46
+
47
+ ### getAllQueryParams - 获取所有查询参数
48
+
49
+ 获取 URL 中所有的查询参数,返回对象。
50
+
51
+ **参数:**
52
+ - `url`: 可选,指定 URL,默认使用当前页面 URL
53
+
54
+ **返回:** 包含所有参数的对象
55
+
56
+ **示例:**
57
+
58
+ ```typescript
59
+ // URL: https://example.com?name=张三&age=25&city=北京
60
+
61
+ const params = getAllQueryParams()
62
+ console.log(params)
63
+ // { name: "张三", age: "25", city: "北京" }
64
+
65
+ // 解析指定 URL
66
+ const params2 = getAllQueryParams('https://example.com?id=1&type=user')
67
+ console.log(params2)
68
+ // { id: "1", type: "user" }
69
+ ```
70
+
71
+ ### getQueryParamAll - 获取同名参数的所有值
72
+
73
+ 获取 URL 中同名参数的所有值(数组形式)。
74
+
75
+ **参数:**
76
+ - `key`: 参数名
77
+ - `url`: 可选,指定 URL,默认使用当前页面 URL
78
+
79
+ **返回:** 参数值数组
80
+
81
+ **示例:**
82
+
83
+ ```typescript
84
+ // URL: https://example.com?tag=前端&tag=React&tag=TypeScript
85
+
86
+ const tags = getQueryParamAll('tag')
87
+ console.log(tags)
88
+ // ["前端", "React", "TypeScript"]
89
+
90
+ const ids = getQueryParamAll('id')
91
+ console.log(ids)
92
+ // [] (不存在返回空数组)
93
+ ```
94
+
95
+ ## 实际应用场景
96
+
97
+ ### 获取分页参数
98
+
99
+ ```typescript
100
+ import { getQueryParam } from 'your-package-name'
101
+
102
+ // 获取当前页码
103
+ const currentPage = Number(getQueryParam('page')) || 1
104
+ const pageSize = Number(getQueryParam('size')) || 20
105
+
106
+ console.log(`当前第 ${currentPage} 页,每页 ${pageSize} 条`)
107
+ ```
108
+
109
+ ### 获取搜索和过滤条件
110
+
111
+ ```typescript
112
+ import { getAllQueryParams } from 'your-package-name'
113
+
114
+ // URL: ?keyword=React&category=前端&sort=latest
115
+
116
+ const filters = getAllQueryParams()
117
+ console.log(filters)
118
+ // { keyword: "React", category: "前端", sort: "latest" }
119
+
120
+ // 使用过滤条件
121
+ function fetchData() {
122
+ const { keyword, category, sort } = filters
123
+ // 调用 API...
124
+ }
125
+ ```
126
+
127
+ ### 获取多选标签
128
+
129
+ ```typescript
130
+ import { getQueryParamAll } from 'your-package-name'
131
+
132
+ // URL: ?tag=React&tag=TypeScript&tag=Hooks
133
+
134
+ const selectedTags = getQueryParamAll('tag')
135
+ console.log(selectedTags) // ["React", "TypeScript", "Hooks"]
136
+
137
+ // 渲染标签列表
138
+ selectedTags.forEach(tag => {
139
+ console.log(`已选择: ${tag}`)
140
+ })
141
+ ```
142
+
143
+ ### 用户认证
144
+
145
+ ```typescript
146
+ import { getQueryParam } from 'your-package-name'
147
+
148
+ // URL: ?token=abc123&redirect=/dashboard
149
+
150
+ const token = getQueryParam('token')
151
+ const redirect = getQueryParam('redirect') || '/'
152
+
153
+ if (token) {
154
+ // 保存 token
155
+ localStorage.setItem('token', token)
156
+ // 跳转到指定页面
157
+ window.location.href = redirect
158
+ }
159
+ ```
160
+
161
+ ### 表单回显
162
+
163
+ ```typescript
164
+ import { getAllQueryParams } from 'your-package-name'
165
+
166
+ // URL: ?name=张三&email=test@example.com&age=25
167
+
168
+ const formData = getAllQueryParams()
169
+
170
+ // 回显到表单
171
+ document.querySelector('#name').value = formData.name || ''
172
+ document.querySelector('#email').value = formData.email || ''
173
+ document.querySelector('#age').value = formData.age || ''
174
+ ```
175
+
176
+ ## TypeScript 支持
177
+
178
+ 完整的 TypeScript 类型支持。
179
+
180
+ ```typescript
181
+ // 类型安全
182
+ const params: Record<string, string> = getAllQueryParams()
183
+ const value: string | null = getQueryParam('id')
184
+ const values: string[] = getQueryParamAll('tag')
185
+
186
+ // 类型转换示例
187
+ const page = Number(getQueryParam('page')) || 1
188
+ const isActive = getQueryParam('active') === 'true'
189
+ ```
190
+
191
+ ## 注意事项
192
+
193
+ - 所有参数值都是字符串类型,使用时需要自行转换(如 `Number()`, `Boolean()` 等)
194
+ - 不存在的参数:`getQueryParam` 返回 `null`,`getQueryParamAll` 返回空数组 `[]`
195
+ - 支持解析当前页面 URL 或指定的 URL 字符串
196
+ - 同名参数使用 `getQueryParamAll` 获取所有值,使用 `getQueryParam` 只获取第一个值
@@ -0,0 +1,41 @@
1
+ /**
2
+ * 获取 URL 中指定的查询参数
3
+ * @param key 参数名
4
+ * @param url 可选的 URL,默认使用当前页面 URL
5
+ * @returns 参数值,不存在则返回 null
6
+ */
7
+ export function getQueryParam(key: string, url?: string): string | null {
8
+ const searchParams = new URLSearchParams(
9
+ url ? new URL(url).search : window.location.search
10
+ )
11
+ return searchParams.get(key)
12
+ }
13
+
14
+ /**
15
+ * 获取 URL 中所有的查询参数
16
+ * @param url 可选的 URL,默认使用当前页面 URL
17
+ * @returns 包含所有参数的对象
18
+ */
19
+ export function getAllQueryParams(url?: string): Record<string, string> {
20
+ const searchParams = new URLSearchParams(
21
+ url ? new URL(url).search : window.location.search
22
+ )
23
+ const params: Record<string, string> = {}
24
+ searchParams.forEach((value, key) => {
25
+ params[key] = value
26
+ })
27
+ return params
28
+ }
29
+
30
+ /**
31
+ * 获取 URL 中指定参数的所有值(支持同名参数)
32
+ * @param key 参数名
33
+ * @param url 可选的 URL,默认使用当前页面 URL
34
+ * @returns 参数值数组
35
+ */
36
+ export function getQueryParamAll(key: string, url?: string): string[] {
37
+ const searchParams = new URLSearchParams(
38
+ url ? new URL(url).search : window.location.search
39
+ )
40
+ return searchParams.getAll(key)
41
+ }