master-utils-lib 1.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/DEVELOPMENT.md ADDED
@@ -0,0 +1,274 @@
1
+ # 开发指南
2
+
3
+ 本指南将帮助你在安装 `h5-utils-lib` 后快速开始开发和扩展。
4
+
5
+ ## 📦 安装依赖
6
+
7
+ 安装包后,首先安装开发依赖:
8
+
9
+ ```bash
10
+ npm install
11
+ # 或
12
+ yarn install
13
+ # 或
14
+ pnpm install
15
+ ```
16
+
17
+ ## 🏗️ 项目结构
18
+
19
+ ```
20
+ h5-utils-lib/
21
+ ├── src/ # 源代码目录
22
+ │ ├── device/ # 设备检测相关
23
+ │ ├── format/ # 格式化相关
24
+ │ ├── storage/ # 存储相关
25
+ │ ├── url/ # URL 处理相关
26
+ │ ├── validate/ # 验证相关
27
+ │ └── index.ts # 入口文件
28
+ ├── tests/ # 测试文件
29
+ ├── dist/ # 构建输出(自动生成)
30
+ ├── rollup.config.js # Rollup 构建配置
31
+ ├── tsconfig.json # TypeScript 配置
32
+ ├── jest.config.js # Jest 测试配置
33
+ ├── babel.config.js # Babel 配置
34
+ └── package.json # 项目配置
35
+ ```
36
+
37
+ ## 🚀 开发命令
38
+
39
+ ### 构建项目
40
+
41
+ ```bash
42
+ # 构建生产版本
43
+ npm run build
44
+
45
+ # 监听模式(开发时使用)
46
+ npm run dev
47
+ ```
48
+
49
+ 构建完成后,会在 `dist/` 目录生成以下文件:
50
+ - `index.js` - CommonJS 格式
51
+ - `index.esm.js` - ES Module 格式
52
+ - `index.min.js` - UMD 格式(浏览器直接使用)
53
+ - `types/` - TypeScript 类型定义文件
54
+
55
+ ### 运行测试
56
+
57
+ ```bash
58
+ # 运行所有测试
59
+ npm test
60
+
61
+ # 监听模式运行测试
62
+ npm run test:watch
63
+
64
+ # 生成覆盖率报告
65
+ npm run test:coverage
66
+ ```
67
+
68
+ ## ✏️ 添加新功能
69
+
70
+ ### 1. 创建新的工具模块
71
+
72
+ 假设要添加一个 `cookie` 相关的工具模块:
73
+
74
+ ```typescript
75
+ // src/cookie/index.ts
76
+ export const setCookie = (name: string, value: string, days?: number): void => {
77
+ // 实现代码
78
+ };
79
+
80
+ export const getCookie = (name: string): string | null => {
81
+ // 实现代码
82
+ };
83
+
84
+ export const removeCookie = (name: string): void => {
85
+ // 实现代码
86
+ };
87
+ ```
88
+
89
+ ### 2. 在入口文件中导出
90
+
91
+ ```typescript
92
+ // src/index.ts
93
+ export * from './device';
94
+ export * from './url';
95
+ export * from './storage';
96
+ export * from './validate';
97
+ export * from './format';
98
+ export * from './cookie'; // 新增
99
+ ```
100
+
101
+ ### 3. 编写测试
102
+
103
+ ```typescript
104
+ // tests/cookie.test.ts
105
+ import { setCookie, getCookie, removeCookie } from '../src/cookie';
106
+
107
+ describe('Cookie Utils', () => {
108
+ beforeEach(() => {
109
+ // 清理 cookies
110
+ document.cookie.split(";").forEach((c) => {
111
+ document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/");
112
+ });
113
+ });
114
+
115
+ test('setCookie and getCookie', () => {
116
+ setCookie('test', 'value');
117
+ expect(getCookie('test')).toBe('value');
118
+ });
119
+
120
+ test('removeCookie', () => {
121
+ setCookie('test', 'value');
122
+ removeCookie('test');
123
+ expect(getCookie('test')).toBeNull();
124
+ });
125
+ });
126
+ ```
127
+
128
+ ### 4. 构建和测试
129
+
130
+ ```bash
131
+ npm run build
132
+ npm test
133
+ ```
134
+
135
+ ## 📝 代码规范
136
+
137
+ ### TypeScript 规范
138
+
139
+ - 使用 TypeScript 严格模式
140
+ - 所有函数都需要类型注解
141
+ - 使用接口定义复杂类型
142
+ - 导出函数需要添加 JSDoc 注释
143
+
144
+ 示例:
145
+
146
+ ```typescript
147
+ /**
148
+ * 设置 Cookie
149
+ * @param name Cookie 名称
150
+ * @param value Cookie 值
151
+ * @param days 过期天数(可选)
152
+ */
153
+ export const setCookie = (name: string, value: string, days?: number): void => {
154
+ // 实现
155
+ };
156
+ ```
157
+
158
+ ### 测试规范
159
+
160
+ - 每个模块都应该有对应的测试文件
161
+ - 测试覆盖率应该保持在 80% 以上
162
+ - 使用描述性的测试用例名称
163
+
164
+ ## 🔧 配置说明
165
+
166
+ ### TypeScript 配置 (`tsconfig.json`)
167
+
168
+ - `target: "es5"` - 编译目标为 ES5,兼容低版本浏览器
169
+ - `strict: true` - 启用严格模式
170
+ - `declaration: true` - 生成类型定义文件
171
+
172
+ ### Rollup 配置 (`rollup.config.js`)
173
+
174
+ 构建输出三种格式:
175
+ - **CommonJS** (`dist/index.js`) - 用于 Node.js 和传统构建工具
176
+ - **ES Module** (`dist/index.esm.js`) - 用于现代构建工具,支持 Tree Shaking
177
+ - **UMD** (`dist/index.min.js`) - 用于浏览器直接引入
178
+
179
+ ### Babel 配置 (`babel.config.js`)
180
+
181
+ 兼容性目标:
182
+ - IE 11+
183
+ - iOS 9+
184
+ - Android 4.4+
185
+
186
+ ## 📦 发布新版本
187
+
188
+ ### 1. 更新版本号
189
+
190
+ ```bash
191
+ # 补丁版本(1.0.0 -> 1.0.1)
192
+ npm version patch
193
+
194
+ # 次要版本(1.0.0 -> 1.1.0)
195
+ npm version minor
196
+
197
+ # 主要版本(1.0.0 -> 2.0.0)
198
+ npm version major
199
+ ```
200
+
201
+ ### 2. 构建项目
202
+
203
+ ```bash
204
+ npm run build
205
+ ```
206
+
207
+ ### 3. 发布到 npm
208
+
209
+ ```bash
210
+ # 发布到公共 npm
211
+ npm publish
212
+
213
+ # 发布到私有 npm registry(需要先配置)
214
+ npm publish --registry=https://your-registry.com
215
+ ```
216
+
217
+ ### 4. 配置私有 npm registry
218
+
219
+ 如果使用公司私有 npm registry,需要配置:
220
+
221
+ ```bash
222
+ # 设置 registry
223
+ npm config set registry https://your-registry.com
224
+
225
+ # 或者使用 .npmrc 文件
226
+ echo "registry=https://your-registry.com" > .npmrc
227
+ ```
228
+
229
+ ## 🐛 常见问题
230
+
231
+ ### Q: 构建失败怎么办?
232
+
233
+ A: 检查以下几点:
234
+ 1. 确保所有依赖已安装:`npm install`
235
+ 2. 检查 TypeScript 类型错误:`npx tsc --noEmit`
236
+ 3. 查看 Rollup 错误信息
237
+
238
+ ### Q: 测试失败怎么办?
239
+
240
+ A:
241
+ 1. 确保测试环境正确配置
242
+ 2. 检查测试文件语法
243
+ 3. 运行 `npm run test:watch` 查看详细错误
244
+
245
+ ### Q: 如何添加新的构建格式?
246
+
247
+ A: 编辑 `rollup.config.js`,在 `output` 数组中添加新的配置:
248
+
249
+ ```javascript
250
+ {
251
+ file: 'dist/index.cjs.js',
252
+ format: 'cjs',
253
+ sourcemap: true,
254
+ }
255
+ ```
256
+
257
+ ## 📚 相关文档
258
+
259
+ - [使用示例](./USAGE_EXAMPLES.md) - 详细的使用示例
260
+ - [README](./README.md) - 项目介绍和 API 文档
261
+ - [设计文档](./docs/DESIGN.md) - 架构设计说明
262
+
263
+ ## 🤝 贡献指南
264
+
265
+ 1. Fork 项目
266
+ 2. 创建功能分支:`git checkout -b feature/new-feature`
267
+ 3. 提交更改:`git commit -am 'Add new feature'`
268
+ 4. 推送分支:`git push origin feature/new-feature`
269
+ 5. 提交 Pull Request
270
+
271
+ ## 📄 License
272
+
273
+ ISC
274
+
package/PUBLISH.md ADDED
@@ -0,0 +1,248 @@
1
+ # 发布指南
2
+
3
+ 本指南将帮助你将 `h5-utils-lib` 发布到 npm(公共或私有 registry)。
4
+
5
+ ## 📋 发布前检查清单
6
+
7
+ - [ ] 确保所有测试通过:`npm test`
8
+ - [ ] 确保代码已构建:`npm run build`
9
+ - [ ] 检查 `package.json` 中的版本号
10
+ - [ ] 更新 `CHANGELOG.md`(如果有)
11
+ - [ ] 确保 `.npmignore` 配置正确
12
+ - [ ] 检查 `files` 字段包含所有需要发布的文件
13
+
14
+ ## 🚀 发布步骤
15
+
16
+ ### 1. 登录 npm
17
+
18
+ ```bash
19
+ # 登录公共 npm
20
+ npm login
21
+
22
+ # 或登录私有 registry
23
+ npm login --registry=https://your-registry.com
24
+ ```
25
+
26
+ ### 2. 更新版本号
27
+
28
+ 使用语义化版本控制(SemVer):
29
+
30
+ ```bash
31
+ # 补丁版本:修复 bug(1.0.0 -> 1.0.1)
32
+ npm version patch
33
+
34
+ # 次要版本:新功能,向后兼容(1.0.0 -> 1.1.0)
35
+ npm version minor
36
+
37
+ # 主要版本:破坏性变更(1.0.0 -> 2.0.0)
38
+ npm version major
39
+ ```
40
+
41
+ 或者手动编辑 `package.json` 中的 `version` 字段。
42
+
43
+ ### 3. 构建项目
44
+
45
+ ```bash
46
+ npm run build
47
+ ```
48
+
49
+ 这会自动执行 `prepublishOnly` 脚本,确保发布前构建最新代码。
50
+
51
+ ### 4. 发布到 npm
52
+
53
+ #### 发布到公共 npm
54
+
55
+ ```bash
56
+ npm publish
57
+ ```
58
+
59
+ #### 发布到私有 npm registry
60
+
61
+ ```bash
62
+ # 方式1:使用 --registry 参数
63
+ npm publish --registry=https://your-registry.com
64
+
65
+ # 方式2:配置 .npmrc 文件
66
+ echo "registry=https://your-registry.com" > .npmrc
67
+ npm publish
68
+ ```
69
+
70
+ #### 发布到 scoped package(推荐用于公司内部)
71
+
72
+ 如果包名是 `@company/h5-utils-lib`:
73
+
74
+ ```bash
75
+ # 发布为公开包
76
+ npm publish --access public
77
+
78
+ # 发布为私有包(需要 npm 付费账户)
79
+ npm publish --access restricted
80
+ ```
81
+
82
+ ### 5. 验证发布
83
+
84
+ ```bash
85
+ # 查看包信息
86
+ npm view h5-utils-lib
87
+
88
+ # 或安装测试
89
+ npm install h5-utils-lib@latest
90
+ ```
91
+
92
+ ## 🔐 配置私有 npm Registry
93
+
94
+ ### 方式1:使用 .npmrc 文件
95
+
96
+ 在项目根目录创建 `.npmrc` 文件:
97
+
98
+ ```
99
+ registry=https://your-registry.com
100
+ ```
101
+
102
+ ### 方式2:使用 npm config
103
+
104
+ ```bash
105
+ # 设置全局 registry
106
+ npm config set registry https://your-registry.com
107
+
108
+ # 或仅针对当前项目
109
+ npm config set registry https://your-registry.com --location=project
110
+ ```
111
+
112
+ ### 方式3:使用 .npmrc 配置认证
113
+
114
+ 如果需要认证:
115
+
116
+ ```
117
+ registry=https://your-registry.com
118
+ //your-registry.com/:_authToken=your-token-here
119
+ ```
120
+
121
+ ## 📦 发布内容检查
122
+
123
+ 发布前,可以使用以下命令检查将要发布的内容:
124
+
125
+ ```bash
126
+ # 查看将要发布的文件列表
127
+ npm pack --dry-run
128
+
129
+ # 打包但不发布(生成 .tgz 文件)
130
+ npm pack
131
+ ```
132
+
133
+ ## 🏷️ 发布标签
134
+
135
+ ### 发布 beta 版本
136
+
137
+ ```bash
138
+ npm version 1.0.1-beta.1
139
+ npm publish --tag beta
140
+ ```
141
+
142
+ 安装时使用:
143
+ ```bash
144
+ npm install h5-utils-lib@beta
145
+ ```
146
+
147
+ ### 发布 rc 版本
148
+
149
+ ```bash
150
+ npm version 1.0.1-rc.1
151
+ npm publish --tag rc
152
+ ```
153
+
154
+ ### 发布正式版本
155
+
156
+ ```bash
157
+ npm publish --tag latest
158
+ # 或直接
159
+ npm publish
160
+ ```
161
+
162
+ ## 🔄 更新已发布的版本
163
+
164
+ ### 更新补丁版本
165
+
166
+ ```bash
167
+ npm version patch
168
+ npm publish
169
+ ```
170
+
171
+ ### 更新次要版本
172
+
173
+ ```bash
174
+ npm version minor
175
+ npm publish
176
+ ```
177
+
178
+ ### 更新主要版本
179
+
180
+ ```bash
181
+ npm version major
182
+ npm publish
183
+ ```
184
+
185
+ ## 🗑️ 撤销发布
186
+
187
+ ### 撤销单个版本(24小时内)
188
+
189
+ ```bash
190
+ npm unpublish h5-utils-lib@1.0.1
191
+ ```
192
+
193
+ ### 撤销整个包(72小时内)
194
+
195
+ ```bash
196
+ npm unpublish h5-utils-lib --force
197
+ ```
198
+
199
+ ⚠️ **注意**:撤销发布有严格的时间限制,请谨慎操作。
200
+
201
+ ## 📝 常见问题
202
+
203
+ ### Q: 发布时提示 "You do not have permission"
204
+
205
+ A:
206
+ 1. 检查是否已登录:`npm whoami`
207
+ 2. 检查包名是否已被占用
208
+ 3. 如果是 scoped package,检查 `--access` 参数
209
+
210
+ ### Q: 如何发布到公司私有 registry?
211
+
212
+ A:
213
+ 1. 配置 `.npmrc` 文件指向私有 registry
214
+ 2. 在私有 registry 中创建账户
215
+ 3. 使用 `npm login --registry=...` 登录
216
+ 4. 使用 `npm publish --registry=...` 发布
217
+
218
+ ### Q: 发布后如何让团队使用?
219
+
220
+ A:
221
+ ```bash
222
+ # 团队成员安装
223
+ npm install h5-utils-lib --registry=https://your-registry.com
224
+
225
+ # 或配置 .npmrc
226
+ echo "registry=https://your-registry.com" > .npmrc
227
+ npm install h5-utils-lib
228
+ ```
229
+
230
+ ### Q: 如何同时支持公共和私有 registry?
231
+
232
+ A: 使用 scoped package:
233
+
234
+ ```json
235
+ {
236
+ "name": "@company/h5-utils-lib",
237
+ "publishConfig": {
238
+ "registry": "https://your-registry.com"
239
+ }
240
+ }
241
+ ```
242
+
243
+ ## 🔗 相关资源
244
+
245
+ - [npm 发布文档](https://docs.npmjs.com/packages-and-modules/contributing-packages-to-the-registry)
246
+ - [语义化版本控制](https://semver.org/)
247
+ - [npm 私有包](https://docs.npmjs.com/about-private-packages)
248
+
package/QUICK_START.md ADDED
@@ -0,0 +1,174 @@
1
+ # 快速开始指南
2
+
3
+ ## 🎯 作为开发模板使用
4
+
5
+ 安装 `h5-utils-lib` 后,你可以直接基于此项目继续开发,无需重新搭建项目结构。
6
+
7
+ ### 1. 安装包
8
+
9
+ ```bash
10
+ npm install h5-utils-lib
11
+ ```
12
+
13
+ ### 2. 进入项目目录
14
+
15
+ ```bash
16
+ cd node_modules/h5-utils-lib
17
+ ```
18
+
19
+ ### 3. 安装开发依赖
20
+
21
+ ```bash
22
+ npm install
23
+ ```
24
+
25
+ ### 4. 开始开发
26
+
27
+ ```bash
28
+ # 开发模式(监听文件变化,自动构建)
29
+ npm run dev
30
+
31
+ # 或手动构建
32
+ npm run build
33
+
34
+ # 运行测试
35
+ npm test
36
+ ```
37
+
38
+ ## 📝 添加新功能示例
39
+
40
+ ### 示例:添加 Cookie 工具函数
41
+
42
+ 1. **创建新模块文件**
43
+
44
+ ```typescript
45
+ // src/cookie/index.ts
46
+ export const setCookie = (name: string, value: string, days?: number): void => {
47
+ if (typeof document === 'undefined') return;
48
+ const expires = days
49
+ ? `; expires=${new Date(Date.now() + days * 864e5).toUTCString()}`
50
+ : '';
51
+ document.cookie = `${name}=${encodeURIComponent(value)}${expires}; path=/`;
52
+ };
53
+
54
+ export const getCookie = (name: string): string | null => {
55
+ if (typeof document === 'undefined') return null;
56
+ const nameEQ = name + '=';
57
+ const ca = document.cookie.split(';');
58
+ for (let i = 0; i < ca.length; i++) {
59
+ let c = ca[i];
60
+ while (c.charAt(0) === ' ') c = c.substring(1, c.length);
61
+ if (c.indexOf(nameEQ) === 0) {
62
+ return decodeURIComponent(c.substring(nameEQ.length, c.length));
63
+ }
64
+ }
65
+ return null;
66
+ };
67
+
68
+ export const removeCookie = (name: string): void => {
69
+ setCookie(name, '', -1);
70
+ };
71
+ ```
72
+
73
+ 2. **在入口文件中导出**
74
+
75
+ ```typescript
76
+ // src/index.ts
77
+ export * from './device';
78
+ export * from './url';
79
+ export * from './storage';
80
+ export * from './validate';
81
+ export * from './format';
82
+ export * from './cookie'; // 新增
83
+ ```
84
+
85
+ 3. **编写测试**
86
+
87
+ ```typescript
88
+ // tests/cookie.test.ts
89
+ import { setCookie, getCookie, removeCookie } from '../src/cookie';
90
+
91
+ describe('Cookie Utils', () => {
92
+ beforeEach(() => {
93
+ // 清理所有 cookies
94
+ document.cookie.split(';').forEach((c) => {
95
+ const eqPos = c.indexOf('=');
96
+ const name = eqPos > -1 ? c.substr(0, eqPos) : c;
97
+ document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
98
+ });
99
+ });
100
+
101
+ test('setCookie and getCookie', () => {
102
+ setCookie('test', 'value');
103
+ expect(getCookie('test')).toBe('value');
104
+ });
105
+
106
+ test('setCookie with expiration', () => {
107
+ setCookie('test', 'value', 7);
108
+ expect(getCookie('test')).toBe('value');
109
+ });
110
+
111
+ test('removeCookie', () => {
112
+ setCookie('test', 'value');
113
+ removeCookie('test');
114
+ expect(getCookie('test')).toBeNull();
115
+ });
116
+ });
117
+ ```
118
+
119
+ 4. **构建和测试**
120
+
121
+ ```bash
122
+ npm run build
123
+ npm test
124
+ ```
125
+
126
+ ## 🚀 发布新版本
127
+
128
+ ### 快速发布
129
+
130
+ ```bash
131
+ # 发布补丁版本(1.0.0 -> 1.0.1)
132
+ npm run publish:patch
133
+
134
+ # 发布次要版本(1.0.0 -> 1.1.0)
135
+ npm run publish:minor
136
+
137
+ # 发布主要版本(1.0.0 -> 2.0.0)
138
+ npm run publish:major
139
+ ```
140
+
141
+ ### 手动发布
142
+
143
+ ```bash
144
+ # 1. 更新版本号
145
+ npm version patch # 或 minor, major
146
+
147
+ # 2. 构建(会自动执行)
148
+ npm run build
149
+
150
+ # 3. 发布
151
+ npm publish
152
+
153
+ # 或发布到私有 registry
154
+ npm publish --registry=https://your-registry.com
155
+ ```
156
+
157
+ ## 📚 更多文档
158
+
159
+ - [开发指南](./DEVELOPMENT.md) - 详细的开发文档
160
+ - [使用示例](./USAGE_EXAMPLES.md) - 完整的使用示例
161
+ - [发布指南](./PUBLISH.md) - 详细的发布说明
162
+ - [README](./README.md) - 项目介绍和 API 文档
163
+
164
+ ## 💡 提示
165
+
166
+ 1. **开发时使用监听模式**:`npm run dev` 会自动监听文件变化并重新构建
167
+ 2. **测试驱动开发**:先写测试,再实现功能
168
+ 3. **保持代码风格一致**:参考现有代码的风格和结构
169
+ 4. **及时更新文档**:添加新功能后记得更新 README 和示例文档
170
+
171
+ ## 🆘 遇到问题?
172
+
173
+ 查看 [开发指南](./DEVELOPMENT.md) 中的"常见问题"部分,或查看相关文档。
174
+