@ivujs/i-utils 2.1.3 → 2.1.4
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/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/math/index.cjs +6 -6
- package/dist/cjs/string/index.cjs +9 -9
- 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/math/index.mjs +6 -6
- package/dist/es/string/index.mjs +9 -9
- package/dist/lib/index.full.umd.js +51 -51
- 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
package/dist/cjs/color/index.cjs
CHANGED
|
@@ -79,7 +79,7 @@ function hexToRgb(hex) {
|
|
|
79
79
|
});
|
|
80
80
|
const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexStr);
|
|
81
81
|
if (!rgb) {
|
|
82
|
-
throw new
|
|
82
|
+
throw new TypeError(`hexToRgb: invalid hex color: ${hex}`);
|
|
83
83
|
}
|
|
84
84
|
color.r = parseInt(rgb[1], 16);
|
|
85
85
|
color.g = parseInt(rgb[2], 16);
|
|
@@ -100,7 +100,7 @@ function hexToRgba(hex, opacity = 1) {
|
|
|
100
100
|
});
|
|
101
101
|
const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexStr);
|
|
102
102
|
if (!rgb) {
|
|
103
|
-
throw new
|
|
103
|
+
throw new TypeError(`hexToRgba: invalid hex color: ${hex}`);
|
|
104
104
|
}
|
|
105
105
|
color.r = parseInt(rgb[1], 16);
|
|
106
106
|
color.g = parseInt(rgb[2], 16);
|
|
@@ -121,7 +121,7 @@ function hexToHsl(hex) {
|
|
|
121
121
|
});
|
|
122
122
|
const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexStr);
|
|
123
123
|
if (!rgb) {
|
|
124
|
-
throw new
|
|
124
|
+
throw new TypeError(`hexToHsl: invalid hex color: ${hex}`);
|
|
125
125
|
}
|
|
126
126
|
// 再组装为hsl格式
|
|
127
127
|
const r = parseInt(rgb[1], 16) / 255;
|
|
@@ -396,7 +396,7 @@ else {
|
|
|
396
396
|
*/
|
|
397
397
|
const fromUint8Array = function (uint8Array, urlSafe = false) {
|
|
398
398
|
if (!(uint8Array instanceof Uint8Array)) {
|
|
399
|
-
throw new TypeError("fromUint8Array
|
|
399
|
+
throw new TypeError("fromUint8Array: input must be Uint8Array");
|
|
400
400
|
}
|
|
401
401
|
const bytes = Array.from(uint8Array);
|
|
402
402
|
let result = bytesToBase64Chars(bytes);
|
|
@@ -413,7 +413,7 @@ const fromUint8Array = function (uint8Array, urlSafe = false) {
|
|
|
413
413
|
*/
|
|
414
414
|
const toUint8Array = function (base64Str, urlSafe = false) {
|
|
415
415
|
if (typeof base64Str !== "string") {
|
|
416
|
-
throw new TypeError("toUint8Array
|
|
416
|
+
throw new TypeError("toUint8Array: input must be string");
|
|
417
417
|
}
|
|
418
418
|
// 处理URL安全字符
|
|
419
419
|
if (urlSafe) {
|
|
@@ -430,7 +430,7 @@ const toUint8Array = function (base64Str, urlSafe = false) {
|
|
|
430
430
|
*/
|
|
431
431
|
const fromHex = function (hexStr, urlSafe = false) {
|
|
432
432
|
if (typeof hexStr !== "string" || !/^[0-9a-fA-F]+$/.test(hexStr)) {
|
|
433
|
-
throw new TypeError("fromHex
|
|
433
|
+
throw new TypeError("fromHex: input must be hex string");
|
|
434
434
|
}
|
|
435
435
|
// 补全偶数长度
|
|
436
436
|
const str = hexStr.length % 2 ? `0${hexStr}` : hexStr;
|
|
@@ -452,7 +452,7 @@ const fromHex = function (hexStr, urlSafe = false) {
|
|
|
452
452
|
*/
|
|
453
453
|
const toHex = function (base64Str, urlSafe = false) {
|
|
454
454
|
if (typeof base64Str !== "string") {
|
|
455
|
-
throw new TypeError("toHex
|
|
455
|
+
throw new TypeError("toHex: input must be string");
|
|
456
456
|
}
|
|
457
457
|
// 处理URL安全字符
|
|
458
458
|
if (urlSafe) {
|
|
@@ -26,7 +26,7 @@ function md5Raw(str) {
|
|
|
26
26
|
*/
|
|
27
27
|
function md5Hmac(str, key) {
|
|
28
28
|
if (!str || !key) {
|
|
29
|
-
throw new
|
|
29
|
+
throw new TypeError("md5Hmac: missing str or key");
|
|
30
30
|
}
|
|
31
31
|
return md5$1.md5(str, key, false);
|
|
32
32
|
}
|
|
@@ -38,7 +38,7 @@ function md5Hmac(str, key) {
|
|
|
38
38
|
*/
|
|
39
39
|
function md5HmacRaw(str, key) {
|
|
40
40
|
if (!str || !key) {
|
|
41
|
-
throw new
|
|
41
|
+
throw new TypeError("md5HmacRaw: missing str or key");
|
|
42
42
|
}
|
|
43
43
|
return md5$1.md5(str, key, true);
|
|
44
44
|
}
|
|
@@ -27,7 +27,7 @@ function sha256Raw(str) {
|
|
|
27
27
|
*/
|
|
28
28
|
function sha256Hmac(str, key) {
|
|
29
29
|
if (!str || !key) {
|
|
30
|
-
throw new
|
|
30
|
+
throw new TypeError("sha256Hmac: missing str or key");
|
|
31
31
|
}
|
|
32
32
|
return sha256$1.sha256_hmac(key, str);
|
|
33
33
|
}
|
|
@@ -39,7 +39,7 @@ function sha256Hmac(str, key) {
|
|
|
39
39
|
*/
|
|
40
40
|
function sha256HmacRaw(str, key) {
|
|
41
41
|
if (!str || !key) {
|
|
42
|
-
throw new
|
|
42
|
+
throw new TypeError("sha256HmacRaw: missing str or key");
|
|
43
43
|
}
|
|
44
44
|
return sha256$1.sha256_hmac_raw(key, str);
|
|
45
45
|
}
|
|
@@ -68,7 +68,7 @@ function sha224Raw(str) {
|
|
|
68
68
|
*/
|
|
69
69
|
function sha224Hmac(str, key) {
|
|
70
70
|
if (!str || !key) {
|
|
71
|
-
throw new
|
|
71
|
+
throw new TypeError("sha224Hmac: missing str or key");
|
|
72
72
|
}
|
|
73
73
|
return sha256$1.sha224_hmac(key, str);
|
|
74
74
|
}
|
|
@@ -80,7 +80,7 @@ function sha224Hmac(str, key) {
|
|
|
80
80
|
*/
|
|
81
81
|
function sha224HmacRaw(str, key) {
|
|
82
82
|
if (!str || !key) {
|
|
83
|
-
throw new
|
|
83
|
+
throw new TypeError("sha224HmacRaw: missing str or key");
|
|
84
84
|
}
|
|
85
85
|
return sha256$1.sha224_hmac_raw(key, str);
|
|
86
86
|
}
|
|
@@ -46,15 +46,15 @@ function _validateSM4Options(options = {}, operation) {
|
|
|
46
46
|
// 1. 校验模式是否合法
|
|
47
47
|
const validModes = Object.values(sm4.MODE);
|
|
48
48
|
if (!validModes.includes(mode)) {
|
|
49
|
-
throw new
|
|
49
|
+
throw new TypeError(`sm4${operation}:unsupported encryption mode "${mode}", supports only ${validModes.join("/")}`);
|
|
50
50
|
}
|
|
51
51
|
// 2. CBC模式必须传IV
|
|
52
52
|
if (mode === sm4.MODE.CBC && !iv) {
|
|
53
|
-
throw new
|
|
53
|
+
throw new TypeError(`sm4${operation}:the CBC mode must be inputted with an IV (initialization vector)`);
|
|
54
54
|
}
|
|
55
55
|
// 3. ECB模式禁止传IV(避免误用)
|
|
56
56
|
if (mode === sm4.MODE.ECB && iv !== undefined) {
|
|
57
|
-
throw new
|
|
57
|
+
throw new TypeError(`sm4${operation}: the ECB mode does not require an IV to be passed in. Please do not pass the iv parameter`);
|
|
58
58
|
}
|
|
59
59
|
// 4. 校验IV长度(如果传了IV)
|
|
60
60
|
if (iv) {
|
|
@@ -76,12 +76,12 @@ function _validateSM4Options(options = {}, operation) {
|
|
|
76
76
|
ivLength = 0;
|
|
77
77
|
}
|
|
78
78
|
if (ivLength !== 16) {
|
|
79
|
-
throw new
|
|
79
|
+
throw new TypeError(`sm4${operation}:IV must be 16 bytes in length, and the current length is ${ivLength}`);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
// 5. 校验填充模式(仅允许pkcs#7)
|
|
83
83
|
if (options.padding && options.padding !== String(sm4.PADDING)) {
|
|
84
|
-
throw new
|
|
84
|
+
throw new TypeError(`sm4${operation}: only pkcs#7 padding mode is supported, currently being input ${String(options.padding)}`);
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
// sm4的配置
|
|
@@ -50,7 +50,7 @@ const FK = new Uint32Array([0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc]);
|
|
|
50
50
|
function hexToUint8Array(str) {
|
|
51
51
|
str = str.replace(/\s+/g, ""); // 去除空格
|
|
52
52
|
if (str.length % 2 !== 0)
|
|
53
|
-
throw new
|
|
53
|
+
throw new TypeError("hexToUint8Array: hex string length must be even from sm4");
|
|
54
54
|
const arr = new Uint8Array(str.length / 2);
|
|
55
55
|
for (let i = 0; i < str.length; i += 2) {
|
|
56
56
|
arr[i / 2] = parseInt(str.substr(i, 2), 16) & 0xff;
|
|
@@ -112,7 +112,7 @@ function normalizeInput(input, cryptFlag) {
|
|
|
112
112
|
return hexToUint8Array(input);
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
|
-
throw new
|
|
115
|
+
throw new TypeError(`normalizeInput: unsupported input type: ${typeof input} from sm4`);
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
118
118
|
* 标准化密钥/IV为Uint8Array
|
|
@@ -136,10 +136,10 @@ function normalizeKeyIv(keyOrIv, expectedLength, name) {
|
|
|
136
136
|
arr = new Uint8Array(keyOrIv);
|
|
137
137
|
}
|
|
138
138
|
else {
|
|
139
|
-
throw new
|
|
139
|
+
throw new TypeError(`normalizeKeyIv: unsupported ${name} type: ${typeof keyOrIv} from sm4`);
|
|
140
140
|
}
|
|
141
141
|
if (arr.length !== expectedLength) {
|
|
142
|
-
throw new
|
|
142
|
+
throw new TypeError(`normalizeKeyIv: ${name} must be ${expectedLength * 8} bits (${expectedLength} bytes) from sm4`);
|
|
143
143
|
}
|
|
144
144
|
return arr;
|
|
145
145
|
}
|
|
@@ -191,7 +191,7 @@ function linearTransformKey(b) {
|
|
|
191
191
|
function sm4BlockCrypt(inputBlock, outputBlock, roundKeys) {
|
|
192
192
|
// 确保输入块是16字节
|
|
193
193
|
if (inputBlock.length !== BLOCK_SIZE) {
|
|
194
|
-
throw new
|
|
194
|
+
throw new TypeError(`sm4BlockCrypt: input block must be ${BLOCK_SIZE} bytes for block crypt from sm4`);
|
|
195
195
|
}
|
|
196
196
|
// 字节数组转32比特字数组(4个字,共128比特)
|
|
197
197
|
const x = new Uint32Array(4);
|
|
@@ -286,7 +286,7 @@ function pkcs7Unpad(data) {
|
|
|
286
286
|
// 验证填充合法性
|
|
287
287
|
for (let i = 1; i <= paddingCount; i++) {
|
|
288
288
|
if (data[data.length - i] !== paddingCount) {
|
|
289
|
-
throw new
|
|
289
|
+
throw new TypeError("pkcs7Unpad: invalid PKCS#7 padding from sm4");
|
|
290
290
|
}
|
|
291
291
|
}
|
|
292
292
|
return data.subarray(0, data.length - paddingCount);
|
|
@@ -309,7 +309,7 @@ function sm4Core(inputData, key, cryptFlag, options = {}) {
|
|
|
309
309
|
} = options;
|
|
310
310
|
// 校验模式
|
|
311
311
|
if (![SM4_MODE_ECB, SM4_MODE_CBC].includes(mode)) {
|
|
312
|
-
throw new
|
|
312
|
+
throw new TypeError(`sm4Core: unsupported mode: ${mode}, only 'ecb' and 'cbc' are supported from sm4`);
|
|
313
313
|
}
|
|
314
314
|
// 标准化输入、密钥、IV
|
|
315
315
|
const input = normalizeInput(inputData, cryptFlag);
|
|
@@ -324,7 +324,7 @@ function sm4Core(inputData, key, cryptFlag, options = {}) {
|
|
|
324
324
|
processedInput = pkcs7Pad(input);
|
|
325
325
|
// 确保填充后是16字节的倍数
|
|
326
326
|
if (processedInput.length % BLOCK_SIZE !== 0) {
|
|
327
|
-
throw new
|
|
327
|
+
throw new TypeError("sm4Core: PKCS7 padding failed: data length is not multiple of block size from sm4");
|
|
328
328
|
}
|
|
329
329
|
}
|
|
330
330
|
// 生成轮密钥
|
|
@@ -384,7 +384,7 @@ function sm4Core(inputData, key, cryptFlag, options = {}) {
|
|
|
384
384
|
case SM4_OUTPUT_ARRAYBUFFER:
|
|
385
385
|
return finalOutput.buffer;
|
|
386
386
|
default:
|
|
387
|
-
throw new
|
|
387
|
+
throw new TypeError(`sm4Core: unsupported output format: ${output} from sm4`);
|
|
388
388
|
}
|
|
389
389
|
}
|
|
390
390
|
// ========== 便捷API(函数式,符合设计准则) ==========
|
|
@@ -396,7 +396,7 @@ function sm4Core(inputData, key, cryptFlag, options = {}) {
|
|
|
396
396
|
function generateIv(outputFormat = SM4_OUTPUT_HEX) {
|
|
397
397
|
// 1. 校验浏览器是否支持 crypto
|
|
398
398
|
if (!window?.crypto?.getRandomValues) {
|
|
399
|
-
throw new
|
|
399
|
+
throw new TypeError("generateIv: your browser does not support secure random generation (crypto API) from sm4");
|
|
400
400
|
}
|
|
401
401
|
// 2. 生成 16 字节安全随机数
|
|
402
402
|
const ivUint8 = new Uint8Array(IV_SIZE);
|
|
@@ -427,7 +427,7 @@ function generateIv(outputFormat = SM4_OUTPUT_HEX) {
|
|
|
427
427
|
function generateKey(outputFormat = SM4_OUTPUT_HEX) {
|
|
428
428
|
// 校验浏览器/Node环境的安全随机数API
|
|
429
429
|
if (!window?.crypto?.getRandomValues && !global?.crypto?.getRandomValues) {
|
|
430
|
-
throw new
|
|
430
|
+
throw new TypeError("generateKey: the current environment does not support secure random number generation. please upgrade your browser/Node.js from sm4");
|
|
431
431
|
}
|
|
432
432
|
// 生成16字节随机数(SM4密钥固定16字节)
|
|
433
433
|
const cryptoObj = window?.crypto || global?.crypto;
|
package/dist/cjs/date/index.cjs
CHANGED
|
@@ -1119,7 +1119,7 @@ function toDateUTCString(date = new Date(), options = { format: "yyyy-MM-dd HH:m
|
|
|
1119
1119
|
function toDate(value) {
|
|
1120
1120
|
// 为空抛出异常
|
|
1121
1121
|
if (index$1.isNull(value)) {
|
|
1122
|
-
throw new TypeError("value cannot be null or undefined");
|
|
1122
|
+
throw new TypeError("toDate: value cannot be null or undefined");
|
|
1123
1123
|
}
|
|
1124
1124
|
// 是日期字符串
|
|
1125
1125
|
if (index$1.isString(value)) {
|
|
@@ -1135,7 +1135,7 @@ function toDate(value) {
|
|
|
1135
1135
|
}
|
|
1136
1136
|
// 不支持的日期格式
|
|
1137
1137
|
else {
|
|
1138
|
-
throw new TypeError(`invalid input type: ${typeof value}, expected string or number`);
|
|
1138
|
+
throw new TypeError(`toDate: invalid input type: ${typeof value}, expected string or number`);
|
|
1139
1139
|
}
|
|
1140
1140
|
}
|
|
1141
1141
|
/**
|
|
@@ -1149,11 +1149,11 @@ function toDateString(date$1, options = { format: "yyyy-MM-dd", lang: "zh" }) {
|
|
|
1149
1149
|
const { format = "yyyy-MM-dd", lang = "zh" } = options;
|
|
1150
1150
|
// 为空抛出异常
|
|
1151
1151
|
if (index$1.isNull(date$1)) {
|
|
1152
|
-
throw new TypeError("date cannot be null or undefined");
|
|
1152
|
+
throw new TypeError("toDateString: date cannot be null or undefined");
|
|
1153
1153
|
}
|
|
1154
1154
|
// 判断是否是日期对象
|
|
1155
1155
|
if (!index$1.isDate(date$1)) {
|
|
1156
|
-
throw new TypeError(`invalid input type: ${typeof date$1}, expected Date object`);
|
|
1156
|
+
throw new TypeError(`toDateString: invalid input type: ${typeof date$1}, expected Date object`);
|
|
1157
1157
|
}
|
|
1158
1158
|
// 日期转化替换
|
|
1159
1159
|
const replaceRules = {
|
package/dist/cjs/math/index.cjs
CHANGED
|
@@ -164,7 +164,7 @@ function toFixed(num, decimals = 2, mode = math.MATH.ROUND) {
|
|
|
164
164
|
return _toFixedFloor(num, decimals);
|
|
165
165
|
}
|
|
166
166
|
else {
|
|
167
|
-
throw new
|
|
167
|
+
throw new TypeError("toFixed: mode is MATH.ROUND 0 or MATH.ROUND_FLOOR");
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
/**
|
|
@@ -185,7 +185,7 @@ function toDecimal(num, decimals = 2, mode = math.MATH.ROUND) {
|
|
|
185
185
|
}
|
|
186
186
|
// 错误保留格式
|
|
187
187
|
else {
|
|
188
|
-
throw new
|
|
188
|
+
throw new TypeError("toDecimal: mode is MATH.ROUND 0 or MATH.ROUND_FLOOR");
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
191
|
/* 内部函数 */
|
|
@@ -248,11 +248,11 @@ function _toFixedFloor(num, decimals = 2) {
|
|
|
248
248
|
// num可能是字符串,先转数字再判断NaN
|
|
249
249
|
const numVal = Number(num);
|
|
250
250
|
if (index.isNaN(numVal)) {
|
|
251
|
-
throw new
|
|
251
|
+
throw new TypeError(`${num} is NaN`);
|
|
252
252
|
}
|
|
253
253
|
// 校验小数位数范围
|
|
254
254
|
if (decimals < 0 || decimals > 20) {
|
|
255
|
-
throw new
|
|
255
|
+
throw new TypeError("_toFixedFloor: decimals places must be between 0 and 20");
|
|
256
256
|
}
|
|
257
257
|
// 默认为保留的小数点后两位
|
|
258
258
|
const dec = Math.max(0, Math.floor(decimals)); // 确保小数位数为非负整数
|
|
@@ -291,7 +291,7 @@ function _toFixedFloor(num, decimals = 2) {
|
|
|
291
291
|
*/
|
|
292
292
|
function _toDecimalRound(num, decimals = 2) {
|
|
293
293
|
if (index.isNaN(Number(num))) {
|
|
294
|
-
throw new
|
|
294
|
+
throw new TypeError(`_toDecimalRound: ${num} is not a number`);
|
|
295
295
|
}
|
|
296
296
|
const n = Math.pow(10, decimals);
|
|
297
297
|
return Math.round(Number(num) * n) / n;
|
|
@@ -304,7 +304,7 @@ function _toDecimalRound(num, decimals = 2) {
|
|
|
304
304
|
*/
|
|
305
305
|
function _toDecimalFloor(num, decimals = 2) {
|
|
306
306
|
if (index.isNaN(Number(num))) {
|
|
307
|
-
throw new
|
|
307
|
+
throw new TypeError(`_toDecimalFloor: ${num} is not a number`);
|
|
308
308
|
}
|
|
309
309
|
const n = Math.pow(10, decimals);
|
|
310
310
|
return Math.floor(Number(num) * n) / n;
|
|
@@ -96,7 +96,7 @@ function toSnakeCase(value) {
|
|
|
96
96
|
}
|
|
97
97
|
// 不符合格式
|
|
98
98
|
else {
|
|
99
|
-
throw new TypeError(
|
|
99
|
+
throw new TypeError(`toSnakeCase: value should be a string`);
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
@@ -121,7 +121,7 @@ function toKebabCase(value) {
|
|
|
121
121
|
}
|
|
122
122
|
// 不符合格式
|
|
123
123
|
else {
|
|
124
|
-
throw new TypeError("value should be a string");
|
|
124
|
+
throw new TypeError("toKebabCase: value should be a string");
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
@@ -149,7 +149,7 @@ function toCamelCase(value) {
|
|
|
149
149
|
}
|
|
150
150
|
// 不符合格式
|
|
151
151
|
else {
|
|
152
|
-
throw new TypeError("value should be a string");
|
|
152
|
+
throw new TypeError("toCamelCase: value should be a string");
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
155
|
/**
|
|
@@ -179,7 +179,7 @@ function toPascalCase(value) {
|
|
|
179
179
|
}
|
|
180
180
|
// 不符合格式
|
|
181
181
|
else {
|
|
182
|
-
throw new TypeError("value should be a string");
|
|
182
|
+
throw new TypeError("toPascalCase: value should be a string");
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
/* 字符串格式化 */
|
|
@@ -193,10 +193,10 @@ function toPascalCase(value) {
|
|
|
193
193
|
function padZeroStart(value, maxLength = 2) {
|
|
194
194
|
value = String(value).trim();
|
|
195
195
|
if (maxLength < 0) {
|
|
196
|
-
throw new TypeError("maxLength should be greater than 0");
|
|
196
|
+
throw new TypeError("padZeroStart: maxLength should be greater than 0");
|
|
197
197
|
}
|
|
198
198
|
if (index.isNull(value) || index.isNaN(value)) {
|
|
199
|
-
throw new
|
|
199
|
+
throw new TypeError("padZeroStart: value must be a valid number or numeric string");
|
|
200
200
|
}
|
|
201
201
|
// 前面补0
|
|
202
202
|
let len = value.toString().length;
|
|
@@ -216,10 +216,10 @@ function padZeroStart(value, maxLength = 2) {
|
|
|
216
216
|
function padZeroEnd(value, maxLength = 2) {
|
|
217
217
|
value = String(value).trim();
|
|
218
218
|
if (maxLength < 0) {
|
|
219
|
-
throw new TypeError("maxLength should be greater than 0");
|
|
219
|
+
throw new TypeError("padZeroEnd: maxLength should be greater than 0");
|
|
220
220
|
}
|
|
221
221
|
if (index.isNull(value) || index.isNaN(value)) {
|
|
222
|
-
throw new
|
|
222
|
+
throw new TypeError("padZeroEnd: value must be a valid number or numeric string");
|
|
223
223
|
}
|
|
224
224
|
// 后面补0
|
|
225
225
|
let len = value.toString().length;
|
|
@@ -305,7 +305,7 @@ function formatRmbChinese(money) {
|
|
|
305
305
|
money = parseFloat(String(money));
|
|
306
306
|
if (money >= maxNum) {
|
|
307
307
|
// 超出最大处理数字,抛出异常
|
|
308
|
-
throw new
|
|
308
|
+
throw new TypeError("formatRmbChinese: calculated number overflow");
|
|
309
309
|
}
|
|
310
310
|
if (money === 0) {
|
|
311
311
|
chineseStr = cnNums[0] + cnIntLast + cnInteger;
|
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的配置
|