@allkit/shared 0.0.2 → 0.0.3
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/dist/package.json +1 -1
- package/dist/skills/SKILL.md +240 -0
- package/dist/skills/examples/usage.ts +88 -0
- package/dist/skills/references/clipboard.md +39 -0
- package/dist/skills/references/cloneDeep.md +60 -0
- package/dist/skills/references/cookie.md +56 -0
- package/dist/skills/references/date.md +466 -0
- package/dist/skills/references/device.md +138 -0
- package/dist/skills/references/element.md +99 -0
- package/dist/skills/references/is.md +415 -0
- package/dist/skills/references/lodash.md +472 -0
- package/dist/skills/references/number.md +248 -0
- package/dist/skills/references/storage.md +113 -0
- package/dist/skills/references/string.md +126 -0
- package/dist/skills/references/timer.md +78 -0
- package/package.json +1 -1
- package/scripts/build.mjs +6 -3
package/dist/package.json
CHANGED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: shared
|
|
3
|
+
description: 前端通用工具函数库,包含类型判断、日期、数字、字符串、防抖节流等多种常用工具函数 / A collection of utility functions for frontend development
|
|
4
|
+
author: allkit
|
|
5
|
+
category: utilities
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# @allkit/shared
|
|
9
|
+
|
|
10
|
+
前端通用工具函数库,解决常见的开发问题。Provide a collection of utility functions for frontend development.
|
|
11
|
+
|
|
12
|
+
## Install / 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @allkit/shared
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Functions / 函数
|
|
19
|
+
|
|
20
|
+
### 类型判断 (is)
|
|
21
|
+
|
|
22
|
+
类型判断工具函数 / Type checking utility functions
|
|
23
|
+
|
|
24
|
+
| Function | Description | 中文说明 |
|
|
25
|
+
|----------|-------------|----------|
|
|
26
|
+
| [is](./references/is.md) | General type checking | 通用类型判断 |
|
|
27
|
+
| [isDef](./references/is.md#isdef) | Check if not undefined | 判断不是 undefined |
|
|
28
|
+
| [isUnDef](./references/is.md#isundef) | Check if undefined | 判断是 undefined |
|
|
29
|
+
| [isObject](./references/is.md#isobject) | Check if is Object (includes Array, Map) | 判断是 Object(含数组、Map) |
|
|
30
|
+
| [isPlainObject](./references/is.md#isplainobject) | Check if is plain Object | 判断是原始 Object(不含数组) |
|
|
31
|
+
| [isEmpty](./references/is.md#isempty) | Check if is empty | 判断是空(含空数组、空对象、空 Map) |
|
|
32
|
+
| [isEmptyValue](./references/is.md#isemptyvalue) | Check if is empty value | 检测是否空值 |
|
|
33
|
+
| [isDate](./references/is.md#isdate) | Check if is Date object | 判断是日期对象 |
|
|
34
|
+
| [isNullOrUnDef](./references/is.md#isnullorundef) | Check if is null or undefined | 判断是 null 或 undefined |
|
|
35
|
+
| [isNumber](./references/is.md#isnumber) | Check if is number | 是否是 number |
|
|
36
|
+
| [isInteger](./references/is.md#isinteger) | Check if is integer | 判断是否为整数 |
|
|
37
|
+
| [isNumeric](./references/is.md#isnumeric) | Check if is numeric | 判断是否为数值 |
|
|
38
|
+
| [isPromise](./references/is.md#ispromise) | Check if is Promise | 是否是 Promise |
|
|
39
|
+
| [isFunction](./references/is.md#isfunction) | Check if is function | 是否是函数 |
|
|
40
|
+
| [isArray](./references/is.md#isarray) | Check if is array | 是否是数组 |
|
|
41
|
+
| [isUrl](./references/is.md#isurl) | Check if is URL | 是否是 URL |
|
|
42
|
+
| [isWindow](./references/is.md#iswindow) | Check if is Window | 是否是 Window |
|
|
43
|
+
| [isMap](./references/is.md#ismap) | Check if is Map | 是否是 Map |
|
|
44
|
+
| [isRegExp](./references/is.md#isregexp) | Check if is RegExp | 是否是正则 |
|
|
45
|
+
|
|
46
|
+
### 日期处理 (date)
|
|
47
|
+
|
|
48
|
+
日期处理工具函数,基于 dayjs / Date handling utilities based on dayjs
|
|
49
|
+
|
|
50
|
+
| Function | Description | 中文说明 |
|
|
51
|
+
|----------|-------------|----------|
|
|
52
|
+
| [useDate](./references/date.md#usedate) | Get dayjs instance | 使用 dayjs 实例 |
|
|
53
|
+
| [dateFormat](./references/date.md#dateformat) | Format date to string | 日期转格式字符串 |
|
|
54
|
+
| [minute](./references/date.md#minute) | Get current time (default to minute) | 返回当前时间(默认到分钟) |
|
|
55
|
+
| [dateMonthDays](./references/date.md#datemonthdays) | Get days in month | 返回日期对应月份天数 |
|
|
56
|
+
| [getCurrDate](./references/date.md#getcurrdate) | Get date before/after i days | 返回前后 i 天的日期字符串 |
|
|
57
|
+
| [dateDiff](./references/date.md#datediff) | Get date difference | 返回两个日期时间差 |
|
|
58
|
+
| [dateFromNow](./references/date.md#datefromnow) | Relative time display | 相对时间显示 |
|
|
59
|
+
| [dateDiffFormat](./references/date.md#datediffformat) | Date difference in natural units | 计算两个日期之间的差值 |
|
|
60
|
+
| [durationFormat](./references/date.md#durationformat) | Format duration | 格式化时长 |
|
|
61
|
+
| [durationFormatNoZero](./references/date.md#durationformatnozero) | Format duration without zero | 格式化时长(去掉 0) |
|
|
62
|
+
|
|
63
|
+
### 字符串操作 (string)
|
|
64
|
+
|
|
65
|
+
字符串操作工具函数 / String manipulation utilities
|
|
66
|
+
|
|
67
|
+
| Function | Description | 中文说明 |
|
|
68
|
+
|----------|-------------|----------|
|
|
69
|
+
| [camelize](./references/string.md#camelize) | Kebab to camelCase | 中划线转小驼峰 |
|
|
70
|
+
| [kebabCase](./references/string.md#kebabcase) | CamelCase to kebab-case | 驼峰转中划线 |
|
|
71
|
+
| [snakeCase](./references/string.md#snakecase) | CamelCase to snake_case | 驼峰转下划线 |
|
|
72
|
+
| [lowerFirst](./references/string.md#lowerfirst) | First char to lower | 首字母转小写 |
|
|
73
|
+
| [upperFirst](./references/string.md#upperfirst) | First char to upper | 首字母转大写 |
|
|
74
|
+
|
|
75
|
+
### 数字处理 (number)
|
|
76
|
+
|
|
77
|
+
数字处理工具函数,基于 big.js / Number utilities based on big.js
|
|
78
|
+
|
|
79
|
+
| Function | Description | 中文说明 |
|
|
80
|
+
|----------|-------------|----------|
|
|
81
|
+
| [useNumber](./references/number.md#usenumber) | Get Big instance | 返回实例化的 Big 对象 |
|
|
82
|
+
| [formatNumber](./references/number.md#formatnumber) | Format number with separator | 格式化数字(千位分隔符) |
|
|
83
|
+
| [formatMoney](./references/number.md#formatmoney) | Format money (万/亿) | 格式化金钱(万、亿) |
|
|
84
|
+
|
|
85
|
+
### 本地存储 (storage)
|
|
86
|
+
|
|
87
|
+
本地存储工具函数,自动处理 JSON 序列化 / Storage utilities with JSON serialization
|
|
88
|
+
|
|
89
|
+
| Function | Description | 中文说明 |
|
|
90
|
+
|----------|-------------|----------|
|
|
91
|
+
| [setLocal](./references/storage.md#setlocal) | Set LocalStorage | 设置 LocalStorage |
|
|
92
|
+
| [getLocal](./references/storage.md#getlocal) | Get LocalStorage | 获取 LocalStorage |
|
|
93
|
+
| [removeLocal](./references/storage.md#removelocal) | Remove LocalStorage | 删除 LocalStorage |
|
|
94
|
+
| [setSession](./references/storage.md#setsession) | Set SessionStorage | 设置 SessionStorage |
|
|
95
|
+
| [getSession](./references/storage.md#getsession) | Get SessionStorage | 获取 SessionStorage |
|
|
96
|
+
| [removeSession](./references/storage.md#removesession) | Remove SessionStorage | 删除 SessionStorage |
|
|
97
|
+
|
|
98
|
+
### Lodash 常用函数 (lodash)
|
|
99
|
+
|
|
100
|
+
Lodash 常用工具函数 / Common Lodash utilities
|
|
101
|
+
|
|
102
|
+
| Function | Description | 中文说明 |
|
|
103
|
+
|----------|-------------|----------|
|
|
104
|
+
| [deepClone](./references/lodash.md#deepclone) | Deep clone | 深拷贝 |
|
|
105
|
+
| [omit](./references/lodash.md#omit) | Omit fields | 删除对象中的某些键值对 |
|
|
106
|
+
| [pick](./references/lodash.md#pick) | Pick fields | 从对象中取出指定的键值对 |
|
|
107
|
+
| [debounce](./references/lodash.md#debounce) | Debounce function | 防抖函数 |
|
|
108
|
+
| [throttle](./references/lodash.md#throttle) | Throttle function | 节流函数 |
|
|
109
|
+
| [uniqueId](./references/lodash.md#uniqueid) | Generate unique ID | 生成浏览器唯一 ID |
|
|
110
|
+
| [get](./references/lodash.md#get) | Get value by path | 从对象中获取指定路径的值 |
|
|
111
|
+
| [objToQString](./references/lodash.md#objtoqstring) | Object to query string | 对象转 URL 字符串 |
|
|
112
|
+
| [qStringToObj](./references/lodash.md#qstringtoobj) | Query string to object | URL 字符串转对象 |
|
|
113
|
+
| [blobToBase64](./references/lodash.md#blobtobase64) | Blob to Base64 | Blob 转 Base64 |
|
|
114
|
+
|
|
115
|
+
### 定时器 (timer)
|
|
116
|
+
|
|
117
|
+
定时器工具函数 / Timer utilities
|
|
118
|
+
|
|
119
|
+
| Function | Description | 中文说明 |
|
|
120
|
+
|----------|-------------|----------|
|
|
121
|
+
| [sleep](./references/timer.md#sleep) | Sleep function | 睡眠函数 |
|
|
122
|
+
| [raf](./references/timer.md#raf) | Request animation frame | 监听下一动画帧 |
|
|
123
|
+
| [doubleRaf](./references/timer.md#doubleraf) | Double animation frame | 监听双帧动画帧 |
|
|
124
|
+
|
|
125
|
+
### 设备判断 (device)
|
|
126
|
+
|
|
127
|
+
设备判断工具函数 / Device detection utilities
|
|
128
|
+
|
|
129
|
+
| Function | Description | 中文说明 |
|
|
130
|
+
|----------|-------------|----------|
|
|
131
|
+
| [isIos](./references/device.md#isios) | Check if iOS | 是否是 iOS 客户端 |
|
|
132
|
+
| [isAndroid](./references/device.md#isandroid) | Check if Android | 是否是 Android 客户端 |
|
|
133
|
+
| [isWeChat](./references/device.md#iswechat) | Check if WeChat | 是否是微信浏览器 |
|
|
134
|
+
| [isWxMiniProgram](./references/device.md#iswxminiprogram) | Check if WeChat Mini Program | 是否是微信小程序 |
|
|
135
|
+
| [isHarmony](./references/device.md#isharmony) | Check if HarmonyOS | 是否是鸿蒙系统 |
|
|
136
|
+
| [getDeviceBrand](./references/device.md#getdevicebrand) | Get device brand | 获取设备厂商 |
|
|
137
|
+
|
|
138
|
+
### 剪贴板 (clipboard)
|
|
139
|
+
|
|
140
|
+
剪贴板操作工具函数 / Clipboard utilities
|
|
141
|
+
|
|
142
|
+
| Function | Description | 中文说明 |
|
|
143
|
+
|----------|-------------|----------|
|
|
144
|
+
| [copyTextToClipboard](./references/clipboard.md#copytexttoclipboard) | Copy text to clipboard | 复制文本到剪贴板 |
|
|
145
|
+
|
|
146
|
+
### Cookie 操作 (cookie)
|
|
147
|
+
|
|
148
|
+
Cookie 操作工具函数 / Cookie utilities
|
|
149
|
+
|
|
150
|
+
| Function | Description | 中文说明 |
|
|
151
|
+
|----------|-------------|----------|
|
|
152
|
+
| [getCookie](./references/cookie.md#getcookie) | Get cookie value | 获取 Cookie 值 |
|
|
153
|
+
| [setCookie](./references/cookie.md#setcookie) | Set cookie | 设置 Cookie |
|
|
154
|
+
| [removeCookie](./references/cookie.md#removecookie) | Remove cookie | 删除 Cookie |
|
|
155
|
+
|
|
156
|
+
### DOM 元素操作 (element)
|
|
157
|
+
|
|
158
|
+
DOM 元素操作工具函数 / DOM element utilities
|
|
159
|
+
|
|
160
|
+
| Function | Description | 中文说明 |
|
|
161
|
+
|----------|-------------|----------|
|
|
162
|
+
| [getBoundingClientRect](./references/element.md#getboundingclientrect) | Get element rect | 获取元素的位置和尺寸 |
|
|
163
|
+
| [addClass](./references/element.md#addclass) | Add CSS class | 添加 CSS 类名 |
|
|
164
|
+
| [removeClass](./references/element.md#removeclass) | Remove CSS class | 移除 CSS 类名 |
|
|
165
|
+
| [toggleClass](./references/element.md#toggleclass) | Toggle CSS class | 切换 CSS 类名 |
|
|
166
|
+
|
|
167
|
+
### 深拷贝 (cloneDeep)
|
|
168
|
+
|
|
169
|
+
深拷贝工具函数 / Deep clone utilities
|
|
170
|
+
|
|
171
|
+
| Function | Description | 中文说明 |
|
|
172
|
+
|----------|-------------|----------|
|
|
173
|
+
| [cloneDeep](./references/cloneDeep.md#clonedeep) | Deep clone | 深拷贝 |
|
|
174
|
+
|
|
175
|
+
## Usage / 使用示例
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
import {
|
|
179
|
+
isString, isNumber, isObject, isEmpty, isInteger,
|
|
180
|
+
useDate, dateFormat, dateFromNow, durationFormat,
|
|
181
|
+
camelize, kebabCase, snakeCase,
|
|
182
|
+
useNumber, formatNumber, formatMoney,
|
|
183
|
+
setLocal, getLocal, setSession, getSession,
|
|
184
|
+
deepClone, omit, pick, debounce, throttle, uniqueId,
|
|
185
|
+
sleep, raf, doubleRaf,
|
|
186
|
+
isIos, isAndroid, isWeChat, getDeviceBrand,
|
|
187
|
+
copyTextToClipboard
|
|
188
|
+
} from '@allkit/shared'
|
|
189
|
+
|
|
190
|
+
// 类型判断 / Type checking
|
|
191
|
+
isString('hello') // true
|
|
192
|
+
isNumber(123) // true
|
|
193
|
+
isObject({ name: 1 }) // true
|
|
194
|
+
isEmpty([]) // true
|
|
195
|
+
isInteger('123') // true
|
|
196
|
+
|
|
197
|
+
// 日期处理 / Date handling
|
|
198
|
+
useDate().format('YYYY-MM-DD HH:mm:ss')
|
|
199
|
+
dateFormat(new Date(), 'YYYY-MM-DD')
|
|
200
|
+
dateFromNow('2024-03-02 00:00')
|
|
201
|
+
durationFormat(61, { unit: 'm' })
|
|
202
|
+
|
|
203
|
+
// 字符串操作 / String manipulation
|
|
204
|
+
camelize('user-info') // 'userInfo'
|
|
205
|
+
kebabCase('userInfo') // 'user-info'
|
|
206
|
+
snakeCase('userInfo') // 'user_info'
|
|
207
|
+
|
|
208
|
+
// 数字处理 / Number handling
|
|
209
|
+
useNumber('1').add('2').toNumber() // 3
|
|
210
|
+
formatNumber(1234567) // '1,234,567'
|
|
211
|
+
formatMoney(1234567) // '123万4567'
|
|
212
|
+
|
|
213
|
+
// 本地存储 / Storage
|
|
214
|
+
setLocal('token', '123')
|
|
215
|
+
const token = getLocal<string>('token')
|
|
216
|
+
setSession('userInfo', { name: '张三' })
|
|
217
|
+
const userInfo = getSession<{ name: string }>('userInfo')
|
|
218
|
+
|
|
219
|
+
// Lodash 常用函数 / Lodash utilities
|
|
220
|
+
deepClone({ name: 'a', person: { age: 18 } })
|
|
221
|
+
omit({ a: 1, b: 2, c: 3 }, ['a']) // { b: 2, c: 3 }
|
|
222
|
+
pick({ a: 1, b: 2, c: 3 }, ['a', 'b']) // { a: 1, b: 2 }
|
|
223
|
+
debounce(fn, 300)
|
|
224
|
+
throttle(fn, 100)
|
|
225
|
+
uniqueId() // 16 位
|
|
226
|
+
|
|
227
|
+
// 定时器 / Timer
|
|
228
|
+
await sleep(1000)
|
|
229
|
+
await raf()
|
|
230
|
+
await doubleRaf()
|
|
231
|
+
|
|
232
|
+
// 设备判断 / Device detection
|
|
233
|
+
isIos() // boolean
|
|
234
|
+
isAndroid() // boolean
|
|
235
|
+
isWeChat() // boolean
|
|
236
|
+
getDeviceBrand() // EnumDeviceBrand
|
|
237
|
+
|
|
238
|
+
// 剪贴板 / Clipboard
|
|
239
|
+
copyTextToClipboard('hello') // boolean
|
|
240
|
+
```
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isString,
|
|
3
|
+
isNumber,
|
|
4
|
+
isObject,
|
|
5
|
+
isEmpty,
|
|
6
|
+
isInteger,
|
|
7
|
+
useDate,
|
|
8
|
+
dateFormat,
|
|
9
|
+
dateFromNow,
|
|
10
|
+
durationFormat,
|
|
11
|
+
camelize,
|
|
12
|
+
kebabCase,
|
|
13
|
+
snakeCase,
|
|
14
|
+
useNumber,
|
|
15
|
+
formatNumber,
|
|
16
|
+
formatMoney,
|
|
17
|
+
setLocal,
|
|
18
|
+
getLocal,
|
|
19
|
+
setSession,
|
|
20
|
+
getSession,
|
|
21
|
+
omit,
|
|
22
|
+
pick,
|
|
23
|
+
uniqueId,
|
|
24
|
+
sleep,
|
|
25
|
+
raf,
|
|
26
|
+
doubleRaf,
|
|
27
|
+
isIos,
|
|
28
|
+
isAndroid,
|
|
29
|
+
isWeChat,
|
|
30
|
+
getDeviceBrand,
|
|
31
|
+
copyTextToClipboard,
|
|
32
|
+
} from '@allkit/shared'
|
|
33
|
+
|
|
34
|
+
console.log('=== is ===')
|
|
35
|
+
console.log('isString("hello"):', isString('hello'))
|
|
36
|
+
console.log('isNumber(123):', isNumber(123))
|
|
37
|
+
console.log('isObject({ name: 1 }):', isObject({ name: 1 }))
|
|
38
|
+
console.log('isEmpty([]):', isEmpty([]))
|
|
39
|
+
console.log('isInteger("123"):', isInteger('123'))
|
|
40
|
+
|
|
41
|
+
console.log('\n=== date ===')
|
|
42
|
+
console.log('useDate():', useDate().format('YYYY-MM-DD'))
|
|
43
|
+
console.log('dateFormat():', dateFormat(new Date(), 'YYYY-MM-DD'))
|
|
44
|
+
console.log('dateFromNow():', dateFromNow('2024-03-02'))
|
|
45
|
+
console.log('durationFormat():', durationFormat(61, { unit: 'm' }))
|
|
46
|
+
|
|
47
|
+
console.log('\n=== string ===')
|
|
48
|
+
console.log('camelize("user-info"):', camelize('user-info'))
|
|
49
|
+
console.log('kebabCase("userInfo"):', kebabCase('userInfo'))
|
|
50
|
+
console.log('snakeCase("userInfo"):', snakeCase('userInfo'))
|
|
51
|
+
|
|
52
|
+
console.log('\n=== number ===')
|
|
53
|
+
console.log('useNumber:', useNumber('1').add('2').toNumber())
|
|
54
|
+
console.log('formatNumber:', formatNumber(1234567))
|
|
55
|
+
console.log('formatMoney:', formatMoney(1234567))
|
|
56
|
+
|
|
57
|
+
console.log('\n=== storage ===')
|
|
58
|
+
setLocal('token', '123')
|
|
59
|
+
console.log('getLocal:', getLocal<string>('token'))
|
|
60
|
+
setSession('userInfo', { name: '张三' })
|
|
61
|
+
console.log('getSession:', getSession<{ name: string }>('userInfo'))
|
|
62
|
+
|
|
63
|
+
console.log('\n=== lodash ===')
|
|
64
|
+
const obj = { a: 1, b: 2, c: 3 }
|
|
65
|
+
console.log('omit:', omit(obj, ['a']))
|
|
66
|
+
console.log('pick:', pick(obj, ['a', 'b']))
|
|
67
|
+
console.log('uniqueId:', uniqueId())
|
|
68
|
+
|
|
69
|
+
console.log('\n=== timer ===')
|
|
70
|
+
async function timerDemo() {
|
|
71
|
+
await sleep(100)
|
|
72
|
+
console.log('sleep done')
|
|
73
|
+
await raf()
|
|
74
|
+
console.log('raf done')
|
|
75
|
+
await doubleRaf()
|
|
76
|
+
console.log('doubleRaf done')
|
|
77
|
+
}
|
|
78
|
+
timerDemo()
|
|
79
|
+
|
|
80
|
+
console.log('\n=== device ===')
|
|
81
|
+
console.log('isIos:', isIos())
|
|
82
|
+
console.log('isAndroid:', isAndroid())
|
|
83
|
+
console.log('isWeChat:', isWeChat())
|
|
84
|
+
console.log('getDeviceBrand:', getDeviceBrand())
|
|
85
|
+
|
|
86
|
+
console.log('\n=== clipboard ===')
|
|
87
|
+
const success = copyTextToClipboard('hello')
|
|
88
|
+
console.log('copy success:', success)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# clipboard
|
|
2
|
+
|
|
3
|
+
剪贴板操作工具函数
|
|
4
|
+
|
|
5
|
+
## Functions
|
|
6
|
+
|
|
7
|
+
### copyTextToClipboard
|
|
8
|
+
|
|
9
|
+
复制文本到剪贴板
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
function copyTextToClipboard(value: string): boolean
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**Example**
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { copyTextToClipboard } from '@allkit/shared'
|
|
19
|
+
|
|
20
|
+
const success = copyTextToClipboard('复制的内容')
|
|
21
|
+
if (success) {
|
|
22
|
+
console.log('复制成功')
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### readClipboard
|
|
27
|
+
|
|
28
|
+
读取剪贴板内容
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
async function readClipboard(): Promise<string>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**Example**
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
const text = await readClipboard()
|
|
38
|
+
console.log(text)
|
|
39
|
+
```
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# cloneDeep
|
|
2
|
+
|
|
3
|
+
深拷贝工具函数
|
|
4
|
+
|
|
5
|
+
## Functions
|
|
6
|
+
|
|
7
|
+
### cloneDeep
|
|
8
|
+
|
|
9
|
+
深拷贝,支持所有数据类型
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
function cloneDeep<T>(obj: T): T
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**支持的数据类型**
|
|
16
|
+
|
|
17
|
+
- 基本类型(string, number, boolean, null, undefined, symbol, bigint)
|
|
18
|
+
- 数组
|
|
19
|
+
- 对象
|
|
20
|
+
- Date
|
|
21
|
+
- RegExp
|
|
22
|
+
- Map
|
|
23
|
+
- Set
|
|
24
|
+
- 嵌套对象/数组
|
|
25
|
+
|
|
26
|
+
**Example**
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { cloneDeep } from '@allkit/shared'
|
|
30
|
+
|
|
31
|
+
// 简单对象
|
|
32
|
+
const original = { name: 'a', person: { age: 18 } }
|
|
33
|
+
const cloned = cloneDeep(original)
|
|
34
|
+
cloned.person.age = 20
|
|
35
|
+
console.log(original.person.age) // 18
|
|
36
|
+
|
|
37
|
+
// 数组
|
|
38
|
+
const arr = [1, 2, { a: 1 }]
|
|
39
|
+
const arrClone = cloneDeep(arr)
|
|
40
|
+
|
|
41
|
+
// Date
|
|
42
|
+
const date = new Date()
|
|
43
|
+
const dateClone = cloneDeep(date)
|
|
44
|
+
|
|
45
|
+
// RegExp
|
|
46
|
+
const reg = /test/g
|
|
47
|
+
const regClone = cloneDeep(reg)
|
|
48
|
+
|
|
49
|
+
// Map
|
|
50
|
+
const map = new Map([['key', 'value']])
|
|
51
|
+
const mapClone = cloneDeep(map)
|
|
52
|
+
|
|
53
|
+
// Set
|
|
54
|
+
const set = new Set([1, 2, 3])
|
|
55
|
+
const setClone = cloneDeep(set)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Difference from deepClone
|
|
59
|
+
|
|
60
|
+
`cloneDeep` 是 `deepClone` 的别名,功能完全相同。
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# cookie
|
|
2
|
+
|
|
3
|
+
Cookie 操作工具函数
|
|
4
|
+
|
|
5
|
+
## Functions
|
|
6
|
+
|
|
7
|
+
### getCookie
|
|
8
|
+
|
|
9
|
+
获取 Cookie 值
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
function getCookie(name: string): string | null
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### setCookie
|
|
16
|
+
|
|
17
|
+
设置 Cookie
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
function setCookie(name: string, value: string, options?: CookieOptions): void
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Options**
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
interface CookieOptions {
|
|
27
|
+
expires?: number | Date // 过期时间
|
|
28
|
+
path?: string // 路径
|
|
29
|
+
domain?: string // 域名
|
|
30
|
+
secure?: boolean // 是否仅 HTTPS
|
|
31
|
+
sameSite?: 'Strict' | 'Lax' | 'None'
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### removeCookie
|
|
36
|
+
|
|
37
|
+
删除 Cookie
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
function removeCookie(name: string, options?: { path?: string; domain?: string }): void
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Example
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { getCookie, setCookie, removeCookie } from '@allkit/shared'
|
|
47
|
+
|
|
48
|
+
// 设置 Cookie
|
|
49
|
+
setCookie('token', '123456', { expires: 7 }) // 7 天后过期
|
|
50
|
+
|
|
51
|
+
// 获取 Cookie
|
|
52
|
+
const token = getCookie('token')
|
|
53
|
+
|
|
54
|
+
// 删除 Cookie
|
|
55
|
+
removeCookie('token')
|
|
56
|
+
```
|