@ivujs/i-utils 2.1.1 → 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.
- package/dist/cjs/ID-card/index.cjs +1 -1
- package/dist/cjs/color/index.cjs +1 -1
- package/dist/cjs/date/index.cjs +109 -118
- package/dist/cjs/device/index.cjs +7 -6
- package/dist/cjs/index.cjs +2 -2
- package/dist/cjs/keycode/index.cjs +1 -1
- package/dist/cjs/math/index.cjs +26 -15
- package/dist/cjs/object/index.cjs +5 -4
- package/dist/cjs/string/index.cjs +38 -12
- package/dist/cjs/url/index.cjs +6 -6
- package/dist/cjs/validate/index.cjs +6 -2
- package/dist/es/ID-card/index.mjs +1 -1
- package/dist/es/array/index.d.ts +1 -1
- package/dist/es/clipboard/index.d.ts +5 -5
- package/dist/es/color/index.mjs +1 -1
- package/dist/es/date/index.d.ts +1 -1
- package/dist/es/date/index.mjs +109 -118
- package/dist/es/device/index.d.ts +2 -2
- package/dist/es/device/index.mjs +7 -6
- package/dist/es/dom/index.d.ts +1 -1
- package/dist/es/function/index.d.ts +2 -2
- package/dist/es/index.mjs +1 -1
- package/dist/es/keycode/index.d.ts +1 -1
- package/dist/es/keycode/index.mjs +1 -1
- package/dist/es/math/index.d.ts +4 -4
- package/dist/es/math/index.mjs +26 -15
- package/dist/es/object/index.d.ts +2 -1
- package/dist/es/object/index.mjs +5 -4
- package/dist/es/string/index.d.ts +4 -7
- package/dist/es/string/index.mjs +37 -11
- package/dist/es/url/index.mjs +6 -6
- package/dist/es/validate/index.mjs +6 -2
- package/dist/index.d.ts +24 -26
- package/dist/lib/index.full.umd.js +776 -744
- package/dist/lib/index.full.umd.min.js +2 -2
- package/dist/lib/index.full.umd.min.js.map +1 -1
- package/dist/resolver/auto-imports.cjs +2 -2
- package/dist/resolver/auto-imports.mjs +2 -2
- package/package.json +1 -1
|
@@ -64,7 +64,7 @@ function getAgeByIDCard(idCard) {
|
|
|
64
64
|
*/
|
|
65
65
|
function getSexByIDCard(idCard$1) {
|
|
66
66
|
if (index.isNull(idCard$1))
|
|
67
|
-
return
|
|
67
|
+
return;
|
|
68
68
|
// 15位身份证
|
|
69
69
|
if (idCard$1.length === 15) {
|
|
70
70
|
return Number(idCard$1.substring(14)) % 2 === 0 ? idCard.ID_CARD.SEX.WOMAN : idCard.ID_CARD.SEX.MAN;
|
package/dist/cjs/color/index.cjs
CHANGED
package/dist/cjs/date/index.cjs
CHANGED
|
@@ -754,7 +754,7 @@ function getBetweenDates(startDate, endDate) {
|
|
|
754
754
|
// 计算
|
|
755
755
|
const array = [];
|
|
756
756
|
while (endDate.getTime() - startDate.getTime() >= 0) {
|
|
757
|
-
const year = startDate.getFullYear(), month =
|
|
757
|
+
const year = startDate.getFullYear(), month = _datePadZero(startDate.getMonth() + 1), day = _datePadZero(startDate.getDate());
|
|
758
758
|
// 加入数组
|
|
759
759
|
array.push(year + "-" + month + "-" + day);
|
|
760
760
|
// 更新日期
|
|
@@ -795,7 +795,7 @@ function getBetweenMonths(startDate, endDate) {
|
|
|
795
795
|
}
|
|
796
796
|
else {
|
|
797
797
|
// 正常月份
|
|
798
|
-
str = curr.getFullYear() + "-" +
|
|
798
|
+
str = curr.getFullYear() + "-" + _datePadZero(month);
|
|
799
799
|
}
|
|
800
800
|
// 将此年月加入数组
|
|
801
801
|
array.push(str);
|
|
@@ -1117,29 +1117,24 @@ function toDataUTCString(date = new Date(), options = { format: "yyyy-MM-dd HH:m
|
|
|
1117
1117
|
* @returns {Date} 返回日期对象
|
|
1118
1118
|
*/
|
|
1119
1119
|
function toDate(value) {
|
|
1120
|
-
if (index$1.isNull(value))
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
}
|
|
1135
|
-
// 不支持的日期格式
|
|
1136
|
-
else {
|
|
1137
|
-
console.error("Not supported date format!");
|
|
1138
|
-
return undefined;
|
|
1139
|
-
}
|
|
1120
|
+
if (index$1.isNull(value)) {
|
|
1121
|
+
throw new TypeError("value must be a string or number");
|
|
1122
|
+
}
|
|
1123
|
+
// 是日期字符串
|
|
1124
|
+
if (index$1.isString(value)) {
|
|
1125
|
+
return new Date(String(value).replace(/-/g, "/"));
|
|
1126
|
+
}
|
|
1127
|
+
// 是时间戳
|
|
1128
|
+
else if (index$1.isInteger(value) && String(value).length === 13) {
|
|
1129
|
+
return new Date(value);
|
|
1130
|
+
}
|
|
1131
|
+
// 是unix时间戳
|
|
1132
|
+
else if (index$1.isInteger(value) && String(value).length === 10) {
|
|
1133
|
+
return new Date(Number(value) * 1000);
|
|
1140
1134
|
}
|
|
1141
|
-
|
|
1142
|
-
|
|
1135
|
+
// 不支持的日期格式
|
|
1136
|
+
else {
|
|
1137
|
+
throw new Error("Not supported date format");
|
|
1143
1138
|
}
|
|
1144
1139
|
}
|
|
1145
1140
|
/**
|
|
@@ -1151,108 +1146,104 @@ function toDate(value) {
|
|
|
1151
1146
|
*/
|
|
1152
1147
|
function toDateString(date$1, options = { format: "yyyy-MM-dd", lang: "zh" }) {
|
|
1153
1148
|
const { format = "yyyy-MM-dd", lang = "zh" } = options;
|
|
1154
|
-
if (index$1.isNull(date$1))
|
|
1155
|
-
|
|
1156
|
-
try {
|
|
1157
|
-
// 判断是否是日期对象
|
|
1158
|
-
if (!index$1.isDate(date$1)) {
|
|
1159
|
-
console.error("Not a Date type!");
|
|
1160
|
-
return "";
|
|
1161
|
-
}
|
|
1162
|
-
// 日期转化替换
|
|
1163
|
-
const replaceRules = {
|
|
1164
|
-
// 年(yyyy/yy)
|
|
1165
|
-
"(y+)": (match) => {
|
|
1166
|
-
const year = date$1.getFullYear().toString();
|
|
1167
|
-
return match.length === 2 ? year.slice(-2) : year;
|
|
1168
|
-
},
|
|
1169
|
-
// 月(M/MM)
|
|
1170
|
-
"(M+)": (match) => {
|
|
1171
|
-
const month = date$1.getMonth() + 1;
|
|
1172
|
-
return match.length === 1 ? month.toString() : month.toString().padStart(2, "0");
|
|
1173
|
-
},
|
|
1174
|
-
// 日(d/dd)
|
|
1175
|
-
"(d+)": (match) => {
|
|
1176
|
-
const day = date$1.getDate();
|
|
1177
|
-
return match.length === 1 ? day.toString() : day.toString().padStart(2, "0");
|
|
1178
|
-
},
|
|
1179
|
-
// 12小时制(h/hh)
|
|
1180
|
-
"(h+)": (match) => {
|
|
1181
|
-
const hour = date$1.getHours() % 12 || 12;
|
|
1182
|
-
return match.length === 1 ? hour.toString() : hour.toString().padStart(2, "0");
|
|
1183
|
-
},
|
|
1184
|
-
// 24小时制(H/HH)
|
|
1185
|
-
"(H+)": (match) => {
|
|
1186
|
-
const hour = date$1.getHours();
|
|
1187
|
-
return match.length === 1 ? hour.toString() : hour.toString().padStart(2, "0");
|
|
1188
|
-
},
|
|
1189
|
-
// 分钟(m/mm)
|
|
1190
|
-
"(m+)": (match) => {
|
|
1191
|
-
const min = date$1.getMinutes();
|
|
1192
|
-
return match.length === 1 ? min.toString() : min.toString().padStart(2, "0");
|
|
1193
|
-
},
|
|
1194
|
-
// 秒(s/ss)
|
|
1195
|
-
"(s+)": (match) => {
|
|
1196
|
-
const sec = date$1.getSeconds();
|
|
1197
|
-
return match.length === 1 ? sec.toString() : sec.toString().padStart(2, "0");
|
|
1198
|
-
},
|
|
1199
|
-
// 毫秒(S/SS/SSS)
|
|
1200
|
-
"(S+)": (match) => {
|
|
1201
|
-
const ms = date$1.getMilliseconds();
|
|
1202
|
-
return ms.toString().padStart(match.length, "0").slice(0, match.length);
|
|
1203
|
-
},
|
|
1204
|
-
// 上午/下午(a/A/aa/AA)
|
|
1205
|
-
"([aA]+)": (match) => {
|
|
1206
|
-
const isAm = date$1.getHours() < 12;
|
|
1207
|
-
// 多语言处理
|
|
1208
|
-
if (match.length > 1) {
|
|
1209
|
-
return isAm ? date.DATE.AM_PM[lang].AM : date.DATE.AM_PM[lang].PM;
|
|
1210
|
-
}
|
|
1211
|
-
else {
|
|
1212
|
-
return isAm ? match.toLowerCase() : match.toUpperCase();
|
|
1213
|
-
}
|
|
1214
|
-
},
|
|
1215
|
-
// 周(E/EE/EEE)
|
|
1216
|
-
"(E+)": (match) => {
|
|
1217
|
-
const day = date$1.getDay();
|
|
1218
|
-
if (match.length === 1)
|
|
1219
|
-
return date.DATE.WEEK[lang].MINI[day];
|
|
1220
|
-
if (match.length === 2)
|
|
1221
|
-
return date.DATE.WEEK[lang].SHORT[day];
|
|
1222
|
-
return date.DATE.WEEK[lang].FULL[day];
|
|
1223
|
-
},
|
|
1224
|
-
// 季度(Q/QQ/QQQ)
|
|
1225
|
-
"(Q+)": (match) => {
|
|
1226
|
-
const quarter = Math.floor((date$1.getMonth() + 3) / 3) - 1;
|
|
1227
|
-
if (match.length === 1)
|
|
1228
|
-
return date.DATE.QUARTER[lang].MINI[quarter];
|
|
1229
|
-
if (match.length === 2)
|
|
1230
|
-
return date.DATE.QUARTER[lang].SHORT[quarter];
|
|
1231
|
-
return date.DATE.QUARTER[lang].FULL[quarter];
|
|
1232
|
-
},
|
|
1233
|
-
};
|
|
1234
|
-
// 批量替换格式
|
|
1235
|
-
let result = format;
|
|
1236
|
-
Object.entries(replaceRules).forEach(([regStr, replaceFn]) => {
|
|
1237
|
-
const reg = new RegExp(regStr, "g");
|
|
1238
|
-
result = result.replace(reg, (_, match) => replaceFn(match));
|
|
1239
|
-
});
|
|
1240
|
-
return result;
|
|
1149
|
+
if (index$1.isNull(date$1)) {
|
|
1150
|
+
throw new TypeError("date input is null");
|
|
1241
1151
|
}
|
|
1242
|
-
|
|
1243
|
-
|
|
1152
|
+
// 判断是否是日期对象
|
|
1153
|
+
if (!index$1.isDate(date$1)) {
|
|
1154
|
+
console.error("Not a Date type!");
|
|
1244
1155
|
return "";
|
|
1245
1156
|
}
|
|
1157
|
+
// 日期转化替换
|
|
1158
|
+
const replaceRules = {
|
|
1159
|
+
// 年(yyyy/yy)
|
|
1160
|
+
"(y+)": (match) => {
|
|
1161
|
+
const year = date$1.getFullYear().toString();
|
|
1162
|
+
return match.length === 2 ? year.slice(-2) : year;
|
|
1163
|
+
},
|
|
1164
|
+
// 月(M/MM)
|
|
1165
|
+
"(M+)": (match) => {
|
|
1166
|
+
const month = date$1.getMonth() + 1;
|
|
1167
|
+
return match.length === 1 ? month.toString() : _datePadZero(month);
|
|
1168
|
+
},
|
|
1169
|
+
// 日(d/dd)
|
|
1170
|
+
"(d+)": (match) => {
|
|
1171
|
+
const day = date$1.getDate();
|
|
1172
|
+
return match.length === 1 ? day.toString() : _datePadZero(day);
|
|
1173
|
+
},
|
|
1174
|
+
// 12小时制(h/hh)
|
|
1175
|
+
"(h+)": (match) => {
|
|
1176
|
+
const hour = date$1.getHours() % 12 || 12;
|
|
1177
|
+
return match.length === 1 ? hour.toString() : _datePadZero(hour);
|
|
1178
|
+
},
|
|
1179
|
+
// 24小时制(H/HH)
|
|
1180
|
+
"(H+)": (match) => {
|
|
1181
|
+
const hour = date$1.getHours();
|
|
1182
|
+
return match.length === 1 ? hour.toString() : _datePadZero(hour);
|
|
1183
|
+
},
|
|
1184
|
+
// 分钟(m/mm)
|
|
1185
|
+
"(m+)": (match) => {
|
|
1186
|
+
const min = date$1.getMinutes();
|
|
1187
|
+
return match.length === 1 ? min.toString() : _datePadZero(min);
|
|
1188
|
+
},
|
|
1189
|
+
// 秒(s/ss)
|
|
1190
|
+
"(s+)": (match) => {
|
|
1191
|
+
const sec = date$1.getSeconds();
|
|
1192
|
+
return match.length === 1 ? sec.toString() : _datePadZero(sec);
|
|
1193
|
+
},
|
|
1194
|
+
// 毫秒(S/SS/SSS)
|
|
1195
|
+
"(S+)": (match) => {
|
|
1196
|
+
const ms = date$1.getMilliseconds();
|
|
1197
|
+
return ms.toString().padStart(match.length, "0").slice(0, match.length);
|
|
1198
|
+
},
|
|
1199
|
+
// 上午/下午(a/A/aa/AA)
|
|
1200
|
+
"([aA]+)": (match) => {
|
|
1201
|
+
const isAm = date$1.getHours() < 12;
|
|
1202
|
+
// 多语言处理
|
|
1203
|
+
if (match.length > 1) {
|
|
1204
|
+
return isAm ? date.DATE.AM_PM[lang].AM : date.DATE.AM_PM[lang].PM;
|
|
1205
|
+
}
|
|
1206
|
+
else {
|
|
1207
|
+
return isAm ? match.toLowerCase() : match.toUpperCase();
|
|
1208
|
+
}
|
|
1209
|
+
},
|
|
1210
|
+
// 周(E/EE/EEE)
|
|
1211
|
+
"(E+)": (match) => {
|
|
1212
|
+
const day = date$1.getDay();
|
|
1213
|
+
if (match.length === 1)
|
|
1214
|
+
return date.DATE.WEEK[lang].MINI[day];
|
|
1215
|
+
if (match.length === 2)
|
|
1216
|
+
return date.DATE.WEEK[lang].SHORT[day];
|
|
1217
|
+
return date.DATE.WEEK[lang].FULL[day];
|
|
1218
|
+
},
|
|
1219
|
+
// 季度(Q/QQ/QQQ)
|
|
1220
|
+
"(Q+)": (match) => {
|
|
1221
|
+
const quarter = Math.floor((date$1.getMonth() + 3) / 3) - 1;
|
|
1222
|
+
if (match.length === 1)
|
|
1223
|
+
return date.DATE.QUARTER[lang].MINI[quarter];
|
|
1224
|
+
if (match.length === 2)
|
|
1225
|
+
return date.DATE.QUARTER[lang].SHORT[quarter];
|
|
1226
|
+
return date.DATE.QUARTER[lang].FULL[quarter];
|
|
1227
|
+
},
|
|
1228
|
+
};
|
|
1229
|
+
// 批量替换格式
|
|
1230
|
+
let result = format;
|
|
1231
|
+
Object.entries(replaceRules).forEach(([regStr, replaceFn]) => {
|
|
1232
|
+
const reg = new RegExp(regStr, "g");
|
|
1233
|
+
result = result.replace(reg, (_, match) => replaceFn(match));
|
|
1234
|
+
});
|
|
1235
|
+
return result;
|
|
1246
1236
|
}
|
|
1247
1237
|
/* 内部使用的函数 */
|
|
1248
1238
|
/**
|
|
1249
|
-
*
|
|
1250
|
-
* @param {string|number} value
|
|
1239
|
+
* 单个日期数字前自动补齐零为两位
|
|
1240
|
+
* @param {string|number} value 值
|
|
1251
1241
|
* @returns {string} 返回处理后的字符串
|
|
1252
1242
|
*/
|
|
1253
|
-
function
|
|
1254
|
-
|
|
1255
|
-
|
|
1243
|
+
function _datePadZero(value) {
|
|
1244
|
+
const num = Number(value);
|
|
1245
|
+
// 数字>9直接转字符串,否则补0
|
|
1246
|
+
return num > 9 ? String(num) : "0" + num;
|
|
1256
1247
|
}
|
|
1257
1248
|
|
|
1258
1249
|
exports.addDate = addDate;
|
|
@@ -26,17 +26,18 @@ function getBrowserInfo() {
|
|
|
26
26
|
// 判断类型
|
|
27
27
|
if (ie)
|
|
28
28
|
return { name: "ie", version: ie[1] };
|
|
29
|
-
if (edge)
|
|
29
|
+
else if (edge)
|
|
30
30
|
return { name: "edge", version: edge[1] };
|
|
31
|
-
if (firefox)
|
|
31
|
+
else if (firefox)
|
|
32
32
|
return { name: "firefox", version: firefox[1] };
|
|
33
|
-
if (opera)
|
|
33
|
+
else if (opera)
|
|
34
34
|
return { name: "opera", version: opera[1] };
|
|
35
|
-
if (chrome)
|
|
35
|
+
else if (chrome)
|
|
36
36
|
return { name: "chrome", version: chrome[1] };
|
|
37
|
-
if (safari)
|
|
37
|
+
else if (safari)
|
|
38
38
|
return { name: "safari", version: safari[1] };
|
|
39
|
-
|
|
39
|
+
else
|
|
40
|
+
return;
|
|
40
41
|
}
|
|
41
42
|
/* 设备类型 */
|
|
42
43
|
/**
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -55,6 +55,8 @@ exports.formatTemplate = index.formatTemplate;
|
|
|
55
55
|
exports.formatThousand = index.formatThousand;
|
|
56
56
|
exports.formatTitle = index.formatTitle;
|
|
57
57
|
exports.inString = index.inString;
|
|
58
|
+
exports.padZeroEnd = index.padZeroEnd;
|
|
59
|
+
exports.padZeroStart = index.padZeroStart;
|
|
58
60
|
exports.replaceAll = index.replaceAll;
|
|
59
61
|
exports.toCamelCase = index.toCamelCase;
|
|
60
62
|
exports.toKebabCase = index.toKebabCase;
|
|
@@ -66,8 +68,6 @@ exports.trim = index.trim;
|
|
|
66
68
|
exports.trimAll = index.trimAll;
|
|
67
69
|
exports.trimEnd = index.trimEnd;
|
|
68
70
|
exports.trimStart = index.trimStart;
|
|
69
|
-
exports.zeroEnd = index.zeroEnd;
|
|
70
|
-
exports.zeroStart = index.zeroStart;
|
|
71
71
|
exports.parseFloat = index$1.parseFloat;
|
|
72
72
|
exports.parseInt = index$1.parseInt;
|
|
73
73
|
exports.arrayAvg = index$2.arrayAvg;
|
package/dist/cjs/math/index.cjs
CHANGED
|
@@ -37,7 +37,7 @@ function add(arg1, arg2) {
|
|
|
37
37
|
* @returns {number} 返回计算后的数字
|
|
38
38
|
*/
|
|
39
39
|
function subtract(arg1, arg2) {
|
|
40
|
-
let r1, r2
|
|
40
|
+
let r1, r2;
|
|
41
41
|
try {
|
|
42
42
|
r1 = arg1.toString().split(".")[1].length;
|
|
43
43
|
}
|
|
@@ -50,15 +50,15 @@ function subtract(arg1, arg2) {
|
|
|
50
50
|
catch (e) {
|
|
51
51
|
r2 = 0;
|
|
52
52
|
}
|
|
53
|
-
m = Math.pow(10, Math.max(r1, r2));
|
|
54
|
-
n = r1 >= r2 ? r1 : r2;
|
|
55
|
-
return ((Number(arg1) * m - Number(arg2) * m) / m).toFixed(n);
|
|
53
|
+
const m = Math.pow(10, Math.max(r1, r2));
|
|
54
|
+
const n = r1 >= r2 ? r1 : r2;
|
|
55
|
+
return Number(((Number(arg1) * m - Number(arg2) * m) / m).toFixed(n));
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
58
|
* 两个数字相乘
|
|
59
59
|
* @param {string|number} arg1 第一个数字
|
|
60
60
|
* @param {string|number} arg2 第二个数字
|
|
61
|
-
* @returns 返回计算后的数字
|
|
61
|
+
* @returns {number} 返回计算后的数字
|
|
62
62
|
*/
|
|
63
63
|
function multiply(arg1, arg2) {
|
|
64
64
|
let m = 0;
|
|
@@ -160,9 +160,12 @@ function toFixed(num, decimals = 2, mode = math.MATH.ROUND) {
|
|
|
160
160
|
return _toFixedRound(num, decimals);
|
|
161
161
|
}
|
|
162
162
|
// 向下舍出
|
|
163
|
-
if (mode === math.MATH.ROUND_FLOOR) {
|
|
163
|
+
else if (mode === math.MATH.ROUND_FLOOR) {
|
|
164
164
|
return _toFixedFloor(num, decimals);
|
|
165
165
|
}
|
|
166
|
+
else {
|
|
167
|
+
throw new Error("toFixed is error");
|
|
168
|
+
}
|
|
166
169
|
}
|
|
167
170
|
/**
|
|
168
171
|
* 尽可能保留小数位数
|
|
@@ -174,11 +177,15 @@ function toFixed(num, decimals = 2, mode = math.MATH.ROUND) {
|
|
|
174
177
|
function toDecimal(num, decimals = 2, mode = math.MATH.ROUND) {
|
|
175
178
|
// 四舍五入
|
|
176
179
|
if (mode === math.MATH.ROUND) {
|
|
177
|
-
return _toDecimalRound(num, decimals);
|
|
180
|
+
return Number(_toDecimalRound(num, decimals));
|
|
178
181
|
}
|
|
179
182
|
// 向下舍出
|
|
180
|
-
if (mode === math.MATH.ROUND_FLOOR) {
|
|
181
|
-
return _toDecimalFloor(num, decimals);
|
|
183
|
+
else if (mode === math.MATH.ROUND_FLOOR) {
|
|
184
|
+
return Number(_toDecimalFloor(num, decimals));
|
|
185
|
+
}
|
|
186
|
+
// 错误保留格式
|
|
187
|
+
else {
|
|
188
|
+
throw new Error("toDecimal is error");
|
|
182
189
|
}
|
|
183
190
|
}
|
|
184
191
|
/* 内部函数 */
|
|
@@ -235,13 +242,17 @@ function _toFixedRound(num, decimals = 2) {
|
|
|
235
242
|
* @description 默认保留两位小数,此方法相当于强制截取小数位数
|
|
236
243
|
* @param {string|number} num 数字
|
|
237
244
|
* @param {number} decimals 保留小数的位数,默认2位
|
|
238
|
-
* @returns {
|
|
245
|
+
* @returns {string} 返回字符串的数字
|
|
239
246
|
*/
|
|
240
247
|
function _toFixedFloor(num, decimals = 2) {
|
|
241
|
-
//
|
|
248
|
+
// num可能是字符串,先转数字再判断NaN
|
|
242
249
|
const numVal = Number(num);
|
|
243
250
|
if (index.isNaN(numVal)) {
|
|
244
|
-
|
|
251
|
+
throw new Error(`${num} is NaN`);
|
|
252
|
+
}
|
|
253
|
+
// 校验小数位数范围
|
|
254
|
+
if (decimals < 0 || decimals > 20) {
|
|
255
|
+
throw new Error("Decimal places must be between 0 and 20");
|
|
245
256
|
}
|
|
246
257
|
// 默认为保留的小数点后两位
|
|
247
258
|
const dec = Math.max(0, Math.floor(decimals)); // 确保小数位数为非负整数
|
|
@@ -276,11 +287,11 @@ function _toFixedFloor(num, decimals = 2) {
|
|
|
276
287
|
* 四舍五入,尽可能保留小数
|
|
277
288
|
* @param {string|number} num 数字
|
|
278
289
|
* @param {number} decimals 保留小数的位数,默认2位
|
|
279
|
-
* @returns {
|
|
290
|
+
* @returns {string} 返回保留后的数字
|
|
280
291
|
*/
|
|
281
292
|
function _toDecimalRound(num, decimals = 2) {
|
|
282
293
|
if (index.isNaN(Number(num))) {
|
|
283
|
-
|
|
294
|
+
throw new Error(`${num} is not a number`);
|
|
284
295
|
}
|
|
285
296
|
const n = Math.pow(10, decimals);
|
|
286
297
|
return Math.round(Number(num) * n) / n;
|
|
@@ -293,7 +304,7 @@ function _toDecimalRound(num, decimals = 2) {
|
|
|
293
304
|
*/
|
|
294
305
|
function _toDecimalFloor(num, decimals = 2) {
|
|
295
306
|
if (index.isNaN(Number(num))) {
|
|
296
|
-
|
|
307
|
+
throw new Error(`${num} is not a number`);
|
|
297
308
|
}
|
|
298
309
|
const n = Math.pow(10, decimals);
|
|
299
310
|
return Math.floor(Number(num) * n) / n;
|
|
@@ -201,7 +201,7 @@ function merge(target, ...source) {
|
|
|
201
201
|
* this._getValueByPath(res, 'data.pages.pageSize'); // 这里会输出20
|
|
202
202
|
* @param {Object} target 目标对象
|
|
203
203
|
* @param {string} path 字符串属性路径
|
|
204
|
-
* @returns {
|
|
204
|
+
* @returns {*} 返回目标对象的值
|
|
205
205
|
*/
|
|
206
206
|
function getValueByPath(target, path = "data") {
|
|
207
207
|
const paths = (path || "data").split(".");
|
|
@@ -213,7 +213,7 @@ function getValueByPath(target, path = "data") {
|
|
|
213
213
|
if (isArray) {
|
|
214
214
|
// 获取的数组下标超出实际的长度
|
|
215
215
|
if (propIndex < 0 || propIndex >= current[propName].length) {
|
|
216
|
-
return
|
|
216
|
+
return;
|
|
217
217
|
}
|
|
218
218
|
// 逐层向下找到对应属性的值
|
|
219
219
|
current = current[propName][propIndex];
|
|
@@ -222,7 +222,7 @@ function getValueByPath(target, path = "data") {
|
|
|
222
222
|
else {
|
|
223
223
|
// 如果属性不存在,则返回空的
|
|
224
224
|
if (!current || !current.hasOwnProperty(part) || typeof current !== "object") {
|
|
225
|
-
return
|
|
225
|
+
return;
|
|
226
226
|
}
|
|
227
227
|
// 逐层向下找到对应属性的值
|
|
228
228
|
current = current[part];
|
|
@@ -238,6 +238,7 @@ function getValueByPath(target, path = "data") {
|
|
|
238
238
|
* @param {Object} target 目标对象
|
|
239
239
|
* @param {string} path 字符串属性路径
|
|
240
240
|
* @param {*} value 值
|
|
241
|
+
* @returns {*} 返回修改过后的目标对象
|
|
241
242
|
*/
|
|
242
243
|
function setValueByPath(target, path = "data", value) {
|
|
243
244
|
const paths = (path || "data").split(".");
|
|
@@ -294,7 +295,7 @@ function setValueByPath(target, path = "data", value) {
|
|
|
294
295
|
/**
|
|
295
296
|
* 获得目标路径的片段
|
|
296
297
|
* @param {string} path
|
|
297
|
-
* @returns {
|
|
298
|
+
* @returns {*} 返回获得到的值
|
|
298
299
|
*/
|
|
299
300
|
function _getTargetPathPart(path) {
|
|
300
301
|
const pathArrayMatch = path.match(/^(\w+)\[(\d+)]$/);
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var index = require('../validate/index.cjs');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* @module 字符串
|
|
5
7
|
*/
|
|
@@ -84,14 +86,18 @@ function toSnakeCase(value) {
|
|
|
84
86
|
return value.replace(/([A-Z])/g, "_$1").toLowerCase();
|
|
85
87
|
}
|
|
86
88
|
// 短横
|
|
87
|
-
if (value.indexOf("-") > 0) {
|
|
89
|
+
else if (value.indexOf("-") > 0) {
|
|
88
90
|
return value.replace(/-/g, "_").toLowerCase();
|
|
89
91
|
}
|
|
90
92
|
// 帕斯卡
|
|
91
|
-
if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
|
|
93
|
+
else if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
|
|
92
94
|
value = value.charAt(0).toLowerCase() + value.slice(1);
|
|
93
95
|
return value.replace(/([A-Z])/g, "_$1").toLowerCase();
|
|
94
96
|
}
|
|
97
|
+
// 不符合格式
|
|
98
|
+
else {
|
|
99
|
+
throw new TypeError("value should be a string");
|
|
100
|
+
}
|
|
95
101
|
}
|
|
96
102
|
/**
|
|
97
103
|
* 转为 kebab-case 短横命名
|
|
@@ -105,14 +111,18 @@ function toKebabCase(value) {
|
|
|
105
111
|
return value.replace(/_/g, "-").toLowerCase();
|
|
106
112
|
}
|
|
107
113
|
// 驼峰
|
|
108
|
-
if (/^[a-z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
|
|
114
|
+
else if (/^[a-z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
|
|
109
115
|
return value.replace(/([A-Z])/g, "-$1").toLowerCase();
|
|
110
116
|
}
|
|
111
117
|
// 帕斯卡
|
|
112
|
-
if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
|
|
118
|
+
else if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
|
|
113
119
|
const newStr = value.charAt(0).toLowerCase() + value.slice(1);
|
|
114
120
|
return newStr.replace(/([A-Z])/g, "-$1").toLowerCase();
|
|
115
121
|
}
|
|
122
|
+
// 不符合格式
|
|
123
|
+
else {
|
|
124
|
+
throw new TypeError("value should be a string");
|
|
125
|
+
}
|
|
116
126
|
}
|
|
117
127
|
/**
|
|
118
128
|
* 转为 camelCase 驼峰命名
|
|
@@ -137,9 +147,9 @@ function toCamelCase(value) {
|
|
|
137
147
|
else if (/^[A-Z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
|
|
138
148
|
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
139
149
|
}
|
|
140
|
-
//
|
|
150
|
+
// 不符合格式
|
|
141
151
|
else {
|
|
142
|
-
|
|
152
|
+
throw new TypeError("value should be a string");
|
|
143
153
|
}
|
|
144
154
|
}
|
|
145
155
|
/**
|
|
@@ -167,9 +177,9 @@ function toPascalCase(value) {
|
|
|
167
177
|
else if (/^[a-z]$/.test(value.charAt(0)) && !(value.indexOf("-") > 0 || value.indexOf("_") > 0)) {
|
|
168
178
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
169
179
|
}
|
|
170
|
-
//
|
|
180
|
+
// 不符合格式
|
|
171
181
|
else {
|
|
172
|
-
|
|
182
|
+
throw new TypeError("value should be a string");
|
|
173
183
|
}
|
|
174
184
|
}
|
|
175
185
|
/* 字符串格式化 */
|
|
@@ -180,7 +190,15 @@ function toPascalCase(value) {
|
|
|
180
190
|
* @param {number} maxLength 补齐0后的最大长度,默认2位
|
|
181
191
|
* @returns {string} 返回补0后指定位数的字符串
|
|
182
192
|
*/
|
|
183
|
-
function
|
|
193
|
+
function padZeroStart(value, maxLength = 2) {
|
|
194
|
+
value = String(value).trim();
|
|
195
|
+
if (maxLength < 0) {
|
|
196
|
+
throw new TypeError("maxLength should be greater than 0");
|
|
197
|
+
}
|
|
198
|
+
if (index.isNull(value) || index.isNaN(value)) {
|
|
199
|
+
throw new Error("value must be a valid number or numeric string");
|
|
200
|
+
}
|
|
201
|
+
// 前面补0
|
|
184
202
|
let len = value.toString().length;
|
|
185
203
|
while (len < maxLength) {
|
|
186
204
|
value = "0" + value;
|
|
@@ -195,7 +213,15 @@ function zeroStart(value, maxLength = 2) {
|
|
|
195
213
|
* @param {number} maxLength 补齐0后的最大长度,默认2位
|
|
196
214
|
* @returns {string} 返回补0后指定位数的字符串
|
|
197
215
|
*/
|
|
198
|
-
function
|
|
216
|
+
function padZeroEnd(value, maxLength = 2) {
|
|
217
|
+
value = String(value).trim();
|
|
218
|
+
if (maxLength < 0) {
|
|
219
|
+
throw new TypeError("maxLength should be greater than 0");
|
|
220
|
+
}
|
|
221
|
+
if (index.isNull(value) || index.isNaN(value)) {
|
|
222
|
+
throw new Error("value must be a valid number or numeric string");
|
|
223
|
+
}
|
|
224
|
+
// 后面补0
|
|
199
225
|
let len = value.toString().length;
|
|
200
226
|
while (len < maxLength) {
|
|
201
227
|
value = value + "0";
|
|
@@ -346,6 +372,8 @@ exports.formatTemplate = formatTemplate;
|
|
|
346
372
|
exports.formatThousand = formatThousand;
|
|
347
373
|
exports.formatTitle = formatTitle;
|
|
348
374
|
exports.inString = inString;
|
|
375
|
+
exports.padZeroEnd = padZeroEnd;
|
|
376
|
+
exports.padZeroStart = padZeroStart;
|
|
349
377
|
exports.replaceAll = replaceAll;
|
|
350
378
|
exports.toCamelCase = toCamelCase;
|
|
351
379
|
exports.toKebabCase = toKebabCase;
|
|
@@ -357,5 +385,3 @@ exports.trim = trim;
|
|
|
357
385
|
exports.trimAll = trimAll;
|
|
358
386
|
exports.trimEnd = trimEnd;
|
|
359
387
|
exports.trimStart = trimStart;
|
|
360
|
-
exports.zeroEnd = zeroEnd;
|
|
361
|
-
exports.zeroStart = zeroStart;
|
package/dist/cjs/url/index.cjs
CHANGED
|
@@ -15,7 +15,7 @@ var index$1 = require('../array/index.cjs');
|
|
|
15
15
|
function getProtocol(url = window.location.href) {
|
|
16
16
|
const match = url.match(regexp.REGEXP.URL);
|
|
17
17
|
if (match) {
|
|
18
|
-
return match[1]
|
|
18
|
+
return match[1];
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
@@ -37,7 +37,7 @@ function getHost(url = window.location.href) {
|
|
|
37
37
|
function getHostName(url = window.location.href) {
|
|
38
38
|
const match = url.match(regexp.REGEXP.URL);
|
|
39
39
|
if (match) {
|
|
40
|
-
return match[2]
|
|
40
|
+
return match[2];
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
@@ -48,7 +48,7 @@ function getHostName(url = window.location.href) {
|
|
|
48
48
|
function getPort(url = window.location.href) {
|
|
49
49
|
const match = url.match(regexp.REGEXP.URL);
|
|
50
50
|
if (match) {
|
|
51
|
-
return match[3]
|
|
51
|
+
return match[3];
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
@@ -59,7 +59,7 @@ function getPort(url = window.location.href) {
|
|
|
59
59
|
function getUrlPath(url = window.location.href) {
|
|
60
60
|
const match = url.match(regexp.REGEXP.URL);
|
|
61
61
|
if (match) {
|
|
62
|
-
return match[4]
|
|
62
|
+
return match[4];
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
@@ -70,7 +70,7 @@ function getUrlPath(url = window.location.href) {
|
|
|
70
70
|
function getUrlHash(url = window.location.href) {
|
|
71
71
|
const match = url.match(regexp.REGEXP.URL);
|
|
72
72
|
if (match) {
|
|
73
|
-
return match[6]
|
|
73
|
+
return match[6];
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
@@ -81,7 +81,7 @@ function getUrlHash(url = window.location.href) {
|
|
|
81
81
|
function getSearchString(url = window.location.href) {
|
|
82
82
|
const match = url.match(regexp.REGEXP.URL);
|
|
83
83
|
if (match) {
|
|
84
|
-
return match[5]
|
|
84
|
+
return match[5];
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
@@ -219,8 +219,12 @@ function isFalse(value) {
|
|
|
219
219
|
* @returns {boolean} 返回结果
|
|
220
220
|
*/
|
|
221
221
|
function isNaN(value) {
|
|
222
|
-
//
|
|
223
|
-
|
|
222
|
+
return (value == null || // 匹配 null/undefined
|
|
223
|
+
typeof value === "boolean" || // 布尔值视为非数字
|
|
224
|
+
Array.isArray(value) || // 数组视为非数字
|
|
225
|
+
value === "" || // 空字符串视为非数字
|
|
226
|
+
window.isNaN(Number(value)) // 原生isNaN判断(转数字后是否为NaN)
|
|
227
|
+
);
|
|
224
228
|
}
|
|
225
229
|
/**
|
|
226
230
|
* 判断是数字
|