@ivujs/i-utils 2.1.0 → 2.1.2

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 (60) hide show
  1. package/dist/cjs/ID-card/index.cjs +1 -1
  2. package/dist/cjs/color/index.cjs +1 -1
  3. package/dist/cjs/constants/date.cjs +55 -1
  4. package/dist/cjs/constants/id-card.cjs +42 -1
  5. package/dist/cjs/constants/keycode.cjs +117 -2
  6. package/dist/cjs/constants/lang.cjs +7 -1
  7. package/dist/cjs/constants/math.cjs +9 -1
  8. package/dist/cjs/constants/regexp.cjs +33 -10
  9. package/dist/cjs/constants/sort.cjs +10 -1
  10. package/dist/cjs/date/index.cjs +160 -133
  11. package/dist/cjs/device/index.cjs +7 -6
  12. package/dist/cjs/index.cjs +4 -2
  13. package/dist/cjs/keycode/index.cjs +1 -1
  14. package/dist/cjs/math/index.cjs +26 -15
  15. package/dist/cjs/object/index.cjs +5 -4
  16. package/dist/cjs/string/index.cjs +38 -12
  17. package/dist/cjs/url/index.cjs +6 -6
  18. package/dist/cjs/validate/index.cjs +6 -2
  19. package/dist/es/ID-card/index.mjs +1 -1
  20. package/dist/es/array/index.d.ts +1 -1
  21. package/dist/es/clipboard/index.d.ts +5 -5
  22. package/dist/es/color/index.mjs +1 -1
  23. package/dist/es/constants/date.d.ts +55 -1
  24. package/dist/es/constants/date.mjs +55 -1
  25. package/dist/es/constants/id-card.d.ts +42 -1
  26. package/dist/es/constants/id-card.mjs +42 -1
  27. package/dist/es/constants/keycode.d.ts +118 -0
  28. package/dist/es/constants/keycode.mjs +117 -2
  29. package/dist/es/constants/lang.d.ts +7 -1
  30. package/dist/es/constants/lang.mjs +7 -1
  31. package/dist/es/constants/math.d.ts +9 -1
  32. package/dist/es/constants/math.mjs +9 -1
  33. package/dist/es/constants/regexp.d.ts +24 -1
  34. package/dist/es/constants/regexp.mjs +33 -10
  35. package/dist/es/constants/sort.d.ts +10 -1
  36. package/dist/es/constants/sort.mjs +10 -1
  37. package/dist/es/date/index.d.ts +40 -11
  38. package/dist/es/date/index.mjs +160 -135
  39. package/dist/es/device/index.d.ts +2 -2
  40. package/dist/es/device/index.mjs +7 -6
  41. package/dist/es/dom/index.d.ts +1 -1
  42. package/dist/es/function/index.d.ts +2 -2
  43. package/dist/es/index.mjs +2 -2
  44. package/dist/es/keycode/index.d.ts +1 -1
  45. package/dist/es/keycode/index.mjs +1 -1
  46. package/dist/es/math/index.d.ts +4 -4
  47. package/dist/es/math/index.mjs +26 -15
  48. package/dist/es/object/index.d.ts +2 -1
  49. package/dist/es/object/index.mjs +5 -4
  50. package/dist/es/string/index.d.ts +4 -7
  51. package/dist/es/string/index.mjs +37 -11
  52. package/dist/es/url/index.mjs +6 -6
  53. package/dist/es/validate/index.mjs +6 -2
  54. package/dist/index.d.ts +327 -41
  55. package/dist/lib/index.full.umd.js +947 -623
  56. package/dist/lib/index.full.umd.min.js +2 -2
  57. package/dist/lib/index.full.umd.min.js.map +1 -1
  58. package/dist/resolver/auto-imports.cjs +5 -3
  59. package/dist/resolver/auto-imports.mjs +5 -3
  60. package/package.json +1 -1
