@allkit/shared 0.0.1 → 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.
Files changed (31) hide show
  1. package/dist/package.json +1 -1
  2. package/{skill → dist/skills}/SKILL.md +1 -1
  3. package/{skill → dist/skills}/examples/usage.ts +30 -9
  4. package/package.json +1 -1
  5. package/scripts/build.mjs +6 -3
  6. package/skills/SKILL.md +240 -0
  7. package/skills/examples/usage.ts +88 -0
  8. package/skills/references/clipboard.md +39 -0
  9. package/skills/references/cloneDeep.md +60 -0
  10. package/skills/references/cookie.md +56 -0
  11. package/skills/references/date.md +466 -0
  12. package/skills/references/device.md +138 -0
  13. package/skills/references/element.md +99 -0
  14. package/skills/references/is.md +415 -0
  15. package/skills/references/lodash.md +472 -0
  16. package/skills/references/number.md +248 -0
  17. package/skills/references/storage.md +113 -0
  18. package/skills/references/string.md +126 -0
  19. package/skills/references/timer.md +78 -0
  20. /package/{skill → dist/skills}/references/clipboard.md +0 -0
  21. /package/{skill → dist/skills}/references/cloneDeep.md +0 -0
  22. /package/{skill → dist/skills}/references/cookie.md +0 -0
  23. /package/{skill → dist/skills}/references/date.md +0 -0
  24. /package/{skill → dist/skills}/references/device.md +0 -0
  25. /package/{skill → dist/skills}/references/element.md +0 -0
  26. /package/{skill → dist/skills}/references/is.md +0 -0
  27. /package/{skill → dist/skills}/references/lodash.md +0 -0
  28. /package/{skill → dist/skills}/references/number.md +0 -0
  29. /package/{skill → dist/skills}/references/storage.md +0 -0
  30. /package/{skill → dist/skills}/references/string.md +0 -0
  31. /package/{skill → dist/skills}/references/timer.md +0 -0
