sybz-components 0.0.6 → 0.0.7

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.
@@ -1,299 +0,0 @@
1
- const n = `/**
2
- * \`Object.prototype.toString\` 的别名,适合在需要手动调用 \`call\` 时直接复用。
3
- *
4
- * @example
5
- * objectToString.call([])
6
- * // => '[object Array]'
7
- */
8
- export const objectToString = Object.prototype.toString
9
-
10
- /**
11
- * 获取对象的完整类型字符串。
12
- *
13
- * @param value 需要判断的值。
14
- * @returns 形如 \`[object Array]\` 的类型结果。
15
- *
16
- * @example
17
- * toTypeString([])
18
- * // => '[object Array]'
19
- */
20
- export const toTypeString = (value: unknown): string => objectToString.call(value)
21
-
22
- /**
23
- * 获取对象的原始类型名称。
24
- *
25
- * @param value 需要判断的值。
26
- * @returns 形如 \`Array\`、\`Date\`、\`Map\` 的类型名。
27
- *
28
- * @example
29
- * toRawType(new Map())
30
- * // => 'Map'
31
- */
32
- export const toRawType = (value: unknown): string => {
33
- return toTypeString(value).slice(8, -1)
34
- }
35
-
36
- /**
37
- * 判断值是否为数组。
38
- *
39
- * @param value 需要判断的值。
40
- * @returns 是否为数组。
41
- *
42
- * @example
43
- * isArray([1, 2, 3])
44
- * // => true
45
- */
46
- export const isArray = Array.isArray
47
-
48
- /**
49
- * 判断值是否为 \`Map\`。
50
- *
51
- * @param val 需要判断的值。
52
- * @returns 是否为 \`Map\`。
53
- *
54
- * @example
55
- * isMap(new Map())
56
- * // => true
57
- */
58
- export const isMap = (val: unknown): val is Map<any, any> => toTypeString(val) === '[object Map]'
59
-
60
- /**
61
- * 判断值是否为 \`Set\`。
62
- *
63
- * @param val 需要判断的值。
64
- * @returns 是否为 \`Set\`。
65
- *
66
- * @example
67
- * isSet(new Set([1, 2, 3]))
68
- * // => true
69
- */
70
- export const isSet = (val: unknown): val is Set<any> => toTypeString(val) === '[object Set]'
71
-
72
- /**
73
- * 判断值是否为 \`Date\` 对象。
74
- *
75
- * @param val 需要判断的值。
76
- * @returns 是否为 \`Date\`。
77
- *
78
- * @example
79
- * isDate(new Date())
80
- * // => true
81
- */
82
- export const isDate = (val: unknown): val is Date => toTypeString(val) === '[object Date]'
83
-
84
- /**
85
- * 判断值是否为正则对象。
86
- *
87
- * @param val 需要判断的值。
88
- * @returns 是否为 \`RegExp\`。
89
- *
90
- * @example
91
- * isRegExp(/sybz/i)
92
- * // => true
93
- */
94
- export const isRegExp = (val: unknown): val is RegExp => toTypeString(val) === '[object RegExp]'
95
-
96
- /**
97
- * 判断值是否为函数。
98
- *
99
- * @param val 需要判断的值。
100
- * @returns 是否为函数。
101
- *
102
- * @example
103
- * isFunction(() => {})
104
- * // => true
105
- */
106
- // eslint-disable-next-line @typescript-eslint/ban-types
107
- export const isFunction = (val: unknown): val is Function => typeof val === 'function'
108
-
109
- /**
110
- * 判断值是否为字符串。
111
- *
112
- * @param val 需要判断的值。
113
- * @returns 是否为字符串。
114
- *
115
- * @example
116
- * isString('sybz')
117
- * // => true
118
- */
119
- export const isString = (val: unknown): val is string => typeof val === 'string'
120
-
121
- /**
122
- * 判断字符串是否可以被转换成有效数字。
123
- *
124
- * @param val 需要判断的字符串。
125
- * @returns 是否可以安全转换成数字。
126
- *
127
- * @example
128
- * isStringNumber('12.5')
129
- * // => true
130
- *
131
- * @example
132
- * isStringNumber('12px')
133
- * // => false
134
- */
135
- export const isStringNumber = (val: string): boolean => {
136
- if (!isString(val)) {
137
- return false
138
- }
139
- return !Number.isNaN(Number(val))
140
- }
141
-
142
- /**
143
- * 判断值是否为数字类型。
144
- *
145
- * @param val 需要判断的值。
146
- * @returns 是否为 \`number\`。
147
- *
148
- * @example
149
- * isNumber(12)
150
- * // => true
151
- */
152
- export const isNumber = (val: unknown): val is number => typeof val === 'number'
153
-
154
- /**
155
- * 判断值是否为 \`Symbol\`。
156
- *
157
- * @param val 需要判断的值。
158
- * @returns 是否为 \`Symbol\`。
159
- *
160
- * @example
161
- * isSymbol(Symbol('id'))
162
- * // => true
163
- */
164
- export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
165
-
166
- /**
167
- * 判断值是否为布尔值。
168
- *
169
- * @param val 需要判断的值。
170
- * @returns 是否为 \`boolean\`。
171
- *
172
- * @example
173
- * isBoolean(false)
174
- * // => true
175
- */
176
- export const isBoolean = (val: unknown): val is boolean => typeof val === 'boolean'
177
-
178
- /**
179
- * 判断值是否为对象且不为 \`null\`。
180
- *
181
- * @param val 需要判断的值。
182
- * @returns 是否为对象。
183
- *
184
- * @example
185
- * isObject({ id: 1 })
186
- * // => true
187
- */
188
- export const isObject = (val: unknown): val is Record<keyof any, any> => val !== null && typeof val === 'object'
189
-
190
- /**
191
- * 判断值是否为 Promise 风格对象。
192
- *
193
- * @param val 需要判断的值。
194
- * @returns 是否为 Promise。
195
- *
196
- * @example
197
- * isPromise(Promise.resolve(1))
198
- * // => true
199
- */
200
- export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
201
- return isObject(val) && isFunction(val.then) && isFunction(val.catch)
202
- }
203
-
204
- /**
205
- * 判断值是否为普通对象。
206
- *
207
- * @param val 需要判断的值。
208
- * @returns 是否为普通对象。
209
- *
210
- * @example
211
- * isPlainObject({ id: 1 })
212
- * // => true
213
- */
214
- export const isPlainObject = (val: unknown): val is Record<keyof any, any> => toTypeString(val) === '[object Object]'
215
-
216
- /**
217
- * 判断值是否为空普通对象。
218
- *
219
- * @param val 需要判断的值。
220
- * @returns 是否为空对象。
221
- *
222
- * @example
223
- * isEmptyObject({})
224
- * // => true
225
- */
226
- export const isEmptyObject = (val: unknown): val is Record<keyof any, any> =>
227
- isPlainObject(val) && Object.keys(val).length === 0
228
-
229
- /**
230
- * 判断字符串是否为合法链接。
231
- *
232
- * @param url 需要判断的链接。
233
- * @returns 是否为合法 URL。
234
- *
235
- * @example
236
- * isUrl('https://sybz-components.com')
237
- * // => true
238
- */
239
- export function isUrl(url: string): url is string {
240
- const regex = new RegExp(
241
- '^(https?:\\\\/\\\\/)?' +
242
- '((([a-z\\\\d]([a-z\\\\d-]*[a-z\\\\d])*)\\\\.)+[a-z]{2,}|' +
243
- '((\\\\d{1,3}\\\\.){3}\\\\d{1,3}))' +
244
- '(\\\\:\\\\d+)?(\\\\/[-a-z\\\\d%_.~+]*)*' +
245
- '(\\\\?[;&a-z\\\\d%_.~+=-]*)?' +
246
- '(\\\\#[-a-z\\\\d_]*)?$',
247
- 'i',
248
- )
249
- return regex.test(url)
250
- }
251
-
252
- /**
253
- * 判断节点是否为 \`SVGElement\`。
254
- *
255
- * @param tag 需要判断的节点。
256
- * @returns 是否为 \`SVGElement\`。
257
- *
258
- * @example
259
- * const svg = document.querySelector('svg')
260
- * isSVGElement(svg)
261
- */
262
- export const isSVGElement = (tag: unknown): tag is SVGElement =>
263
- typeof SVGElement !== 'undefined' && tag instanceof SVGElement
264
-
265
- /**
266
- * 判断对象是否为 Vue 组件配置。
267
- *
268
- * @param val 需要判断的值。
269
- * @returns 是否像一个 Vue 组件。
270
- *
271
- * @example
272
- * isComponent({
273
- * render() {
274
- * return null
275
- * },
276
- * })
277
- */
278
- export const isComponent = (val: unknown): boolean =>
279
- isPlainObject(val) && (isFunction(val.render) || isFunction(val.setup))
280
-
281
- /**
282
- * 判断当前运行环境是否为 iOS。
283
- *
284
- * @returns 是否为 iOS 设备。
285
- *
286
- * @example
287
- * if (isIOS()) {
288
- * console.log('当前设备是 iOS')
289
- * }
290
- */
291
- export function isIOS(): boolean {
292
- const isIphone = navigator.userAgent.includes('iPhone')
293
- const isIpad = navigator.userAgent.includes('iPad')
294
- return isIphone || isIpad
295
- }
296
- `;
297
- export {
298
- n as default
299
- };
@@ -1,18 +0,0 @@
1
- const n = `/**
2
- * 返回当前测试占位字符串。
3
- *
4
- * 这个方法主要用于确认工具函数导出链路是否正常。
5
- *
6
- * @returns 固定字符串 \`test\`。
7
- *
8
- * @example
9
- * onlyTest()
10
- * // => 'test'
11
- */
12
- export function onlyTest(): string {
13
- return 'test'
14
- }
15
- `;
16
- export {
17
- n as default
18
- };
@@ -1,252 +0,0 @@
1
- const n = `import qs from 'qs'
2
-
3
- type Timeout = ReturnType<typeof setTimeout>
4
- type Interval = ReturnType<typeof setInterval>
5
- type Nullable<T> = T | null
6
-
7
- const reconnectMaxCount = 3
8
- const message = 'ping'
9
- const interval = 3000
10
- const timeout = 1000
11
-
12
- /**
13
- * WebSocket 自动重连配置。
14
- */
15
- export interface WSAutoReconnectOptions {
16
- /**
17
- * 最大重连次数,默认 \`3\`。
18
- */
19
- reconnectMaxCount?: number
20
- }
21
-
22
- /**
23
- * WebSocket 心跳配置。
24
- */
25
- export interface WSHeartbeatOptions {
26
- /**
27
- * 心跳消息内容,默认 \`ping\`。
28
- */
29
- message: string
30
- /**
31
- * 心跳间隔,单位毫秒,默认 \`3000\`。
32
- */
33
- interval: number
34
- }
35
-
36
- /**
37
- * \`WS\` 构造函数可选配置。
38
- */
39
- export interface WSOptions {
40
- /**
41
- * 是否自动重连。
42
- *
43
- * - \`true\`: 使用默认重连策略
44
- * - \`false\`: 不自动重连
45
- * - 对象: 自定义重连次数
46
- */
47
- autoReconnect: boolean | WSAutoReconnectOptions
48
- /**
49
- * 是否开启心跳。
50
- *
51
- * - \`false\`: 不发送心跳
52
- * - \`true\`: 使用默认心跳策略
53
- * - 对象: 自定义心跳内容和间隔
54
- */
55
- heartbeat: boolean | WSHeartbeatOptions
56
- /**
57
- * 需要拼接到 url 上的查询参数。
58
- */
59
- query: Record<string, string>
60
- }
61
-
62
- /**
63
- * WebSocket 轻量封装,支持自动连接、自动重连、心跳和 query 参数。
64
- *
65
- * @example
66
- * const ws = new WS('wss://example.com/ws', {
67
- * autoReconnect: true,
68
- * heartbeat: { message: 'ping', interval: 3000 },
69
- * query: { token: 'demo-token' },
70
- * })
71
- *
72
- * ws.onMessage((message) => {
73
- * console.log(message)
74
- * })
75
- *
76
- * ws.send('hello')
77
- */
78
- export class WS {
79
- url: string
80
- socket: WebSocket | null = null
81
- reconnectCount = 0
82
- delay: Nullable<Timeout> = null
83
- timer: Nullable<Interval> = null
84
- autoReconnect: WSOptions['autoReconnect']
85
- heartbeat: WSOptions['heartbeat']
86
- query: WSOptions['query']
87
-
88
- /**
89
- * 创建并立即连接一个 WebSocket 实例。
90
- *
91
- * @param url WebSocket 地址,例如 \`wss://example.com/ws\`。
92
- * @param options 连接配置,支持重连、心跳和 query 参数。
93
- *
94
- * @example
95
- * const ws = new WS('wss://example.com/ws', {
96
- * autoReconnect: { reconnectMaxCount: 5 },
97
- * heartbeat: { message: 'ping', interval: 5000 },
98
- * query: { token: 'abc123' },
99
- * })
100
- */
101
- constructor(url?: string, options?: WSOptions) {
102
- const { autoReconnect = true, query = {}, heartbeat = false } = options || {}
103
- this.autoReconnect = autoReconnect
104
- this.heartbeat = heartbeat
105
- this.query = query
106
-
107
- this.url = \`\${url}\` + qs.stringify({ ...this.query }, { addQueryPrefix: true })
108
-
109
- this.connect()
110
- }
111
-
112
- /**
113
- * 主动发起连接。
114
- *
115
- * 调用时会先关闭旧连接,再创建新的 WebSocket 实例。
116
- *
117
- * @example
118
- * const ws = new WS('wss://example.com/ws')
119
- * ws.connect()
120
- */
121
- connect(): void {
122
- this.close()
123
- this.socket = new WebSocket(this.url)
124
- this.onError()
125
- this.onOpen()
126
- }
127
-
128
- /**
129
- * 注册连接成功后的处理逻辑,并在需要时开启心跳。
130
- *
131
- * @example
132
- * const ws = new WS('wss://example.com/ws')
133
- * ws.onOpen()
134
- */
135
- onOpen(): void {
136
- if (this.socket) {
137
- this.socket.onopen = () => {
138
- this.send('ping')
139
- this.heartbeat && this.startHeartbeat()
140
- }
141
- }
142
- }
143
-
144
- /**
145
- * 开启心跳发送。
146
- *
147
- * @example
148
- * const ws = new WS('wss://example.com/ws', {
149
- * heartbeat: { message: 'ping', interval: 3000 },
150
- * })
151
- * ws.startHeartbeat()
152
- */
153
- startHeartbeat(): void {
154
- const msg = (this.heartbeat as WSHeartbeatOptions)?.message || message
155
- const int = (this.heartbeat as WSHeartbeatOptions)?.interval || interval
156
- this.timer = setInterval(() => {
157
- this.send(msg)
158
- }, int)
159
- }
160
-
161
- /**
162
- * 注册错误处理和自动重连逻辑。
163
- *
164
- * @example
165
- * const ws = new WS('wss://example.com/ws', { autoReconnect: true })
166
- * ws.onError()
167
- */
168
- onError(): void {
169
- if (this.socket) {
170
- this.socket.onerror = () => {
171
- const count = (this.autoReconnect as WSAutoReconnectOptions)?.reconnectMaxCount || reconnectMaxCount
172
- if (this.autoReconnect && this.reconnectCount < count) {
173
- this.reconnectCount++
174
- this.connect()
175
- }
176
- }
177
- }
178
- }
179
-
180
- /**
181
- * 关闭连接并清理相关定时器。
182
- *
183
- * @example
184
- * ws.close()
185
- */
186
- close(): void {
187
- this.socket && this.socket.close()
188
- this.delay && clearTimeout(this.delay)
189
- this.timer && clearInterval(this.timer)
190
- this.socket = null
191
- }
192
-
193
- /**
194
- * 监听服务端消息。
195
- *
196
- * 内部会优先尝试将消息解析为 JSON;解析失败时会回传原始 \`MessageEvent\`。
197
- *
198
- * @param callback 接收到消息时触发的回调。
199
- *
200
- * @example
201
- * ws.onMessage((message) => {
202
- * console.log(message)
203
- * })
204
- */
205
- onMessage(callback: (data: unknown) => any): void {
206
- if (this.socket) {
207
- this.socket.onmessage = (data) => {
208
- try {
209
- const res = JSON.parse(data.data)
210
- callback(res)
211
- } catch (err) {
212
- callback(data)
213
- }
214
- }
215
- }
216
- }
217
-
218
- /**
219
- * 发送消息。
220
- *
221
- * - 如果连接已就绪,会立即发送
222
- * - 如果正在连接,会延迟发送
223
- * - 如果连接已关闭,会先重新连接再发送
224
- *
225
- * @param data 需要发送的消息内容。
226
- *
227
- * @example
228
- * ws.send('hello')
229
- *
230
- * @example
231
- * ws.send(JSON.stringify({ type: 'ping' }))
232
- */
233
- send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {
234
- if (!this.socket) return
235
- if (this.socket.readyState === this.socket.OPEN) {
236
- this.socket.send(JSON.stringify(data))
237
- } else if (this.socket.readyState === this.socket.CONNECTING) {
238
- this.delay = setTimeout(() => {
239
- this.socket?.send(data)
240
- }, timeout)
241
- } else {
242
- this.connect()
243
- this.delay = setTimeout(() => {
244
- this.socket?.send(data)
245
- }, timeout)
246
- }
247
- }
248
- }
249
- `;
250
- export {
251
- n as default
252
- };