@i17hush/h5-utils 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.
@@ -0,0 +1,356 @@
1
+ /**
2
+ * 适配器接口定义
3
+ * 跨端差异化的操作通过适配器抽象,浏览器和 Taro 各自实现
4
+ */
5
+ interface StorageAdapter {
6
+ get(key: string): string | null;
7
+ set(key: string, value: string, options?: StorageSetOptions): void;
8
+ remove(key: string): void;
9
+ clear(): void;
10
+ }
11
+ interface StorageSetOptions {
12
+ /** 过期时间(毫秒) */
13
+ expires?: number;
14
+ }
15
+ interface ClipboardAdapter {
16
+ write(text: string): Promise<void>;
17
+ read(): Promise<string>;
18
+ }
19
+ interface EventEmitterAdapter {
20
+ on(event: string, handler: (...args: any[]) => void): void;
21
+ off(event: string, handler: (...args: any[]) => void): void;
22
+ emit(event: string, ...args: any[]): void;
23
+ }
24
+ interface DomAdapter {
25
+ select(selector: string): Element | null;
26
+ selectAll(selector: string): Element[];
27
+ }
28
+ interface ScrollAdapter {
29
+ /** 滚动到指定位置 */
30
+ scrollTo(options: {
31
+ top?: number;
32
+ left?: number;
33
+ animated?: boolean;
34
+ }): void;
35
+ /** 获取当前滚动位置 */
36
+ getScrollPosition(): {
37
+ scrollTop: number;
38
+ scrollLeft: number;
39
+ };
40
+ /** 锁定滚动 */
41
+ lockScroll(): void;
42
+ /** 解锁滚动 */
43
+ unlockScroll(): void;
44
+ }
45
+ interface DeviceInfo {
46
+ /** 是否 iOS */
47
+ ios: boolean;
48
+ /** 是否 Android */
49
+ android: boolean;
50
+ /** 是否移动端 */
51
+ mobile: boolean;
52
+ /** 是否微信 */
53
+ weChat: boolean;
54
+ /** 系统信息 */
55
+ os: string;
56
+ /** 品牌 */
57
+ brand: string;
58
+ /** 型号 */
59
+ model: string;
60
+ /** 屏幕宽度 */
61
+ screenWidth: number;
62
+ /** 屏幕高度 */
63
+ screenHeight: number;
64
+ /** 像素比 */
65
+ pixelRatio: number;
66
+ }
67
+ interface DeviceAdapter {
68
+ /** 获取设备信息 */
69
+ getInfo(): DeviceInfo;
70
+ /** 获取网络类型 */
71
+ getNetworkType(): Promise<string>;
72
+ }
73
+ interface PlatformAdapter {
74
+ storage: StorageAdapter;
75
+ clipboard: ClipboardAdapter;
76
+ event: EventEmitterAdapter;
77
+ dom: DomAdapter;
78
+ scroll: ScrollAdapter;
79
+ device: DeviceAdapter;
80
+ }
81
+
82
+ /**
83
+ * 设置平台适配器(如切换到 Taro)
84
+ */
85
+ declare function setAdapter(adapter: PlatformAdapter): void;
86
+ /**
87
+ * 获取当前平台适配器
88
+ */
89
+ declare function getAdapter(): PlatformAdapter;
90
+
91
+ /**
92
+ * URL 解析与操作工具
93
+ */
94
+ interface ParsedUrl {
95
+ protocol: string;
96
+ host: string;
97
+ port: string;
98
+ pathname: string;
99
+ search: string;
100
+ hash: string;
101
+ query: Record<string, string>;
102
+ }
103
+ /**
104
+ * 解析 URL 为结构化对象
105
+ */
106
+ declare function parseUrl(url: string): ParsedUrl;
107
+ /**
108
+ * 拼接带查询参数的 URL
109
+ */
110
+ declare function buildUrl(base: string, params: Record<string, string | number | boolean>): string;
111
+ /**
112
+ * 获取当前页面指定查询参数
113
+ */
114
+ declare function getQueryParam(name: string): string | null;
115
+ /**
116
+ * 获取当前页面所有查询参数
117
+ */
118
+ declare function getAllQueryParams(): Record<string, string>;
119
+
120
+ /**
121
+ * Storage 封装
122
+ * 支持过期时间、JSON 自动序列化
123
+ * 通过适配器支持跨端
124
+ */
125
+
126
+ /**
127
+ * 获取存储值,支持 JSON 自动反序列化
128
+ */
129
+ declare function get$1<T = unknown>(key: string): T | null;
130
+ /**
131
+ * 设置存储值,支持过期时间
132
+ */
133
+ declare function set$1(key: string, value: unknown, options?: StorageSetOptions): void;
134
+ /**
135
+ * 删除存储值
136
+ */
137
+ declare function remove$1(key: string): void;
138
+ /**
139
+ * 清空所有存储
140
+ */
141
+ declare function clear(): void;
142
+
143
+ /**
144
+ * Cookie 读写删工具
145
+ */
146
+ interface CookieOptions {
147
+ /** 过期天数 */
148
+ days?: number;
149
+ /** 域名 */
150
+ domain?: string;
151
+ /** 路径 */
152
+ path?: string;
153
+ /** 仅 HTTPS */
154
+ secure?: boolean;
155
+ /** SameSite 策略 */
156
+ sameSite?: 'strict' | 'lax' | 'none';
157
+ }
158
+ /**
159
+ * 获取 Cookie 值
160
+ */
161
+ declare function get(name: string): string | null;
162
+ /**
163
+ * 设置 Cookie
164
+ */
165
+ declare function set(name: string, value: string, options?: CookieOptions): void;
166
+ /**
167
+ * 删除 Cookie
168
+ */
169
+ declare function remove(name: string, options?: CookieOptions): void;
170
+
171
+ /**
172
+ * 设备/浏览器检测工具
173
+ */
174
+ /**
175
+ * 是否为 iOS
176
+ */
177
+ declare function isIOS(): boolean;
178
+ /**
179
+ * 是否为 Android
180
+ */
181
+ declare function isAndroid(): boolean;
182
+ /**
183
+ * 是否为微信浏览器
184
+ */
185
+ declare function isWeChat(): boolean;
186
+ /**
187
+ * 是否为移动端
188
+ */
189
+ declare function isMobile(): boolean;
190
+ type BrowserType = 'weixin' | 'qq' | 'uc' | 'safari' | 'chrome' | 'firefox' | 'edge' | 'ie' | 'unknown';
191
+ /**
192
+ * 获取浏览器类型(H5 专用)
193
+ */
194
+ declare function getBrowserType(): BrowserType;
195
+ /**
196
+ * 获取操作系统信息
197
+ */
198
+ declare function getOS(): string;
199
+ /**
200
+ * 获取完整设备信息
201
+ */
202
+ declare function getDeviceInfo(): DeviceInfo;
203
+
204
+ /**
205
+ * DOM 操作工具
206
+ */
207
+ /**
208
+ * 查询单个元素
209
+ */
210
+ declare function $<T extends Element = Element>(selector: string): T | null;
211
+ /**
212
+ * 查询多个元素
213
+ */
214
+ declare function $$<T extends Element = Element>(selector: string): T[];
215
+ /**
216
+ * 添加 class
217
+ */
218
+ declare function addClass(el: Element, cls: string): void;
219
+ /**
220
+ * 移除 class
221
+ */
222
+ declare function removeClass(el: Element, cls: string): void;
223
+ /**
224
+ * 切换 class
225
+ */
226
+ declare function toggleClass(el: Element, cls: string): void;
227
+ /**
228
+ * 判断是否包含 class
229
+ */
230
+ declare function hasClass(el: Element, cls: string): boolean;
231
+ /**
232
+ * 获取元素样式
233
+ */
234
+ declare function getStyle(el: Element, prop: string): string;
235
+ /**
236
+ * 判断元素是否在可视区域内
237
+ */
238
+ declare function isInViewport(el: Element): boolean;
239
+
240
+ /**
241
+ * 事件工具
242
+ */
243
+ /**
244
+ * 绑定事件
245
+ */
246
+ declare function on(el: EventTarget, event: string, handler: EventListener, options?: AddEventListenerOptions): void;
247
+ /**
248
+ * 解绑事件
249
+ */
250
+ declare function off(el: EventTarget, event: string, handler: EventListener, options?: EventListenerOptions): void;
251
+ /**
252
+ * 单次监听事件
253
+ */
254
+ declare function once(el: EventTarget, event: string, handler: EventListener): void;
255
+ /**
256
+ * 事件委托
257
+ * @param parent 父容器
258
+ * @param selector 子元素选择器
259
+ * @param event 事件名
260
+ * @param handler 回调函数
261
+ */
262
+ declare function delegate(parent: EventTarget, selector: string, event: string, handler: (el: Element, e: Event) => void): void;
263
+
264
+ /**
265
+ * 剪贴板操作
266
+ */
267
+ /**
268
+ * 复制文本到剪贴板
269
+ */
270
+ declare function copyText(text: string): Promise<void>;
271
+ /**
272
+ * 读取剪贴板文本
273
+ */
274
+ declare function readText(): Promise<string>;
275
+
276
+ /**
277
+ * 滚动相关工具
278
+ */
279
+ /**
280
+ * 滚动到页面顶部
281
+ */
282
+ declare function scrollToTop(smooth?: boolean): void;
283
+ /**
284
+ * 滚动到指定元素
285
+ */
286
+ declare function scrollToElement(el: Element, options?: {
287
+ offset?: number;
288
+ smooth?: boolean;
289
+ }): void;
290
+ /**
291
+ * 锁定页面滚动
292
+ */
293
+ declare function lockScroll(): void;
294
+ /**
295
+ * 解锁页面滚动
296
+ */
297
+ declare function unlockScroll(): void;
298
+
299
+ /**
300
+ * 格式化工具
301
+ */
302
+ /**
303
+ * 日期格式化
304
+ * @example formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss')
305
+ */
306
+ declare function formatDate(date: Date | number | string, fmt?: string): string;
307
+ /**
308
+ * 数字格式化(千分位等)
309
+ */
310
+ declare function formatNumber(num: number, options?: {
311
+ precision?: number;
312
+ separator?: string;
313
+ }): string;
314
+ /**
315
+ * 手机号格式化 (xxx xxxx xxxx)
316
+ */
317
+ declare function formatPhone(phone: string): string;
318
+ /**
319
+ * 金额格式化
320
+ */
321
+ declare function formatMoney(amount: number, precision?: number): string;
322
+ /**
323
+ * 节流
324
+ */
325
+ declare function throttle<T extends (...args: any[]) => any>(fn: T, delay: number): T;
326
+ /**
327
+ * 防抖
328
+ */
329
+ declare function debounce<T extends (...args: any[]) => any>(fn: T, delay: number): T;
330
+
331
+ /**
332
+ * 校验器工具
333
+ */
334
+ /**
335
+ * 手机号格式校验
336
+ */
337
+ declare function isMobilePhone(str: string): boolean;
338
+ /**
339
+ * 邮箱校验
340
+ */
341
+ declare function isEmail(str: string): boolean;
342
+ /**
343
+ * 身份证号校验(18位)
344
+ */
345
+ declare function isIdCard(str: string): boolean;
346
+ /**
347
+ * URL 校验
348
+ */
349
+ declare function isUrl(str: string): boolean;
350
+ /**
351
+ * 中文校验
352
+ */
353
+ declare function isChinese(str: string): boolean;
354
+
355
+ export { $, $$, addClass, buildUrl, clear as clearStorage, copyText, debounce, delegate, formatDate, formatMoney, formatNumber, formatPhone, getAdapter, getAllQueryParams, getBrowserType, get as getCookie, getDeviceInfo, getOS, getQueryParam, get$1 as getStorage, getStyle, hasClass, isAndroid, isChinese, isEmail, isIOS, isIdCard, isInViewport, isMobile, isMobilePhone, isUrl, isWeChat, lockScroll, off, on, once, parseUrl, readText, removeClass, remove as removeCookie, remove$1 as removeStorage, scrollToElement, scrollToTop, setAdapter, set as setCookie, set$1 as setStorage, throttle, toggleClass, unlockScroll };
356
+ export type { BrowserType, ClipboardAdapter, CookieOptions, DeviceAdapter, DeviceInfo, DomAdapter, EventEmitterAdapter, ParsedUrl, PlatformAdapter, ScrollAdapter, StorageAdapter, StorageSetOptions };