@@ -0,0 +1,466 @@
1
+ # date
2
+
3
+ 日期处理工具函数 / Date handling utilities
4
+
5
+ 基于 dayjs / Based on dayjs
6
+
7
+ ## Overview / 概述
8
+
9
+ 提供一系列日期处理函数,包括日期格式化、相对时间显示、时长格式化等。所有函数基于 [dayjs](https://dayjs.gitee.io/) 库。
10
+
11
+ Provide a series of date handling functions including date formatting, relative time display, duration formatting, etc. All functions are based on [dayjs](https://dayjs.gitee.io/).
12
+
13
+ ## Functions
14
+
15
+ ### useDate
16
+
17
+ 使用 dayjs 实例 / Get dayjs instance
18
+
19
+ ```ts
20
+ function useDate(date?: ConfigType, format?: OptionType, strict?: boolean): Dayjs
21
+ ```
22
+
23
+ **Parameters / 参数**
24
+
25
+ | Name | Type | Default | Description |
26
+ |------|------|---------|-------------|
27
+ | `date` | `ConfigType` | `undefined` | 日期值,支持时间戳、Date 对象、字符串 |
28
+ | `format` | `OptionType` | `undefined` | 日期格式 |
29
+ | `strict` | `boolean` | `false` | 是否严格模式 |
30
+
31
+ **Returns / 返回值**
32
+
33
+ - `Dayjs`: dayjs 实例
34
+
35
+ **Example / 示例**
36
+
37
+ ```ts
38
+ // 默认当前时间
39
+ useDate().format('YYYY-MM-DD HH:mm:ss')
40
+ // '2024-03-02 15:30:00'
41
+
42
+ // 时间戳
43
+ useDate(1709286300000).format('YYYY-MM-DD')
44
+ // '2024-03-02'
45
+
46
+ // 字符串日期
47
+ useDate('2024-03-02').format('YYYY-MM-DD')
48
+ // '2024-03-02'
49
+
50
+ // Date 对象
51
+ useDate(new Date()).format('YYYY-MM-DD')
52
+ ```
53
+
54
+ ---
55
+
56
+ ### dateFormat
57
+
58
+ 日期转格式字符串 / Format date to string
59
+
60
+ ```ts
61
+ function dateFormat(date: ConfigType, fmt?: string): string
62
+ ```
63
+
64
+ **Parameters / 参数**
65
+
66
+ | Name | Type | Default | Description |
67
+ |------|------|---------|-------------|
68
+ | `date` | `ConfigType` | - | 日期值 |
69
+ | `fmt` | `string` | `'YYYY-MM-DD HH:mm:ss'` | 格式化字符串 |
70
+
71
+ **Format Tokens / 格式化占位符**
72
+
73
+ | Token | Description | Example |
74
+ |-------|-------------|---------|
75
+ | `YYYY` | 4位年份 | 2024 |
76
+ | `YY` | 2位年份 | 24 |
77
+ | `MM` | 2位月份 | 03 |
78
+ | `M` | 1-2位月份 | 3 |
79
+ | `DD` | 2位日期 | 02 |
80
+ | `D` | 1-2位日期 | 2 |
81
+ | `HH` | 24小时制 | 15 |
82
+ | `hh` | 12小时制 | 03 |
83
+ | `mm` | 分钟 | 30 |
84
+ | `ss` | 秒数 | 45 |
85
+
86
+ **Example / 示例**
87
+
88
+ ```ts
89
+ // 完整日期时间
90
+ dateFormat(1709286300000, 'YYYY-MM-DD HH:mm:ss')
91
+ // '2024-03-02 15:30:00'
92
+
93
+ // 仅日期
94
+ dateFormat(new Date(), 'YYYY-MM-DD')
95
+ // '2024-03-02'
96
+
97
+ // 中文格式
98
+ dateFormat(new Date(), 'YYYY年MM月DD日')
99
+ // '2024年03月02日'
100
+
101
+ // 时间
102
+ dateFormat(new Date(), 'HH:mm:ss')
103
+ // '15:30:45'
104
+ ```
105
+
106
+ ---
107
+
108
+ ### minute
109
+
110
+ 返回当前时间(默认到分钟)/ Get current time (default to minute)
111
+
112
+ ```ts
113
+ function minute(date?: ConfigType, fmt?: string): string
114
+ ```
115
+
116
+ **Parameters / 参数**
117
+
118
+ | Name | Type | Default | Description |
119
+ |------|------|---------|-------------|
120
+ | `date` | `ConfigType` | `undefined` | 日期值 |
121
+ | `fmt` | `string` | `'YYYY-MM-DD HH:mm'` | 格式化字符串 |
122
+
123
+ **Example / 示例**
124
+
125
+ ```ts
126
+ minute()
127
+ // '2024-03-02 15:30'
128
+
129
+ minute(new Date(), 'YYYY-MM-DD HH:mm:ss')
130
+ // '2024-03-02 15:30:45'
131
+ ```
132
+
133
+ ---
134
+
135
+ ### dateMonthDays
136
+
137
+ 返回日期对应月份天数 / Get days in month
138
+
139
+ ```ts
140
+ function dateMonthDays(date?: ConfigType): number
141
+ ```
142
+
143
+ **Parameters / 参数**
144
+
145
+ | Name | Type | Default | Description |
146
+ |------|------|---------|-------------|
147
+ | `date` | `ConfigType` | `undefined` | 日期值,默认当前月份 |
148
+
149
+ **Example / 示例**
150
+
151
+ ```ts
152
+ dateMonthDays('2024-02-01') // 29 (2024年是闰年)
153
+ dateMonthDays('2023-02-01') // 28
154
+ dateMonthDays('2024-04-01') // 30
155
+ ```
156
+
157
+ ---
158
+
159
+ ### getCurrDate
160
+
161
+ 返回前后 i 天的日期字符串 / Get date before/after i days
162
+
163
+ ```ts
164
+ function getCurrDate(i?: number, format?: string): string
165
+ ```
166
+
167
+ **Parameters / 参数**
168
+
169
+ | Name | Type | Default | Description |
170
+ |------|------|---------|-------------|
171
+ | `i` | `number` | `0` | 天数偏移量,正数为未来,负数为过去 |
172
+ | `format` | `string` | `'YYYY-MM-DD'` | 格式化字符串 |
173
+
174
+ **Example / 示例**
175
+
176
+ ```ts
177
+ // 今天
178
+ getCurrDate()
179
+ // '2024-03-02'
180
+
181
+ // 明天
182
+ getCurrDate(1)
183
+ // '2024-03-03'
184
+
185
+ // 前天
186
+ getCurrDate(-1)
187
+ // '2024-03-01'
188
+
189
+ // 一周后
190
+ getCurrDate(7)
191
+ // '2024-03-09'
192
+
193
+ // 自定义格式
194
+ getCurrDate(1, 'YYYY/MM/DD')
195
+ // '2024/03/03'
196
+ ```
197
+
198
+ ---
199
+
200
+ ### dateDiff
201
+
202
+ 返回两个日期时间差 / Get date difference
203
+
204
+ ```ts
205
+ function dateDiff(date1: ConfigType, date2?: ConfigType, unit?: QUnitType | OpUnitType): number
206
+ ```
207
+
208
+ **Parameters / 参数**
209
+
210
+ | Name | Type | Default | Description |
211
+ |------|------|---------|-------------|
212
+ | `date1` | `ConfigType` | - | 第一个日期 |
213
+ | `date2` | `ConfigType` | `undefined` | 第二个日期,默认当前时间 |
214
+ | `unit` | `QUnitType \| OpUnitType` | `undefined` | 返回单位 |
215
+
216
+ **Unit Options / 单位选项**
217
+
218
+ | Unit | Description |
219
+ |------|-------------|
220
+ | `ms` / `millisecond` | 毫秒 |
221
+ | `s` / `second` | 秒 |
222
+ | `m` / `minute` | 分钟 |
223
+ | `h` / `hour` | 小时 |
224
+ | `d` / `day` | 天 |
225
+ | `M` / `month` | 月 |
226
+ | `y` / `year` | 年 |
227
+
228
+ **Example / 示例**
229
+
230
+ ```ts
231
+ // 相差天数
232
+ dateDiff('2024-03-02', '2024-03-01')
233
+ // 1
234
+
235
+ // 相差小时
236
+ dateDiff('2024-03-02 15:00', '2024-03-02 10:00', 'hour')
237
+ // 5
238
+
239
+ // 相差分钟
240
+ dateDiff('2024-03-02 15:30:00', '2024-03-02 15:00:00', 'minute')
241
+ // 30
242
+
243
+ // 相对于当前时间
244
+ dateDiff('2024-03-01')
245
+ // -1 (昨天)
246
+ // 或正数如果是未来
247
+ ```
248
+
249
+ ---
250
+
251
+ ### dateFromNow
252
+
253
+ 相对时间显示 / Relative time display
254
+
255
+ ```ts
256
+ function dateFromNow(date: ConfigType, format?: Format): string
257
+ ```
258
+
259
+ **Parameters / 参数**
260
+
261
+ | Name | Type | Default | Description |
262
+ |------|------|---------|-------------|
263
+ | `date` | `ConfigType` | - | 目标日期 |
264
+ | `format` | `Format` | `undefined` | 自定义格式 |
265
+
266
+ **Default Format / 默认格式**
267
+
268
+ ```ts
269
+ {
270
+ zero: '刚刚',
271
+ minuteAgo: '${m}分钟前',
272
+ today: '今天 ${HH:mm}',
273
+ yesterday: '昨天 ${HH:mm}',
274
+ other: '${YYYY/MM/DD}',
275
+ future: '${YYYY/MM/DD}'
276
+ }
277
+ ```
278
+
279
+ **Example / 示例**
280
+
281
+ ```ts
282
+ // 几分钟前
283
+ dateFromNow('2024-03-02 15:25:00')
284
+ // '5分钟前' (假设当前15:30)
285
+
286
+ // 今天内
287
+ dateFromNow('2024-03-02 10:00:00')
288
+ // '今天 10:00'
289
+
290
+ // 昨天
291
+ dateFromNow('2024-03-01 10:00:00')
292
+ // '昨天 10:00'
293
+
294
+ // 更早
295
+ dateFromNow('2024-02-15 10:00:00')
296
+ // '2024/02/15'
297
+
298
+ // 自定义格式
299
+ dateFromNow('2024-03-02 10:00:00', {
300
+ today: '今日 ${HH:mm}',
301
+ yesterday: '昨日 ${HH:mm}'
302
+ })
303
+ // '今日 10:00'
304
+ ```
305
+
306
+ ---
307
+
308
+ ### dateDiffFormat
309
+
310
+ 计算两个日期之间的差值(自然年月日)/ Date difference in natural units
311
+
312
+ ```ts
313
+ function dateDiffFormat(startDate: string | number | Date, endDate?: string | number | Date, format?: string): string
314
+ ```
315
+
316
+ **Parameters / 参数**
317
+
318
+ | Name | Type | Default | Description |
319
+ |------|------|---------|-------------|
320
+ | `startDate` | `string \| number \| Date` | - | 起始日期 |
321
+ | `endDate` | `string \| number \| Date` | `undefined` | 结束日期,默认当前时间 |
322
+ | `format` | `string` | `'YY年MM个月DD天'` | 输出格式 |
323
+
324
+ **Format Tokens / 格式化占位符**
325
+
326
+ | Token | Description |
327
+ |-------|-------------|
328
+ | `YY` | 年 |
329
+ | `MM` | 月 |
330
+ | `DD` | 天 |
331
+ | `HH` | 小时 |
332
+ | `mm` | 分钟 |
333
+ | `ss` | 秒 |
334
+
335
+ **Example / 示例**
336
+
337
+ ```ts
338
+ dateDiffFormat('2024-01-01', '2024-03-15')
339
+ // '2月14天'
340
+
341
+ dateDiffFormat('2024-01-01', '2024-03-15', 'YY年MM个月DD天')
342
+ // '0年2个月14天'
343
+
344
+ dateDiffFormat('2024-01-01 10:00', '2024-01-01 10:30', 'HH:mm')
345
+ // '00:30'
346
+
347
+ dateDiffFormat('2024-01-01', '2025-06-15', 'YY年MM个月DD天')
348
+ // '1年5个月14天'
349
+ ```
350
+
351
+ ---
352
+
353
+ ### durationFormat
354
+
355
+ 格式化时长 / Format duration
356
+
357
+ ```ts
358
+ function durationFormat(duration: string | number, config?: { unit?: DurationUnitType; format?: string | string[] }): string | string[]
359
+ ```
360
+
361
+ **Parameters / 参数**
362
+
363
+ | Name | Type | Default | Description |
364
+ |------|------|---------|-------------|
365
+ | `duration` | `string \| number` | - | 时长数值 |
366
+ | `config.unit` | `DurationUnitType` | `'ms'` | 输入数值的单位 |
367
+ | `config.format` | `string \| string[]` | `'HH小时mm分钟'` | 输出格式 |
368
+
369
+ **Unit Options / 单位选项**
370
+
371
+ | Unit | Description |
372
+ |------|-------------|
373
+ | `d` / `D` | 天 |
374
+ | `h` / `H` | 小时 |
375
+ | `m` | 分钟 |
376
+ | `s` | 秒 |
377
+ | `ms` | 毫秒 |
378
+
379
+ **Example / 示例**
380
+
381
+ ```ts
382
+ // 毫秒转小时分钟
383
+ durationFormat(3661000, { unit: 'ms' })
384
+ // '1小时1分钟'
385
+
386
+ // 指定格式
387
+ durationFormat(61, { unit: 'm', format: 'HH:mm' })
388
+ // '01:01'
389
+
390
+ // 返回数组
391
+ durationFormat(61, { unit: 'm', format: ['H', 'm'] })
392
+ // ['1', '1']
393
+
394
+ // 秒转时分秒
395
+ durationFormat(3665, { unit: 's', format: 'HH:mm:ss' })
396
+ // '01:01:05'
397
+ ```
398
+
399
+ ---
400
+
401
+ ### durationFormatNoZero
402
+
403
+ 格式化时长(去掉 0)/ Format duration without zero
404
+
405
+ ```ts
406
+ function durationFormatNoZero(duration: string | number, config?: { unit?: DurationUnitType; format?: string }): string | string[]
407
+ ```
408
+
409
+ **Example / 示例**
410
+
411
+ ```ts
412
+ // 去掉为0的单位
413
+ durationFormatNoZero(61, { unit: 'm' })
414
+ // '1小时1分钟'
415
+
416
+ // 长时间只显示天数
417
+ durationFormatNoZero(3600000, { unit: 'ms', format: 'D天H小时m分钟' })
418
+ // '1天'
419
+
420
+ // 整天不带0
421
+ durationFormatNoZero(86400000, { unit: 'ms', format: 'D天H小时m分钟' })
422
+ // '1天'
423
+ ```
424
+
425
+ ---
426
+
427
+ ## Use Cases / 使用场景
428
+
429
+ ### 消息列表时间显示 / Message List Time Display
430
+
431
+ ```ts
432
+ function formatMessageTime(date: string | Date) {
433
+ return dateFromNow(date)
434
+ }
435
+ ```
436
+
437
+ ### 订单耗时显示 / Order Duration Display
438
+
439
+ ```ts
440
+ function formatOrderDuration(startTime: number) {
441
+ const duration = Date.now() - startTime
442
+ return durationFormat(duration, { unit: 'ms', format: 'DD天HH小时mm分钟' })
443
+ }
444
+ ```
445
+
446
+ ### 会员有效期 / Membership Validity
447
+
448
+ ```ts
449
+ function getMembershipExpiry(startDate: string, months: number) {
450
+ const expiryDate = useDate(startDate).add(months, 'month')
451
+ return dateFormat(expiryDate, 'YYYY-MM-DD')
452
+ }
453
+ ```
454
+
455
+ ### 聊天时间分割线 / Chat Time Divider
456
+
457
+ ```ts
458
+ function shouldShowDateDivider(messages: Message[]) {
459
+ return messages.map((msg, index) => {
460
+ if (index === 0) return true
461
+ const prevDate = useDate(messages[index - 1].createdAt)
462
+ const currDate = useDate(msg.createdAt)
463
+ return !currDate.isSame(prevDate, 'day')
464
+ })
465
+ }
466
+ ```
@@ -0,0 +1,138 @@
1
+ # device
2
+
3
+ 设备判断工具函数
4
+
5
+ ## Functions
6
+
7
+ ### isIos
8
+
9
+ 是否是 iOS 客户端
10
+
11
+ ```ts
12
+ function isIos(): boolean
13
+ ```
14
+
15
+ ### isAndroid
16
+
17
+ 是否是 Android 客户端
18
+
19
+ ```ts
20
+ function isAndroid(): boolean
21
+ ```
22
+
23
+ ### isWeChat
24
+
25
+ 是否是微信浏览器
26
+
27
+ ```ts
28
+ function isWeChat(): boolean
29
+ ```
30
+
31
+ ### isWxMiniProgram
32
+
33
+ 是否是微信小程序
34
+
35
+ ```ts
36
+ function isWxMiniProgram(): boolean
37
+ ```
38
+
39
+ ### isHarmony
40
+
41
+ 是否是鸿蒙系统
42
+
43
+ ```ts
44
+ function isHarmony(): boolean
45
+ ```
46
+
47
+ ### getDeviceBrand
48
+
49
+ 获取设备厂商
50
+
51
+ ```ts
52
+ function getDeviceBrand(): EnumDeviceBrand
53
+ ```
54
+
55
+ **Return**
56
+
57
+ ```ts
58
+ enum EnumDeviceBrand {
59
+ HUAWEI = 'HUAWEI',
60
+ XIAOMI = 'XIAOMI',
61
+ OPPO = 'OPPO',
62
+ VIVO = 'VIVO',
63
+ HONOR = 'HONOR',
64
+ SAMSUNG = 'SAMSUNG',
65
+ MEIZU = 'MEIZU',
66
+ ONEPLUS = 'ONEPLUS',
67
+ OTHER = 'OTHER'
68
+ }
69
+ ```
70
+
71
+ ### isMobile
72
+
73
+ 是否是移动设备
74
+
75
+ ```ts
76
+ function isMobile(): boolean
77
+ ```
78
+
79
+ ### isPC
80
+
81
+ 是否是 PC 设备
82
+
83
+ ```ts
84
+ function isPC(): boolean
85
+ ```
86
+
87
+ ### isSafari
88
+
89
+ 是否是 Safari 浏览器
90
+
91
+ ```ts
92
+ function isSafari(): boolean
93
+ ```
94
+
95
+ ### isChrome
96
+
97
+ 是否是 Chrome 浏览器
98
+
99
+ ```ts
100
+ function isChrome(): boolean
101
+ ```
102
+
103
+ ### isFirefox
104
+
105
+ 是否是 Firefox 浏览器
106
+
107
+ ```ts
108
+ function isFirefox(): boolean
109
+ ```
110
+
111
+ ### isEdge
112
+
113
+ 是否是 Edge 浏览器
114
+
115
+ ```ts
116
+ function isEdge(): boolean
117
+ ```
118
+
119
+ ## Example
120
+
121
+ ```ts
122
+ import { isIos, isAndroid, isWeChat, getDeviceBrand } from '@allkit/shared'
123
+
124
+ if (isIos()) {
125
+ // iOS 特殊处理
126
+ }
127
+
128
+ if (isAndroid()) {
129
+ // Android 特殊处理
130
+ }
131
+
132
+ if (isWeChat()) {
133
+ // 微信浏览器特殊处理
134
+ }
135
+
136
+ const brand = getDeviceBrand()
137
+ // 返回 EnumDeviceBrand 枚举值
138
+ ```
@@ -0,0 +1,99 @@
1
+ # element
2
+
3
+ DOM 元素操作工具函数
4
+
5
+ ## Functions
6
+
7
+ ### getBoundingClientRect
8
+
9
+ 获取元素的位置和尺寸
10
+
11
+ ```ts
12
+ function getBoundingClientRect(el: Element): DOMRect
13
+ ```
14
+
15
+ ### scrollTo
16
+
17
+ 平滑滚动到指定位置
18
+
19
+ ```ts
20
+ function scrollTo(options?: ScrollToOptions): void
21
+ ```
22
+
23
+ ### addClass
24
+
25
+ 添加 CSS 类名
26
+
27
+ ```ts
28
+ function addClass(el: HTMLElement, className: string | string[]): void
29
+ ```
30
+
31
+ ### removeClass
32
+
33
+ 移除 CSS 类名
34
+
35
+ ```ts
36
+ function removeClass(el: HTMLElement, className: string | string[]): void
37
+ ```
38
+
39
+ ### toggleClass
40
+
41
+ 切换 CSS 类名
42
+
43
+ ```ts
44
+ function toggleClass(el: HTMLElement, className: string): void
45
+ ```
46
+
47
+ ### hasClass
48
+
49
+ 检查是否包含 CSS 类名
50
+
51
+ ```ts
52
+ function hasClass(el: HTMLElement, className: string): boolean
53
+ ```
54
+
55
+ ### style
56
+
57
+ 设置或获取元素样式
58
+
59
+ ```ts
60
+ function style(el: HTMLElement, prop?: string, value?: string): string | void
61
+ ```
62
+
63
+ ### attr
64
+
65
+ 设置或获取元素属性
66
+
67
+ ```ts
68
+ function attr(el: HTMLElement, name: string, value?: string): string | void
69
+ ```
70
+
71
+ ### remove
72
+
73
+ 移除元素
74
+
75
+ ```ts
76
+ function remove(el: HTMLElement): void
77
+ ```
78
+
79
+ ### createEl
80
+
81
+ 创建 DOM 元素
82
+
83
+ ```ts
84
+ function createEl(tag: string, className?: string, html?: string): HTMLElement
85
+ ```
86
+
87
+ ## Example
88
+
89
+ ```ts
90
+ import { addClass, removeClass, hasClass, scrollTo } from '@allkit/shared'
91
+
92
+ const el = document.querySelector('.box')
93
+
94
+ addClass(el, 'active')
95
+ removeClass(el, 'disabled')
96
+ hasClass(el, 'active')
97
+
98
+ scrollTo({ top: 0, behavior: 'smooth' })
99
+ ```