@ivujs/i-utils 2.1.3 → 2.1.5
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/clipboard/index.cjs +3 -3
- package/dist/cjs/color/index.cjs +3 -3
- package/dist/cjs/crypto/base64/base64.cjs +4 -4
- package/dist/cjs/crypto/md5/index.cjs +2 -2
- package/dist/cjs/crypto/sha256/index.cjs +4 -4
- package/dist/cjs/crypto/sm3/index.cjs +1 -1
- package/dist/cjs/crypto/sm3/sm3.cjs +1 -1
- package/dist/cjs/crypto/sm4/index.cjs +5 -5
- package/dist/cjs/crypto/sm4/sm4.cjs +11 -11
- package/dist/cjs/date/index.cjs +4 -4
- package/dist/cjs/device/index.cjs +12 -12
- package/dist/cjs/file/index.cjs +9 -8
- package/dist/cjs/math/index.cjs +6 -6
- package/dist/cjs/storage/localStorage.cjs +4 -4
- package/dist/cjs/storage/sessionStorage.cjs +4 -4
- package/dist/cjs/string/index.cjs +9 -9
- package/dist/cjs/validate/index.cjs +1 -1
- package/dist/es/clipboard/index.mjs +3 -3
- package/dist/es/color/index.mjs +3 -3
- package/dist/es/crypto/base64/base64.mjs +4 -4
- package/dist/es/crypto/md5/index.mjs +2 -2
- package/dist/es/crypto/sha256/index.mjs +4 -4
- package/dist/es/crypto/sm3/index.mjs +1 -1
- package/dist/es/crypto/sm3/sm3.mjs +1 -1
- package/dist/es/crypto/sm4/index.mjs +5 -5
- package/dist/es/crypto/sm4/sm4.mjs +11 -11
- package/dist/es/date/index.mjs +4 -4
- package/dist/es/device/index.mjs +12 -12
- package/dist/es/file/index.mjs +9 -8
- package/dist/es/math/index.mjs +6 -6
- package/dist/es/storage/localStorage.mjs +4 -4
- package/dist/es/storage/sessionStorage.mjs +4 -4
- package/dist/es/string/index.mjs +9 -9
- package/dist/es/validate/index.mjs +1 -1
- package/dist/lib/index.full.umd.js +84 -83
- package/dist/lib/index.full.umd.min.js +2 -2
- package/dist/lib/index.full.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -42,7 +42,7 @@ function getClipboard() {
|
|
|
42
42
|
*/
|
|
43
43
|
function getClipboardText() {
|
|
44
44
|
return new Promise((resolve, reject) => {
|
|
45
|
-
if (window.navigator
|
|
45
|
+
if (window && window.navigator) {
|
|
46
46
|
window.navigator.clipboard
|
|
47
47
|
.readText()
|
|
48
48
|
.then((text) => {
|
|
@@ -66,7 +66,7 @@ function getClipboardText() {
|
|
|
66
66
|
function setClipboard(data) {
|
|
67
67
|
return new Promise((resolve, reject) => {
|
|
68
68
|
// 现代浏览器
|
|
69
|
-
if (window.navigator
|
|
69
|
+
if (window && window.navigator) {
|
|
70
70
|
let clipboardItem = null;
|
|
71
71
|
// 是文本类型
|
|
72
72
|
if (typeof data === "string") {
|
|
@@ -99,7 +99,7 @@ function setClipboard(data) {
|
|
|
99
99
|
function setClipboardText(text) {
|
|
100
100
|
return new Promise((resolve, reject) => {
|
|
101
101
|
// 现代浏览器
|
|
102
|
-
if (window.navigator
|
|
102
|
+
if (window && window.navigator) {
|
|
103
103
|
window.navigator.clipboard.writeText(text).then((success) => {
|
|
104
104
|
resolve(true);
|
|
105
105
|
}, (failed) => {
|
package/dist/es/color/index.mjs
CHANGED
|
@@ -77,7 +77,7 @@ function hexToRgb(hex) {
|
|
|
77
77
|
});
|
|
78
78
|
const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexStr);
|
|
79
79
|
if (!rgb) {
|
|
80
|
-
throw new
|
|
80
|
+
throw new TypeError(`hexToRgb: invalid hex color: ${hex}`);
|
|
81
81
|
}
|
|
82
82
|
color.r = parseInt(rgb[1], 16);
|
|
83
83
|
color.g = parseInt(rgb[2], 16);
|
|
@@ -98,7 +98,7 @@ function hexToRgba(hex, opacity = 1) {
|
|
|
98
98
|
});
|
|
99
99
|
const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexStr);
|
|
100
100
|
if (!rgb) {
|
|
101
|
-
throw new
|
|
101
|
+
throw new TypeError(`hexToRgba: invalid hex color: ${hex}`);
|
|
102
102
|
}
|
|
103
103
|
color.r = parseInt(rgb[1], 16);
|
|
104
104
|
color.g = parseInt(rgb[2], 16);
|
|
@@ -119,7 +119,7 @@ function hexToHsl(hex) {
|
|
|
119
119
|
});
|
|
120
120
|
const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexStr);
|
|
121
121
|
if (!rgb) {
|
|
122
|
-
throw new
|
|
122
|
+
throw new TypeError(`hexToHsl: invalid hex color: ${hex}`);
|
|
123
123
|
}
|
|
124
124
|
// 再组装为hsl格式
|
|
125
125
|
const r = parseInt(rgb[1], 16) / 255;
|
|
@@ -394,7 +394,7 @@ else {
|
|
|
394
394
|
*/
|
|
395
395
|
const fromUint8Array = function (uint8Array, urlSafe = false) {
|
|
396
396
|
if (!(uint8Array instanceof Uint8Array)) {
|
|
397
|
-
throw new TypeError("fromUint8Array
|
|
397
|
+
throw new TypeError("fromUint8Array: input must be Uint8Array");
|
|
398
398
|
}
|
|
399
399
|
const bytes = Array.from(uint8Array);
|
|
400
400
|
let result = bytesToBase64Chars(bytes);
|
|
@@ -411,7 +411,7 @@ const fromUint8Array = function (uint8Array, urlSafe = false) {
|
|
|
411
411
|
*/
|
|
412
412
|
const toUint8Array = function (base64Str, urlSafe = false) {
|
|
413
413
|
if (typeof base64Str !== "string") {
|
|
414
|
-
throw new TypeError("toUint8Array
|
|
414
|
+
throw new TypeError("toUint8Array: input must be string");
|
|
415
415
|
}
|
|
416
416
|
// 处理URL安全字符
|
|
417
417
|
if (urlSafe) {
|
|
@@ -428,7 +428,7 @@ const toUint8Array = function (base64Str, urlSafe = false) {
|
|
|
428
428
|
*/
|
|
429
429
|
const fromHex = function (hexStr, urlSafe = false) {
|
|
430
430
|
if (typeof hexStr !== "string" || !/^[0-9a-fA-F]+$/.test(hexStr)) {
|
|
431
|
-
throw new TypeError("fromHex
|
|
431
|
+
throw new TypeError("fromHex: input must be hex string");
|
|
432
432
|
}
|
|
433
433
|
// 补全偶数长度
|
|
434
434
|
const str = hexStr.length % 2 ? `0${hexStr}` : hexStr;
|
|
@@ -450,7 +450,7 @@ const fromHex = function (hexStr, urlSafe = false) {
|
|
|
450
450
|
*/
|
|
451
451
|
const toHex = function (base64Str, urlSafe = false) {
|
|
452
452
|
if (typeof base64Str !== "string") {
|
|
453
|
-
throw new TypeError("toHex
|
|
453
|
+
throw new TypeError("toHex: input must be string");
|
|
454
454
|
}
|
|
455
455
|
// 处理URL安全字符
|
|
456
456
|
if (urlSafe) {
|
|
@@ -24,7 +24,7 @@ function md5Raw(str) {
|
|
|
24
24
|
*/
|
|
25
25
|
function md5Hmac(str, key) {
|
|
26
26
|
if (!str || !key) {
|
|
27
|
-
throw new
|
|
27
|
+
throw new TypeError("md5Hmac: missing str or key");
|
|
28
28
|
}
|
|
29
29
|
return md5$1(str, key, false);
|
|
30
30
|
}
|
|
@@ -36,7 +36,7 @@ function md5Hmac(str, key) {
|
|
|
36
36
|
*/
|
|
37
37
|
function md5HmacRaw(str, key) {
|
|
38
38
|
if (!str || !key) {
|
|
39
|
-
throw new
|
|
39
|
+
throw new TypeError("md5HmacRaw: missing str or key");
|
|
40
40
|
}
|
|
41
41
|
return md5$1(str, key, true);
|
|
42
42
|
}
|
|
@@ -25,7 +25,7 @@ function sha256Raw(str) {
|
|
|
25
25
|
*/
|
|
26
26
|
function sha256Hmac(str, key) {
|
|
27
27
|
if (!str || !key) {
|
|
28
|
-
throw new
|
|
28
|
+
throw new TypeError("sha256Hmac: missing str or key");
|
|
29
29
|
}
|
|
30
30
|
return sha256_hmac(key, str);
|
|
31
31
|
}
|
|
@@ -37,7 +37,7 @@ function sha256Hmac(str, key) {
|
|
|
37
37
|
*/
|
|
38
38
|
function sha256HmacRaw(str, key) {
|
|
39
39
|
if (!str || !key) {
|
|
40
|
-
throw new
|
|
40
|
+
throw new TypeError("sha256HmacRaw: missing str or key");
|
|
41
41
|
}
|
|
42
42
|
return sha256_hmac_raw(key, str);
|
|
43
43
|
}
|
|
@@ -66,7 +66,7 @@ function sha224Raw(str) {
|
|
|
66
66
|
*/
|
|
67
67
|
function sha224Hmac(str, key) {
|
|
68
68
|
if (!str || !key) {
|
|
69
|
-
throw new
|
|
69
|
+
throw new TypeError("sha224Hmac: missing str or key");
|
|
70
70
|
}
|
|
71
71
|
return sha224_hmac(key, str);
|
|
72
72
|
}
|
|
@@ -78,7 +78,7 @@ function sha224Hmac(str, key) {
|
|
|
78
78
|
*/
|
|
79
79
|
function sha224HmacRaw(str, key) {
|
|
80
80
|
if (!str || !key) {
|
|
81
|
-
throw new
|
|
81
|
+
throw new TypeError("sha224HmacRaw: missing str or key");
|
|
82
82
|
}
|
|
83
83
|
return sha224_hmac_raw(key, str);
|
|
84
84
|
}
|
|
@@ -44,15 +44,15 @@ function _validateSM4Options(options = {}, operation) {
|
|
|
44
44
|
// 1. 校验模式是否合法
|
|
45
45
|
const validModes = Object.values(MODE);
|
|
46
46
|
if (!validModes.includes(mode)) {
|
|
47
|
-
throw new
|
|
47
|
+
throw new TypeError(`sm4${operation}:unsupported encryption mode "${mode}", supports only ${validModes.join("/")}`);
|
|
48
48
|
}
|
|
49
49
|
// 2. CBC模式必须传IV
|
|
50
50
|
if (mode === MODE.CBC && !iv) {
|
|
51
|
-
throw new
|
|
51
|
+
throw new TypeError(`sm4${operation}:the CBC mode must be inputted with an IV (initialization vector)`);
|
|
52
52
|
}
|
|
53
53
|
// 3. ECB模式禁止传IV(避免误用)
|
|
54
54
|
if (mode === MODE.ECB && iv !== undefined) {
|
|
55
|
-
throw new
|
|
55
|
+
throw new TypeError(`sm4${operation}: the ECB mode does not require an IV to be passed in. Please do not pass the iv parameter`);
|
|
56
56
|
}
|
|
57
57
|
// 4. 校验IV长度(如果传了IV)
|
|
58
58
|
if (iv) {
|
|
@@ -74,12 +74,12 @@ function _validateSM4Options(options = {}, operation) {
|
|
|
74
74
|
ivLength = 0;
|
|
75
75
|
}
|
|
76
76
|
if (ivLength !== 16) {
|
|
77
|
-
throw new
|
|
77
|
+
throw new TypeError(`sm4${operation}:IV must be 16 bytes in length, and the current length is ${ivLength}`);
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
// 5. 校验填充模式(仅允许pkcs#7)
|
|
81
81
|
if (options.padding && options.padding !== String(PADDING)) {
|
|
82
|
-
throw new
|
|
82
|
+
throw new TypeError(`sm4${operation}: only pkcs#7 padding mode is supported, currently being input ${String(options.padding)}`);
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
// sm4的配置
|
|
@@ -48,7 +48,7 @@ const FK = new Uint32Array([0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc]);
|
|
|
48
48
|
function hexToUint8Array(str) {
|
|
49
49
|
str = str.replace(/\s+/g, ""); // 去除空格
|
|
50
50
|
if (str.length % 2 !== 0)
|
|
51
|
-
throw new
|
|
51
|
+
throw new TypeError("hexToUint8Array: hex string length must be even from sm4");
|
|
52
52
|
const arr = new Uint8Array(str.length / 2);
|
|
53
53
|
for (let i = 0; i < str.length; i += 2) {
|
|
54
54
|
arr[i / 2] = parseInt(str.substr(i, 2), 16) & 0xff;
|
|
@@ -110,7 +110,7 @@ function normalizeInput(input, cryptFlag) {
|
|
|
110
110
|
return hexToUint8Array(input);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
throw new
|
|
113
|
+
throw new TypeError(`normalizeInput: unsupported input type: ${typeof input} from sm4`);
|
|
114
114
|
}
|
|
115
115
|
/**
|
|
116
116
|
* 标准化密钥/IV为Uint8Array
|
|
@@ -134,10 +134,10 @@ function normalizeKeyIv(keyOrIv, expectedLength, name) {
|
|
|
134
134
|
arr = new Uint8Array(keyOrIv);
|
|
135
135
|
}
|
|
136
136
|
else {
|
|
137
|
-
throw new
|
|
137
|
+
throw new TypeError(`normalizeKeyIv: unsupported ${name} type: ${typeof keyOrIv} from sm4`);
|
|
138
138
|
}
|
|
139
139
|
if (arr.length !== expectedLength) {
|
|
140
|
-
throw new
|
|
140
|
+
throw new TypeError(`normalizeKeyIv: ${name} must be ${expectedLength * 8} bits (${expectedLength} bytes) from sm4`);
|
|
141
141
|
}
|
|
142
142
|
return arr;
|
|
143
143
|
}
|
|
@@ -189,7 +189,7 @@ function linearTransformKey(b) {
|
|
|
189
189
|
function sm4BlockCrypt(inputBlock, outputBlock, roundKeys) {
|
|
190
190
|
// 确保输入块是16字节
|
|
191
191
|
if (inputBlock.length !== BLOCK_SIZE) {
|
|
192
|
-
throw new
|
|
192
|
+
throw new TypeError(`sm4BlockCrypt: input block must be ${BLOCK_SIZE} bytes for block crypt from sm4`);
|
|
193
193
|
}
|
|
194
194
|
// 字节数组转32比特字数组(4个字,共128比特)
|
|
195
195
|
const x = new Uint32Array(4);
|
|
@@ -284,7 +284,7 @@ function pkcs7Unpad(data) {
|
|
|
284
284
|
// 验证填充合法性
|
|
285
285
|
for (let i = 1; i <= paddingCount; i++) {
|
|
286
286
|
if (data[data.length - i] !== paddingCount) {
|
|
287
|
-
throw new
|
|
287
|
+
throw new TypeError("pkcs7Unpad: invalid PKCS#7 padding from sm4");
|
|
288
288
|
}
|
|
289
289
|
}
|
|
290
290
|
return data.subarray(0, data.length - paddingCount);
|
|
@@ -307,7 +307,7 @@ function sm4Core(inputData, key, cryptFlag, options = {}) {
|
|
|
307
307
|
} = options;
|
|
308
308
|
// 校验模式
|
|
309
309
|
if (![SM4_MODE_ECB, SM4_MODE_CBC].includes(mode)) {
|
|
310
|
-
throw new
|
|
310
|
+
throw new TypeError(`sm4Core: unsupported mode: ${mode}, only 'ecb' and 'cbc' are supported from sm4`);
|
|
311
311
|
}
|
|
312
312
|
// 标准化输入、密钥、IV
|
|
313
313
|
const input = normalizeInput(inputData, cryptFlag);
|
|
@@ -322,7 +322,7 @@ function sm4Core(inputData, key, cryptFlag, options = {}) {
|
|
|
322
322
|
processedInput = pkcs7Pad(input);
|
|
323
323
|
// 确保填充后是16字节的倍数
|
|
324
324
|
if (processedInput.length % BLOCK_SIZE !== 0) {
|
|
325
|
-
throw new
|
|
325
|
+
throw new TypeError("sm4Core: PKCS7 padding failed: data length is not multiple of block size from sm4");
|
|
326
326
|
}
|
|
327
327
|
}
|
|
328
328
|
// 生成轮密钥
|
|
@@ -382,7 +382,7 @@ function sm4Core(inputData, key, cryptFlag, options = {}) {
|
|
|
382
382
|
case SM4_OUTPUT_ARRAYBUFFER:
|
|
383
383
|
return finalOutput.buffer;
|
|
384
384
|
default:
|
|
385
|
-
throw new
|
|
385
|
+
throw new TypeError(`sm4Core: unsupported output format: ${output} from sm4`);
|
|
386
386
|
}
|
|
387
387
|
}
|
|
388
388
|
// ========== 便捷API(函数式,符合设计准则) ==========
|
|
@@ -394,7 +394,7 @@ function sm4Core(inputData, key, cryptFlag, options = {}) {
|
|
|
394
394
|
function generateIv(outputFormat = SM4_OUTPUT_HEX) {
|
|
395
395
|
// 1. 校验浏览器是否支持 crypto
|
|
396
396
|
if (!window?.crypto?.getRandomValues) {
|
|
397
|
-
throw new
|
|
397
|
+
throw new TypeError("generateIv: your browser does not support secure random generation (crypto API) from sm4");
|
|
398
398
|
}
|
|
399
399
|
// 2. 生成 16 字节安全随机数
|
|
400
400
|
const ivUint8 = new Uint8Array(IV_SIZE);
|
|
@@ -425,7 +425,7 @@ function generateIv(outputFormat = SM4_OUTPUT_HEX) {
|
|
|
425
425
|
function generateKey(outputFormat = SM4_OUTPUT_HEX) {
|
|
426
426
|
// 校验浏览器/Node环境的安全随机数API
|
|
427
427
|
if (!window?.crypto?.getRandomValues && !global?.crypto?.getRandomValues) {
|
|
428
|
-
throw new
|
|
428
|
+
throw new TypeError("generateKey: the current environment does not support secure random number generation. please upgrade your browser/Node.js from sm4");
|
|
429
429
|
}
|
|
430
430
|
// 生成16字节随机数(SM4密钥固定16字节)
|
|
431
431
|
const cryptoObj = window?.crypto || global?.crypto;
|
package/dist/es/date/index.mjs
CHANGED
|
@@ -1117,7 +1117,7 @@ function toDateUTCString(date = new Date(), options = { format: "yyyy-MM-dd HH:m
|
|
|
1117
1117
|
function toDate(value) {
|
|
1118
1118
|
// 为空抛出异常
|
|
1119
1119
|
if (isNull(value)) {
|
|
1120
|
-
throw new TypeError("value cannot be null or undefined");
|
|
1120
|
+
throw new TypeError("toDate: value cannot be null or undefined");
|
|
1121
1121
|
}
|
|
1122
1122
|
// 是日期字符串
|
|
1123
1123
|
if (isString(value)) {
|
|
@@ -1133,7 +1133,7 @@ function toDate(value) {
|
|
|
1133
1133
|
}
|
|
1134
1134
|
// 不支持的日期格式
|
|
1135
1135
|
else {
|
|
1136
|
-
throw new TypeError(`invalid input type: ${typeof value}, expected string or number`);
|
|
1136
|
+
throw new TypeError(`toDate: invalid input type: ${typeof value}, expected string or number`);
|
|
1137
1137
|
}
|
|
1138
1138
|
}
|
|
1139
1139
|
/**
|
|
@@ -1147,11 +1147,11 @@ function toDateString(date, options = { format: "yyyy-MM-dd", lang: "zh" }) {
|
|
|
1147
1147
|
const { format = "yyyy-MM-dd", lang = "zh" } = options;
|
|
1148
1148
|
// 为空抛出异常
|
|
1149
1149
|
if (isNull(date)) {
|
|
1150
|
-
throw new TypeError("date cannot be null or undefined");
|
|
1150
|
+
throw new TypeError("toDateString: date cannot be null or undefined");
|
|
1151
1151
|
}
|
|
1152
1152
|
// 判断是否是日期对象
|
|
1153
1153
|
if (!isDate(date)) {
|
|
1154
|
-
throw new TypeError(`invalid input type: ${typeof date}, expected Date object`);
|
|
1154
|
+
throw new TypeError(`toDateString: invalid input type: ${typeof date}, expected Date object`);
|
|
1155
1155
|
}
|
|
1156
1156
|
// 日期转化替换
|
|
1157
1157
|
const replaceRules = {
|
package/dist/es/device/index.mjs
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* @returns {Object} 返回浏览器信息
|
|
9
9
|
*/
|
|
10
10
|
function getBrowserInfo() {
|
|
11
|
-
const ua = window.navigator.userAgent.toLowerCase();
|
|
11
|
+
const ua = window && window.navigator.userAgent.toLowerCase();
|
|
12
12
|
// ie
|
|
13
13
|
const ie = ua.match(/rv:([\d.]+)\) like gecko/) || ua.match(/msie ([\d\.]+)/);
|
|
14
14
|
// edge
|
|
@@ -51,7 +51,7 @@ function isPc() {
|
|
|
51
51
|
* @returns {boolean} 返回true和false
|
|
52
52
|
*/
|
|
53
53
|
function isPhone() {
|
|
54
|
-
const ua = window.navigator.userAgent;
|
|
54
|
+
const ua = window && window.navigator.userAgent;
|
|
55
55
|
return /Android|webOS|iPhone|iPod|BlackBerry|Windows Phone|IEMobile/i.test(ua);
|
|
56
56
|
}
|
|
57
57
|
/* 操作系统类型 */
|
|
@@ -60,7 +60,7 @@ function isPhone() {
|
|
|
60
60
|
* @returns {boolean} 返回true和false
|
|
61
61
|
*/
|
|
62
62
|
function isAndroid() {
|
|
63
|
-
const ua = window.navigator.userAgent;
|
|
63
|
+
const ua = window && window.navigator.userAgent;
|
|
64
64
|
return /Android|BlackBerry/i.test(ua);
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
@@ -68,7 +68,7 @@ function isAndroid() {
|
|
|
68
68
|
* @returns {boolean} 返回true和false
|
|
69
69
|
*/
|
|
70
70
|
function isIos() {
|
|
71
|
-
const ua = window.navigator.userAgent;
|
|
71
|
+
const ua = window && window.navigator.userAgent;
|
|
72
72
|
return /iPhone|iPad|iPod|iOS/i.test(ua);
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
@@ -76,7 +76,7 @@ function isIos() {
|
|
|
76
76
|
* @returns {boolean} 返回true和false
|
|
77
77
|
*/
|
|
78
78
|
function isWindowsPhone() {
|
|
79
|
-
const ua = window.navigator.userAgent;
|
|
79
|
+
const ua = window && window.navigator.userAgent;
|
|
80
80
|
return /Windows Phone/i.test(ua);
|
|
81
81
|
}
|
|
82
82
|
/**
|
|
@@ -84,7 +84,7 @@ function isWindowsPhone() {
|
|
|
84
84
|
* @returns {boolean} 返回true和false
|
|
85
85
|
*/
|
|
86
86
|
function isWindows() {
|
|
87
|
-
const ua = window.navigator.userAgent;
|
|
87
|
+
const ua = window && window.navigator.userAgent;
|
|
88
88
|
return /win/i.test(ua);
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
@@ -92,7 +92,7 @@ function isWindows() {
|
|
|
92
92
|
* @returns {boolean} 返回true和false
|
|
93
93
|
*/
|
|
94
94
|
function isLinux() {
|
|
95
|
-
const ua = window.navigator.userAgent;
|
|
95
|
+
const ua = window && window.navigator.userAgent;
|
|
96
96
|
return /linux/i.test(ua);
|
|
97
97
|
}
|
|
98
98
|
/**
|
|
@@ -100,7 +100,7 @@ function isLinux() {
|
|
|
100
100
|
* @returns {boolean} 返回true和false
|
|
101
101
|
*/
|
|
102
102
|
function isMac() {
|
|
103
|
-
const ua = window.navigator.userAgent;
|
|
103
|
+
const ua = window && window.navigator.userAgent;
|
|
104
104
|
return /mac/i.test(ua);
|
|
105
105
|
}
|
|
106
106
|
/* 苹果设备类型 */
|
|
@@ -109,7 +109,7 @@ function isMac() {
|
|
|
109
109
|
*@returns {boolean} 返回true和false
|
|
110
110
|
*/
|
|
111
111
|
function isIphone() {
|
|
112
|
-
const ua = window.navigator.userAgent;
|
|
112
|
+
const ua = window && window.navigator.userAgent;
|
|
113
113
|
return /iPhone/i.test(ua);
|
|
114
114
|
}
|
|
115
115
|
/**
|
|
@@ -117,7 +117,7 @@ function isIphone() {
|
|
|
117
117
|
*@return {boolean} 返回true和false
|
|
118
118
|
*/
|
|
119
119
|
function isIpad() {
|
|
120
|
-
const ua = window.navigator.userAgent;
|
|
120
|
+
const ua = window && window.navigator.userAgent;
|
|
121
121
|
return /iPod/i.test(ua);
|
|
122
122
|
}
|
|
123
123
|
/* 手机浏览器类型 */
|
|
@@ -126,7 +126,7 @@ function isIpad() {
|
|
|
126
126
|
* @returns {boolean} 返回true和false
|
|
127
127
|
*/
|
|
128
128
|
function isWeixin() {
|
|
129
|
-
const ua = window.navigator.userAgent;
|
|
129
|
+
const ua = window && window.navigator.userAgent;
|
|
130
130
|
return /MicroMessenger/i.test(ua);
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
@@ -134,7 +134,7 @@ function isWeixin() {
|
|
|
134
134
|
* @returns {boolean} 返回true和false
|
|
135
135
|
*/
|
|
136
136
|
function isQQ() {
|
|
137
|
-
const ua = window.navigator.userAgent;
|
|
137
|
+
const ua = window && window.navigator.userAgent;
|
|
138
138
|
return /QQ/i.test(ua);
|
|
139
139
|
}
|
|
140
140
|
|
package/dist/es/file/index.mjs
CHANGED
|
@@ -118,15 +118,16 @@ function fileToUrl(file) {
|
|
|
118
118
|
function urlToFile(url) {
|
|
119
119
|
return new Promise((resolve, reject) => {
|
|
120
120
|
try {
|
|
121
|
-
window
|
|
122
|
-
|
|
123
|
-
res.
|
|
124
|
-
|
|
125
|
-
|
|
121
|
+
window &&
|
|
122
|
+
window.fetch(url).then((res) => {
|
|
123
|
+
if (res.status === 200) {
|
|
124
|
+
res.blob().then((blob) => {
|
|
125
|
+
blobToFile(blob).then((file) => {
|
|
126
|
+
resolve(file);
|
|
127
|
+
});
|
|
126
128
|
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
129
|
+
}
|
|
130
|
+
});
|
|
130
131
|
}
|
|
131
132
|
catch (err) {
|
|
132
133
|
console.error(err);
|
package/dist/es/math/index.mjs
CHANGED
|
@@ -162,7 +162,7 @@ function toFixed(num, decimals = 2, mode = MATH.ROUND) {
|
|
|
162
162
|
return _toFixedFloor(num, decimals);
|
|
163
163
|
}
|
|
164
164
|
else {
|
|
165
|
-
throw new
|
|
165
|
+
throw new TypeError("toFixed: mode is MATH.ROUND 0 or MATH.ROUND_FLOOR");
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
@@ -183,7 +183,7 @@ function toDecimal(num, decimals = 2, mode = MATH.ROUND) {
|
|
|
183
183
|
}
|
|
184
184
|
// 错误保留格式
|
|
185
185
|
else {
|
|
186
|
-
throw new
|
|
186
|
+
throw new TypeError("toDecimal: mode is MATH.ROUND 0 or MATH.ROUND_FLOOR");
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
189
|
/* 内部函数 */
|
|
@@ -246,11 +246,11 @@ function _toFixedFloor(num, decimals = 2) {
|
|
|
246
246
|
// num可能是字符串,先转数字再判断NaN
|
|
247
247
|
const numVal = Number(num);
|
|
248
248
|
if (isNaN(numVal)) {
|
|
249
|
-
throw new
|
|
249
|
+
throw new TypeError(`${num} is NaN`);
|
|
250
250
|
}
|
|
251
251
|
// 校验小数位数范围
|
|
252
252
|
if (decimals < 0 || decimals > 20) {
|
|
253
|
-
throw new
|
|
253
|
+
throw new TypeError("_toFixedFloor: decimals places must be between 0 and 20");
|
|
254
254
|
}
|
|
255
255
|
// 默认为保留的小数点后两位
|
|
256
256
|
const dec = Math.max(0, Math.floor(decimals)); // 确保小数位数为非负整数
|
|
@@ -289,7 +289,7 @@ function _toFixedFloor(num, decimals = 2) {
|
|
|
289
289
|
*/
|
|
290
290
|
function _toDecimalRound(num, decimals = 2) {
|
|
291
291
|
if (isNaN(Number(num))) {
|
|
292
|
-
throw new
|
|
292
|
+
throw new TypeError(`_toDecimalRound: ${num} is not a number`);
|
|
293
293
|
}
|
|
294
294
|
const n = Math.pow(10, decimals);
|
|
295
295
|
return Math.round(Number(num) * n) / n;
|
|
@@ -302,7 +302,7 @@ function _toDecimalRound(num, decimals = 2) {
|
|
|
302
302
|
*/
|
|
303
303
|
function _toDecimalFloor(num, decimals = 2) {
|
|
304
304
|
if (isNaN(Number(num))) {
|
|
305
|
-
throw new
|
|
305
|
+
throw new TypeError(`_toDecimalFloor: ${num} is not a number`);
|
|
306
306
|
}
|
|
307
307
|
const n = Math.pow(10, decimals);
|
|
308
308
|
return Math.floor(Number(num) * n) / n;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @returns {string} 返回数据
|
|
6
6
|
*/
|
|
7
7
|
function getLocalStorage(key) {
|
|
8
|
-
return window.localStorage.getItem(key) || undefined;
|
|
8
|
+
return (window && window.localStorage.getItem(key)) || undefined;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* 设置localStorage缓存数据
|
|
@@ -13,20 +13,20 @@ function getLocalStorage(key) {
|
|
|
13
13
|
* @param {string} value value值
|
|
14
14
|
*/
|
|
15
15
|
function setLocalStorage(key, value) {
|
|
16
|
-
window.localStorage.setItem(key, value);
|
|
16
|
+
window && window.localStorage.setItem(key, value);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* 通过key从localStorage缓存中删除数据
|
|
20
20
|
* @param {string} key key值
|
|
21
21
|
*/
|
|
22
22
|
function removeLocalStorage(key) {
|
|
23
|
-
window.localStorage.removeItem(key);
|
|
23
|
+
window && window.localStorage.removeItem(key);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* 清空localStorage缓存中所有数据
|
|
27
27
|
*/
|
|
28
28
|
function clearLocalStorage() {
|
|
29
|
-
window.localStorage.clear();
|
|
29
|
+
window && window.localStorage.clear();
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export { clearLocalStorage, getLocalStorage, removeLocalStorage, setLocalStorage };
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @returns {string} 返回数据
|
|
6
6
|
*/
|
|
7
7
|
function getSessionStorage(key) {
|
|
8
|
-
return window.sessionStorage.getItem(key) || undefined;
|
|
8
|
+
return (window && window.sessionStorage.getItem(key)) || undefined;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* 设置sessionStorage缓存数据
|
|
@@ -13,20 +13,20 @@ function getSessionStorage(key) {
|
|
|
13
13
|
* @param {string} value value值
|
|
14
14
|
*/
|
|
15
15
|
function setSessionStorage(key, value) {
|
|
16
|
-
window.sessionStorage.setItem(key, value);
|
|
16
|
+
window && window.sessionStorage.setItem(key, value);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* 通过key从sessionStorage缓存中删除数据
|
|
20
20
|
* @param {string} key key值
|
|
21
21
|
*/
|
|
22
22
|
function removeSessionStorage(key) {
|
|
23
|
-
window.sessionStorage.removeItem(key);
|
|
23
|
+
window && window.sessionStorage.removeItem(key);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* 清空sessionStorage缓存中所有数据
|
|
27
27
|
*/
|
|
28
28
|
function clearSessionStorage() {
|
|
29
|
-
window.sessionStorage.clear();
|
|
29
|
+
window && window.sessionStorage.clear();
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export { clearSessionStorage, getSessionStorage, removeSessionStorage, setSessionStorage };
|
package/dist/es/string/index.mjs
CHANGED
|
@@ -94,7 +94,7 @@ function toSnakeCase(value) {
|
|
|
94
94
|
}
|
|
95
95
|
// 不符合格式
|
|
96
96
|
else {
|
|
97
|
-
throw new TypeError(
|
|
97
|
+
throw new TypeError(`toSnakeCase: value should be a string`);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
@@ -119,7 +119,7 @@ function toKebabCase(value) {
|
|
|
119
119
|
}
|
|
120
120
|
// 不符合格式
|
|
121
121
|
else {
|
|
122
|
-
throw new TypeError("value should be a string");
|
|
122
|
+
throw new TypeError("toKebabCase: value should be a string");
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
/**
|
|
@@ -147,7 +147,7 @@ function toCamelCase(value) {
|
|
|
147
147
|
}
|
|
148
148
|
// 不符合格式
|
|
149
149
|
else {
|
|
150
|
-
throw new TypeError("value should be a string");
|
|
150
|
+
throw new TypeError("toCamelCase: value should be a string");
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
/**
|
|
@@ -177,7 +177,7 @@ function toPascalCase(value) {
|
|
|
177
177
|
}
|
|
178
178
|
// 不符合格式
|
|
179
179
|
else {
|
|
180
|
-
throw new TypeError("value should be a string");
|
|
180
|
+
throw new TypeError("toPascalCase: value should be a string");
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
/* 字符串格式化 */
|
|
@@ -191,10 +191,10 @@ function toPascalCase(value) {
|
|
|
191
191
|
function padZeroStart(value, maxLength = 2) {
|
|
192
192
|
value = String(value).trim();
|
|
193
193
|
if (maxLength < 0) {
|
|
194
|
-
throw new TypeError("maxLength should be greater than 0");
|
|
194
|
+
throw new TypeError("padZeroStart: maxLength should be greater than 0");
|
|
195
195
|
}
|
|
196
196
|
if (isNull(value) || isNaN(value)) {
|
|
197
|
-
throw new
|
|
197
|
+
throw new TypeError("padZeroStart: value must be a valid number or numeric string");
|
|
198
198
|
}
|
|
199
199
|
// 前面补0
|
|
200
200
|
let len = value.toString().length;
|
|
@@ -214,10 +214,10 @@ function padZeroStart(value, maxLength = 2) {
|
|
|
214
214
|
function padZeroEnd(value, maxLength = 2) {
|
|
215
215
|
value = String(value).trim();
|
|
216
216
|
if (maxLength < 0) {
|
|
217
|
-
throw new TypeError("maxLength should be greater than 0");
|
|
217
|
+
throw new TypeError("padZeroEnd: maxLength should be greater than 0");
|
|
218
218
|
}
|
|
219
219
|
if (isNull(value) || isNaN(value)) {
|
|
220
|
-
throw new
|
|
220
|
+
throw new TypeError("padZeroEnd: value must be a valid number or numeric string");
|
|
221
221
|
}
|
|
222
222
|
// 后面补0
|
|
223
223
|
let len = value.toString().length;
|
|
@@ -303,7 +303,7 @@ function formatRmbChinese(money) {
|
|
|
303
303
|
money = parseFloat(String(money));
|
|
304
304
|
if (money >= maxNum) {
|
|
305
305
|
// 超出最大处理数字,抛出异常
|
|
306
|
-
throw new
|
|
306
|
+
throw new TypeError("formatRmbChinese: calculated number overflow");
|
|
307
307
|
}
|
|
308
308
|
if (money === 0) {
|
|
309
309
|
chineseStr = cnNums[0] + cnIntLast + cnInteger;
|
|
@@ -221,7 +221,7 @@ function isNaN(value) {
|
|
|
221
221
|
typeof value === "boolean" || // 布尔值视为非数字
|
|
222
222
|
Array.isArray(value) || // 数组视为非数字
|
|
223
223
|
value === "" || // 空字符串视为非数字
|
|
224
|
-
window.isNaN(Number(value)) // 原生isNaN判断(转数字后是否为NaN)
|
|
224
|
+
(window && window.isNaN(Number(value))) // 原生isNaN判断(转数字后是否为NaN)
|
|
225
225
|
);
|
|
226
226
|
}
|
|
227
227
|
/**
|