sculp-js 1.19.1 → 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.
Files changed (63) hide show
  1. package/dist/cjs/array.cjs +1 -1
  2. package/dist/cjs/async.cjs +1 -1
  3. package/dist/cjs/base64.cjs +157 -71
  4. package/dist/cjs/clipboard.cjs +1 -1
  5. package/dist/cjs/cloneDeep.cjs +1 -1
  6. package/dist/cjs/cookie.cjs +1 -1
  7. package/dist/cjs/date.cjs +130 -70
  8. package/dist/cjs/dom.cjs +1 -1
  9. package/dist/cjs/download.cjs +1 -1
  10. package/dist/cjs/file.cjs +1 -1
  11. package/dist/cjs/func.cjs +1 -1
  12. package/dist/cjs/index.cjs +11 -3
  13. package/dist/cjs/isEqual.cjs +1 -1
  14. package/dist/cjs/math.cjs +1 -1
  15. package/dist/cjs/number.cjs +1 -1
  16. package/dist/cjs/object.cjs +1 -1
  17. package/dist/cjs/path.cjs +1 -1
  18. package/dist/cjs/qs.cjs +1 -1
  19. package/dist/cjs/random.cjs +1 -1
  20. package/dist/cjs/string.cjs +1 -1
  21. package/dist/cjs/tooltip.cjs +1 -1
  22. package/dist/cjs/tree.cjs +1 -1
  23. package/dist/cjs/type.cjs +71 -3
  24. package/dist/cjs/unicodeToolkit.cjs +19 -19
  25. package/dist/cjs/unique.cjs +1 -1
  26. package/dist/cjs/url.cjs +1 -1
  27. package/dist/cjs/validator.cjs +1 -1
  28. package/dist/cjs/variable.cjs +1 -1
  29. package/dist/cjs/watermark.cjs +1 -1
  30. package/dist/esm/array.mjs +1 -1
  31. package/dist/esm/async.mjs +1 -1
  32. package/dist/esm/base64.mjs +157 -71
  33. package/dist/esm/clipboard.mjs +1 -1
  34. package/dist/esm/cloneDeep.mjs +1 -1
  35. package/dist/esm/cookie.mjs +1 -1
  36. package/dist/esm/date.mjs +130 -78
  37. package/dist/esm/dom.mjs +1 -1
  38. package/dist/esm/download.mjs +1 -1
  39. package/dist/esm/file.mjs +1 -1
  40. package/dist/esm/func.mjs +1 -1
  41. package/dist/esm/index.mjs +11 -10
  42. package/dist/esm/isEqual.mjs +1 -1
  43. package/dist/esm/math.mjs +1 -1
  44. package/dist/esm/number.mjs +1 -1
  45. package/dist/esm/object.mjs +1 -1
  46. package/dist/esm/path.mjs +1 -1
  47. package/dist/esm/qs.mjs +1 -1
  48. package/dist/esm/random.mjs +1 -1
  49. package/dist/esm/string.mjs +1 -1
  50. package/dist/esm/tooltip.mjs +1 -1
  51. package/dist/esm/tree.mjs +1 -1
  52. package/dist/esm/type.mjs +71 -3
  53. package/dist/esm/unicodeToolkit.mjs +19 -19
  54. package/dist/esm/unique.mjs +1 -1
  55. package/dist/esm/url.mjs +1 -1
  56. package/dist/esm/validator.mjs +1 -1
  57. package/dist/esm/variable.mjs +1 -1
  58. package/dist/esm/watermark.mjs +1 -1
  59. package/dist/types/base64.d.ts +6 -6
  60. package/dist/types/date.d.ts +124 -28
  61. package/dist/types/type.d.ts +86 -3
  62. package/dist/umd/index.min.js +2 -2
  63. package/package.json +1 -1
package/dist/cjs/qs.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/dist/cjs/tree.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/dist/cjs/type.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -32,17 +32,37 @@ function arrayLike(any) {
32
32
  return objectHas(any, 'length');
33
33
  }
34
34
  /**
35
- * 判断任意值的数据类型,检查非对象时不如typeof、instanceof的性能高
35
+ * 判断任意值的数据类型,检查非对象时不如 typeof、instanceof 的性能高
36
36
  *
37
37
  * 当检查类对象时是不可靠的,对象可以通过定义 Symbol.toStringTag 属性来更改检查结果
38
38
  *
39
39
  * 详见:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
40
40
  * @param {unknown} any
41
- * @returns
41
+ * @returns {TypeTag} 类型标签字符串
42
42
  */
