sculp-js 1.19.2 → 1.19.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/cjs/array.cjs +1 -1
- package/dist/cjs/async.cjs +1 -1
- package/dist/cjs/base64.cjs +157 -71
- package/dist/cjs/clipboard.cjs +1 -1
- package/dist/cjs/cloneDeep.cjs +1 -1
- package/dist/cjs/cookie.cjs +1 -1
- package/dist/cjs/date.cjs +1 -1
- package/dist/cjs/dom.cjs +1 -1
- package/dist/cjs/download.cjs +1 -1
- package/dist/cjs/file.cjs +1 -1
- package/dist/cjs/func.cjs +1 -1
- package/dist/cjs/index.cjs +10 -1
- package/dist/cjs/isEqual.cjs +1 -1
- package/dist/cjs/math.cjs +1 -1
- package/dist/cjs/number.cjs +1 -1
- package/dist/cjs/object.cjs +1 -1
- package/dist/cjs/path.cjs +1 -1
- package/dist/cjs/qs.cjs +1 -1
- package/dist/cjs/random.cjs +1 -1
- package/dist/cjs/string.cjs +1 -1
- package/dist/cjs/tooltip.cjs +1 -1
- package/dist/cjs/tree.cjs +1 -1
- package/dist/cjs/type.cjs +71 -3
- package/dist/cjs/unicodeToolkit.cjs +1 -1
- package/dist/cjs/unique.cjs +1 -1
- package/dist/cjs/url.cjs +1 -1
- package/dist/cjs/validator.cjs +1 -1
- package/dist/cjs/variable.cjs +1 -1
- package/dist/cjs/watermark.cjs +1 -1
- package/dist/esm/array.mjs +1 -1
- package/dist/esm/async.mjs +1 -1
- package/dist/esm/base64.mjs +157 -71
- package/dist/esm/clipboard.mjs +1 -1
- package/dist/esm/cloneDeep.mjs +1 -1
- package/dist/esm/cookie.mjs +1 -1
- package/dist/esm/date.mjs +1 -1
- package/dist/esm/dom.mjs +1 -1
- package/dist/esm/download.mjs +1 -1
- package/dist/esm/file.mjs +1 -1
- package/dist/esm/func.mjs +1 -1
- package/dist/esm/index.mjs +10 -1
- package/dist/esm/isEqual.mjs +1 -1
- package/dist/esm/math.mjs +1 -1
- package/dist/esm/number.mjs +1 -1
- package/dist/esm/object.mjs +1 -1
- package/dist/esm/path.mjs +1 -1
- package/dist/esm/qs.mjs +1 -1
- package/dist/esm/random.mjs +1 -1
- package/dist/esm/string.mjs +1 -1
- package/dist/esm/tooltip.mjs +1 -1
- package/dist/esm/tree.mjs +1 -1
- package/dist/esm/type.mjs +71 -3
- package/dist/esm/unicodeToolkit.mjs +1 -1
- package/dist/esm/unique.mjs +1 -1
- package/dist/esm/url.mjs +1 -1
- package/dist/esm/validator.mjs +1 -1
- package/dist/esm/variable.mjs +1 -1
- package/dist/esm/watermark.mjs +1 -1
- package/dist/types/base64.d.ts +6 -6
- package/dist/types/type.d.ts +86 -3
- package/dist/umd/index.min.js +2 -2
- package/package.json +1 -1
package/dist/esm/async.mjs
CHANGED
package/dist/esm/base64.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.19.
|
|
2
|
+
* sculp-js v1.19.3
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -11,112 +11,198 @@ const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
|
11
11
|
// eslint-disable-next-line
|
|
12
12
|
const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* 高性能 UTF-8 字符串转 Uint8Array
|
|
15
|
+
*/
|
|
16
|
+
function stringToUint8Array(str) {
|
|
17
|
+
const len = str.length;
|
|
18
|
+
const bytes = new Uint8Array(len * 3); // 最多 3 倍长度
|
|
19
|
+
let j = 0;
|
|
20
|
+
for (let i = 0; i < len; i++) {
|
|
21
|
+
let code = str.charCodeAt(i);
|
|
22
|
+
if (code < 0x80) {
|
|
23
|
+
bytes[j++] = code;
|
|
24
|
+
} else if (code < 0x800) {
|
|
25
|
+
bytes[j++] = 0xc0 | (code >> 6);
|
|
26
|
+
bytes[j++] = 0x80 | (code & 0x3f);
|
|
27
|
+
} else if (code < 0xd800 || code >= 0xe000) {
|
|
28
|
+
bytes[j++] = 0xe0 | (code >> 12);
|
|
29
|
+
bytes[j++] = 0x80 | ((code >> 6) & 0x3f);
|
|
30
|
+
bytes[j++] = 0x80 | (code & 0x3f);
|
|
31
|
+
} else {
|
|
32
|
+
// 处理代理对(emoji 等)
|
|
33
|
+
i++;
|
|
34
|
+
code = 0x10000 + (((code & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
|
|
35
|
+
bytes[j++] = 0xf0 | (code >> 18);
|
|
36
|
+
bytes[j++] = 0x80 | ((code >> 12) & 0x3f);
|
|
37
|
+
bytes[j++] = 0x80 | ((code >> 6) & 0x3f);
|
|
38
|
+
bytes[j++] = 0x80 | (code & 0x3f);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return bytes.subarray(0, j);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 高性能 Uint8Array 转 UTF-8 字符串
|
|
45
|
+
*/
|
|
46
|
+
function uint8ArrayToString(bytes) {
|
|
47
|
+
const len = bytes.length;
|
|
48
|
+
let result = '';
|
|
49
|
+
let i = 0;
|
|
50
|
+
while (i < len) {
|
|
51
|
+
const byte1 = bytes[i++];
|
|
52
|
+
if (byte1 < 0x80) {
|
|
53
|
+
result += String.fromCharCode(byte1);
|
|
54
|
+
} else if (byte1 < 0xe0) {
|
|
55
|
+
const byte2 = bytes[i++] & 0x3f;
|
|
56
|
+
result += String.fromCharCode(((byte1 & 0x1f) << 6) | byte2);
|
|
57
|
+
} else if (byte1 < 0xf0) {
|
|
58
|
+
const byte2 = bytes[i++] & 0x3f;
|
|
59
|
+
const byte3 = bytes[i++] & 0x3f;
|
|
60
|
+
result += String.fromCharCode(((byte1 & 0x0f) << 12) | (byte2 << 6) | byte3);
|
|
61
|
+
} else {
|
|
62
|
+
const byte2 = bytes[i++] & 0x3f;
|
|
63
|
+
const byte3 = bytes[i++] & 0x3f;
|
|
64
|
+
const byte4 = bytes[i++] & 0x3f;
|
|
65
|
+
let code = ((byte1 & 0x07) << 18) | (byte2 << 12) | (byte3 << 6) | byte4;
|
|
66
|
+
code -= 0x10000;
|
|
67
|
+
result += String.fromCharCode(0xd800 + ((code >> 10) & 0x3ff), 0xdc00 + (code & 0x3ff));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 高性能 Base64 编码(二进制字符串转 Base64)
|
|
74
|
+
*/
|
|
75
|
+
function binaryToBase64(binary) {
|
|
76
|
+
const len = binary.length;
|
|
77
|
+
let result = '';
|
|
78
|
+
let i = 0;
|
|
79
|
+
for (; i < len - 2; i += 3) {
|
|
80
|
+
const n = (binary.charCodeAt(i) << 16) | (binary.charCodeAt(i + 1) << 8) | binary.charCodeAt(i + 2);
|
|
81
|
+
result += b64.charAt(n >> 18) + b64.charAt((n >> 12) & 0x3f) + b64.charAt((n >> 6) & 0x3f) + b64.charAt(n & 0x3f);
|
|
82
|
+
}
|
|
83
|
+
// 处理剩余字节
|
|
84
|
+
const remaining = len - i;
|
|
85
|
+
if (remaining === 1) {
|
|
86
|
+
const n = binary.charCodeAt(i);
|
|
87
|
+
result += b64.charAt(n >> 2) + b64.charAt((n << 4) & 0x3f) + '==';
|
|
88
|
+
} else if (remaining === 2) {
|
|
89
|
+
const n = (binary.charCodeAt(i) << 8) | binary.charCodeAt(i + 1);
|
|
90
|
+
result += b64.charAt(n >> 10) + b64.charAt((n >> 4) & 0x3f) + b64.charAt((n << 2) & 0x3f) + '=';
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 高性能 Base64 解码(Base64 转二进制字符串)
|
|
96
|
+
*/
|
|
97
|
+
function base64ToBinary(base64Str) {
|
|
98
|
+
base64Str = String(base64Str).replace(/[\t\n\f\r ]+/g, '');
|
|
99
|
+
if (!b64re.test(base64Str)) {
|
|
100
|
+
throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
|
|
101
|
+
}
|
|
102
|
+
const len = base64Str.length;
|
|
103
|
+
const padding = base64Str.endsWith('==') ? 2 : base64Str.endsWith('=') ? 1 : 0;
|
|
104
|
+
const result = new Array(len * 0.75 - padding);
|
|
105
|
+
let j = 0;
|
|
106
|
+
for (let i = 0; i < len; i += 4) {
|
|
107
|
+
const n =
|
|
108
|
+
(b64.indexOf(base64Str.charAt(i)) << 18) |
|
|
109
|
+
(b64.indexOf(base64Str.charAt(i + 1)) << 12) |
|
|
110
|
+
((b64.indexOf(base64Str.charAt(i + 2)) & 0x3f) << 6) |
|
|
111
|
+
(b64.indexOf(base64Str.charAt(i + 3)) & 0x3f);
|
|
112
|
+
result[j++] = String.fromCharCode((n >> 16) & 0xff);
|
|
113
|
+
if (i + 2 < len - padding || padding < 2) {
|
|
114
|
+
result[j++] = String.fromCharCode((n >> 8) & 0xff);
|
|
115
|
+
}
|
|
116
|
+
if (i + 3 < len - padding || padding < 1) {
|
|
117
|
+
result[j++] = String.fromCharCode(n & 0xff);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return result.join('');
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* 字符串编码成 Base64(适用于任何环境,包括小程序)
|
|
15
124
|
* @param {string} string
|
|
16
125
|
* @returns {string}
|
|
17
126
|
*/
|
|
18
127
|
function weBtoa(string) {
|
|
19
|
-
// 同window.btoa: 字符串编码成Base64
|
|
20
128
|
string = String(string);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
c,
|
|
25
|
-
result = '',
|
|
26
|
-
i = 0;
|
|
27
|
-
const strLen = string.length;
|
|
28
|
-
const rest = strLen % 3;
|
|
29
|
-
for (; i < strLen; ) {
|
|
30
|
-
if ((a = string.charCodeAt(i++)) > 255 || (b = string.charCodeAt(i++)) > 255 || (c = string.charCodeAt(i++)) > 255)
|
|
129
|
+
const len = string.length;
|
|
130
|
+
for (let i = 0; i < len; i++) {
|
|
131
|
+
if (string.charCodeAt(i) > 255) {
|
|
31
132
|
throw new TypeError(
|
|
32
133
|
"Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range."
|
|
33
134
|
);
|
|
34
|
-
|
|
35
|
-
result +=
|
|
36
|
-
b64.charAt((bitmap >> 18) & 63) +
|
|
37
|
-
b64.charAt((bitmap >> 12) & 63) +
|
|
38
|
-
b64.charAt((bitmap >> 6) & 63) +
|
|
39
|
-
b64.charAt(bitmap & 63);
|
|
135
|
+
}
|
|
40
136
|
}
|
|
41
|
-
return
|
|
137
|
+
return binaryToBase64(string);
|
|
42
138
|
}
|
|
43
139
|
/**
|
|
44
|
-
* Base64
|
|
140
|
+
* Base64 解码为原始字符串(适用于任何环境,包括小程序)
|
|
45
141
|
* @param {string} string
|
|
46
142
|
* @returns {string}
|
|
47
143
|
*/
|
|
48
144
|
function weAtob(string) {
|
|
49
|
-
|
|
50
|
-
string = String(string).replace(/[\t\n\f\r ]+/g, '');
|
|
51
|
-
if (!b64re.test(string))
|
|
52
|
-
throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
|
|
53
|
-
string += '=='.slice(2 - (string.length & 3));
|
|
54
|
-
let bitmap,
|
|
55
|
-
result = '',
|
|
56
|
-
r1,
|
|
57
|
-
r2,
|
|
58
|
-
i = 0;
|
|
59
|
-
for (const strLen = string.length; i < strLen; ) {
|
|
60
|
-
bitmap =
|
|
61
|
-
(b64.indexOf(string.charAt(i++)) << 18) |
|
|
62
|
-
(b64.indexOf(string.charAt(i++)) << 12) |
|
|
63
|
-
((r1 = b64.indexOf(string.charAt(i++))) << 6) |
|
|
64
|
-
(r2 = b64.indexOf(string.charAt(i++)));
|
|
65
|
-
result +=
|
|
66
|
-
r1 === 64
|
|
67
|
-
? String.fromCharCode((bitmap >> 16) & 255)
|
|
68
|
-
: r2 === 64
|
|
69
|
-
? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
|
|
70
|
-
: String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
|
|
71
|
-
}
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
function stringToUint8Array(str) {
|
|
75
|
-
const utf8 = encodeURIComponent(str); // 将字符串转换为 UTF-8 编码
|
|
76
|
-
const uint8Array = new Uint8Array(utf8.length); // 创建 Uint8Array
|
|
77
|
-
for (let i = 0; i < utf8.length; i++) {
|
|
78
|
-
uint8Array[i] = utf8.charCodeAt(i); // 填充 Uint8Array
|
|
79
|
-
}
|
|
80
|
-
return uint8Array;
|
|
81
|
-
}
|
|
82
|
-
function uint8ArrayToString(uint8Array) {
|
|
83
|
-
const utf8 = String.fromCharCode.apply(null, uint8Array); // 将 Uint8Array 转为字符串
|
|
84
|
-
return decodeURIComponent(utf8); // 将 UTF-8 字符串解码回正常字符串
|
|
145
|
+
return base64ToBinary(string);
|
|
85
146
|
}
|
|
86
147
|
/**
|
|
87
|
-
* 将base64编码的字符串转换为原始字符串,包括对中文内容的处理(高性能,且支持Web、Node、小程序等任意平台)
|
|
88
|
-
* @param base64 base64编码的字符串
|
|
148
|
+
* 将 base64 编码的字符串转换为原始字符串,包括对中文内容的处理 (高性能,且支持 Web、Node、小程序等任意平台)
|
|
149
|
+
* @param base64 base64 编码的字符串
|
|
89
150
|
* @returns 原始字符串,包括中文内容
|
|
90
151
|
*/
|
|
91
152
|
function b64decode(base64) {
|
|
92
|
-
|
|
153
|
+
// 优先使用原生方法(性能最优)
|
|
154
|
+
if (!isNullOrUnDef(getGlobal('atob')) && !isNullOrUnDef(getGlobal('TextDecoder'))) {
|
|
155
|
+
try {
|
|
156
|
+
const binaryString = getGlobal('atob')(base64);
|
|
157
|
+
const len = binaryString.length;
|
|
158
|
+
const bytes = new Uint8Array(len);
|
|
159
|
+
for (let i = 0; i < len; i++) {
|
|
160
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
161
|
+
}
|
|
162
|
+
return new (getGlobal('TextDecoder'))('utf-8').decode(bytes);
|
|
163
|
+
} catch (e) {
|
|
164
|
+
// 如果原生方法失败,使用降级方案
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// 降级方案:使用自定义实现
|
|
168
|
+
const binaryString = base64ToBinary(base64);
|
|
93
169
|
const len = binaryString.length;
|
|
94
170
|
const bytes = new Uint8Array(len);
|
|
95
171
|
for (let i = 0; i < len; i++) {
|
|
96
172
|
bytes[i] = binaryString.charCodeAt(i);
|
|
97
173
|
}
|
|
98
|
-
|
|
99
|
-
return !isNullOrUnDef(getGlobal('TextDecoder'))
|
|
100
|
-
? new (getGlobal('TextDecoder'))('utf-8').decode(bytes)
|
|
101
|
-
: uint8ArrayToString(bytes);
|
|
174
|
+
return uint8ArrayToString(bytes);
|
|
102
175
|
}
|
|
103
176
|
/**
|
|
104
|
-
* 将原始字符串,包括中文内容,转换为base64编码的字符串(高性能,且支持Web、Node、小程序等任意平台)
|
|
177
|
+
* 将原始字符串,包括中文内容,转换为 base64 编码的字符串 (高性能,且支持 Web、Node、小程序等任意平台)
|
|
105
178
|
* @param rawStr 原始字符串,包括中文内容
|
|
106
|
-
* @returns base64编码的字符串
|
|
179
|
+
* @returns base64 编码的字符串
|
|
107
180
|
*/
|
|
108
181
|
function b64encode(rawStr) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
182
|
+
// 优先使用原生方法(性能最优)
|
|
183
|
+
if (!isNullOrUnDef(getGlobal('btoa')) && !isNullOrUnDef(getGlobal('TextEncoder'))) {
|
|
184
|
+
try {
|
|
185
|
+
const utf8Array = new (getGlobal('TextEncoder'))().encode(rawStr);
|
|
186
|
+
let binaryString = '';
|
|
187
|
+
const len = utf8Array.length;
|
|
188
|
+
for (let i = 0; i < len; i++) {
|
|
189
|
+
binaryString += String.fromCharCode(utf8Array[i]);
|
|
190
|
+
}
|
|
191
|
+
return getGlobal('btoa')(binaryString);
|
|
192
|
+
} catch (e) {
|
|
193
|
+
// 如果原生方法失败,使用降级方案
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// 降级方案:使用自定义实现
|
|
197
|
+
const utf8Array = stringToUint8Array(rawStr);
|
|
112
198
|
// 将 Uint8Array 转换为二进制字符串
|
|
113
199
|
let binaryString = '';
|
|
114
200
|
const len = utf8Array.length;
|
|
115
201
|
for (let i = 0; i < len; i++) {
|
|
116
202
|
binaryString += String.fromCharCode(utf8Array[i]);
|
|
117
203
|
}
|
|
118
|
-
// 将二进制字符串转换为base64
|
|
119
|
-
return
|
|
204
|
+
// 将二进制字符串转换为 base64 编码
|
|
205
|
+
return binaryToBase64(binaryString);
|
|
120
206
|
}
|
|
121
207
|
var base64 = {
|
|
122
208
|
weBtoa,
|
package/dist/esm/clipboard.mjs
CHANGED
package/dist/esm/cloneDeep.mjs
CHANGED
package/dist/esm/cookie.mjs
CHANGED
package/dist/esm/date.mjs
CHANGED
package/dist/esm/dom.mjs
CHANGED
package/dist/esm/download.mjs
CHANGED
package/dist/esm/file.mjs
CHANGED
package/dist/esm/func.mjs
CHANGED
package/dist/esm/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.19.
|
|
2
|
+
* sculp-js v1.19.3
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -58,14 +58,20 @@ export {
|
|
|
58
58
|
import * as type from './type.mjs';
|
|
59
59
|
export {
|
|
60
60
|
arrayLike,
|
|
61
|
+
is,
|
|
62
|
+
isArguments,
|
|
61
63
|
isArray,
|
|
64
|
+
isArrayBuffer,
|
|
65
|
+
isAsyncFunction,
|
|
62
66
|
isBigInt,
|
|
63
67
|
isBoolean,
|
|
68
|
+
isDataView,
|
|
64
69
|
isDate,
|
|
65
70
|
isEmpty,
|
|
66
71
|
isError,
|
|
67
72
|
isFunction,
|
|
68
73
|
isJsonString,
|
|
74
|
+
isMap,
|
|
69
75
|
isNaN,
|
|
70
76
|
isNodeList,
|
|
71
77
|
isNull,
|
|
@@ -74,9 +80,12 @@ export {
|
|
|
74
80
|
isNumber,
|
|
75
81
|
isObject,
|
|
76
82
|
isPrimitive,
|
|
83
|
+
isPromise,
|
|
77
84
|
isRegExp,
|
|
85
|
+
isSet,
|
|
78
86
|
isString,
|
|
79
87
|
isSymbol,
|
|
88
|
+
isTypedArray,
|
|
80
89
|
isUndefined,
|
|
81
90
|
objectHas,
|
|
82
91
|
typeIs
|
package/dist/esm/isEqual.mjs
CHANGED
package/dist/esm/math.mjs
CHANGED
package/dist/esm/number.mjs
CHANGED
package/dist/esm/object.mjs
CHANGED
package/dist/esm/path.mjs
CHANGED
package/dist/esm/qs.mjs
CHANGED
package/dist/esm/random.mjs
CHANGED
package/dist/esm/string.mjs
CHANGED
package/dist/esm/tooltip.mjs
CHANGED
package/dist/esm/tree.mjs
CHANGED
package/dist/esm/type.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.19.
|
|
2
|
+
* sculp-js v1.19.3
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -28,17 +28,37 @@ function arrayLike(any) {
|
|
|
28
28
|
return objectHas(any, 'length');
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
|
-
* 判断任意值的数据类型,检查非对象时不如typeof、instanceof的性能高
|
|
31
|
+
* 判断任意值的数据类型,检查非对象时不如 typeof、instanceof 的性能高
|
|
32
32
|
*
|
|
33
33
|
* 当检查类对象时是不可靠的,对象可以通过定义 Symbol.toStringTag 属性来更改检查结果
|
|
34
34
|
*
|
|
35
35
|
* 详见:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
|
|
36
36
|
* @param {unknown} any
|
|
37
|
-
* @returns
|
|
37
|
+
* @returns {TypeTag} 类型标签字符串
|
|
38
38
|
*/
|
|
39
39
|
function typeIs(any) {
|
|
40
40
|
return toString.call(any).slice(8, -1);
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* 判断值是否为指定类型(带类型守卫)
|
|
44
|
+
* @param {unknown} value - 要检查的值
|
|
45
|
+
* @param {TypeTag} type - 期望的类型
|
|
46
|
+
* @returns {boolean} 如果值是指定类型则返回 true
|
|
47
|
+
* @example
|
|
48
|
+
* is(value, 'String')
|
|
49
|
+
* is(value, 'Array')
|
|
50
|
+
* is(value, 'Date')
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // 使用类型守卫
|
|
54
|
+
* if (is(value, 'String')) {
|
|
55
|
+
* // value 在这里被推断为 string 类型
|
|
56
|
+
* console.log(value.toUpperCase());
|
|
57
|
+
* }
|
|
58
|
+
*/
|
|
59
|
+
function is(value, type) {
|
|
60
|
+
return typeIs(value) === type;
|
|
61
|
+
}
|
|
42
62
|
// 基本数据类型判断
|
|
43
63
|
const isString = any => typeof any === 'string';
|
|
44
64
|
const isBoolean = any => typeof any === 'boolean';
|
|
@@ -65,6 +85,34 @@ const isNaN = any => Number.isNaN(any);
|
|
|
65
85
|
const isDate = any => typeIs(any) === 'Date';
|
|
66
86
|
const isError = any => typeIs(any) === 'Error';
|
|
67
87
|
const isRegExp = any => typeIs(any) === 'RegExp';
|
|
88
|
+
const isPromise = any => typeIs(any) === 'Promise';
|
|
89
|
+
const isMap = any => typeIs(any) === 'Map';
|
|
90
|
+
const isSet = any => typeIs(any) === 'Set';
|
|
91
|
+
/**
|
|
92
|
+
* 判断是否为 TypedArray
|
|
93
|
+
* @param {unknown} any
|
|
94
|
+
* @returns {boolean}
|
|
95
|
+
*/
|
|
96
|
+
const isTypedArray = any => {
|
|
97
|
+
const tag = typeIs(any);
|
|
98
|
+
return (
|
|
99
|
+
tag === 'Int8Array' ||
|
|
100
|
+
tag === 'Uint8Array' ||
|
|
101
|
+
tag === 'Uint8ClampedArray' ||
|
|
102
|
+
tag === 'Int16Array' ||
|
|
103
|
+
tag === 'Uint16Array' ||
|
|
104
|
+
tag === 'Int32Array' ||
|
|
105
|
+
tag === 'Uint32Array' ||
|
|
106
|
+
tag === 'Float32Array' ||
|
|
107
|
+
tag === 'Float64Array' ||
|
|
108
|
+
tag === 'BigInt64Array' ||
|
|
109
|
+
tag === 'BigUint64Array'
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
const isDataView = any => typeIs(any) === 'DataView';
|
|
113
|
+
const isArrayBuffer = any => typeIs(any) === 'ArrayBuffer';
|
|
114
|
+
const isArguments = any => typeIs(any) === 'Arguments';
|
|
115
|
+
const isAsyncFunction = any => typeIs(any) === 'AsyncFunction';
|
|
68
116
|
/**
|
|
69
117
|
* 判断一个字符串是否为有效的 JSON, 若有效则返回有效的JSON对象,否则false
|
|
70
118
|
* @param {string} str
|
|
@@ -128,6 +176,9 @@ function isNodeList(value) {
|
|
|
128
176
|
}
|
|
129
177
|
var type = {
|
|
130
178
|
typeIs,
|
|
179
|
+
is,
|
|
180
|
+
TypeMapping: undefined,
|
|
181
|
+
TypeTag: undefined,
|
|
131
182
|
objectHas,
|
|
132
183
|
arrayLike,
|
|
133
184
|
isString,
|
|
@@ -147,6 +198,14 @@ var type = {
|
|
|
147
198
|
isDate,
|
|
148
199
|
isError,
|
|
149
200
|
isRegExp,
|
|
201
|
+
isPromise,
|
|
202
|
+
isMap,
|
|
203
|
+
isSet,
|
|
204
|
+
isTypedArray,
|
|
205
|
+
isDataView,
|
|
206
|
+
isArrayBuffer,
|
|
207
|
+
isArguments,
|
|
208
|
+
isAsyncFunction,
|
|
150
209
|
isJsonString,
|
|
151
210
|
isEmpty,
|
|
152
211
|
isNodeList
|
|
@@ -155,14 +214,20 @@ var type = {
|
|
|
155
214
|
export {
|
|
156
215
|
arrayLike,
|
|
157
216
|
type as default,
|
|
217
|
+
is,
|
|
218
|
+
isArguments,
|
|
158
219
|
isArray,
|
|
220
|
+
isArrayBuffer,
|
|
221
|
+
isAsyncFunction,
|
|
159
222
|
isBigInt,
|
|
160
223
|
isBoolean,
|
|
224
|
+
isDataView,
|
|
161
225
|
isDate,
|
|
162
226
|
isEmpty,
|
|
163
227
|
isError,
|
|
164
228
|
isFunction,
|
|
165
229
|
isJsonString,
|
|
230
|
+
isMap,
|
|
166
231
|
isNaN,
|
|
167
232
|
isNodeList,
|
|
168
233
|
isNull,
|
|
@@ -171,9 +236,12 @@ export {
|
|
|
171
236
|
isNumber,
|
|
172
237
|
isObject,
|
|
173
238
|
isPrimitive,
|
|
239
|
+
isPromise,
|
|
174
240
|
isRegExp,
|
|
241
|
+
isSet,
|
|
175
242
|
isString,
|
|
176
243
|
isSymbol,
|
|
244
|
+
isTypedArray,
|
|
177
245
|
isUndefined,
|
|
178
246
|
objectHas,
|
|
179
247
|
typeIs
|
package/dist/esm/unique.mjs
CHANGED
package/dist/esm/url.mjs
CHANGED
package/dist/esm/validator.mjs
CHANGED
package/dist/esm/variable.mjs
CHANGED
package/dist/esm/watermark.mjs
CHANGED