@kivii.com/bridge 0.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/README.md +226 -0
- package/dist/kivii-bridge.es.js +114 -0
- package/dist/kivii-bridge.umd.js +1 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# Kivii Bridge
|
|
2
|
+
|
|
3
|
+
Kivii Bridge 是一个轻量级的 JavaScript/TypeScript 工具库,提供常用的工具函数和类,帮助开发者快速构建应用程序。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🚀 **轻量级** - 体积小,性能优秀
|
|
8
|
+
- 📦 **模块化** - 支持按需导入
|
|
9
|
+
- 🔧 **TypeScript** - 完整的类型支持
|
|
10
|
+
- 🌐 **多格式支持** - 支持 UMD 和 ES 模块
|
|
11
|
+
- 🛠️ **实用工具** - 提供常用的工具函数和类
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @kivii.com/bridge
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 使用方法
|
|
20
|
+
|
|
21
|
+
### ES 模块导入
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
import { Utils, Storage } from '@kivii.com/bridge'
|
|
25
|
+
|
|
26
|
+
// 或者导入整个库
|
|
27
|
+
import Kivii from '@kivii.com/bridge'
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### UMD 方式使用
|
|
31
|
+
|
|
32
|
+
```html
|
|
33
|
+
<script src="path/to/kivii-bridge.umd.js"></script>
|
|
34
|
+
<script>
|
|
35
|
+
// 全局变量 Kivii 可用
|
|
36
|
+
console.log(Kivii.version)
|
|
37
|
+
</script>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API 文档
|
|
41
|
+
|
|
42
|
+
### Utils 工具类
|
|
43
|
+
|
|
44
|
+
提供常用的工具方法:
|
|
45
|
+
|
|
46
|
+
#### formatDate(date, format)
|
|
47
|
+
|
|
48
|
+
格式化日期
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
import { Utils } from '@kivii.com/bridge'
|
|
52
|
+
|
|
53
|
+
const now = new Date()
|
|
54
|
+
Utils.formatDate(now, 'YYYY-MM-DD') // '2024-01-01'
|
|
55
|
+
Utils.formatDate(now, 'YYYY-MM-DD HH:mm:ss') // '2024-01-01 12:30:45'
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**参数:**
|
|
59
|
+
- `date` (Date): 要格式化的日期对象
|
|
60
|
+
- `format` (string): 格式字符串,默认 'YYYY-MM-DD'
|
|
61
|
+
|
|
62
|
+
**支持的格式占位符:**
|
|
63
|
+
- `YYYY` - 四位年份
|
|
64
|
+
- `MM` - 两位月份
|
|
65
|
+
- `DD` - 两位日期
|
|
66
|
+
- `HH` - 两位小时
|
|
67
|
+
- `mm` - 两位分钟
|
|
68
|
+
- `ss` - 两位秒数
|
|
69
|
+
|
|
70
|
+
#### debounce(func, delay)
|
|
71
|
+
|
|
72
|
+
防抖函数,在指定时间内只执行最后一次调用
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
import { Utils } from '@kivii.com/bridge'
|
|
76
|
+
|
|
77
|
+
const debouncedFn = Utils.debounce(() => {
|
|
78
|
+
console.log('执行搜索')
|
|
79
|
+
}, 300)
|
|
80
|
+
|
|
81
|
+
// 多次快速调用,只会在最后一次调用后 300ms 执行
|
|
82
|
+
debouncedFn()
|
|
83
|
+
debouncedFn()
|
|
84
|
+
debouncedFn()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**参数:**
|
|
88
|
+
- `func` (Function): 要防抖的函数
|
|
89
|
+
- `delay` (number): 延迟时间(毫秒)
|
|
90
|
+
|
|
91
|
+
#### deepClone(obj)
|
|
92
|
+
|
|
93
|
+
深拷贝对象
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
import { Utils } from '@kivii.com/bridge'
|
|
97
|
+
|
|
98
|
+
const original = { a: 1, b: { c: 2 } }
|
|
99
|
+
const cloned = Utils.deepClone(original)
|
|
100
|
+
|
|
101
|
+
cloned.b.c = 3
|
|
102
|
+
console.log(original.b.c) // 2 (原对象未被修改)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**参数:**
|
|
106
|
+
- `obj` (any): 要拷贝的对象
|
|
107
|
+
|
|
108
|
+
### Storage 存储类
|
|
109
|
+
|
|
110
|
+
提供本地存储的封装方法:
|
|
111
|
+
|
|
112
|
+
#### set(key, value)
|
|
113
|
+
|
|
114
|
+
设置本地存储
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
import { Storage } from '@kivii.com/bridge'
|
|
118
|
+
|
|
119
|
+
Storage.set('user', { name: 'John', age: 30 })
|
|
120
|
+
Storage.set('theme', 'dark')
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**参数:**
|
|
124
|
+
- `key` (string): 键名
|
|
125
|
+
- `value` (any): 要存储的值(会自动序列化为 JSON)
|
|
126
|
+
|
|
127
|
+
#### get(key, defaultValue)
|
|
128
|
+
|
|
129
|
+
获取本地存储
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
import { Storage } from '@kivii.com/bridge'
|
|
133
|
+
|
|
134
|
+
const user = Storage.get('user') // { name: 'John', age: 30 }
|
|
135
|
+
const theme = Storage.get('theme', 'light') // 'dark' 或默认值 'light'
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**参数:**
|
|
139
|
+
- `key` (string): 键名
|
|
140
|
+
- `defaultValue` (any): 默认值(可选)
|
|
141
|
+
|
|
142
|
+
**返回值:**
|
|
143
|
+
- 存储的值或默认值,如果不存在且没有默认值则返回 null
|
|
144
|
+
|
|
145
|
+
#### remove(key)
|
|
146
|
+
|
|
147
|
+
移除本地存储
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
import { Storage } from '@kivii.com/bridge'
|
|
151
|
+
|
|
152
|
+
Storage.remove('user')
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**参数:**
|
|
156
|
+
- `key` (string): 要移除的键名
|
|
157
|
+
|
|
158
|
+
#### clear()
|
|
159
|
+
|
|
160
|
+
清空所有本地存储
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
import { Storage } from '@kivii.com/bridge'
|
|
164
|
+
|
|
165
|
+
Storage.clear()
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## 开发
|
|
169
|
+
|
|
170
|
+
### 环境要求
|
|
171
|
+
|
|
172
|
+
- Node.js >= 16
|
|
173
|
+
- pnpm (推荐) 或 npm
|
|
174
|
+
|
|
175
|
+
### 开发命令
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
# 安装依赖
|
|
179
|
+
pnpm install
|
|
180
|
+
|
|
181
|
+
# 开发模式(监听文件变化)
|
|
182
|
+
pnpm dev
|
|
183
|
+
|
|
184
|
+
# 构建生产版本
|
|
185
|
+
pnpm build
|
|
186
|
+
|
|
187
|
+
# 清理构建文件
|
|
188
|
+
pnpm clean
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### 项目结构
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
kivii-bridge/
|
|
195
|
+
├── src/
|
|
196
|
+
│ ├── index.ts # 主入口文件
|
|
197
|
+
│ ├── utils/
|
|
198
|
+
│ │ └── index.ts # 工具函数
|
|
199
|
+
│ └── modules/
|
|
200
|
+
│ ├── index.ts # 模块导出
|
|
201
|
+
│ └── Storage.ts # 存储管理类
|
|
202
|
+
├── dist/ # 构建输出目录
|
|
203
|
+
├── package.json
|
|
204
|
+
├── vite.config.ts # Vite 配置
|
|
205
|
+
└── tsconfig.json # TypeScript 配置
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## 构建输出
|
|
209
|
+
|
|
210
|
+
构建后会生成以下文件:
|
|
211
|
+
|
|
212
|
+
- `dist/kivii-bridge.umd.js` - UMD 格式,可直接在浏览器中使用
|
|
213
|
+
- `dist/kivii-bridge.es.js` - ES 模块格式
|
|
214
|
+
- `dist/types/` - TypeScript 类型定义文件
|
|
215
|
+
|
|
216
|
+
## 许可证
|
|
217
|
+
|
|
218
|
+
MIT License
|
|
219
|
+
|
|
220
|
+
## 作者
|
|
221
|
+
|
|
222
|
+
Kivii.com
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
如有问题或建议,欢迎提交 Issue 或 Pull Request。
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
class Utils {
|
|
2
|
+
/**
|
|
3
|
+
* 格式化日期
|
|
4
|
+
* @param date 日期对象
|
|
5
|
+
* @param format 格式字符串,默认 'YYYY-MM-DD'
|
|
6
|
+
*/
|
|
7
|
+
static formatDate(date, format = "YYYY-MM-DD") {
|
|
8
|
+
const year = date.getFullYear();
|
|
9
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
10
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
11
|
+
const hours = String(date.getHours()).padStart(2, "0");
|
|
12
|
+
const minutes = String(date.getMinutes()).padStart(2, "0");
|
|
13
|
+
const seconds = String(date.getSeconds()).padStart(2, "0");
|
|
14
|
+
return format.replace("YYYY", String(year)).replace("MM", month).replace("DD", day).replace("HH", hours).replace("mm", minutes).replace("ss", seconds);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 防抖函数
|
|
18
|
+
* @param func 要防抖的函数
|
|
19
|
+
* @param delay 延迟时间(毫秒)
|
|
20
|
+
*/
|
|
21
|
+
static debounce(func, delay) {
|
|
22
|
+
let timeoutId;
|
|
23
|
+
return (...args) => {
|
|
24
|
+
clearTimeout(timeoutId);
|
|
25
|
+
timeoutId = setTimeout(() => func.apply(this, args), delay);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 深拷贝对象
|
|
30
|
+
* @param obj 要拷贝的对象
|
|
31
|
+
*/
|
|
32
|
+
static deepClone(obj) {
|
|
33
|
+
if (obj === null || typeof obj !== "object") {
|
|
34
|
+
return obj;
|
|
35
|
+
}
|
|
36
|
+
if (obj instanceof Date) {
|
|
37
|
+
return new Date(obj.getTime());
|
|
38
|
+
}
|
|
39
|
+
if (obj instanceof Array) {
|
|
40
|
+
return obj.map((item) => this.deepClone(item));
|
|
41
|
+
}
|
|
42
|
+
if (typeof obj === "object") {
|
|
43
|
+
const clonedObj = {};
|
|
44
|
+
for (const key in obj) {
|
|
45
|
+
if (obj.hasOwnProperty(key)) {
|
|
46
|
+
clonedObj[key] = this.deepClone(obj[key]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return clonedObj;
|
|
50
|
+
}
|
|
51
|
+
return obj;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
class Storage {
|
|
55
|
+
/**
|
|
56
|
+
* 设置本地存储
|
|
57
|
+
* @param key 键名
|
|
58
|
+
* @param value 值
|
|
59
|
+
*/
|
|
60
|
+
static set(key, value) {
|
|
61
|
+
try {
|
|
62
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.error("Storage setItem error:", error);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 获取本地存储
|
|
69
|
+
* @param key 键名
|
|
70
|
+
* @param defaultValue 默认值
|
|
71
|
+
*/
|
|
72
|
+
static get(key, defaultValue) {
|
|
73
|
+
try {
|
|
74
|
+
const item = localStorage.getItem(key);
|
|
75
|
+
return item ? JSON.parse(item) : defaultValue || null;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error("Storage getItem error:", error);
|
|
78
|
+
return defaultValue || null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 移除本地存储
|
|
83
|
+
* @param key 键名
|
|
84
|
+
*/
|
|
85
|
+
static remove(key) {
|
|
86
|
+
try {
|
|
87
|
+
localStorage.removeItem(key);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error("Storage removeItem error:", error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 清空本地存储
|
|
94
|
+
*/
|
|
95
|
+
static clear() {
|
|
96
|
+
try {
|
|
97
|
+
localStorage.clear();
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error("Storage clear error:", error);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const version = "1.0.0";
|
|
104
|
+
const index = {
|
|
105
|
+
Utils,
|
|
106
|
+
Storage,
|
|
107
|
+
version: "1.0.0"
|
|
108
|
+
};
|
|
109
|
+
export {
|
|
110
|
+
Storage,
|
|
111
|
+
Utils,
|
|
112
|
+
index as default,
|
|
113
|
+
version
|
|
114
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Kivii={})}(this,function(e){"use strict";class t{static formatDate(e,t="YYYY-MM-DD"){const r=e.getFullYear(),o=String(e.getMonth()+1).padStart(2,"0"),a=String(e.getDate()).padStart(2,"0"),n=String(e.getHours()).padStart(2,"0"),c=String(e.getMinutes()).padStart(2,"0"),s=String(e.getSeconds()).padStart(2,"0");return t.replace("YYYY",String(r)).replace("MM",o).replace("DD",a).replace("HH",n).replace("mm",c).replace("ss",s)}static debounce(e,t){let r;return(...o)=>{clearTimeout(r),r=setTimeout(()=>e.apply(this,o),t)}}static deepClone(e){if(null===e||"object"!=typeof e)return e;if(e instanceof Date)return new Date(e.getTime());if(e instanceof Array)return e.map(e=>this.deepClone(e));if("object"==typeof e){const t={};for(const r in e)e.hasOwnProperty(r)&&(t[r]=this.deepClone(e[r]));return t}return e}}class r{static set(e,t){try{localStorage.setItem(e,JSON.stringify(t))}catch(r){console.error("Storage setItem error:",r)}}static get(e,t){try{const r=localStorage.getItem(e);return r?JSON.parse(r):t||null}catch(r){return console.error("Storage getItem error:",r),t||null}}static remove(e){try{localStorage.removeItem(e)}catch(t){console.error("Storage removeItem error:",t)}}static clear(){try{localStorage.clear()}catch(e){console.error("Storage clear error:",e)}}}const o={Utils:t,Storage:r,version:"1.0.0"};e.Storage=r,e.Utils=t,e.default=o,e.version="1.0.0",Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kivii.com/bridge",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Kivii bridge 类库 - 提供基础工具函数和类",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"kivii",
|
|
7
|
+
"bridge",
|
|
8
|
+
"utils"
|
|
9
|
+
],
|
|
10
|
+
"author": "Kivii.com",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"main": "dist/kivii-bridge.cjs.js",
|
|
13
|
+
"module": "dist/kivii-bridge.es.js",
|
|
14
|
+
"types": "dist/types/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./dist/kivii-bridge.es.js",
|
|
18
|
+
"require": "./dist/kivii-bridge.cjs.js",
|
|
19
|
+
"types": "./dist/types/index.d.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "vite build",
|
|
30
|
+
"dev": "vite build --watch",
|
|
31
|
+
"clean": "rimraf dist",
|
|
32
|
+
"prebuild": "npm run clean",
|
|
33
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^24.6.1",
|
|
37
|
+
"rimraf": "^5.0.0",
|
|
38
|
+
"terser": "^5.0.0",
|
|
39
|
+
"typescript": "^5.0.0",
|
|
40
|
+
"vite": "^5.0.0",
|
|
41
|
+
"vite-plugin-dts": "^4.5.4"
|
|
42
|
+
}
|
|
43
|
+
}
|