@allkit/shared 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 +1 -0
- package/dist/README.md +1 -0
- package/dist/clipboard/index.d.ts +9 -0
- package/dist/cloneDeep/index.d.ts +47 -0
- package/dist/constants.d.ts +26 -0
- package/dist/cookie/index.d.ts +31 -0
- package/dist/date/index.d.ts +133 -0
- package/dist/device/index.d.ts +70 -0
- package/dist/element/index.d.ts +12 -0
- package/dist/index.d.ts +13 -0
- package/dist/is/index.d.ts +181 -0
- package/dist/lodash/index.d.ts +184 -0
- package/dist/number/index.d.ts +84 -0
- package/dist/package.json +21 -0
- package/dist/shared.es.d.ts +2 -0
- package/dist/shared.es.js +1367 -0
- package/dist/shared.umd.js +1 -0
- package/dist/storage/index.d.ts +144 -0
- package/dist/string/index.d.ts +40 -0
- package/dist/timer/index.d.ts +9 -0
- package/eslint.config.js +10 -0
- package/package.json +26 -0
- package/scripts/build.mjs +92 -0
- package/skill/SKILL.md +240 -0
- package/skill/examples/usage.ts +67 -0
- package/skill/references/clipboard.md +39 -0
- package/skill/references/cloneDeep.md +60 -0
- package/skill/references/cookie.md +56 -0
- package/skill/references/date.md +466 -0
- package/skill/references/device.md +138 -0
- package/skill/references/element.md +99 -0
- package/skill/references/is.md +415 -0
- package/skill/references/lodash.md +472 -0
- package/skill/references/number.md +248 -0
- package/skill/references/storage.md +113 -0
- package/skill/references/string.md +126 -0
- package/skill/references/timer.md +78 -0
- package/src/clipboard/index.ts +26 -0
- package/src/cloneDeep/__test__/cloneDeep.test.ts +92 -0
- package/src/cloneDeep/index.ts +168 -0
- package/src/constants.ts +27 -0
- package/src/cookie/index.ts +44 -0
- package/src/date/__test__/date-diff.test.ts +23 -0
- package/src/date/__test__/date-duration.test.ts +140 -0
- package/src/date/__test__/date-from.test.ts +64 -0
- package/src/date/__test__/date.test.ts +67 -0
- package/src/date/index.ts +331 -0
- package/src/device/__test__/device.test.ts +138 -0
- package/src/device/index.ts +125 -0
- package/src/element/index.ts +100 -0
- package/src/index.ts +14 -0
- package/src/is/__test__/is.test.ts +320 -0
- package/src/is/index.ts +293 -0
- package/src/lodash/__test__/lodash.test.ts +111 -0
- package/src/lodash/__test__/obj-string.test.ts +107 -0
- package/src/lodash/__test__/uniqueId.test.ts +40 -0
- package/src/lodash/index.ts +396 -0
- package/src/number/__test__/number.test.ts +137 -0
- package/src/number/index.ts +161 -0
- package/src/storage/__test__/storage.test.ts +44 -0
- package/src/storage/index.ts +185 -0
- package/src/string/__test__/string.test.ts +24 -0
- package/src/string/index.ts +49 -0
- package/src/timer/index.ts +15 -0
- package/tsconfig.json +25 -0
- package/types/global.d.ts +13 -0
- package/vite.config.ts +16 -0
- package/vitest.config.ts +28 -0
|
@@ -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
|
+
```
|
|
@@ -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
|
+
```
|