@@ -35,7 +35,7 @@ function add(arg1, arg2) {
35
35
  * @returns {number} 返回计算后的数字
36
36
  */
37
37
  function subtract(arg1, arg2) {
38
- let r1, r2, m, n;
38
+ let r1, r2;
39
39
  try {
40
40
  r1 = arg1.toString().split(".")[1].length;
41
41
  }
@@ -48,15 +48,15 @@ function subtract(arg1, arg2) {
48
48
  catch (e) {
49
49
  r2 = 0;
50
50
  }
51
- m = Math.pow(10, Math.max(r1, r2));
52
- n = r1 >= r2 ? r1 : r2;
53
- return ((Number(arg1) * m - Number(arg2) * m) / m).toFixed(n);
51
+ const m = Math.pow(10, Math.max(r1, r2));
52
+ const n = r1 >= r2 ? r1 : r2;
53
+ return Number(((Number(arg1) * m - Number(arg2) * m) / m).toFixed(n));
54
54
  }
55
55
  /**
56
56
  * 两个数字相乘
57
57
  * @param {string|number} arg1 第一个数字
58
58
  * @param {string|number} arg2 第二个数字
59
- * @returns 返回计算后的数字
59
+ * @returns {number} 返回计算后的数字
60
60
  */
61
61
  function multiply(arg1, arg2) {
62
62
  let m = 0;
@@ -158,9 +158,12 @@ function toFixed(num, decimals = 2, mode = MATH.ROUND) {
158
158
  return _toFixedRound(num, decimals);
159
159
  }
160
160
  // 向下舍出
161
- if (mode === MATH.ROUND_FLOOR) {
161
+ else if (mode === MATH.ROUND_FLOOR) {
162
162
  return _toFixedFloor(num, decimals);
163
163
  }
164
+ else {
165
+ throw new Error("toFixed is error");
166
+ }
164
167
  }
165
168
  /**
166
169
  * 尽可能保留小数位数
@@ -172,11 +175,15 @@ function toFixed(num, decimals = 2, mode = MATH.ROUND) {
172
175
  function toDecimal(num, decimals = 2, mode = MATH.ROUND) {
173
176
  // 四舍五入
174
177
  if (mode === MATH.ROUND) {
175
- return _toDecimalRound(num, decimals);
178
+ return Number(_toDecimalRound(num, decimals));
176
179
  }
177
180
  // 向下舍出
178
- if (mode === MATH.ROUND_FLOOR) {
179
- return _toDecimalFloor(num, decimals);
181
+ else if (mode === MATH.ROUND_FLOOR) {
182
+ return Number(_toDecimalFloor(num, decimals));
183
+ }
184
+ // 错误保留格式
185
+ else {
186
+ throw new Error("toDecimal is error");
180
187
  }
181
188
  }
182
189
  /* 内部函数 */
@@ -233,13 +240,17 @@ function _toFixedRound(num, decimals = 2) {
233
240
  * @description 默认保留两位小数,此方法相当于强制截取小数位数
234
241
  * @param {string|number} num 数字
235
242
  * @param {number} decimals 保留小数的位数,默认2位
236
- * @returns {number} 返回字符串的数字
243
+ * @returns {string} 返回字符串的数字
237
244
  */
238
245
  function _toFixedFloor(num, decimals = 2) {
239
- // 修复1:num可能是字符串,先转数字再判断NaN
246
+ // num可能是字符串,先转数字再判断NaN
240
247
  const numVal = Number(num);
241
248
  if (isNaN(numVal)) {
242
- return "--";
249
+ throw new Error(`${num} is NaN`);
250
+ }
251
+ // 校验小数位数范围
252
+ if (decimals < 0 || decimals > 20) {
253
+ throw new Error("Decimal places must be between 0 and 20");
243
254
  }
244
255
  // 默认为保留的小数点后两位
245
256
  const dec = Math.max(0, Math.floor(decimals)); // 确保小数位数为非负整数
@@ -274,11 +285,11 @@ function _toFixedFloor(num, decimals = 2) {
274
285
  * 四舍五入,尽可能保留小数
275
286
  * @param {string|number} num 数字
276
287
  * @param {number} decimals 保留小数的位数,默认2位
277
- * @returns {number} 返回保留后的数字
288
+ * @returns {string} 返回保留后的数字
278
289
  */
279
290
  function _toDecimalRound(num, decimals = 2) {
280
291
  if (isNaN(Number(num))) {
281
- return "--";
292
+ throw new Error(`${num} is not a number`);
282
293
  }
283
294
  const n = Math.pow(10, decimals);
284
295
  return Math.round(Number(num) * n) / n;
@@ -291,7 +302,7 @@ function _toDecimalRound(num, decimals = 2) {
291
302
  */
292
303
  function _toDecimalFloor(num, decimals = 2) {
293
304
  if (isNaN(Number(num))) {
294
- return "--";
305
+ throw new Error(`${num} is not a number`);
295
306
  }
296
307
  const n = Math.pow(10, decimals);
297
308
  return Math.floor(Number(num) * n) / n;
@@ -69,7 +69,7 @@ export declare function merge(target: Record<any, any>, ...source: Record<any, a
69
69
  * this._getValueByPath(res, 'data.pages.pageSize'); // 这里会输出20
70
70
  * @param {Object} target 目标对象
71
71
  * @param {string} path 字符串属性路径
72
- * @returns {Object} 返回目标对象
72
+ * @returns {*} 返回目标对象的值
73
73
  */
74
74
  export declare function getValueByPath(target: Record<any, any>, path?: string): any;
75
75
  /**
@@ -80,5 +80,6 @@ export declare function getValueByPath(target: Record<any, any>, path?: string):
80
80
  * @param {Object} target 目标对象
81
81
  * @param {string} path 字符串属性路径
82
82
  * @param {*} value 值
83
+ * @returns {*} 返回修改过后的目标对象
83
84
  */
84
85
  export declare function setValueByPath(target: Record<any, any>, path: string | undefined, value: any): Record<any, any>;
@@ -199,7 +199,7 @@ function merge(target, ...source) {
199
199
  * this._getValueByPath(res, 'data.pages.pageSize'); // 这里会输出20
200
200
  * @param {Object} target 目标对象
201
201
  * @param {string} path 字符串属性路径
202
- * @returns {Object} 返回目标对象
202
+ * @returns {*} 返回目标对象的值
203
203
  */
204
204
  function getValueByPath(target, path = "data") {
205
205
  const paths = (path || "data").split(".");
@@ -211,7 +211,7 @@ function getValueByPath(target, path = "data") {
211
211
  if (isArray) {
212
212
  // 获取的数组下标超出实际的长度
213
213
  if (propIndex < 0 || propIndex >= current[propName].length) {
214
- return undefined;
214
+ return;
215
215
  }
216
216
  // 逐层向下找到对应属性的值
217
217
  current = current[propName][propIndex];
@@ -220,7 +220,7 @@ function getValueByPath(target, path = "data") {
220
220
  else {
221
221
  // 如果属性不存在,则返回空的
222
222
  if (!current || !current.hasOwnProperty(part) || typeof current !== "object") {
223
- return undefined;
223
+ return;
224
224
  }
225
225
  // 逐层向下找到对应属性的值
226
226
  current = current[part];
@@ -236,6 +236,7 @@ function getValueByPath(target, path = "data") {
236
236
  * @param {Object} target 目标对象
237
237
  * @param {string} path 字符串属性路径
238
238
  * @param {*} value 值
239
+ * @returns {*} 返回修改过后的目标对象
239
240
  */
240
241
  function setValueByPath(target, path = "data", value) {
241
242
  const paths = (path || "data").split(".");
@@ -292,7 +293,7 @@ function setValueByPath(target, path = "data", value) {
292
293
  /**
293
294
  * 获得目标路径的片段
294
295
  * @param {string} path
295
- * @returns {{propName: (*|undefined), propIndex: (*|number|undefined), isArray: boolean}}
296
+ * @returns {*} 返回获得到的值
296
297
  */
297
298
  function _getTargetPathPart(path) {
298
299
  const pathArrayMatch = path.match(/^(\w+)\[(\d+)]$/);
@@ -1,6 +1,3 @@
1
- /**
2
- * @module 字符串
3
- */
4
1
  /**
5
2
  * 字符串中是否包含指定的元素
6
3
  * @param {string} value 包含的元素
@@ -58,14 +55,14 @@ export declare function toLowerCase(value: string): string;
58
55
  * @param {string} value 参数
59
56
  * @returns {string} 返回处理后的字符串
60
57
  */
61
- export declare function toSnakeCase(value: string): string | undefined;
58
+ export declare function toSnakeCase(value: string): string;
62
59
  /**
63
60
  * 转为 kebab-case 短横命名
64
61
  * @description 支持 下划线,驼峰命名,帕斯卡命名
65
62
  * @param {string} value 参数
66
63
  * @returns {string} 返回处理后的字符串
67
64
  */
68
- export declare function toKebabCase(value: string): string | undefined;
65
+ export declare function toKebabCase(value: string): string;
69
66
  /**
70
67
  * 转为 camelCase 驼峰命名
71
68
  * @description 支持 下划线命名,短横命名,帕斯卡命名
@@ -87,7 +84,7 @@ export declare function toPascalCase(value: string): string;
87
84
  * @param {number} maxLength 补齐0后的最大长度,默认2位
88
85
  * @returns {string} 返回补0后指定位数的字符串
89
86
  */
90
- export declare function zeroStart(value: string, maxLength?: number): string;
87
+ export declare function padZeroStart(value: string | number, maxLength?: number): string;
91
88
  /**
92
89
  * 数字后补齐0达到指定位数
93
90
  * @description 相当于原生的 padEnd(2,'0')
@@ -95,7 +92,7 @@ export declare function zeroStart(value: string, maxLength?: number): string;
95
92
  * @param {number} maxLength 补齐0后的最大长度,默认2位
96
93
  * @returns {string} 返回补0后指定位数的字符串
97
94
  */
98
- export declare function zeroEnd(value: string, maxLength?: number): string;
95
+ export declare function padZeroEnd(value: string | number, maxLength?: number): string;
99
96
  /**
100
97
  * 格式化为标题样式
101
98
  * @param {string} value 字符串值
@@ -1,3 +1,5 @@
1
+ import { isNull, isNaN } from '../validate/index.mjs';
2
+
1
3
  /**
2
4
  * @module 字符串
3
5
  */
@@ -82,14 +84,18 @@ function toSnakeCase(value) {
82
84
  return value.replace(/([A-Z])/g, "_$1").toLowerCase();
83
85
  }
84
86
  // 短横
85
- if (value.indexOf("-") > 0) {
87
+ else if (value.indexOf("-") > 0) {
86
88
  return value.replace(/-/g, "_").toLowerCase();
87
89
  }
88
90
  // 帕斯卡
89
- if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
91
+ else if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
90
92
  value = value.charAt(0).toLowerCase() + value.slice(1);
91
93
  return value.replace(/([A-Z])/g, "_$1").toLowerCase();
92
94
  }
95
+ // 不符合格式
96
+ else {
97
+ throw new TypeError("value should be a string");
98
+ }
93
99
  }
94
100
  /**
95
101
  * 转为 kebab-case 短横命名
@@ -103,14 +109,18 @@ function toKebabCase(value) {
103
109
  return value.replace(/_/g, "-").toLowerCase();
104
110
  }
105
111
  // 驼峰
106
- if (/^[a-z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
112
+ else if (/^[a-z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
107
113
  return value.replace(/([A-Z])/g, "-$1").toLowerCase();
108
114
  }
109
115
  // 帕斯卡
110
- if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
116
+ else if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
111
117
  const newStr = value.charAt(0).toLowerCase() + value.slice(1);
112
118
  return newStr.replace(/([A-Z])/g, "-$1").toLowerCase();
113
119
  }
120
+ // 不符合格式
121
+ else {
122
+ throw new TypeError("value should be a string");
123
+ }
114
124
  }
115
125
  /**
116
126
  * 转为 camelCase 驼峰命名
@@ -135,9 +145,9 @@ function toCamelCase(value) {
135
145
  else if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
136
146
  return value.charAt(0).toLowerCase() + value.slice(1);
137
147
  }
138
- // 返回自身
148
+ // 不符合格式
139
149
  else {
140
- return value;
150
+ throw new TypeError("value should be a string");
141
151
  }
142
152
  }
143
153
  /**
@@ -165,9 +175,9 @@ function toPascalCase(value) {
165
175
  else if (/^[a-z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
166
176
  return value.charAt(0).toUpperCase() + value.slice(1);
167
177
  }
168
- // 返回自身
178
+ // 不符合格式
169
179
  else {
170
- return value;
180
+ throw new TypeError("value should be a string");
171
181
  }
172
182
  }
173
183
  /* 字符串格式化 */
@@ -178,7 +188,15 @@ function toPascalCase(value) {
178
188
  * @param {number} maxLength 补齐0后的最大长度,默认2位
179
189
  * @returns {string} 返回补0后指定位数的字符串
180
190
  */
181
- function zeroStart(value, maxLength = 2) {
191
+ function padZeroStart(value, maxLength = 2) {
192
+ value = String(value).trim();
193
+ if (maxLength < 0) {
194
+ throw new TypeError("maxLength should be greater than 0");
195
+ }
196
+ if (isNull(value) || isNaN(value)) {
197
+ throw new Error("value must be a valid number or numeric string");
198
+ }
199
+ // 前面补0
182
200
  let len = value.toString().length;
183
201
  while (len < maxLength) {
184
202
  value = "0" + value;
@@ -193,7 +211,15 @@ function zeroStart(value, maxLength = 2) {
193
211
  * @param {number} maxLength 补齐0后的最大长度,默认2位
194
212
  * @returns {string} 返回补0后指定位数的字符串
195
213
  */
196
- function zeroEnd(value, maxLength = 2) {
214
+ function padZeroEnd(value, maxLength = 2) {
215
+ value = String(value).trim();
216
+ if (maxLength < 0) {
217
+ throw new TypeError("maxLength should be greater than 0");
218
+ }
219
+ if (isNull(value) || isNaN(value)) {
220
+ throw new Error("value must be a valid number or numeric string");
221
+ }
222
+ // 后面补0
197
223
  let len = value.toString().length;
198
224
  while (len < maxLength) {
199
225
  value = value + "0";
@@ -339,4 +365,4 @@ function formatRmbChinese(money) {
339
365
  return chineseStr;
340
366
  }
341
367
 
342
- export { formatRmbChinese, formatTemplate, formatThousand, formatTitle, inString, replaceAll, toCamelCase, toKebabCase, toLowerCase, toPascalCase, toSnakeCase, toUpperCase, trim, trimAll, trimEnd, trimStart, zeroEnd, zeroStart };
368
+ export { formatRmbChinese, formatTemplate, formatThousand, formatTitle, inString, padZeroEnd, padZeroStart, replaceAll, toCamelCase, toKebabCase, toLowerCase, toPascalCase, toSnakeCase, toUpperCase, trim, trimAll, trimEnd, trimStart };
@@ -13,7 +13,7 @@ import { arrayInsertBefore, arrayInsertAfter } from '../array/index.mjs';
13
13
  function getProtocol(url = window.location.href) {
14
14
  const match = url.match(REGEXP.URL);
15
15
  if (match) {
16
- return match[1] || "";
16
+ return match[1];
17
17
  }
18
18
  }
19
19
  /**
@@ -35,7 +35,7 @@ function getHost(url = window.location.href) {
35
35
  function getHostName(url = window.location.href) {
36
36
  const match = url.match(REGEXP.URL);
37
37
  if (match) {
38
- return match[2] || "";
38
+ return match[2];
39
39
  }
40
40
  }
41
41
  /**
@@ -46,7 +46,7 @@ function getHostName(url = window.location.href) {
46
46
  function getPort(url = window.location.href) {
47
47
  const match = url.match(REGEXP.URL);
48
48
  if (match) {
49
- return match[3] || "";
49
+ return match[3];
50
50
  }
51
51
  }
52
52
  /**
@@ -57,7 +57,7 @@ function getPort(url = window.location.href) {
57
57
  function getUrlPath(url = window.location.href) {
58
58
  const match = url.match(REGEXP.URL);
59
59
  if (match) {
60
- return match[4] || "";
60
+ return match[4];
61
61
  }
62
62
  }
63
63
  /**
@@ -68,7 +68,7 @@ function getUrlPath(url = window.location.href) {
68
68
  function getUrlHash(url = window.location.href) {
69
69
  const match = url.match(REGEXP.URL);
70
70
  if (match) {
71
- return match[6] || "";
71
+ return match[6];
72
72
  }
73
73
  }
74
74
  /**
@@ -79,7 +79,7 @@ function getUrlHash(url = window.location.href) {
79
79
  function getSearchString(url = window.location.href) {
80
80
  const match = url.match(REGEXP.URL);
81
81
  if (match) {
82
- return match[5] || "";
82
+ return match[5];
83
83
  }
84
84
  }
85
85
  /**
@@ -217,8 +217,12 @@ function isFalse(value) {
217
217
  * @returns {boolean} 返回结果
218
218
  */
219
219
  function isNaN(value) {
220
- // window的isNaN函数是有缺陷的,空数组/数组有一个元素,null,空字符串 都会被认为是数字
221
- return window.isNaN(value) || isArray(value) || value == null || value === "";
220
+ return (value == null || // 匹配 null/undefined
221
+ typeof value === "boolean" || // 布尔值视为非数字
222
+ Array.isArray(value) || // 数组视为非数字
223
+ value === "" || // 空字符串视为非数字
224
+ window.isNaN(Number(value)) // 原生isNaN判断(转数字后是否为NaN)
225
+ );
222
226
  }
223
227
  /**
224
228
  * 判断是数字