@ivujs/i-utils 1.1.18 → 2.1.0

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 (142) hide show
  1. package/README.md +122 -58
  2. package/dist/cjs/ID-card/index.cjs +95 -0
  3. package/dist/cjs/array/index.cjs +476 -0
  4. package/dist/cjs/clipboard/index.cjs +132 -0
  5. package/dist/cjs/color/index.cjs +207 -0
  6. package/dist/cjs/constants/date.cjs +178 -0
  7. package/dist/cjs/constants/id-card.cjs +52 -0
  8. package/dist/cjs/constants/keycode.cjs +117 -0
  9. package/dist/cjs/constants/lang.cjs +13 -0
  10. package/dist/cjs/constants/math.cjs +13 -0
  11. package/dist/cjs/constants/regexp.cjs +53 -0
  12. package/dist/cjs/constants/sort.cjs +15 -0
  13. package/dist/cjs/cookie/index.cjs +73 -0
  14. package/dist/cjs/crypto/base64/base64.cjs +540 -0
  15. package/dist/cjs/crypto/base64/index.cjs +82 -0
  16. package/dist/cjs/crypto/md5/index.cjs +49 -0
  17. package/dist/cjs/crypto/md5/md5.cjs +351 -0
  18. package/dist/cjs/crypto/sha256/index.cjs +95 -0
  19. package/dist/cjs/crypto/sha256/sha256.cjs +557 -0
  20. package/dist/cjs/crypto/sm3/index.cjs +27 -0
  21. package/dist/cjs/crypto/sm3/sm3.cjs +200 -0
  22. package/dist/cjs/crypto/sm4/index.cjs +101 -0
  23. package/dist/cjs/crypto/sm4/sm4.cjs +498 -0
  24. package/dist/cjs/date/index.cjs +1310 -0
  25. package/dist/cjs/desensitized/index.cjs +78 -0
  26. package/dist/cjs/device/index.cjs +154 -0
  27. package/dist/cjs/dom/index.cjs +132 -0
  28. package/dist/cjs/file/index.cjs +343 -0
  29. package/dist/cjs/function/index.cjs +72 -0
  30. package/dist/cjs/id/index.cjs +6 -2
  31. package/dist/cjs/index.cjs +383 -7
  32. package/dist/cjs/keycode/index.cjs +30 -0
  33. package/dist/cjs/math/index.cjs +310 -0
  34. package/dist/cjs/number/index.cjs +34 -0
  35. package/dist/cjs/object/index.cjs +319 -0
  36. package/dist/cjs/pagination/index.cjs +146 -0
  37. package/dist/cjs/random/index.cjs +27 -0
  38. package/dist/cjs/regexp/index.cjs +103 -0
  39. package/dist/cjs/storage/index.cjs +14 -0
  40. package/dist/cjs/storage/localStorage.cjs +37 -0
  41. package/dist/cjs/storage/sessionStorage.cjs +37 -0
  42. package/dist/cjs/string/index.cjs +361 -0
  43. package/dist/cjs/url/index.cjs +261 -0
  44. package/dist/cjs/validate/index.cjs +484 -0
  45. package/dist/es/ID-card/index.d.ts +36 -0
  46. package/dist/es/ID-card/index.mjs +89 -0
  47. package/dist/es/array/index.d.ts +209 -0
  48. package/dist/es/array/index.mjs +447 -0
  49. package/dist/es/clipboard/index.d.ts +32 -0
  50. package/dist/es/clipboard/index.mjs +126 -0
  51. package/dist/es/color/index.d.ts +55 -0
  52. package/dist/es/color/index.mjs +197 -0
  53. package/dist/es/constants/date.d.ts +4 -0
  54. package/dist/es/constants/date.mjs +176 -0
  55. package/dist/es/constants/id-card.d.ts +4 -0
  56. package/dist/es/constants/id-card.mjs +50 -0
  57. package/dist/es/constants/index.d.ts +11 -0
  58. package/dist/es/constants/keycode.d.ts +1 -0
  59. package/dist/es/constants/keycode.mjs +115 -0
  60. package/dist/es/constants/lang.d.ts +4 -0
  61. package/dist/es/constants/lang.mjs +11 -0
  62. package/dist/es/constants/math.d.ts +4 -0
  63. package/dist/es/constants/math.mjs +11 -0
  64. package/dist/es/constants/regexp.d.ts +4 -0
  65. package/dist/es/constants/regexp.mjs +51 -0
  66. package/dist/es/constants/sort.d.ts +4 -0
  67. package/dist/es/constants/sort.mjs +13 -0
  68. package/dist/es/cookie/index.d.ts +29 -0
  69. package/dist/es/cookie/index.mjs +67 -0
  70. package/dist/es/crypto/base64/base64.d.ts +8 -0
  71. package/dist/es/crypto/base64/base64.mjs +531 -0
  72. package/dist/es/crypto/base64/index.d.ts +53 -0
  73. package/dist/es/crypto/base64/index.mjs +73 -0
  74. package/dist/es/crypto/index.d.ts +8 -0
  75. package/dist/es/crypto/md5/index.d.ts +26 -0
  76. package/dist/es/crypto/md5/index.mjs +44 -0
  77. package/dist/es/crypto/md5/md5.d.ts +11 -0
  78. package/dist/es/crypto/md5/md5.mjs +349 -0
  79. package/dist/es/crypto/sha256/index.d.ts +52 -0
  80. package/dist/es/crypto/sha256/index.mjs +86 -0
  81. package/dist/es/crypto/sha256/sha256.d.ts +82 -0
  82. package/dist/es/crypto/sha256/sha256.mjs +548 -0
  83. package/dist/es/crypto/sm3/index.d.ts +13 -0
  84. package/dist/es/crypto/sm3/index.mjs +24 -0
  85. package/dist/es/crypto/sm3/sm3.d.ts +3 -0
  86. package/dist/es/crypto/sm3/sm3.mjs +197 -0
  87. package/dist/es/crypto/sm4/index.d.ts +56 -0
  88. package/dist/es/crypto/sm4/index.mjs +95 -0
  89. package/dist/es/crypto/sm4/sm4.d.ts +48 -0
  90. package/dist/es/crypto/sm4/sm4.mjs +490 -0
  91. package/dist/es/date/index.d.ts +608 -0
  92. package/dist/es/date/index.mjs +1222 -0
  93. package/dist/es/desensitized/index.d.ts +41 -0
  94. package/dist/es/desensitized/index.mjs +72 -0
  95. package/dist/es/device/index.d.ts +73 -0
  96. package/dist/es/device/index.mjs +140 -0
  97. package/dist/es/dom/index.d.ts +60 -0
  98. package/dist/es/dom/index.mjs +122 -0
  99. package/dist/es/file/index.d.ts +93 -0
  100. package/dist/es/file/index.mjs +327 -0
  101. package/dist/es/function/index.d.ts +26 -0
  102. package/dist/es/function/index.mjs +68 -0
  103. package/dist/es/id/index.d.ts +3 -0
  104. package/dist/es/id/index.mjs +6 -2
  105. package/dist/es/index.d.ts +26 -2
  106. package/dist/es/index.mjs +40 -4
  107. package/dist/es/keycode/index.d.ts +12 -0
  108. package/dist/es/keycode/index.mjs +27 -0
  109. package/dist/es/math/index.d.ts +66 -0
  110. package/dist/es/math/index.mjs +300 -0
  111. package/dist/es/number/index.d.ts +14 -0
  112. package/dist/es/number/index.mjs +31 -0
  113. package/dist/es/object/index.d.ts +84 -0
  114. package/dist/es/object/index.mjs +306 -0
  115. package/dist/es/pagination/index.d.ts +67 -0
  116. package/dist/es/pagination/index.mjs +140 -0
  117. package/dist/es/random/index.d.ts +18 -0
  118. package/dist/es/random/index.mjs +24 -0
  119. package/dist/es/regexp/index.d.ts +63 -0
  120. package/dist/es/regexp/index.mjs +92 -0
  121. package/dist/es/storage/index.d.ts +13 -0
  122. package/dist/es/storage/index.mjs +12 -0
  123. package/dist/es/storage/localStorage.d.ts +21 -0
  124. package/dist/es/storage/localStorage.mjs +32 -0
  125. package/dist/es/storage/sessionStorage.d.ts +21 -0
  126. package/dist/es/storage/sessionStorage.mjs +32 -0
  127. package/dist/es/string/index.d.ts +124 -0
  128. package/dist/es/string/index.mjs +342 -0
  129. package/dist/es/url/index.d.ts +118 -0
  130. package/dist/es/url/index.mjs +243 -0
  131. package/dist/es/validate/index.d.ts +231 -0
  132. package/dist/es/validate/index.mjs +447 -0
  133. package/dist/index.d.ts +2394 -2
  134. package/dist/lib/index.full.umd.js +8059 -29
  135. package/dist/lib/index.full.umd.min.js +2 -2
  136. package/dist/lib/index.full.umd.min.js.map +1 -1
  137. package/dist/resolver/auto-imports.cjs +345 -2
  138. package/dist/resolver/auto-imports.mjs +345 -2
  139. package/dist/resolver/index.cjs +1 -2
  140. package/dist/resolver/index.d.ts +1 -1
  141. package/dist/resolver/index.mjs +1 -2
  142. package/package.json +16 -7
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ var index = require('../validate/index.cjs');
4
+
5
+ /**
6
+ * @module 浏览器Cookie
7
+ */
8
+ /**
9
+ * 浏览器是否支持 Cookie
10
+ * @returns {boolean} 返回true和false
11
+ */
12
+ function isSupportCookie() {
13
+ return window.navigator.cookieEnabled;
14
+ }
15
+ /* cookie存储 */
16
+ /**
17
+ * 通过name获取cookie
18
+ * @param {string} name 参数name
19
+ * @returns {string} 返回获取的值
20
+ */
21
+ function getCookie(name) {
22
+ if (typeof document == "undefined")
23
+ return "";
24
+ // cookie中每个分号后面是有个空格的,需要替换掉
25
+ const arr = document.cookie ? document.cookie.replace(/\s/g, "").split(";") : [];
26
+ for (const i in arr) {
27
+ const tempArr = arr[i].split("=");
28
+ if (tempArr[0] === name) {
29
+ return decodeURIComponent(tempArr[1]);
30
+ }
31
+ }
32
+ return "";
33
+ }
34
+ /**
35
+ * 通过name设置cookie
36
+ * 注:timestamp参数不填,则默认为session级别,浏览器关闭即cookie过期
37
+ * @param {string} name 参数name
38
+ * @param {string} value 设置的value
39
+ * @param {number} timestamp 过期的时间戳值,默认为一天,设置一天过期则为:24*60*60*1000
40
+ */
41
+ function setCookie(name, value, timestamp = 24 * 60 * 60 * 1000) {
42
+ if (typeof document == "undefined")
43
+ return;
44
+ document.cookie = name + "=" + value + ";expires=" + new Date(Date.now() + timestamp);
45
+ }
46
+ /**
47
+ * 通过name删除cookie
48
+ * @param {string} name 参数name
49
+ */
50
+ function removeCookie(name) {
51
+ if (index.isEmpty(name))
52
+ return;
53
+ setCookie(name, "", -1);
54
+ }
55
+ /**
56
+ * 清空当前站点域名的cookie
57
+ * @param {string} domain 域名地址,默认是当前站点域名
58
+ */
59
+ function clearCookie(domain = document.domain) {
60
+ const names = document.cookie.match(/[^ =;]+(?==)/g) || [];
61
+ if (names && names.length > 0) {
62
+ for (let i = names.length; i--;) {
63
+ document.cookie =
64
+ names[i] + "=0;path=/;" + domain ? "domain=" + domain + ";" : "" + "expires=" + new Date(0).toUTCString();
65
+ }
66
+ }
67
+ }
68
+
69
+ exports.clearCookie = clearCookie;
70
+ exports.getCookie = getCookie;
71
+ exports.isSupportCookie = isSupportCookie;
72
+ exports.removeCookie = removeCookie;
73
+ exports.setCookie = setCookie;
@@ -0,0 +1,540 @@
1
+ 'use strict';
2
+
3
+ /*
4
+ * base64加解密
5
+ * 高性能、高健壮性、纯JS实现,兼容Node/浏览器/Worker
6
+ */
7
+ // 核心常量定义
8
+ const ENCODING_ERROR = "not a UTF-8 string";
9
+ const BASE64_ENCODE_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
10
+ const BASE64_DECODE_CHAR = {};
11
+ for (let i = 0; i < 64; ++i) {
12
+ BASE64_DECODE_CHAR[BASE64_ENCODE_CHAR[i]] = i;
13
+ }
14
+ // 兼容URL安全的Base64
15
+ BASE64_DECODE_CHAR["-"] = 62;
16
+ BASE64_DECODE_CHAR["_"] = 63;
17
+ // ======================== 精简版环境判断(核心场景全覆盖,保留!)========================
18
+ const isNodeEnv = typeof process === "object" && process.versions && process.versions.node;
19
+ const isBrowserEnv = typeof window === "object" && typeof window.btoa === "function";
20
+ const root = isNodeEnv ? global : typeof self === "object" ? self : window;
21
+ // ======================== 核心工具函数(全部保留,原生API更快!)========================
22
+ /**
23
+ * 清理Base64字符串(移除等号、换行符、非法字符)
24
+ */
25
+ const cleanBase64Str = function (base64Str) {
26
+ return base64Str
27
+ .split("=")[0]
28
+ .replace(/[\r\n]/g, "")
29
+ .replace(/[^A-Za-z0-9+/=_-]/g, ""); // 过滤非法字符
30
+ };
31
+ /**
32
+ * UTF-8字符串转字节数组(性能优化版)
33
+ */
34
+ const utf8ToBytes = function (str) {
35
+ const len = str.length; // 缓存长度,减少属性访问
36
+ const bytes = new Array(len * 4); // 预分配最大可能长度(避免频繁扩容)
37
+ let byteIndex = 0;
38
+ for (let i = 0; i < len; i++) {
39
+ const c = str.charCodeAt(i);
40
+ if (c < 0x80) {
41
+ bytes[byteIndex++] = c;
42
+ }
43
+ else if (c < 0x800) {
44
+ bytes[byteIndex++] = 0xc0 | (c >> 6);
45
+ bytes[byteIndex++] = 0x80 | (c & 0x3f);
46
+ }
47
+ else if (c < 0xd800 || c >= 0xe000) {
48
+ bytes[byteIndex++] = 0xe0 | (c >> 12);
49
+ bytes[byteIndex++] = 0x80 | ((c >> 6) & 0x3f);
50
+ bytes[byteIndex++] = 0x80 | (c & 0x3f);
51
+ }
52
+ else {
53
+ // 处理代理对
54
+ const code = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
55
+ bytes[byteIndex++] = 0xf0 | (code >> 18);
56
+ bytes[byteIndex++] = 0x80 | ((code >> 12) & 0x3f);
57
+ bytes[byteIndex++] = 0x80 | ((code >> 6) & 0x3f);
58
+ bytes[byteIndex++] = 0x80 | (code & 0x3f);
59
+ }
60
+ }
61
+ // 截断数组到实际长度
62
+ return bytes.slice(0, byteIndex);
63
+ };
64
+ /**
65
+ * Base64字符串解码为字节数组(性能优化版)
66
+ */
67
+ const decodeAsBytes = function (base64Str) {
68
+ // 空值快速返回
69
+ if (!base64Str)
70
+ return [];
71
+ base64Str = cleanBase64Str(base64Str);
72
+ const len = base64Str.length;
73
+ const count = (len >> 2) << 2;
74
+ // 预计算最大长度:3/4 * 总字符数,减少数组扩容
75
+ const bytes = new Array(Math.floor((len * 3) / 4));
76
+ let byteIndex = 0;
77
+ let i = 0; // 把i的作用域提升到函数级,确保后续能访问
78
+ // 4字符转3字节
79
+ for (; i < count;) {
80
+ const v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
81
+ const v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
82
+ const v3 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
83
+ const v4 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
84
+ bytes[byteIndex++] = ((v1 << 2) | (v2 >>> 4)) & 255;
85
+ bytes[byteIndex++] = ((v2 << 4) | (v3 >>> 2)) & 255;
86
+ bytes[byteIndex++] = ((v3 << 6) | v4) & 255;
87
+ }
88
+ // 处理剩余字符(此时i的作用域有效)
89
+ const remain = len - count;
90
+ if (remain === 2) {
91
+ const v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
92
+ const v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
93
+ bytes[byteIndex++] = ((v1 << 2) | (v2 >>> 4)) & 255;
94
+ }
95
+ else if (remain === 3) {
96
+ const v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
97
+ const v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
98
+ const v3 = BASE64_DECODE_CHAR[base64Str.charAt(i++)] || 0;
99
+ bytes[byteIndex++] = ((v1 << 2) | (v2 >>> 4)) & 255;
100
+ bytes[byteIndex++] = ((v2 << 4) | (v3 >>> 2)) & 255;
101
+ }
102
+ // 截断数组到实际长度
103
+ return bytes.slice(0, byteIndex);
104
+ };
105
+ /**
106
+ * 抽离公共逻辑:字节数组转Base64字符(减少代码冗余)
107
+ */
108
+ const bytesToBase64Chars = function (bytes) {
109
+ const len = bytes.length;
110
+ const count = Math.floor(len / 3) * 3;
111
+ const base64Str = new Array(Math.ceil((len * 4) / 3)); // 预分配长度
112
+ let strIndex = 0;
113
+ // 3字节转4字符
114
+ for (let i = 0; i < count;) {
115
+ const v1 = bytes[i++];
116
+ const v2 = bytes[i++];
117
+ const v3 = bytes[i++];
118
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[v1 >>> 2];
119
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[((v1 << 4) | (v2 >>> 4)) & 63];
120
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[((v2 << 2) | (v3 >>> 6)) & 63];
121
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[v3 & 63];
122
+ }
123
+ // 处理剩余字节
124
+ const remain = len - count;
125
+ if (remain === 1) {
126
+ const v1 = bytes[count];
127
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[v1 >>> 2];
128
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[(v1 << 4) & 63];
129
+ base64Str[strIndex++] = "=";
130
+ base64Str[strIndex++] = "=";
131
+ }
132
+ else if (remain === 2) {
133
+ const v1 = bytes[count];
134
+ const v2 = bytes[count + 1];
135
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[v1 >>> 2];
136
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[((v1 << 4) | (v2 >>> 4)) & 63];
137
+ base64Str[strIndex++] = BASE64_ENCODE_CHAR[(v2 << 2) & 63];
138
+ base64Str[strIndex++] = "=";
139
+ }
140
+ return base64Str.join("");
141
+ };
142
+ /**
143
+ * 字节数组编码为Base64字符串(复用公共逻辑)
144
+ */
145
+ let encodeFromBytes = function (bytes) {
146
+ // 空值快速返回
147
+ if (!bytes || bytes.length === 0)
148
+ return "";
149
+ return bytesToBase64Chars(bytes);
150
+ };
151
+ // ======================== 环境适配的btoa/atob实现(全部保留,原生更快!)=======================
152
+ let btoa = root.btoa;
153
+ let atob = root.atob;
154
+ let utf8Base64Encode;
155
+ let utf8Base64Decode;
156
+ // 1. Node.js环境(纯JS实现,不使用require)
157
+ if (isNodeEnv) {
158
+ // 纯JS版btoa(替代Buffer实现)
159
+ btoa = function (str) {
160
+ if (!str)
161
+ return "";
162
+ const bytes = Array.from(str).map((c) => c.charCodeAt(0));
163
+ return bytesToBase64Chars(bytes);
164
+ };
165
+ // 纯JS版atob
166
+ atob = function (base64Str) {
167
+ if (!base64Str)
168
+ return "";
169
+ const bytes = decodeAsBytes(base64Str);
170
+ return String.fromCharCode.apply(null, bytes);
171
+ };
172
+ // 纯JS版UTF8编码/解码(和浏览器环境保持一致)
173
+ utf8Base64Encode = function (str) {
174
+ if (!str)
175
+ return "";
176
+ const bytes = utf8ToBytes(str);
177
+ return bytesToBase64Chars(bytes);
178
+ };
179
+ utf8Base64Decode = function (base64Str) {
180
+ if (!base64Str)
181
+ return "";
182
+ const bytes = decodeAsBytes(base64Str);
183
+ const str = [];
184
+ const len = bytes.length;
185
+ let i = 0;
186
+ while (i < len) {
187
+ const b = bytes[i++];
188
+ if (b <= 0x7f) {
189
+ str.push(String.fromCharCode(b));
190
+ continue;
191
+ }
192
+ let c, followingChars;
193
+ if (b > 0xbf && b <= 0xdf) {
194
+ c = b & 0x1f;
195
+ followingChars = 1;
196
+ }
197
+ else if (b <= 0xef) {
198
+ c = b & 0x0f;
199
+ followingChars = 2;
200
+ }
201
+ else if (b <= 0xf7) {
202
+ c = b & 0x07;
203
+ followingChars = 3;
204
+ }
205
+ else {
206
+ throw new Error(ENCODING_ERROR);
207
+ }
208
+ // 校验后续字节合法性
209
+ for (let j = 0; j < followingChars; ++j) {
210
+ const byte = bytes[i++];
211
+ if (i > len || byte < 0x80 || byte > 0xbf) {
212
+ throw new Error(ENCODING_ERROR);
213
+ }
214
+ c = (c << 6) + (byte & 0x3f);
215
+ }
216
+ // 校验Unicode范围
217
+ if (c >= 0xd800 && c <= 0xdfff)
218
+ throw new Error(ENCODING_ERROR);
219
+ if (c > 0x10ffff)
220
+ throw new Error(ENCODING_ERROR);
221
+ // 转换为字符
222
+ if (c <= 0xffff) {
223
+ str.push(String.fromCharCode(c));
224
+ }
225
+ else {
226
+ c -= 0x10000;
227
+ str.push(String.fromCharCode((c >> 10) + 0xd800), String.fromCharCode((c & 0x3ff) + 0xdc00));
228
+ }
229
+ }
230
+ return str.join("");
231
+ };
232
+ // 2. 无原生btoa/atob的环境(老旧浏览器/Worker)
233
+ }
234
+ else if (!isBrowserEnv) {
235
+ // 纯JS版btoa(复用公共逻辑)
236
+ btoa = function (str) {
237
+ if (!str)
238
+ return "";
239
+ const bytes = Array.from(str).map((c) => c.charCodeAt(0));
240
+ return bytesToBase64Chars(bytes);
241
+ };
242
+ // 纯JS版atob
243
+ atob = function (base64Str) {
244
+ if (!base64Str)
245
+ return "";
246
+ const bytes = decodeAsBytes(base64Str);
247
+ return String.fromCharCode.apply(null, bytes);
248
+ };
249
+ // 纯JS版UTF8编码/解码
250
+ utf8Base64Encode = function (str) {
251
+ if (!str)
252
+ return "";
253
+ const bytes = utf8ToBytes(str);
254
+ return bytesToBase64Chars(bytes);
255
+ };
256
+ utf8Base64Decode = function (base64Str) {
257
+ if (!base64Str)
258
+ return "";
259
+ const bytes = decodeAsBytes(base64Str);
260
+ const str = [];
261
+ const len = bytes.length;
262
+ let i = 0;
263
+ while (i < len) {
264
+ const b = bytes[i++];
265
+ if (b <= 0x7f) {
266
+ str.push(String.fromCharCode(b));
267
+ continue;
268
+ }
269
+ let c, followingChars;
270
+ if (b > 0xbf && b <= 0xdf) {
271
+ c = b & 0x1f;
272
+ followingChars = 1;
273
+ }
274
+ else if (b <= 0xef) {
275
+ c = b & 0x0f;
276
+ followingChars = 2;
277
+ }
278
+ else if (b <= 0xf7) {
279
+ c = b & 0x07;
280
+ followingChars = 3;
281
+ }
282
+ else {
283
+ throw new Error(ENCODING_ERROR);
284
+ }
285
+ // 校验后续字节合法性
286
+ for (let j = 0; j < followingChars; ++j) {
287
+ const byte = bytes[i++];
288
+ if (i > len || byte < 0x80 || byte > 0xbf) {
289
+ throw new Error(ENCODING_ERROR);
290
+ }
291
+ c = (c << 6) + (byte & 0x3f);
292
+ }
293
+ // 校验Unicode范围
294
+ if (c >= 0xd800 && c <= 0xdfff)
295
+ throw new Error(ENCODING_ERROR);
296
+ if (c > 0x10ffff)
297
+ throw new Error(ENCODING_ERROR);
298
+ // 转换为字符
299
+ if (c <= 0xffff) {
300
+ str.push(String.fromCharCode(c));
301
+ }
302
+ else {
303
+ c -= 0x10000;
304
+ str.push(String.fromCharCode((c >> 10) + 0xd800), String.fromCharCode((c & 0x3ff) + 0xdc00));
305
+ }
306
+ }
307
+ return str.join("");
308
+ };
309
+ // 3. 浏览器原生支持btoa/atob
310
+ }
311
+ else {
312
+ utf8Base64Encode = function (str) {
313
+ if (!str)
314
+ return "";
315
+ const result = [];
316
+ const len = str.length;
317
+ for (let i = 0; i < len; i++) {
318
+ const charcode = str.charCodeAt(i);
319
+ if (charcode < 0x80) {
320
+ result.push(String.fromCharCode(charcode));
321
+ }
322
+ else if (charcode < 0x800) {
323
+ result.push(String.fromCharCode(0xc0 | (charcode >> 6)), String.fromCharCode(0x80 | (charcode & 0x3f)));
324
+ }
325
+ else if (charcode < 0xd800 || charcode >= 0xe000) {
326
+ result.push(String.fromCharCode(0xe0 | (charcode >> 12)), String.fromCharCode(0x80 | ((charcode >> 6) & 0x3f)), String.fromCharCode(0x80 | (charcode & 0x3f)));
327
+ }
328
+ else {
329
+ const code = 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
330
+ result.push(String.fromCharCode(0xf0 | (code >> 18)), String.fromCharCode(0x80 | ((code >> 12) & 0x3f)), String.fromCharCode(0x80 | ((code >> 6) & 0x3f)), String.fromCharCode(0x80 | (code & 0x3f)));
331
+ }
332
+ }
333
+ return btoa(result.join(""));
334
+ };
335
+ utf8Base64Decode = function (base64Str) {
336
+ if (!base64Str)
337
+ return "";
338
+ const tmpStr = atob(base64Str.replace(/-/g, "+").replace(/_/g, "/"));
339
+ // 替换Unicode转义,避免ESLint控制字符警告
340
+ // eslint-disable-next-line no-control-regex
341
+ if (!/[^\u0000-\u007F]/.test(tmpStr)) {
342
+ return tmpStr;
343
+ }
344
+ const str = [];
345
+ const len = tmpStr.length;
346
+ let i = 0;
347
+ while (i < len) {
348
+ const b = tmpStr.charCodeAt(i++);
349
+ if (b <= 0x7f) {
350
+ str.push(String.fromCharCode(b));
351
+ continue;
352
+ }
353
+ let c, followingChars;
354
+ if (b > 0xbf && b <= 0xdf) {
355
+ c = b & 0x1f;
356
+ followingChars = 1;
357
+ }
358
+ else if (b <= 0xef) {
359
+ c = b & 0x0f;
360
+ followingChars = 2;
361
+ }
362
+ else if (b <= 0xf7) {
363
+ c = b & 0x07;
364
+ followingChars = 3;
365
+ }
366
+ else {
367
+ throw new Error(ENCODING_ERROR);
368
+ }
369
+ for (let j = 0; j < followingChars; ++j) {
370
+ const byte = tmpStr.charCodeAt(i++);
371
+ if (i > len || byte < 0x80 || byte > 0xbf)
372
+ throw new Error(ENCODING_ERROR);
373
+ c = (c << 6) + (byte & 0x3f);
374
+ }
375
+ if (c >= 0xd800 && c <= 0xdfff)
376
+ throw new Error(ENCODING_ERROR);
377
+ if (c > 0x10ffff)
378
+ throw new Error(ENCODING_ERROR);
379
+ if (c <= 0xffff) {
380
+ str.push(String.fromCharCode(c));
381
+ }
382
+ else {
383
+ c -= 0x10000;
384
+ str.push(String.fromCharCode((c >> 10) + 0xd800), String.fromCharCode((c & 0x3ff) + 0xdc00));
385
+ }
386
+ }
387
+ return str.join("");
388
+ };
389
+ }
390
+ // ======================== 类型转换工具函数(全部保留)========================
391
+ /**
392
+ * Uint8Array转Base64字符串
393
+ * @param {Uint8Array} uint8Array 字节数组
394
+ * @param {boolean} [urlSafe=false] 是否URL安全
395
+ * @returns {string} Base64字符串
396
+ */
397
+ const fromUint8Array = function (uint8Array, urlSafe = false) {
398
+ if (!(uint8Array instanceof Uint8Array)) {
399
+ throw new TypeError("fromUint8Array方法仅支持Uint8Array输入");
400
+ }
401
+ const bytes = Array.from(uint8Array);
402
+ let result = bytesToBase64Chars(bytes);
403
+ if (urlSafe) {
404
+ result = result.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
405
+ }
406
+ return result;
407
+ };
408
+ /**
409
+ * Base64字符串转Uint8Array
410
+ * @param {string} base64Str Base64字符串
411
+ * @param {boolean} [urlSafe=false] 是否URL安全
412
+ * @returns {Uint8Array} 字节数组
413
+ */
414
+ const toUint8Array = function (base64Str, urlSafe = false) {
415
+ if (typeof base64Str !== "string") {
416
+ throw new TypeError("toUint8Array方法仅支持字符串输入");
417
+ }
418
+ // 处理URL安全字符
419
+ if (urlSafe) {
420
+ base64Str = base64Str.replace(/-/g, "+").replace(/_/g, "/");
421
+ }
422
+ const bytes = decodeAsBytes(base64Str);
423
+ return new Uint8Array(bytes);
424
+ };
425
+ /**
426
+ * 十六进制字符串转Base64字符串
427
+ * @param {string} hexStr 十六进制字符串
428
+ * @param {boolean} [urlSafe=false] 是否URL安全
429
+ * @returns {string} Base64字符串
430
+ */
431
+ const fromHex = function (hexStr, urlSafe = false) {
432
+ if (typeof hexStr !== "string" || !/^[0-9a-fA-F]+$/.test(hexStr)) {
433
+ throw new TypeError("fromHex方法仅支持十六进制字符串输入");
434
+ }
435
+ // 补全偶数长度
436
+ const str = hexStr.length % 2 ? `0${hexStr}` : hexStr;
437
+ const bytes = new Array(str.length / 2);
438
+ for (let i = 0; i < bytes.length; i++) {
439
+ bytes[i] = parseInt(str.substr(i * 2, 2), 16);
440
+ }
441
+ let result = bytesToBase64Chars(bytes);
442
+ if (urlSafe) {
443
+ result = result.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
444
+ }
445
+ return result;
446
+ };
447
+ /**
448
+ * Base64字符串转十六进制字符串
449
+ * @param {string} base64Str Base64字符串
450
+ * @param {boolean} [urlSafe=false] 是否URL安全
451
+ * @returns {string} 十六进制字符串
452
+ */
453
+ const toHex = function (base64Str, urlSafe = false) {
454
+ if (typeof base64Str !== "string") {
455
+ throw new TypeError("toHex方法仅支持字符串输入");
456
+ }
457
+ // 处理URL安全字符
458
+ if (urlSafe) {
459
+ base64Str = base64Str.replace(/-/g, "+").replace(/_/g, "/");
460
+ }
461
+ const bytes = decodeAsBytes(base64Str);
462
+ return Array.from(bytes)
463
+ .map((byte) => byte.toString(16).padStart(2, "0"))
464
+ .join("");
465
+ };
466
+ // ======================== 核心方法(仅删除asciiOnly开关,默认UTF8)========================
467
+ /**
468
+ * Base64编码(高性能、高健壮性版,默认UTF8)
469
+ * @param {string | number[] | Uint8Array | ArrayBuffer} data 待编码数据
470
+ * @param {boolean} [urlSafe=false] 是否生成URL安全的Base64
471
+ * @returns {string} Base64字符串
472
+ */
473
+ const encode = function (data, urlSafe = false) {
474
+ // 空值快速返回
475
+ if (data == null)
476
+ return "";
477
+ const notString = typeof data !== "string";
478
+ let result = "";
479
+ if (notString) {
480
+ // 处理ArrayBuffer
481
+ if (data.constructor === root.ArrayBuffer) {
482
+ data = new Uint8Array(data);
483
+ }
484
+ // 处理字节数组/Uint8Array
485
+ result = encodeFromBytes(data);
486
+ }
487
+ else {
488
+ // 空字符串快速返回
489
+ if (data === "")
490
+ return "";
491
+ // 强制走UTF8编码(删掉asciiOnly判断,直接用utf8Base64Encode)
492
+ result = utf8Base64Encode(data);
493
+ }
494
+ // URL安全处理:替换+->-,/->_,去掉=
495
+ if (urlSafe) {
496
+ result = result.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
497
+ }
498
+ return result;
499
+ };
500
+ /**
501
+ * Base64解码(高性能、高健壮性版,默认UTF8)
502
+ * @param {string} base64Str 待解码的Base64字符串
503
+ * @returns {string} 解码后的UTF8字符串
504
+ */
505
+ const decode = function (base64Str) {
506
+ // 空值快速返回
507
+ if (base64Str == null || base64Str === "")
508
+ return "";
509
+ // 处理URL安全的Base64
510
+ base64Str = base64Str.replace(/-/g, "+").replace(/_/g, "/");
511
+ base64Str = cleanBase64Str(base64Str);
512
+ // 强制走UTF8解码(删掉asciiOnly判断,直接用utf8Base64Decode)
513
+ return utf8Base64Decode(base64Str);
514
+ };
515
+ // ======================== URL安全专用方法(同步删除asciiOnly开关)========================
516
+ /**
517
+ * URL安全Base64编码(专用方法,等价于encode(data, true))
518
+ * @param {string | number[] | Uint8Array | ArrayBuffer} data 待编码数据
519
+ * @returns {string} URL安全的Base64字符串
520
+ */
521
+ const encodeURI = function (data) {
522
+ return encode(data, true);
523
+ };
524
+ /**
525
+ * URL安全Base64解码(专用方法,兼容URL安全字符)
526
+ * @param {string} base64Str URL安全的Base64字符串
527
+ * @returns {string} 解码后的UTF8字符串
528
+ */
529
+ const decodeURI = function (base64Str) {
530
+ return decode(base64Str);
531
+ };
532
+
533
+ exports.decode = decode;
534
+ exports.decodeURI = decodeURI;
535
+ exports.encode = encode;
536
+ exports.encodeURI = encodeURI;
537
+ exports.fromHex = fromHex;
538
+ exports.fromUint8Array = fromUint8Array;
539
+ exports.toHex = toHex;
540
+ exports.toUint8Array = toUint8Array;
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ var base64 = require('./base64.cjs');
4
+
5
+ /**
6
+ * Base64编码
7
+ * @param str 待编码的UTF8字符串
8
+ * @param urlSafe 是否生成URL安全的Base64(默认false)
9
+ * @returns Base64字符串
10
+ */
11
+ function base64Encode(str, urlSafe = false) {
12
+ return base64.encode(str, urlSafe);
13
+ }
14
+ /**
15
+ * Base64解码
16
+ * @param str 待解码的Base64字符串
17
+ * @returns 解码后的UTF8字符串
18
+ */
19
+ function base64Decode(str) {
20
+ return base64.decode(str);
21
+ }
22
+ /**
23
+ * URL安全Base64编码
24
+ * @param str 待编码的UTF8字符串
25
+ * @returns URL安全的Base64字符串
26
+ */
27
+ function base64EncodeURI(str) {
28
+ return base64.encodeURI(str);
29
+ }
30
+ /**
31
+ * URL安全Base64解码
32
+ * @param str 待解码的URL安全Base64字符串
33
+ * @returns 解码后的UTF8字符串
34
+ */
35
+ function base64DecodeURI(str) {
36
+ return base64.decodeURI(str);
37
+ }
38
+ /**
39
+ * Uint8Array转Base64字符串
40
+ * @param uint8Array 待编码的字节数组
41
+ * @param urlSafe 是否生成URL安全的Base64(默认false)
42
+ * @returns Base64字符串
43
+ */
44
+ function base64FromUint8Array(uint8Array, urlSafe = false) {
45
+ return base64.fromUint8Array(uint8Array, urlSafe);
46
+ }
47
+ /**
48
+ * Base64字符串转Uint8Array
49
+ * @param base64Str 待解码的Base64字符串
50
+ * @param urlSafe 是否为URL安全的Base64(默认false)
51
+ * @returns 解码后的字节数组
52
+ */
53
+ function base64ToUint8Array(base64Str, urlSafe = false) {
54
+ return base64.toUint8Array(base64Str, urlSafe);
55
+ }
56
+ /**
57
+ * 十六进制字符串转Base64字符串
58
+ * @param hexStr 待编码的十六进制字符串
59
+ * @param urlSafe 是否生成URL安全的Base64(默认false)
60
+ * @returns Base64字符串
61
+ */
62
+ function base64FromHex(hexStr, urlSafe = false) {
63
+ return base64.fromHex(hexStr, urlSafe);
64
+ }
65
+ /**
66
+ * Base64字符串转十六进制字符串
67
+ * @param base64Str 待解码的Base64字符串
68
+ * @param urlSafe 是否为URL安全的Base64(默认false)
69
+ * @returns 解码后的十六进制字符串
70
+ */
71
+ function base64ToHex(base64Str, urlSafe = false) {
72
+ return base64.toHex(base64Str, urlSafe);
73
+ }
74
+
75
+ exports.base64Decode = base64Decode;
76
+ exports.base64DecodeURI = base64DecodeURI;
77
+ exports.base64Encode = base64Encode;
78
+ exports.base64EncodeURI = base64EncodeURI;
79
+ exports.base64FromHex = base64FromHex;
80
+ exports.base64FromUint8Array = base64FromUint8Array;
81
+ exports.base64ToHex = base64ToHex;
82
+ exports.base64ToUint8Array = base64ToUint8Array;