@oeos-components/utils 0.0.21 → 0.0.22
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/base.cjs +682 -0
- package/dist/base.d.cts +342 -0
- package/dist/base.d.mts +342 -0
- package/dist/base.d.ts +342 -0
- package/dist/base.mjs +655 -0
- package/dist/day.cjs +41 -0
- package/dist/day.d.cts +37 -0
- package/dist/day.d.mts +37 -0
- package/dist/day.d.ts +37 -0
- package/dist/day.mjs +31 -0
- package/dist/format.cjs +240 -0
- package/dist/format.d.cts +134 -0
- package/dist/format.d.mts +134 -0
- package/dist/format.d.ts +134 -0
- package/dist/format.mjs +230 -0
- package/dist/index.cjs +76 -866
- package/dist/index.d.cts +10 -396
- package/dist/index.d.mts +10 -396
- package/dist/index.d.ts +10 -396
- package/dist/index.mjs +12 -831
- package/dist/is.cjs +66 -0
- package/dist/is.d.cts +124 -0
- package/dist/is.d.mts +124 -0
- package/dist/is.d.ts +124 -0
- package/dist/is.mjs +43 -0
- package/dist/test.cjs +7 -0
- package/dist/test.d.cts +3 -0
- package/dist/test.d.mts +3 -0
- package/dist/test.d.ts +3 -0
- package/dist/test.mjs +5 -0
- package/dist/ws.cjs +120 -0
- package/dist/ws.d.cts +86 -0
- package/dist/ws.d.mts +86 -0
- package/dist/ws.d.ts +86 -0
- package/dist/ws.mjs +114 -0
- package/package.json +3 -1
package/dist/is.cjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const objectToString = Object.prototype.toString;
|
|
4
|
+
const toTypeString = (value) => objectToString.call(value);
|
|
5
|
+
const toRawType = (value) => {
|
|
6
|
+
return toTypeString(value).slice(8, -1);
|
|
7
|
+
};
|
|
8
|
+
const isArray = Array.isArray;
|
|
9
|
+
const isMap = (val) => toTypeString(val) === "[object Map]";
|
|
10
|
+
const isSet = (val) => toTypeString(val) === "[object Set]";
|
|
11
|
+
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
12
|
+
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
13
|
+
const isFunction = (val) => typeof val === "function";
|
|
14
|
+
const isString = (val) => typeof val === "string";
|
|
15
|
+
const isStringNumber = (val) => {
|
|
16
|
+
if (!isString(val)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return !Number.isNaN(Number(val));
|
|
20
|
+
};
|
|
21
|
+
const isNumber = (val) => typeof val === "number";
|
|
22
|
+
const isSymbol = (val) => typeof val === "symbol";
|
|
23
|
+
const isBoolean = (val) => typeof val === "boolean";
|
|
24
|
+
const isObject = (val) => val !== null && typeof val === "object";
|
|
25
|
+
const isPromise = (val) => {
|
|
26
|
+
return isObject(val) && isFunction(val.then) && isFunction(val.catch);
|
|
27
|
+
};
|
|
28
|
+
const isPlainObject = (val) => toTypeString(val) === "[object Object]";
|
|
29
|
+
const isEmptyObject = (val) => isPlainObject(val) && Object.keys(val).length === 0;
|
|
30
|
+
function isUrl(url) {
|
|
31
|
+
const regex = new RegExp(
|
|
32
|
+
"^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$",
|
|
33
|
+
"i"
|
|
34
|
+
);
|
|
35
|
+
return regex.test(url);
|
|
36
|
+
}
|
|
37
|
+
const isSVGElement = (tag) => typeof SVGElement !== "undefined" && tag instanceof SVGElement;
|
|
38
|
+
const isComponent = (val) => isPlainObject(val) && (isFunction(val.render) || isFunction(val.setup));
|
|
39
|
+
function isIOS() {
|
|
40
|
+
const isIphone = navigator.userAgent.includes("iPhone");
|
|
41
|
+
const isIpad = navigator.userAgent.includes("iPad");
|
|
42
|
+
return isIphone || isIpad;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
exports.isArray = isArray;
|
|
46
|
+
exports.isBoolean = isBoolean;
|
|
47
|
+
exports.isComponent = isComponent;
|
|
48
|
+
exports.isDate = isDate;
|
|
49
|
+
exports.isEmptyObject = isEmptyObject;
|
|
50
|
+
exports.isFunction = isFunction;
|
|
51
|
+
exports.isIOS = isIOS;
|
|
52
|
+
exports.isMap = isMap;
|
|
53
|
+
exports.isNumber = isNumber;
|
|
54
|
+
exports.isObject = isObject;
|
|
55
|
+
exports.isPlainObject = isPlainObject;
|
|
56
|
+
exports.isPromise = isPromise;
|
|
57
|
+
exports.isRegExp = isRegExp;
|
|
58
|
+
exports.isSVGElement = isSVGElement;
|
|
59
|
+
exports.isSet = isSet;
|
|
60
|
+
exports.isString = isString;
|
|
61
|
+
exports.isStringNumber = isStringNumber;
|
|
62
|
+
exports.isSymbol = isSymbol;
|
|
63
|
+
exports.isUrl = isUrl;
|
|
64
|
+
exports.objectToString = objectToString;
|
|
65
|
+
exports.toRawType = toRawType;
|
|
66
|
+
exports.toTypeString = toTypeString;
|
package/dist/is.d.cts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object.prototype.toString 别名
|
|
3
|
+
*/
|
|
4
|
+
declare const objectToString: () => string;
|
|
5
|
+
/**
|
|
6
|
+
* 获取类型
|
|
7
|
+
* @param value
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
declare const toTypeString: (value: any) => string;
|
|
11
|
+
/**
|
|
12
|
+
* 拿到类型字符串
|
|
13
|
+
* @param value
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
declare const toRawType: (value: any) => string;
|
|
17
|
+
/**
|
|
18
|
+
* 判断是否是数组
|
|
19
|
+
*/
|
|
20
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
21
|
+
/**
|
|
22
|
+
* 判断是否是Map
|
|
23
|
+
* @param val
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
declare const isMap: (val: any) => val is Map<any, any>;
|
|
27
|
+
/**
|
|
28
|
+
* 判断是否是Set
|
|
29
|
+
* @param val
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
declare const isSet: (val: any) => val is Set<any>;
|
|
33
|
+
/**
|
|
34
|
+
* 判断是否是Date
|
|
35
|
+
* @param val
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
declare const isDate: (val: any) => val is Date;
|
|
39
|
+
/**
|
|
40
|
+
* 判断是否是Reg
|
|
41
|
+
* @param val
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
declare const isRegExp: (val: any) => val is RegExp;
|
|
45
|
+
/**
|
|
46
|
+
* 判断是否是函数
|
|
47
|
+
* @param val
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
declare const isFunction: (val: any) => val is Function;
|
|
51
|
+
/**
|
|
52
|
+
* 判断是否是字符串
|
|
53
|
+
* @param val
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
declare const isString: (val: any) => val is string;
|
|
57
|
+
/**
|
|
58
|
+
* 判断是否是字符串数字
|
|
59
|
+
* @param val
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
declare const isStringNumber: (val: string) => boolean;
|
|
63
|
+
declare const isNumber: (val: any) => val is number;
|
|
64
|
+
/**
|
|
65
|
+
* 判断是否是Symbol
|
|
66
|
+
* @param val
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
declare const isSymbol: (val: any) => val is symbol;
|
|
70
|
+
/**
|
|
71
|
+
* 判断是否是boolean
|
|
72
|
+
* @param val
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
declare const isBoolean: (val: any) => val is boolean;
|
|
76
|
+
/**
|
|
77
|
+
* 判断是否是object
|
|
78
|
+
* @param val
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
declare const isObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
82
|
+
/**
|
|
83
|
+
* 判断是否是Promise
|
|
84
|
+
* @param val
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
declare const isPromise: <T = any>(val: any) => val is Promise<T>;
|
|
88
|
+
/**
|
|
89
|
+
* 判断是否是 纯对象 object
|
|
90
|
+
* @param val
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
declare const isPlainObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
94
|
+
/**
|
|
95
|
+
* 是否是空对象
|
|
96
|
+
* @param val
|
|
97
|
+
* @returns
|
|
98
|
+
*/
|
|
99
|
+
declare const isEmptyObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
100
|
+
/**
|
|
101
|
+
* 是否是链接
|
|
102
|
+
* @param url
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
declare function isUrl(url: string): url is string;
|
|
106
|
+
/**
|
|
107
|
+
* 是否是SVGElement
|
|
108
|
+
* @param tag
|
|
109
|
+
* @returns
|
|
110
|
+
*/
|
|
111
|
+
declare const isSVGElement: (tag: any) => tag is SVGElement;
|
|
112
|
+
/**
|
|
113
|
+
* 是否是vue 组件
|
|
114
|
+
* @param val
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
declare const isComponent: (val: any) => boolean;
|
|
118
|
+
/**
|
|
119
|
+
* 是否是ios
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
declare function isIOS(): boolean;
|
|
123
|
+
|
|
124
|
+
export { isArray, isBoolean, isComponent, isDate, isEmptyObject, isFunction, isIOS, isMap, isNumber, isObject, isPlainObject, isPromise, isRegExp, isSVGElement, isSet, isString, isStringNumber, isSymbol, isUrl, objectToString, toRawType, toTypeString };
|
package/dist/is.d.mts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object.prototype.toString 别名
|
|
3
|
+
*/
|
|
4
|
+
declare const objectToString: () => string;
|
|
5
|
+
/**
|
|
6
|
+
* 获取类型
|
|
7
|
+
* @param value
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
declare const toTypeString: (value: any) => string;
|
|
11
|
+
/**
|
|
12
|
+
* 拿到类型字符串
|
|
13
|
+
* @param value
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
declare const toRawType: (value: any) => string;
|
|
17
|
+
/**
|
|
18
|
+
* 判断是否是数组
|
|
19
|
+
*/
|
|
20
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
21
|
+
/**
|
|
22
|
+
* 判断是否是Map
|
|
23
|
+
* @param val
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
declare const isMap: (val: any) => val is Map<any, any>;
|
|
27
|
+
/**
|
|
28
|
+
* 判断是否是Set
|
|
29
|
+
* @param val
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
declare const isSet: (val: any) => val is Set<any>;
|
|
33
|
+
/**
|
|
34
|
+
* 判断是否是Date
|
|
35
|
+
* @param val
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
declare const isDate: (val: any) => val is Date;
|
|
39
|
+
/**
|
|
40
|
+
* 判断是否是Reg
|
|
41
|
+
* @param val
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
declare const isRegExp: (val: any) => val is RegExp;
|
|
45
|
+
/**
|
|
46
|
+
* 判断是否是函数
|
|
47
|
+
* @param val
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
declare const isFunction: (val: any) => val is Function;
|
|
51
|
+
/**
|
|
52
|
+
* 判断是否是字符串
|
|
53
|
+
* @param val
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
declare const isString: (val: any) => val is string;
|
|
57
|
+
/**
|
|
58
|
+
* 判断是否是字符串数字
|
|
59
|
+
* @param val
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
declare const isStringNumber: (val: string) => boolean;
|
|
63
|
+
declare const isNumber: (val: any) => val is number;
|
|
64
|
+
/**
|
|
65
|
+
* 判断是否是Symbol
|
|
66
|
+
* @param val
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
declare const isSymbol: (val: any) => val is symbol;
|
|
70
|
+
/**
|
|
71
|
+
* 判断是否是boolean
|
|
72
|
+
* @param val
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
declare const isBoolean: (val: any) => val is boolean;
|
|
76
|
+
/**
|
|
77
|
+
* 判断是否是object
|
|
78
|
+
* @param val
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
declare const isObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
82
|
+
/**
|
|
83
|
+
* 判断是否是Promise
|
|
84
|
+
* @param val
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
declare const isPromise: <T = any>(val: any) => val is Promise<T>;
|
|
88
|
+
/**
|
|
89
|
+
* 判断是否是 纯对象 object
|
|
90
|
+
* @param val
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
declare const isPlainObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
94
|
+
/**
|
|
95
|
+
* 是否是空对象
|
|
96
|
+
* @param val
|
|
97
|
+
* @returns
|
|
98
|
+
*/
|
|
99
|
+
declare const isEmptyObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
100
|
+
/**
|
|
101
|
+
* 是否是链接
|
|
102
|
+
* @param url
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
declare function isUrl(url: string): url is string;
|
|
106
|
+
/**
|
|
107
|
+
* 是否是SVGElement
|
|
108
|
+
* @param tag
|
|
109
|
+
* @returns
|
|
110
|
+
*/
|
|
111
|
+
declare const isSVGElement: (tag: any) => tag is SVGElement;
|
|
112
|
+
/**
|
|
113
|
+
* 是否是vue 组件
|
|
114
|
+
* @param val
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
declare const isComponent: (val: any) => boolean;
|
|
118
|
+
/**
|
|
119
|
+
* 是否是ios
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
declare function isIOS(): boolean;
|
|
123
|
+
|
|
124
|
+
export { isArray, isBoolean, isComponent, isDate, isEmptyObject, isFunction, isIOS, isMap, isNumber, isObject, isPlainObject, isPromise, isRegExp, isSVGElement, isSet, isString, isStringNumber, isSymbol, isUrl, objectToString, toRawType, toTypeString };
|
package/dist/is.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object.prototype.toString 别名
|
|
3
|
+
*/
|
|
4
|
+
declare const objectToString: () => string;
|
|
5
|
+
/**
|
|
6
|
+
* 获取类型
|
|
7
|
+
* @param value
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
declare const toTypeString: (value: any) => string;
|
|
11
|
+
/**
|
|
12
|
+
* 拿到类型字符串
|
|
13
|
+
* @param value
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
declare const toRawType: (value: any) => string;
|
|
17
|
+
/**
|
|
18
|
+
* 判断是否是数组
|
|
19
|
+
*/
|
|
20
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
21
|
+
/**
|
|
22
|
+
* 判断是否是Map
|
|
23
|
+
* @param val
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
declare const isMap: (val: any) => val is Map<any, any>;
|
|
27
|
+
/**
|
|
28
|
+
* 判断是否是Set
|
|
29
|
+
* @param val
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
declare const isSet: (val: any) => val is Set<any>;
|
|
33
|
+
/**
|
|
34
|
+
* 判断是否是Date
|
|
35
|
+
* @param val
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
declare const isDate: (val: any) => val is Date;
|
|
39
|
+
/**
|
|
40
|
+
* 判断是否是Reg
|
|
41
|
+
* @param val
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
declare const isRegExp: (val: any) => val is RegExp;
|
|
45
|
+
/**
|
|
46
|
+
* 判断是否是函数
|
|
47
|
+
* @param val
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
declare const isFunction: (val: any) => val is Function;
|
|
51
|
+
/**
|
|
52
|
+
* 判断是否是字符串
|
|
53
|
+
* @param val
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
declare const isString: (val: any) => val is string;
|
|
57
|
+
/**
|
|
58
|
+
* 判断是否是字符串数字
|
|
59
|
+
* @param val
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
declare const isStringNumber: (val: string) => boolean;
|
|
63
|
+
declare const isNumber: (val: any) => val is number;
|
|
64
|
+
/**
|
|
65
|
+
* 判断是否是Symbol
|
|
66
|
+
* @param val
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
declare const isSymbol: (val: any) => val is symbol;
|
|
70
|
+
/**
|
|
71
|
+
* 判断是否是boolean
|
|
72
|
+
* @param val
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
declare const isBoolean: (val: any) => val is boolean;
|
|
76
|
+
/**
|
|
77
|
+
* 判断是否是object
|
|
78
|
+
* @param val
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
declare const isObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
82
|
+
/**
|
|
83
|
+
* 判断是否是Promise
|
|
84
|
+
* @param val
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
declare const isPromise: <T = any>(val: any) => val is Promise<T>;
|
|
88
|
+
/**
|
|
89
|
+
* 判断是否是 纯对象 object
|
|
90
|
+
* @param val
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
declare const isPlainObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
94
|
+
/**
|
|
95
|
+
* 是否是空对象
|
|
96
|
+
* @param val
|
|
97
|
+
* @returns
|
|
98
|
+
*/
|
|
99
|
+
declare const isEmptyObject: (val: any) => val is Record<string | number | symbol, any>;
|
|
100
|
+
/**
|
|
101
|
+
* 是否是链接
|
|
102
|
+
* @param url
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
declare function isUrl(url: string): url is string;
|
|
106
|
+
/**
|
|
107
|
+
* 是否是SVGElement
|
|
108
|
+
* @param tag
|
|
109
|
+
* @returns
|
|
110
|
+
*/
|
|
111
|
+
declare const isSVGElement: (tag: any) => tag is SVGElement;
|
|
112
|
+
/**
|
|
113
|
+
* 是否是vue 组件
|
|
114
|
+
* @param val
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
declare const isComponent: (val: any) => boolean;
|
|
118
|
+
/**
|
|
119
|
+
* 是否是ios
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
declare function isIOS(): boolean;
|
|
123
|
+
|
|
124
|
+
export { isArray, isBoolean, isComponent, isDate, isEmptyObject, isFunction, isIOS, isMap, isNumber, isObject, isPlainObject, isPromise, isRegExp, isSVGElement, isSet, isString, isStringNumber, isSymbol, isUrl, objectToString, toRawType, toTypeString };
|
package/dist/is.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const objectToString = Object.prototype.toString;
|
|
2
|
+
const toTypeString = (value) => objectToString.call(value);
|
|
3
|
+
const toRawType = (value) => {
|
|
4
|
+
return toTypeString(value).slice(8, -1);
|
|
5
|
+
};
|
|
6
|
+
const isArray = Array.isArray;
|
|
7
|
+
const isMap = (val) => toTypeString(val) === "[object Map]";
|
|
8
|
+
const isSet = (val) => toTypeString(val) === "[object Set]";
|
|
9
|
+
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
10
|
+
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
11
|
+
const isFunction = (val) => typeof val === "function";
|
|
12
|
+
const isString = (val) => typeof val === "string";
|
|
13
|
+
const isStringNumber = (val) => {
|
|
14
|
+
if (!isString(val)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
return !Number.isNaN(Number(val));
|
|
18
|
+
};
|
|
19
|
+
const isNumber = (val) => typeof val === "number";
|
|
20
|
+
const isSymbol = (val) => typeof val === "symbol";
|
|
21
|
+
const isBoolean = (val) => typeof val === "boolean";
|
|
22
|
+
const isObject = (val) => val !== null && typeof val === "object";
|
|
23
|
+
const isPromise = (val) => {
|
|
24
|
+
return isObject(val) && isFunction(val.then) && isFunction(val.catch);
|
|
25
|
+
};
|
|
26
|
+
const isPlainObject = (val) => toTypeString(val) === "[object Object]";
|
|
27
|
+
const isEmptyObject = (val) => isPlainObject(val) && Object.keys(val).length === 0;
|
|
28
|
+
function isUrl(url) {
|
|
29
|
+
const regex = new RegExp(
|
|
30
|
+
"^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$",
|
|
31
|
+
"i"
|
|
32
|
+
);
|
|
33
|
+
return regex.test(url);
|
|
34
|
+
}
|
|
35
|
+
const isSVGElement = (tag) => typeof SVGElement !== "undefined" && tag instanceof SVGElement;
|
|
36
|
+
const isComponent = (val) => isPlainObject(val) && (isFunction(val.render) || isFunction(val.setup));
|
|
37
|
+
function isIOS() {
|
|
38
|
+
const isIphone = navigator.userAgent.includes("iPhone");
|
|
39
|
+
const isIpad = navigator.userAgent.includes("iPad");
|
|
40
|
+
return isIphone || isIpad;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { isArray, isBoolean, isComponent, isDate, isEmptyObject, isFunction, isIOS, isMap, isNumber, isObject, isPlainObject, isPromise, isRegExp, isSVGElement, isSet, isString, isStringNumber, isSymbol, isUrl, objectToString, toRawType, toTypeString };
|
package/dist/test.cjs
ADDED
package/dist/test.d.cts
ADDED
package/dist/test.d.mts
ADDED
package/dist/test.d.ts
ADDED
package/dist/test.mjs
ADDED
package/dist/ws.cjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const qs = require('qs');
|
|
4
|
+
|
|
5
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
6
|
+
|
|
7
|
+
const qs__default = /*#__PURE__*/_interopDefaultCompat(qs);
|
|
8
|
+
|
|
9
|
+
const reconnectMaxCount = 3;
|
|
10
|
+
const message = "ping";
|
|
11
|
+
const interval = 3e3;
|
|
12
|
+
const timeout = 1e3;
|
|
13
|
+
class WS {
|
|
14
|
+
url;
|
|
15
|
+
socket = null;
|
|
16
|
+
reconnectCount = 0;
|
|
17
|
+
delay = null;
|
|
18
|
+
timer = null;
|
|
19
|
+
autoReconnect;
|
|
20
|
+
heartbeat;
|
|
21
|
+
query;
|
|
22
|
+
constructor(url, options) {
|
|
23
|
+
const { autoReconnect = true, query = {}, heartbeat = false } = options || {};
|
|
24
|
+
this.autoReconnect = autoReconnect;
|
|
25
|
+
this.heartbeat = heartbeat;
|
|
26
|
+
this.query = query;
|
|
27
|
+
this.url = `${url}` + qs__default.stringify({ ...this.query }, { addQueryPrefix: true });
|
|
28
|
+
this.connect();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 连接
|
|
32
|
+
*/
|
|
33
|
+
connect() {
|
|
34
|
+
this.close();
|
|
35
|
+
this.socket = new WebSocket(this.url);
|
|
36
|
+
this.onError();
|
|
37
|
+
this.onOpen();
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 监听连接
|
|
41
|
+
*/
|
|
42
|
+
onOpen() {
|
|
43
|
+
if (this.socket) {
|
|
44
|
+
this.socket.onopen = () => {
|
|
45
|
+
this.send("ping");
|
|
46
|
+
this.heartbeat && this.startHeartbeat();
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 开启心跳
|
|
52
|
+
*/
|
|
53
|
+
startHeartbeat() {
|
|
54
|
+
const msg = this.heartbeat?.message || message;
|
|
55
|
+
const int = this.heartbeat?.interval || interval;
|
|
56
|
+
this.timer = setInterval(() => {
|
|
57
|
+
this.send(msg);
|
|
58
|
+
}, int);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 监听错误
|
|
62
|
+
*/
|
|
63
|
+
onError() {
|
|
64
|
+
if (this.socket) {
|
|
65
|
+
this.socket.onerror = () => {
|
|
66
|
+
const count = this.autoReconnect?.reconnectMaxCount || reconnectMaxCount;
|
|
67
|
+
if (this.autoReconnect && this.reconnectCount < count) {
|
|
68
|
+
this.reconnectCount++;
|
|
69
|
+
this.connect();
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 关闭连接
|
|
76
|
+
*/
|
|
77
|
+
close() {
|
|
78
|
+
this.socket && this.socket.close();
|
|
79
|
+
this.delay && clearTimeout(this.delay);
|
|
80
|
+
this.timer && clearInterval(this.timer);
|
|
81
|
+
this.socket = null;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 监听消息
|
|
85
|
+
* @param callback
|
|
86
|
+
*/
|
|
87
|
+
onMessage(callback) {
|
|
88
|
+
if (this.socket) {
|
|
89
|
+
this.socket.onmessage = (data) => {
|
|
90
|
+
try {
|
|
91
|
+
const res = JSON.parse(data.data);
|
|
92
|
+
callback(res);
|
|
93
|
+
} catch (err) {
|
|
94
|
+
callback(data);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 发送消息
|
|
101
|
+
* @param data
|
|
102
|
+
*/
|
|
103
|
+
send(data) {
|
|
104
|
+
if (!this.socket) return;
|
|
105
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
106
|
+
this.socket.send(JSON.stringify(data));
|
|
107
|
+
} else if (this.socket.readyState === this.socket.CONNECTING) {
|
|
108
|
+
this.delay = setTimeout(() => {
|
|
109
|
+
this.socket?.send(data);
|
|
110
|
+
}, timeout);
|
|
111
|
+
} else {
|
|
112
|
+
this.connect();
|
|
113
|
+
this.delay = setTimeout(() => {
|
|
114
|
+
this.socket?.send(data);
|
|
115
|
+
}, timeout);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
exports.WS = WS;
|