43
43
  function typeIs(any) {
44
44
  return toString.call(any).slice(8, -1);
45
45
  }
46
+ /**
47
+ * 判断值是否为指定类型(带类型守卫)
48
+ * @param {unknown} value - 要检查的值
49
+ * @param {TypeTag} type - 期望的类型
50
+ * @returns {boolean} 如果值是指定类型则返回 true
51
+ * @example
52
+ * is(value, 'String')
53
+ * is(value, 'Array')
54
+ * is(value, 'Date')
55
+ *
56
+ * @example
57
+ * // 使用类型守卫
58
+ * if (is(value, 'String')) {
59
+ * // value 在这里被推断为 string 类型
60
+ * console.log(value.toUpperCase());
61
+ * }
62
+ */
63
+ function is(value, type) {
64
+ return typeIs(value) === type;
65
+ }
46
66
  // 基本数据类型判断
47
67
  const isString = any => typeof any === 'string';
48
68
  const isBoolean = any => typeof any === 'boolean';
@@ -69,6 +89,34 @@ const isNaN = any => Number.isNaN(any);
69
89
  const isDate = any => typeIs(any) === 'Date';
70
90
  const isError = any => typeIs(any) === 'Error';
71
91
  const isRegExp = any => typeIs(any) === 'RegExp';
92
+ const isPromise = any => typeIs(any) === 'Promise';
93
+ const isMap = any => typeIs(any) === 'Map';
94
+ const isSet = any => typeIs(any) === 'Set';
95
+ /**
96
+ * 判断是否为 TypedArray
97
+ * @param {unknown} any
98
+ * @returns {boolean}
99
+ */
100
+ const isTypedArray = any => {
101
+ const tag = typeIs(any);
102
+ return (
103
+ tag === 'Int8Array' ||
104
+ tag === 'Uint8Array' ||
105
+ tag === 'Uint8ClampedArray' ||
106
+ tag === 'Int16Array' ||
107
+ tag === 'Uint16Array' ||
108
+ tag === 'Int32Array' ||
109
+ tag === 'Uint32Array' ||
110
+ tag === 'Float32Array' ||
111
+ tag === 'Float64Array' ||
112
+ tag === 'BigInt64Array' ||
113
+ tag === 'BigUint64Array'
114
+ );
115
+ };
116
+ const isDataView = any => typeIs(any) === 'DataView';
117
+ const isArrayBuffer = any => typeIs(any) === 'ArrayBuffer';
118
+ const isArguments = any => typeIs(any) === 'Arguments';
119
+ const isAsyncFunction = any => typeIs(any) === 'AsyncFunction';
72
120
  /**
73
121
  * 判断一个字符串是否为有效的 JSON, 若有效则返回有效的JSON对象,否则false
74
122
  * @param {string} str
@@ -132,6 +180,9 @@ function isNodeList(value) {
132
180
  }
133
181
  var type = {
134
182
  typeIs,
183
+ is,
184
+ TypeMapping: undefined,
185
+ TypeTag: undefined,
135
186
  objectHas,
136
187
  arrayLike,
137
188
  isString,
@@ -151,6 +202,14 @@ var type = {
151
202
  isDate,
152
203
  isError,
153
204
  isRegExp,
205
+ isPromise,
206
+ isMap,
207
+ isSet,
208
+ isTypedArray,
209
+ isDataView,
210
+ isArrayBuffer,
211
+ isArguments,
212
+ isAsyncFunction,
154
213
  isJsonString,
155
214
  isEmpty,
156
215
  isNodeList
@@ -158,14 +217,20 @@ var type = {
158
217
 
159
218
  exports.arrayLike = arrayLike;
160
219
  exports.default = type;
220
+ exports.is = is;
221
+ exports.isArguments = isArguments;
161
222
  exports.isArray = isArray;
223
+ exports.isArrayBuffer = isArrayBuffer;
224
+ exports.isAsyncFunction = isAsyncFunction;
162
225
  exports.isBigInt = isBigInt;
163
226
  exports.isBoolean = isBoolean;
227
+ exports.isDataView = isDataView;
164
228
  exports.isDate = isDate;
165
229
  exports.isEmpty = isEmpty;
166
230
  exports.isError = isError;
167
231
  exports.isFunction = isFunction;
168
232
  exports.isJsonString = isJsonString;
233
+ exports.isMap = isMap;
169
234
  exports.isNaN = isNaN;
170
235
  exports.isNodeList = isNodeList;
171
236
  exports.isNull = isNull;
@@ -174,9 +239,12 @@ exports.isNullish = isNullOrUnDef;
174
239
  exports.isNumber = isNumber;
175
240
  exports.isObject = isObject;
176
241
  exports.isPrimitive = isPrimitive;
242
+ exports.isPromise = isPromise;
177
243
  exports.isRegExp = isRegExp;
244
+ exports.isSet = isSet;
178
245
  exports.isString = isString;
179
246
  exports.isSymbol = isSymbol;
247
+ exports.isTypedArray = isTypedArray;
180
248
  exports.isUndefined = isUndefined;
181
249
  exports.objectHas = objectHas;
182
250
  exports.typeIs = typeIs;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -12,24 +12,6 @@ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toString
12
12
  * 增强型 Unicode/HTML/UTF-8 编码解码工具
13
13
  */
