@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
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
# is
|
|
2
|
+
|
|
3
|
+
类型判断工具函数 / Type checking utility functions
|
|
4
|
+
|
|
5
|
+
## Overview / 概述
|
|
6
|
+
|
|
7
|
+
提供一系列类型判断函数,用于在运行时检测变量的类型。Provide a series of type checking functions for runtime type detection.
|
|
8
|
+
|
|
9
|
+
## Functions
|
|
10
|
+
|
|
11
|
+
### isDef
|
|
12
|
+
|
|
13
|
+
判断不是 undefined / Check if not undefined
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
function isDef<T>(val?: T): val is T
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Parameters / 参数**
|
|
20
|
+
|
|
21
|
+
| Name | Type | Description |
|
|
22
|
+
|------|------|-------------|
|
|
23
|
+
| `val` | `T \| undefined` | 要检测的值 |
|
|
24
|
+
|
|
25
|
+
**Returns / 返回值**
|
|
26
|
+
|
|
27
|
+
- `val is T`: 如果值不是 undefined,返回 true
|
|
28
|
+
|
|
29
|
+
**Example / 示例**
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
isDef('test') // true
|
|
33
|
+
isDef(undefined) // false
|
|
34
|
+
isDef(null) // true (null is not undefined)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
### isUnDef
|
|
40
|
+
|
|
41
|
+
判断是 undefined / Check if undefined
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
function isUnDef<T>(val?: T): val is T
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**Example / 示例**
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
isUnDef(undefined) // true
|
|
51
|
+
isUnDef('test') // false
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
### isObject
|
|
57
|
+
|
|
58
|
+
判断是 Object(含数组、Map)/ Check if is Object (includes Array, Map)
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
function isObject(val: any): val is Record<any, any>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Example / 示例**
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
isObject({}) // true
|
|
68
|
+
isObject([]) // true
|
|
69
|
+
isObject(new Map()) // true
|
|
70
|
+
isObject(null) // false
|
|
71
|
+
isObject('string') // false
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
### isPlainObject
|
|
77
|
+
|
|
78
|
+
判断是原始 Object(不含数组)/ Check if is plain Object
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
function isPlainObject(val: unknown): val is Record<string, unknown>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Example / 示例**
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
isPlainObject({}) // true
|
|
88
|
+
isPlainObject({ a: 1 }) // true
|
|
89
|
+
isPlainObject([]) // false
|
|
90
|
+
isPlainObject(new Map()) // false
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
### isEmpty
|
|
96
|
+
|
|
97
|
+
判断是空(含空数组、空对象、空 Map)/ Check if is empty
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
function isEmpty<T>(val: T): val is T
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**Example / 示例**
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
isEmpty([]) // true
|
|
107
|
+
isEmpty({}) // true
|
|
108
|
+
isEmpty(new Map()) // true
|
|
109
|
+
isEmpty(new Set()) // true
|
|
110
|
+
isEmpty('') // true
|
|
111
|
+
isEmpty([1, 2]) // false
|
|
112
|
+
isEmpty({ a: 1 }) // false
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
### isEmptyValue
|
|
118
|
+
|
|
119
|
+
检测是否空值(空 string/undefined/null)/ Check if is empty value
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
function isEmptyValue(val: string | number | null | undefined): boolean
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**Example / 示例**
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
isEmptyValue('') // true
|
|
129
|
+
isEmptyValue(null) // true
|
|
130
|
+
isEmptyValue(undefined) // true
|
|
131
|
+
isEmptyValue('test') // false
|
|
132
|
+
isEmptyValue(0) // false
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
### isDate
|
|
138
|
+
|
|
139
|
+
判断是日期对象 / Check if is Date object
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
function isDate(val: unknown): val is Date
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
**Example / 示例**
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
isDate(new Date()) // true
|
|
149
|
+
isDate('2024-01-01') // false
|
|
150
|
+
isDate(Date.now()) // false
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
### isNullOrUnDef
|
|
156
|
+
|
|
157
|
+
判断是 null 或 undefined / Check if is null or undefined
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
function isNullOrUnDef(val: unknown): val is null | undefined
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**Example / 示例**
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
isNullOrUnDef(null) // true
|
|
167
|
+
isNullOrUnDef(undefined) // true
|
|
168
|
+
isNullOrUnDef('') // false
|
|
169
|
+
isNullOrUnDef(0) // false
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
### isNumber
|
|
175
|
+
|
|
176
|
+
是否是 number / Check if is number
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
function isNumber(val: unknown): val is number
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**Example / 示例**
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
isNumber(123) // true
|
|
186
|
+
isNumber('123') // false
|
|
187
|
+
isNumber(NaN) // true
|
|
188
|
+
isNumber(Infinity) // true
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
### isInteger
|
|
194
|
+
|
|
195
|
+
判断是否为整数(支持负数)/ Check if is integer
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
function isInteger(val: unknown): boolean
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**Example / 示例**
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
isInteger(123) // true
|
|
205
|
+
isInteger('123') // true
|
|
206
|
+
isInteger(-456) // true
|
|
207
|
+
isInteger('3.14') // false
|
|
208
|
+
isInteger(3.14) // false
|
|
209
|
+
isInteger('') // false
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
### isNumeric
|
|
215
|
+
|
|
216
|
+
判断是否为数值(整数或小数)/ Check if is numeric
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
function isNumeric(val: unknown): boolean
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Example / 示例**
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
isNumeric(123) // true
|
|
226
|
+
isNumeric('123') // true
|
|
227
|
+
isNumeric('3.14') // true
|
|
228
|
+
isNumeric(-456.78) // true
|
|
229
|
+
isNumeric('abc') // false
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
### isPromise
|
|
235
|
+
|
|
236
|
+
是否是 Promise / Check if is Promise
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
function isPromise<T>(val: any): val is Promise<T>
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
**Example / 示例**
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
isPromise(Promise.resolve()) // true
|
|
246
|
+
isPromise(async () => {}) // true
|
|
247
|
+
isPromise({ then: () => {} }) // true (duck typing)
|
|
248
|
+
isPromise('test') // false
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
### isFunction
|
|
254
|
+
|
|
255
|
+
是否是函数 / Check if is function
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
function isFunction(val: unknown): val is Function
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
**Example /示例**
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
isFunction(() => {}) // true
|
|
265
|
+
isFunction(class {}) // true
|
|
266
|
+
isFunction(function(){})// true
|
|
267
|
+
isFunction('test') // false
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
### isArray
|
|
273
|
+
|
|
274
|
+
是否是数组 / Check if is array
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
function isArray(val: any): val is Array<any>
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
**Example / 示例**
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
isArray([]) // true
|
|
284
|
+
isArray(new Array()) // true
|
|
285
|
+
isArray({ length: 0 })// false
|
|
286
|
+
isArray('test') // false
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
### isUrl
|
|
292
|
+
|
|
293
|
+
是否是 URL / Check if is URL
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
function isUrl(path: string): boolean
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
**Parameters / 参数**
|
|
300
|
+
|
|
301
|
+
| Name | Type | Description |
|
|
302
|
+
|------|------|-------------|
|
|
303
|
+
| `path` | `string` | 要检测的字符串 |
|
|
304
|
+
|
|
305
|
+
**Returns / 返回值**
|
|
306
|
+
|
|
307
|
+
- `boolean`: 如果是有效的 URL 返回 true
|
|
308
|
+
|
|
309
|
+
**Example / 示例**
|
|
310
|
+
|
|
311
|
+
```ts
|
|
312
|
+
isUrl('http://www.baidu.com') // true
|
|
313
|
+
isUrl('https://example.com') // true
|
|
314
|
+
isUrl('/api/user') // true
|
|
315
|
+
isUrl('not-a-url') // false
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
---
|
|
319
|
+
|
|
320
|
+
### isWindow
|
|
321
|
+
|
|
322
|
+
是否是 Window / Check if is Window
|
|
323
|
+
|
|
324
|
+
```ts
|
|
325
|
+
function isWindow(val: any): val is Window
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
**Example / 示例**
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
isWindow(window) // true (in browser)
|
|
332
|
+
isWindow(document) // false
|
|
333
|
+
isWindow({}) // false
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
338
|
+
### isMap
|
|
339
|
+
|
|
340
|
+
是否是 Map / Check if is Map
|
|
341
|
+
|
|
342
|
+
```ts
|
|
343
|
+
function isMap(val: unknown): val is Map<any, any>
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
**Example / 示例**
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
isMap(new Map()) // true
|
|
350
|
+
isMap({}) // false
|
|
351
|
+
isMap([]) // false
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
---
|
|
355
|
+
|
|
356
|
+
### isRegExp
|
|
357
|
+
|
|
358
|
+
是否是正则 / Check if is RegExp
|
|
359
|
+
|
|
360
|
+
```ts
|
|
361
|
+
function isRegExp(val: unknown): val is RegExp
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
**Example / 示例**
|
|
365
|
+
|
|
366
|
+
```ts
|
|
367
|
+
isRegExp(/test/) // true
|
|
368
|
+
isRegExp(new RegExp('test')) // true
|
|
369
|
+
isRegExp('test') // false
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
## Use Cases / 使用场景
|
|
375
|
+
|
|
376
|
+
### 表单验证 / Form Validation
|
|
377
|
+
|
|
378
|
+
```ts
|
|
379
|
+
function validateValue(value: unknown) {
|
|
380
|
+
if (isEmptyValue(value)) {
|
|
381
|
+
return 'Value is required'
|
|
382
|
+
}
|
|
383
|
+
if (!isString(value) && !isNumber(value)) {
|
|
384
|
+
return 'Invalid value type'
|
|
385
|
+
}
|
|
386
|
+
return null
|
|
387
|
+
}
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### 数据处理 / Data Processing
|
|
391
|
+
|
|
392
|
+
```ts
|
|
393
|
+
function processData(data: unknown) {
|
|
394
|
+
if (isPlainObject(data)) {
|
|
395
|
+
// 处理普通对象
|
|
396
|
+
} else if (isArray(data)) {
|
|
397
|
+
// 处理数组
|
|
398
|
+
} else if (isDate(data)) {
|
|
399
|
+
// 处理日期
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### 类型守卫 / Type Guards
|
|
405
|
+
|
|
406
|
+
```ts
|
|
407
|
+
function handleValue(value: string | number | Date) {
|
|
408
|
+
if (isDate(value)) {
|
|
409
|
+
// TypeScript knows value is Date here
|
|
410
|
+
return value.getTime()
|
|
411
|
+
}
|
|
412
|
+
// TypeScript knows value is string | number here
|
|
413
|
+
return value
|
|
414
|
+
}
|
|
415
|
+
```
|