master-utils-lib 1.0.2 → 1.2.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/jest.config.js +0 -1
- package/package.json +4 -4
- package/USAGE_EXAMPLES.md +0 -549
package/jest.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "master-utils-lib",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A professional, lightweight, and compatible utility library for H5 and PC web development with TypeScript support",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -56,9 +56,9 @@
|
|
|
56
56
|
"@rollup/plugin-commonjs": "^29.0.0",
|
|
57
57
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
58
58
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
59
|
-
"@types/jest": "^
|
|
60
|
-
"jest": "^
|
|
61
|
-
"jest-environment-jsdom": "^
|
|
59
|
+
"@types/jest": "^29.5.14",
|
|
60
|
+
"jest": "^29.7.0",
|
|
61
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
62
62
|
"rimraf": "^6.1.2",
|
|
63
63
|
"rollup": "^4.53.3",
|
|
64
64
|
"ts-jest": "^29.4.5",
|
package/USAGE_EXAMPLES.md
DELETED
|
@@ -1,549 +0,0 @@
|
|
|
1
|
-
# h5-utils-lib 使用示例
|
|
2
|
-
|
|
3
|
-
## 📋 库标准评估
|
|
4
|
-
|
|
5
|
-
### ✅ 符合业界标准
|
|
6
|
-
|
|
7
|
-
h5-utils-lib 完全符合业界标准,具备以下特性:
|
|
8
|
-
|
|
9
|
-
1. **多格式支持**
|
|
10
|
-
- ✅ CommonJS (`main`: `dist/index.js`) - 支持 Node.js 和传统构建工具
|
|
11
|
-
- ✅ ES Module (`module`: `dist/index.esm.js`) - 支持现代构建工具和 Tree Shaking
|
|
12
|
-
- ✅ UMD (`dist/index.min.js`) - 支持浏览器直接引入
|
|
13
|
-
- ✅ TypeScript 类型定义 (`types`: `dist/types/index.d.ts`)
|
|
14
|
-
|
|
15
|
-
2. **构建配置**
|
|
16
|
-
- ✅ Rollup 构建,输出多种格式
|
|
17
|
-
- ✅ Source Map 支持,便于调试
|
|
18
|
-
- ✅ TypeScript 编译配置完善
|
|
19
|
-
- ✅ Babel 转译,兼容低版本浏览器
|
|
20
|
-
|
|
21
|
-
3. **开发体验**
|
|
22
|
-
- ✅ 完整的 TypeScript 类型支持
|
|
23
|
-
- ✅ Jest 测试配置
|
|
24
|
-
- ✅ 代码覆盖率报告
|
|
25
|
-
|
|
26
|
-
### ✅ 同时支持 TypeScript 和 JavaScript
|
|
27
|
-
|
|
28
|
-
库完全支持 TypeScript 和 JavaScript 两种使用方式,通过 `package.json` 中的配置自动识别:
|
|
29
|
-
|
|
30
|
-
- **TypeScript**: 自动识别 `types` 字段,提供完整的类型提示
|
|
31
|
-
- **JavaScript**: 通过 `main` 或 `module` 字段引入,无需额外配置
|
|
32
|
-
|
|
33
|
-
---
|
|
34
|
-
|
|
35
|
-
## 📦 安装
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
npm install h5-utils-lib
|
|
39
|
-
# 或
|
|
40
|
-
yarn add h5-utils-lib
|
|
41
|
-
# 或
|
|
42
|
-
pnpm add h5-utils-lib
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
---
|
|
46
|
-
|
|
47
|
-
## 🚀 TypeScript 使用示例
|
|
48
|
-
|
|
49
|
-
### 1. ES Module 导入(推荐)
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
// 全部导入
|
|
53
|
-
import * as H5Utils from 'h5-utils-lib';
|
|
54
|
-
|
|
55
|
-
// 按需导入(支持 Tree Shaking)
|
|
56
|
-
import {
|
|
57
|
-
isIOS,
|
|
58
|
-
isAndroid,
|
|
59
|
-
getQueryString,
|
|
60
|
-
setStorage,
|
|
61
|
-
formatMoney
|
|
62
|
-
} from 'h5-utils-lib';
|
|
63
|
-
|
|
64
|
-
// 使用示例
|
|
65
|
-
if (isIOS()) {
|
|
66
|
-
console.log('当前设备是 iOS');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const userId = getQueryString('userId'); // 类型推断: string | null
|
|
70
|
-
|
|
71
|
-
setStorage('userInfo', { name: '张三', age: 25 }, 3600000); // 1小时后过期
|
|
72
|
-
|
|
73
|
-
const price = formatMoney(1234.56); // 类型推断: string,输出: "¥1,234.56"
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
### 2. CommonJS 导入
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
const { isIOS, getQueryString, setStorage } = require('h5-utils-lib');
|
|
80
|
-
|
|
81
|
-
// TypeScript 中需要类型声明
|
|
82
|
-
declare const require: (module: string) => any;
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
### 3. Vue 3 + TypeScript 示例
|
|
86
|
-
|
|
87
|
-
```vue
|
|
88
|
-
<template>
|
|
89
|
-
<div>
|
|
90
|
-
<p>设备类型: {{ deviceType }}</p>
|
|
91
|
-
<p>价格: {{ formattedPrice }}</p>
|
|
92
|
-
</div>
|
|
93
|
-
</template>
|
|
94
|
-
|
|
95
|
-
<script setup lang="ts">
|
|
96
|
-
import { ref, onMounted } from 'vue';
|
|
97
|
-
import { isIOS, isAndroid, formatMoney } from 'h5-utils-lib';
|
|
98
|
-
|
|
99
|
-
const deviceType = ref<string>('');
|
|
100
|
-
const formattedPrice = ref<string>('');
|
|
101
|
-
|
|
102
|
-
onMounted(() => {
|
|
103
|
-
if (isIOS()) {
|
|
104
|
-
deviceType.value = 'iOS';
|
|
105
|
-
} else if (isAndroid()) {
|
|
106
|
-
deviceType.value = 'Android';
|
|
107
|
-
} else {
|
|
108
|
-
deviceType.value = '其他';
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
formattedPrice.value = formatMoney(999.99);
|
|
112
|
-
});
|
|
113
|
-
</script>
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
### 4. React + TypeScript 示例
|
|
117
|
-
|
|
118
|
-
```tsx
|
|
119
|
-
import React, { useEffect, useState } from 'react';
|
|
120
|
-
import { isIOS, getQueryString, setStorage, getStorage } from 'h5-utils-lib';
|
|
121
|
-
|
|
122
|
-
interface UserInfo {
|
|
123
|
-
name: string;
|
|
124
|
-
age: number;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const App: React.FC = () => {
|
|
128
|
-
const [device, setDevice] = useState<string>('');
|
|
129
|
-
const [userId, setUserId] = useState<string | null>(null);
|
|
130
|
-
|
|
131
|
-
useEffect(() => {
|
|
132
|
-
// 设备检测
|
|
133
|
-
setDevice(isIOS() ? 'iOS' : '其他');
|
|
134
|
-
|
|
135
|
-
// 获取 URL 参数
|
|
136
|
-
const id = getQueryString('id');
|
|
137
|
-
setUserId(id);
|
|
138
|
-
|
|
139
|
-
// 存储数据
|
|
140
|
-
const userInfo: UserInfo = { name: '李四', age: 30 };
|
|
141
|
-
setStorage('userInfo', userInfo, 7200000); // 2小时过期
|
|
142
|
-
|
|
143
|
-
// 读取数据
|
|
144
|
-
const stored = getStorage<UserInfo>('userInfo');
|
|
145
|
-
console.log('存储的用户信息:', stored);
|
|
146
|
-
}, []);
|
|
147
|
-
|
|
148
|
-
return (
|
|
149
|
-
<div>
|
|
150
|
-
<p>设备: {device}</p>
|
|
151
|
-
<p>用户ID: {userId || '未找到'}</p>
|
|
152
|
-
</div>
|
|
153
|
-
);
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export default App;
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
### 5. Node.js + TypeScript 示例
|
|
160
|
-
|
|
161
|
-
```typescript
|
|
162
|
-
import { formatDate, formatMoney, isEmail, isPhone } from 'h5-utils-lib';
|
|
163
|
-
|
|
164
|
-
// 格式化日期
|
|
165
|
-
const dateStr = formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
|
|
166
|
-
console.log(dateStr); // "2024-01-01 12:30:45"
|
|
167
|
-
|
|
168
|
-
// 格式化金额
|
|
169
|
-
const money = formatMoney(1234567.89, 2, '$');
|
|
170
|
-
console.log(money); // "$1,234,567.89"
|
|
171
|
-
|
|
172
|
-
// 验证邮箱
|
|
173
|
-
const email = 'test@example.com';
|
|
174
|
-
if (isEmail(email)) {
|
|
175
|
-
console.log('邮箱格式正确');
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// 验证手机号
|
|
179
|
-
const phone = '13800138000';
|
|
180
|
-
if (isPhone(phone)) {
|
|
181
|
-
console.log('手机号格式正确');
|
|
182
|
-
}
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
---
|
|
186
|
-
|
|
187
|
-
## 🎯 JavaScript 使用示例
|
|
188
|
-
|
|
189
|
-
### 1. ES Module 导入(现代浏览器/构建工具)
|
|
190
|
-
|
|
191
|
-
```javascript
|
|
192
|
-
// 全部导入
|
|
193
|
-
import * as H5Utils from 'h5-utils-lib';
|
|
194
|
-
|
|
195
|
-
// 按需导入(推荐,支持 Tree Shaking)
|
|
196
|
-
import {
|
|
197
|
-
isIOS,
|
|
198
|
-
isAndroid,
|
|
199
|
-
isWeChat,
|
|
200
|
-
getQueryString,
|
|
201
|
-
stringifyQuery,
|
|
202
|
-
setStorage,
|
|
203
|
-
getStorage,
|
|
204
|
-
removeStorage,
|
|
205
|
-
formatMoney,
|
|
206
|
-
formatDate,
|
|
207
|
-
isPhone,
|
|
208
|
-
isEmail
|
|
209
|
-
} from 'h5-utils-lib';
|
|
210
|
-
|
|
211
|
-
// 使用示例
|
|
212
|
-
if (isIOS()) {
|
|
213
|
-
console.log('当前设备是 iOS');
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (isWeChat()) {
|
|
217
|
-
console.log('在微信中打开');
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// URL 参数处理
|
|
221
|
-
const userId = getQueryString('userId');
|
|
222
|
-
console.log('用户ID:', userId);
|
|
223
|
-
|
|
224
|
-
const queryStr = stringifyQuery({ name: '张三', age: 25 });
|
|
225
|
-
console.log('查询字符串:', queryStr); // "name=%E5%BC%A0%E4%B8%89&age=25"
|
|
226
|
-
|
|
227
|
-
// 存储操作
|
|
228
|
-
setStorage('token', 'abc123', 3600000); // 1小时后过期
|
|
229
|
-
const token = getStorage('token');
|
|
230
|
-
console.log('Token:', token);
|
|
231
|
-
|
|
232
|
-
// 格式化
|
|
233
|
-
const price = formatMoney(1234.56);
|
|
234
|
-
console.log('价格:', price); // "¥1,234.56"
|
|
235
|
-
|
|
236
|
-
const date = formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
|
|
237
|
-
console.log('日期:', date);
|
|
238
|
-
|
|
239
|
-
// 验证
|
|
240
|
-
if (isPhone('13800138000')) {
|
|
241
|
-
console.log('手机号正确');
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (isEmail('test@example.com')) {
|
|
245
|
-
console.log('邮箱正确');
|
|
246
|
-
}
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
### 2. CommonJS 导入(Node.js/Webpack)
|
|
250
|
-
|
|
251
|
-
```javascript
|
|
252
|
-
// 全部导入
|
|
253
|
-
const H5Utils = require('h5-utils-lib');
|
|
254
|
-
|
|
255
|
-
// 解构导入
|
|
256
|
-
const {
|
|
257
|
-
isIOS,
|
|
258
|
-
getQueryString,
|
|
259
|
-
setStorage,
|
|
260
|
-
formatMoney
|
|
261
|
-
} = require('h5-utils-lib');
|
|
262
|
-
|
|
263
|
-
// 使用
|
|
264
|
-
if (isIOS()) {
|
|
265
|
-
console.log('iOS 设备');
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
const id = getQueryString('id');
|
|
269
|
-
setStorage('data', { key: 'value' });
|
|
270
|
-
const money = formatMoney(999.99);
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
### 3. Vue 2 + JavaScript 示例
|
|
274
|
-
|
|
275
|
-
```vue
|
|
276
|
-
<template>
|
|
277
|
-
<div>
|
|
278
|
-
<p>设备: {{ device }}</p>
|
|
279
|
-
<p>价格: {{ price }}</p>
|
|
280
|
-
</div>
|
|
281
|
-
</template>
|
|
282
|
-
|
|
283
|
-
<script>
|
|
284
|
-
import { isIOS, isAndroid, formatMoney } from 'h5-utils-lib';
|
|
285
|
-
|
|
286
|
-
export default {
|
|
287
|
-
data() {
|
|
288
|
-
return {
|
|
289
|
-
device: '',
|
|
290
|
-
price: ''
|
|
291
|
-
};
|
|
292
|
-
},
|
|
293
|
-
mounted() {
|
|
294
|
-
if (isIOS()) {
|
|
295
|
-
this.device = 'iOS';
|
|
296
|
-
} else if (isAndroid()) {
|
|
297
|
-
this.device = 'Android';
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
this.price = formatMoney(1234.56);
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
</script>
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
### 4. React + JavaScript 示例
|
|
307
|
-
|
|
308
|
-
```jsx
|
|
309
|
-
import React, { useEffect, useState } from 'react';
|
|
310
|
-
import { isIOS, getQueryString, setStorage, getStorage } from 'h5-utils-lib';
|
|
311
|
-
|
|
312
|
-
function App() {
|
|
313
|
-
const [device, setDevice] = useState('');
|
|
314
|
-
const [userId, setUserId] = useState(null);
|
|
315
|
-
|
|
316
|
-
useEffect(() => {
|
|
317
|
-
setDevice(isIOS() ? 'iOS' : '其他');
|
|
318
|
-
|
|
319
|
-
const id = getQueryString('id');
|
|
320
|
-
setUserId(id);
|
|
321
|
-
|
|
322
|
-
setStorage('userInfo', { name: '王五', age: 28 }, 3600000);
|
|
323
|
-
const stored = getStorage('userInfo');
|
|
324
|
-
console.log('存储的数据:', stored);
|
|
325
|
-
}, []);
|
|
326
|
-
|
|
327
|
-
return (
|
|
328
|
-
<div>
|
|
329
|
-
<p>设备: {device}</p>
|
|
330
|
-
<p>用户ID: {userId || '未找到'}</p>
|
|
331
|
-
</div>
|
|
332
|
-
);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
export default App;
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
### 5. 浏览器直接引入(UMD)
|
|
339
|
-
|
|
340
|
-
```html
|
|
341
|
-
<!DOCTYPE html>
|
|
342
|
-
<html>
|
|
343
|
-
<head>
|
|
344
|
-
<title>h5-utils-lib 示例</title>
|
|
345
|
-
</head>
|
|
346
|
-
<body>
|
|
347
|
-
<script src="./node_modules/h5-utils-lib/dist/index.min.js"></script>
|
|
348
|
-
<script>
|
|
349
|
-
// 通过全局变量 H5Utils 访问
|
|
350
|
-
if (H5Utils.isIOS()) {
|
|
351
|
-
console.log('iOS 设备');
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (H5Utils.isWeChat()) {
|
|
355
|
-
console.log('微信环境');
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const userId = H5Utils.getQueryString('userId');
|
|
359
|
-
console.log('用户ID:', userId);
|
|
360
|
-
|
|
361
|
-
H5Utils.setStorage('token', 'abc123', 3600000);
|
|
362
|
-
const token = H5Utils.getStorage('token');
|
|
363
|
-
console.log('Token:', token);
|
|
364
|
-
|
|
365
|
-
const price = H5Utils.formatMoney(1234.56);
|
|
366
|
-
console.log('价格:', price);
|
|
367
|
-
|
|
368
|
-
const date = H5Utils.formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
|
|
369
|
-
console.log('日期:', date);
|
|
370
|
-
|
|
371
|
-
if (H5Utils.isPhone('13800138000')) {
|
|
372
|
-
console.log('手机号正确');
|
|
373
|
-
}
|
|
374
|
-
</script>
|
|
375
|
-
</body>
|
|
376
|
-
</html>
|
|
377
|
-
```
|
|
378
|
-
|
|
379
|
-
### 6. Vite 项目示例
|
|
380
|
-
|
|
381
|
-
```javascript
|
|
382
|
-
// main.js
|
|
383
|
-
import { createApp } from 'vue';
|
|
384
|
-
import App from './App.vue';
|
|
385
|
-
import { isIOS, formatMoney } from 'h5-utils-lib';
|
|
386
|
-
|
|
387
|
-
const app = createApp(App);
|
|
388
|
-
|
|
389
|
-
// 全局使用
|
|
390
|
-
app.config.globalProperties.$utils = {
|
|
391
|
-
isIOS,
|
|
392
|
-
formatMoney
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
app.mount('#app');
|
|
396
|
-
```
|
|
397
|
-
|
|
398
|
-
### 7. Webpack 项目示例
|
|
399
|
-
|
|
400
|
-
```javascript
|
|
401
|
-
// webpack.config.js
|
|
402
|
-
module.exports = {
|
|
403
|
-
// ... 其他配置
|
|
404
|
-
resolve: {
|
|
405
|
-
// 自动识别 package.json 中的 main/module/types 字段
|
|
406
|
-
// 无需额外配置
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
|
|
410
|
-
// 使用
|
|
411
|
-
import { isIOS, formatMoney } from 'h5-utils-lib';
|
|
412
|
-
```
|
|
413
|
-
|
|
414
|
-
---
|
|
415
|
-
|
|
416
|
-
## 📚 API 完整列表
|
|
417
|
-
|
|
418
|
-
### Device(设备检测)
|
|
419
|
-
|
|
420
|
-
```typescript
|
|
421
|
-
// TypeScript
|
|
422
|
-
import { isIOS, isAndroid, isWeChat } from 'h5-utils-lib';
|
|
423
|
-
|
|
424
|
-
isIOS(): boolean; // 检测是否为 iOS 设备
|
|
425
|
-
isAndroid(): boolean; // 检测是否为 Android 设备
|
|
426
|
-
isWeChat(): boolean; // 检测是否在微信中打开
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
```javascript
|
|
430
|
-
// JavaScript
|
|
431
|
-
import { isIOS, isAndroid, isWeChat } from 'h5-utils-lib';
|
|
432
|
-
|
|
433
|
-
if (isIOS()) { /* ... */ }
|
|
434
|
-
if (isAndroid()) { /* ... */ }
|
|
435
|
-
if (isWeChat()) { /* ... */ }
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
### URL(URL 处理)
|
|
439
|
-
|
|
440
|
-
```typescript
|
|
441
|
-
// TypeScript
|
|
442
|
-
import { getQueryString, stringifyQuery } from 'h5-utils-lib';
|
|
443
|
-
|
|
444
|
-
getQueryString(name: string): string | null;
|
|
445
|
-
stringifyQuery(obj: Record<string, any>): string;
|
|
446
|
-
```
|
|
447
|
-
|
|
448
|
-
```javascript
|
|
449
|
-
// JavaScript
|
|
450
|
-
import { getQueryString, stringifyQuery } from 'h5-utils-lib';
|
|
451
|
-
|
|
452
|
-
const id = getQueryString('id');
|
|
453
|
-
const query = stringifyQuery({ name: 'test', age: 20 });
|
|
454
|
-
```
|
|
455
|
-
|
|
456
|
-
### Storage(本地存储)
|
|
457
|
-
|
|
458
|
-
```typescript
|
|
459
|
-
// TypeScript
|
|
460
|
-
import { setStorage, getStorage, removeStorage } from 'h5-utils-lib';
|
|
461
|
-
|
|
462
|
-
setStorage<T>(key: string, value: T, expire?: number | null): void;
|
|
463
|
-
getStorage<T>(key: string): T | null;
|
|
464
|
-
removeStorage(key: string): void;
|
|
465
|
-
```
|
|
466
|
-
|
|
467
|
-
```javascript
|
|
468
|
-
// JavaScript
|
|
469
|
-
import { setStorage, getStorage, removeStorage } from 'h5-utils-lib';
|
|
470
|
-
|
|
471
|
-
setStorage('key', 'value', 3600000); // 1小时后过期
|
|
472
|
-
const value = getStorage('key');
|
|
473
|
-
removeStorage('key');
|
|
474
|
-
```
|
|
475
|
-
|
|
476
|
-
### Format(格式化)
|
|
477
|
-
|
|
478
|
-
```typescript
|
|
479
|
-
// TypeScript
|
|
480
|
-
import { formatMoney, formatDate } from 'h5-utils-lib';
|
|
481
|
-
|
|
482
|
-
formatMoney(amount: number | string, decimals?: number, symbol?: string): string;
|
|
483
|
-
formatDate(date: Date | number | string, fmt?: string): string;
|
|
484
|
-
```
|
|
485
|
-
|
|
486
|
-
```javascript
|
|
487
|
-
// JavaScript
|
|
488
|
-
import { formatMoney, formatDate } from 'h5-utils-lib';
|
|
489
|
-
|
|
490
|
-
formatMoney(1234.56, 2, '¥'); // "¥1,234.56"
|
|
491
|
-
formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
|
|
492
|
-
```
|
|
493
|
-
|
|
494
|
-
### Validate(验证)
|
|
495
|
-
|
|
496
|
-
```typescript
|
|
497
|
-
// TypeScript
|
|
498
|
-
import { isPhone, isEmail } from 'h5-utils-lib';
|
|
499
|
-
|
|
500
|
-
isPhone(phone: string): boolean;
|
|
501
|
-
isEmail(email: string): boolean;
|
|
502
|
-
```
|
|
503
|
-
|
|
504
|
-
```javascript
|
|
505
|
-
// JavaScript
|
|
506
|
-
import { isPhone, isEmail } from 'h5-utils-lib';
|
|
507
|
-
|
|
508
|
-
isPhone('13800138000'); // true
|
|
509
|
-
isEmail('test@example.com'); // true
|
|
510
|
-
```
|
|
511
|
-
|
|
512
|
-
---
|
|
513
|
-
|
|
514
|
-
## 🔍 Tree Shaking 支持
|
|
515
|
-
|
|
516
|
-
由于库使用 ES Module 导出,支持 Tree Shaking,只会打包使用到的代码:
|
|
517
|
-
|
|
518
|
-
```javascript
|
|
519
|
-
// ✅ 推荐:按需导入,只打包使用的函数
|
|
520
|
-
import { isIOS, formatMoney } from 'h5-utils-lib';
|
|
521
|
-
|
|
522
|
-
// ❌ 不推荐:全部导入,会打包所有代码
|
|
523
|
-
import * as H5Utils from 'h5-utils-lib';
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
---
|
|
527
|
-
|
|
528
|
-
## 📝 注意事项
|
|
529
|
-
|
|
530
|
-
1. **TypeScript 类型支持**:库提供了完整的类型定义,TypeScript 项目会自动获得类型提示和检查
|
|
531
|
-
2. **浏览器兼容性**:支持 iOS 9+, Android 4.4+, IE11+
|
|
532
|
-
3. **SSR 支持**:所有函数都做了 SSR 兼容处理(检查 `typeof window`)
|
|
533
|
-
4. **存储过期时间**:`setStorage` 的 `expire` 参数单位为毫秒
|
|
534
|
-
|
|
535
|
-
---
|
|
536
|
-
|
|
537
|
-
## 🎉 总结
|
|
538
|
-
|
|
539
|
-
h5-utils-lib 是一个**完全符合业界标准**的工具库,具备:
|
|
540
|
-
|
|
541
|
-
- ✅ 多格式支持(CJS/ESM/UMD)
|
|
542
|
-
- ✅ 完整的 TypeScript 类型定义
|
|
543
|
-
- ✅ 同时支持 TypeScript 和 JavaScript
|
|
544
|
-
- ✅ Tree Shaking 支持
|
|
545
|
-
- ✅ Source Map 支持
|
|
546
|
-
- ✅ 完善的构建配置
|
|
547
|
-
|
|
548
|
-
无论是 TypeScript 还是 JavaScript 项目,都可以无缝使用!
|
|
549
|
-
|