14
14
  class UnicodeToolkit {
15
- // 基础 HTML 实体映射
16
- static ENTITY_MAP = {
17
- '&': '&',
18
- '<': '&lt;',
19
- '>': '&gt;',
20
- '"': '&quot;',
21
- "'": '&#39;'
22
- };
23
- static NAMED_ENTITIES = {
24
- '&quot;': '"',
25
- '&amp;': '&',
26
- '&lt;': '<',
27
- '&gt;': '>',
28
- '&nbsp;': '\u00A0',
29
- '&copy;': '©',
30
- '&trade;': '™',
31
- '&reg;': '®'
32
- };
33
15
  /**
34
16
  * 编码函数
35
17
  * @param {string} str 原始字符串
@@ -117,6 +99,24 @@ class UnicodeToolkit {
117
99
  return decoded;
118
100
  }
119
101
  }
102
+ // 基础 HTML 实体映射
103
+ UnicodeToolkit.ENTITY_MAP = {
104
+ '&': '&amp;',
105
+ '<': '&lt;',
106
+ '>': '&gt;',
107
+ '"': '&quot;',
108
+ "'": '&#39;'
109
+ };
110
+ UnicodeToolkit.NAMED_ENTITIES = {
111
+ '&quot;': '"',
112
+ '&amp;': '&',
113
+ '&lt;': '<',
114
+ '&gt;': '>',
115
+ '&nbsp;': '\u00A0',
116
+ '&copy;': '©',
117
+ '&trade;': '™',
118
+ '&reg;': '®'
119
+ };
120
120
  var unicodeToolkit = {
121
121
  UnicodeToolkit
122
122
  };
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/dist/cjs/url.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
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
- * 字符串编码成Base64, 平替浏览器的btoa, 不包含中文的处理 (适用于任何环境,包括小程序)
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
- let bitmap,
22
- a,
23
- b,
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
- bitmap = (a << 16) | (b << 8) | c;
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 rest ? result.slice(0, rest - 3) + '==='.substring(rest) : result;
137
+ return binaryToBase64(string);
42
138
  }
43
139
  /**
44
- * Base64解码为原始字符串,平替浏览器的atob, 不包含中文的处理(适用于任何环境,包括小程序)
140
+ * Base64 解码为原始字符串(适用于任何环境,包括小程序)
45
141
  * @param {string} string
46
142
  * @returns {string}
47
143
  */
48
144
  function weAtob(string) {
49
- // 同window.atob: Base64解码为原始字符串
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
- const binaryString = !isNullOrUnDef(getGlobal('atob')) ? getGlobal('atob')(base64) : weAtob(base64);
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
- // 使用TextDecoder将Uint8Array转换为原始字符串,包括中文内容
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
- const utf8Array = !isNullOrUnDef(getGlobal('TextEncoder'))
110
- ? new (getGlobal('TextEncoder'))().encode(rawStr)
111
- : stringToUint8Array(rawStr);
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 !isNullOrUnDef(getGlobal('btoa')) ? getGlobal('btoa')(binaryString) : weBtoa(binaryString);
204
+ // 将二进制字符串转换为 base64 编码
205
+ return binaryToBase64(binaryString);
120
206
  }
121
207
  var base64 = {
122
208
  weBtoa,
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.19.1
2
+ * sculp-js v1.19.3
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */