react-util-tools 1.0.27 → 1.0.29
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/index.cjs +130 -0
- package/dist/index.d.cts +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/index.js +122 -0
- package/package.json +1 -1
- package/src/index.ts +10 -1
- package/src/string/README.md +18 -0
- package/src/string/index.ts +25 -0
package/dist/index.cjs
CHANGED
|
@@ -64,6 +64,9 @@ __export(index_exports, {
|
|
|
64
64
|
formatUTC: () => formatUTC,
|
|
65
65
|
formatUTCDateOnly: () => formatUTCDateOnly,
|
|
66
66
|
formatUTCTimeOnly: () => formatUTCTimeOnly,
|
|
67
|
+
formateAmount: () => formateAmount,
|
|
68
|
+
formateFaitAmount: () => formateFaitAmount,
|
|
69
|
+
formatePrecision: () => formatePrecision,
|
|
67
70
|
fromBase64: () => fromBase64,
|
|
68
71
|
fromUTC: () => fromUTC,
|
|
69
72
|
getAllCookies: () => getAllCookies,
|
|
@@ -120,6 +123,7 @@ __export(index_exports, {
|
|
|
120
123
|
greaterThanOrEqual: () => greaterThanOrEqual,
|
|
121
124
|
hasCookie: () => hasCookie,
|
|
122
125
|
includes: () => includes,
|
|
126
|
+
integerTokenArr: () => integerTokenArr,
|
|
123
127
|
isAfterDate: () => isAfterDate,
|
|
124
128
|
isAndroid: () => isAndroid,
|
|
125
129
|
isBeforeDate: () => isBeforeDate,
|
|
@@ -147,6 +151,7 @@ __export(index_exports, {
|
|
|
147
151
|
maskIdCard: () => maskIdCard,
|
|
148
152
|
maskName: () => maskName,
|
|
149
153
|
maskPhone: () => maskPhone,
|
|
154
|
+
maskString: () => maskString,
|
|
150
155
|
multiply: () => multiply,
|
|
151
156
|
negate: () => negate,
|
|
152
157
|
normalizeSpaces: () => normalizeSpaces,
|
|
@@ -161,6 +166,7 @@ __export(index_exports, {
|
|
|
161
166
|
readExcelToJSON: () => readExcelToJSON,
|
|
162
167
|
readFile: () => readFile,
|
|
163
168
|
removeCookie: () => removeCookie,
|
|
169
|
+
removeInvalidZero: () => removeInvalidZero,
|
|
164
170
|
removeSpaces: () => removeSpaces,
|
|
165
171
|
repeat: () => repeat,
|
|
166
172
|
replaceAll: () => replaceAll,
|
|
@@ -184,6 +190,7 @@ __export(index_exports, {
|
|
|
184
190
|
titleCase: () => titleCase,
|
|
185
191
|
toBase64: () => toBase64,
|
|
186
192
|
toISOString: () => toISOString,
|
|
193
|
+
toLocalString: () => toLocalString,
|
|
187
194
|
toLowerCase: () => toLowerCase,
|
|
188
195
|
toUTC: () => toUTC,
|
|
189
196
|
toUpperCase: () => toUpperCase,
|
|
@@ -191,6 +198,7 @@ __export(index_exports, {
|
|
|
191
198
|
trimEnd: () => trimEnd,
|
|
192
199
|
trimStart: () => trimStart,
|
|
193
200
|
truncate: () => truncate,
|
|
201
|
+
tryRun: () => tryRun,
|
|
194
202
|
unescapeHtml: () => unescapeHtml,
|
|
195
203
|
unmaskEmail: () => unmaskEmail,
|
|
196
204
|
utils: () => utils2,
|
|
@@ -601,6 +609,111 @@ function unmaskEmail(maskedEmail, originalEmail) {
|
|
|
601
609
|
}
|
|
602
610
|
return maskedEmail;
|
|
603
611
|
}
|
|
612
|
+
function toLocalString(value) {
|
|
613
|
+
let result = value;
|
|
614
|
+
if (Number(value) >= 1e3) {
|
|
615
|
+
result = Number(value).toLocaleString("en-US");
|
|
616
|
+
} else {
|
|
617
|
+
result = value;
|
|
618
|
+
}
|
|
619
|
+
return result;
|
|
620
|
+
}
|
|
621
|
+
var integerTokenArr = ["SATS"];
|
|
622
|
+
function removeInvalidZero(num) {
|
|
623
|
+
let result = num;
|
|
624
|
+
if (num.includes(".")) {
|
|
625
|
+
result = result.replace(/\.?0+$/, "");
|
|
626
|
+
}
|
|
627
|
+
return result;
|
|
628
|
+
}
|
|
629
|
+
function formatePrecision(n, precision, tokenSymbol) {
|
|
630
|
+
if (!isNaN(Number(n))) {
|
|
631
|
+
let prec = precision && precision <= 8 ? precision : 8;
|
|
632
|
+
if (tokenSymbol && integerTokenArr.includes(tokenSymbol)) {
|
|
633
|
+
prec = 0;
|
|
634
|
+
}
|
|
635
|
+
return new import_decimal.default(Number(n)).toFixed(prec, import_decimal.default.ROUND_DOWN);
|
|
636
|
+
}
|
|
637
|
+
const num = new import_decimal.default(n).toNumber();
|
|
638
|
+
return num;
|
|
639
|
+
}
|
|
640
|
+
function formateAmount({
|
|
641
|
+
num = 0,
|
|
642
|
+
precision = 8,
|
|
643
|
+
tokenSymbol = "",
|
|
644
|
+
type = "0"
|
|
645
|
+
}) {
|
|
646
|
+
return tryRun(() => {
|
|
647
|
+
const dec = new import_decimal.default(num);
|
|
648
|
+
const { length: length2 } = dec.abs().floor().toString();
|
|
649
|
+
const pres = precision;
|
|
650
|
+
precision = precision > 8 ? 9 : precision + 1;
|
|
651
|
+
if (length2 >= precision) {
|
|
652
|
+
let value = "";
|
|
653
|
+
if (dec.greaterThan(1)) {
|
|
654
|
+
value = dec.toFixed(8, import_decimal.default.ROUND_DOWN);
|
|
655
|
+
} else {
|
|
656
|
+
value = dec.toFixed(precision - length2, import_decimal.default.ROUND_DOWN);
|
|
657
|
+
}
|
|
658
|
+
value = formatePrecision(value, pres).toString();
|
|
659
|
+
if (integerTokenArr.includes(tokenSymbol)) {
|
|
660
|
+
value = new import_decimal.default(value).toFixed(0, import_decimal.default.ROUND_CEIL);
|
|
661
|
+
}
|
|
662
|
+
let result = "";
|
|
663
|
+
if (typeof value === "string") {
|
|
664
|
+
if (value.includes(".")) {
|
|
665
|
+
result = value.replace(/\d(?=(\d{3})+\.)/g, "$&,");
|
|
666
|
+
} else {
|
|
667
|
+
result = toLocalString(value);
|
|
668
|
+
}
|
|
669
|
+
} else {
|
|
670
|
+
result = value;
|
|
671
|
+
}
|
|
672
|
+
if (Number(num) === 0) {
|
|
673
|
+
return result;
|
|
674
|
+
} else {
|
|
675
|
+
return dec.greaterThan(new import_decimal.default(0)) ? removeInvalidZero(result) : type === "0" ? "-" + removeInvalidZero(result) : removeInvalidZero(result);
|
|
676
|
+
}
|
|
677
|
+
} else {
|
|
678
|
+
let value = "";
|
|
679
|
+
if (dec.greaterThan(1)) {
|
|
680
|
+
value = dec.toFixed(8, import_decimal.default.ROUND_DOWN);
|
|
681
|
+
} else {
|
|
682
|
+
value = dec.toFixed(precision - length2, import_decimal.default.ROUND_DOWN);
|
|
683
|
+
}
|
|
684
|
+
value = formatePrecision(value, pres).toString();
|
|
685
|
+
if (integerTokenArr.includes(tokenSymbol)) {
|
|
686
|
+
value = new import_decimal.default(value).toFixed(0, import_decimal.default.ROUND_CEIL);
|
|
687
|
+
}
|
|
688
|
+
let result = "";
|
|
689
|
+
if (typeof value === "string") {
|
|
690
|
+
if (value.includes(".")) {
|
|
691
|
+
result = value.replace(/\d(?=(\d{3})+\.)/g, "$&,");
|
|
692
|
+
} else {
|
|
693
|
+
result = toLocalString(value);
|
|
694
|
+
}
|
|
695
|
+
} else {
|
|
696
|
+
result = value;
|
|
697
|
+
}
|
|
698
|
+
return removeInvalidZero(result);
|
|
699
|
+
}
|
|
700
|
+
}) ?? "0.00";
|
|
701
|
+
}
|
|
702
|
+
function formateFaitAmount(num = 0) {
|
|
703
|
+
return tryRun(() => {
|
|
704
|
+
const dec = new import_decimal.default(num);
|
|
705
|
+
const fnum = dec.toFixed(2, import_decimal.default.ROUND_DOWN);
|
|
706
|
+
if (Number(num) >= 1e3) {
|
|
707
|
+
let result = toLocalString(fnum);
|
|
708
|
+
if (!result.includes(".")) {
|
|
709
|
+
result = result + ".00";
|
|
710
|
+
}
|
|
711
|
+
return result;
|
|
712
|
+
} else {
|
|
713
|
+
return fnum;
|
|
714
|
+
}
|
|
715
|
+
}) ?? "0.00";
|
|
716
|
+
}
|
|
604
717
|
function tryRun(fn) {
|
|
605
718
|
try {
|
|
606
719
|
return fn();
|
|
@@ -1262,6 +1375,15 @@ function maskName(name) {
|
|
|
1262
1375
|
}
|
|
1263
1376
|
return name.charAt(0) + "*".repeat(name.length - 2) + name.charAt(name.length - 1);
|
|
1264
1377
|
}
|
|
1378
|
+
function maskString(str, visibleChars = 3, mask = "...") {
|
|
1379
|
+
if (!str) return "";
|
|
1380
|
+
if (str.length <= visibleChars * 2) {
|
|
1381
|
+
return str;
|
|
1382
|
+
}
|
|
1383
|
+
const start = str.slice(0, visibleChars);
|
|
1384
|
+
const end = str.slice(-visibleChars);
|
|
1385
|
+
return `${start}${mask}${end}`;
|
|
1386
|
+
}
|
|
1265
1387
|
function isValidPhone(phone) {
|
|
1266
1388
|
if (!phone) return false;
|
|
1267
1389
|
return /^1[3-9]\d{9}$/.test(phone);
|
|
@@ -1358,6 +1480,9 @@ function fromBase64(base64) {
|
|
|
1358
1480
|
formatUTC,
|
|
1359
1481
|
formatUTCDateOnly,
|
|
1360
1482
|
formatUTCTimeOnly,
|
|
1483
|
+
formateAmount,
|
|
1484
|
+
formateFaitAmount,
|
|
1485
|
+
formatePrecision,
|
|
1361
1486
|
fromBase64,
|
|
1362
1487
|
fromUTC,
|
|
1363
1488
|
getAllCookies,
|
|
@@ -1414,6 +1539,7 @@ function fromBase64(base64) {
|
|
|
1414
1539
|
greaterThanOrEqual,
|
|
1415
1540
|
hasCookie,
|
|
1416
1541
|
includes,
|
|
1542
|
+
integerTokenArr,
|
|
1417
1543
|
isAfterDate,
|
|
1418
1544
|
isAndroid,
|
|
1419
1545
|
isBeforeDate,
|
|
@@ -1441,6 +1567,7 @@ function fromBase64(base64) {
|
|
|
1441
1567
|
maskIdCard,
|
|
1442
1568
|
maskName,
|
|
1443
1569
|
maskPhone,
|
|
1570
|
+
maskString,
|
|
1444
1571
|
multiply,
|
|
1445
1572
|
negate,
|
|
1446
1573
|
normalizeSpaces,
|
|
@@ -1455,6 +1582,7 @@ function fromBase64(base64) {
|
|
|
1455
1582
|
readExcelToJSON,
|
|
1456
1583
|
readFile,
|
|
1457
1584
|
removeCookie,
|
|
1585
|
+
removeInvalidZero,
|
|
1458
1586
|
removeSpaces,
|
|
1459
1587
|
repeat,
|
|
1460
1588
|
replaceAll,
|
|
@@ -1478,6 +1606,7 @@ function fromBase64(base64) {
|
|
|
1478
1606
|
titleCase,
|
|
1479
1607
|
toBase64,
|
|
1480
1608
|
toISOString,
|
|
1609
|
+
toLocalString,
|
|
1481
1610
|
toLowerCase,
|
|
1482
1611
|
toUTC,
|
|
1483
1612
|
toUpperCase,
|
|
@@ -1485,6 +1614,7 @@ function fromBase64(base64) {
|
|
|
1485
1614
|
trimEnd,
|
|
1486
1615
|
trimStart,
|
|
1487
1616
|
truncate,
|
|
1617
|
+
tryRun,
|
|
1488
1618
|
unescapeHtml,
|
|
1489
1619
|
unmaskEmail,
|
|
1490
1620
|
utils,
|
package/dist/index.d.cts
CHANGED
|
@@ -89,6 +89,19 @@ declare function formatPercent(value: number, options?: {
|
|
|
89
89
|
}): string;
|
|
90
90
|
declare function maskEmail(email: string): string;
|
|
91
91
|
declare function unmaskEmail(maskedEmail: string, originalEmail: string): string;
|
|
92
|
+
declare function toLocalString(value: string): string;
|
|
93
|
+
declare const integerTokenArr: string[];
|
|
94
|
+
type AmountNum = string | number | Decimal;
|
|
95
|
+
declare function removeInvalidZero(num: string): string;
|
|
96
|
+
declare function formatePrecision(n: AmountNum, precision?: number, tokenSymbol?: string): string | number;
|
|
97
|
+
declare function formateAmount({ num, precision, tokenSymbol, type, }: {
|
|
98
|
+
num: AmountNum;
|
|
99
|
+
precision?: number;
|
|
100
|
+
tokenSymbol?: string;
|
|
101
|
+
type?: string;
|
|
102
|
+
}): any;
|
|
103
|
+
declare function formateFaitAmount(num?: AmountNum): any;
|
|
104
|
+
declare function tryRun(fn: () => any): any;
|
|
92
105
|
|
|
93
106
|
declare function formatDate(date: Date | number | string, formatStr?: string): string;
|
|
94
107
|
declare function formatDateOnly(date: Date | number | string): string;
|
|
@@ -227,6 +240,7 @@ declare function maskPhone(phone: string): string;
|
|
|
227
240
|
declare function maskIdCard(idCard: string): string;
|
|
228
241
|
declare function maskBankCard(cardNumber: string): string;
|
|
229
242
|
declare function maskName(name: string): string;
|
|
243
|
+
declare function maskString(str: string, visibleChars?: number, mask?: string): string;
|
|
230
244
|
declare function isValidPhone(phone: string): boolean;
|
|
231
245
|
declare function isValidEmail(email: string): boolean;
|
|
232
246
|
declare function isValidUrl(url: string): boolean;
|
|
@@ -234,4 +248,4 @@ declare function isValidIdCard(idCard: string): boolean;
|
|
|
234
248
|
declare function toBase64(str: string): string;
|
|
235
249
|
declare function fromBase64(base64: string): string;
|
|
236
250
|
|
|
237
|
-
export { type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, aoaToSheet, camelCase, capitalize, ceil, clearAllCookies, countOccurrences, debounceFn as debounce, divide, endsWith, equals, escapeHtml, exportExcelFile, exportJSONToExcel, extractNumbers, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromBase64, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getSheet, getSheetNames, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, includes, isAfterDate, isAndroid, isBeforeDate, isDesktop, isEmpty, isIOS, isMobile, isNotEmpty, isSameDayDate, isTablet, isTouchDevice, isValidDate, isValidEmail, isValidIdCard, isValidPhone, isValidUrl, isWeChat, jsonToWorkbook, kebabCase, length, lessThan, lessThanOrEqual, maskBankCard, maskEmail, maskIdCard, maskName, maskPhone, multiply, negate, normalizeSpaces, padEnd, padStart, parseDate, parseMoney, pascalCase, randomString, read, readExcelFile, readExcelToJSON, readFile, removeCookie, removeSpaces, repeat, replaceAll, reverse, round, setCookie, sheetToAOA, sheetToCSV, sheetToHTML, snakeCase, split, startsWith, stripHtml, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, tableToSheet, throttleFn as throttle, titleCase, toBase64, toISOString, toLowerCase, toUTC, toUpperCase, trim, trimEnd, trimStart, truncate, unescapeHtml, unmaskEmail, utils, uuid, workbookToJSON, write, writeFile, writeFileXLSX };
|
|
251
|
+
export { type AmountNum, type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, aoaToSheet, camelCase, capitalize, ceil, clearAllCookies, countOccurrences, debounceFn as debounce, divide, endsWith, equals, escapeHtml, exportExcelFile, exportJSONToExcel, extractNumbers, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, formateAmount, formateFaitAmount, formatePrecision, fromBase64, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getSheet, getSheetNames, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, includes, integerTokenArr, isAfterDate, isAndroid, isBeforeDate, isDesktop, isEmpty, isIOS, isMobile, isNotEmpty, isSameDayDate, isTablet, isTouchDevice, isValidDate, isValidEmail, isValidIdCard, isValidPhone, isValidUrl, isWeChat, jsonToWorkbook, kebabCase, length, lessThan, lessThanOrEqual, maskBankCard, maskEmail, maskIdCard, maskName, maskPhone, maskString, multiply, negate, normalizeSpaces, padEnd, padStart, parseDate, parseMoney, pascalCase, randomString, read, readExcelFile, readExcelToJSON, readFile, removeCookie, removeInvalidZero, removeSpaces, repeat, replaceAll, reverse, round, setCookie, sheetToAOA, sheetToCSV, sheetToHTML, snakeCase, split, startsWith, stripHtml, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, tableToSheet, throttleFn as throttle, titleCase, toBase64, toISOString, toLocalString, toLowerCase, toUTC, toUpperCase, trim, trimEnd, trimStart, truncate, tryRun, unescapeHtml, unmaskEmail, utils, uuid, workbookToJSON, write, writeFile, writeFileXLSX };
|
package/dist/index.d.ts
CHANGED
|
@@ -89,6 +89,19 @@ declare function formatPercent(value: number, options?: {
|
|
|
89
89
|
}): string;
|
|
90
90
|
declare function maskEmail(email: string): string;
|
|
91
91
|
declare function unmaskEmail(maskedEmail: string, originalEmail: string): string;
|
|
92
|
+
declare function toLocalString(value: string): string;
|
|
93
|
+
declare const integerTokenArr: string[];
|
|
94
|
+
type AmountNum = string | number | Decimal;
|
|
95
|
+
declare function removeInvalidZero(num: string): string;
|
|
96
|
+
declare function formatePrecision(n: AmountNum, precision?: number, tokenSymbol?: string): string | number;
|
|
97
|
+
declare function formateAmount({ num, precision, tokenSymbol, type, }: {
|
|
98
|
+
num: AmountNum;
|
|
99
|
+
precision?: number;
|
|
100
|
+
tokenSymbol?: string;
|
|
101
|
+
type?: string;
|
|
102
|
+
}): any;
|
|
103
|
+
declare function formateFaitAmount(num?: AmountNum): any;
|
|
104
|
+
declare function tryRun(fn: () => any): any;
|
|
92
105
|
|
|
93
106
|
declare function formatDate(date: Date | number | string, formatStr?: string): string;
|
|
94
107
|
declare function formatDateOnly(date: Date | number | string): string;
|
|
@@ -227,6 +240,7 @@ declare function maskPhone(phone: string): string;
|
|
|
227
240
|
declare function maskIdCard(idCard: string): string;
|
|
228
241
|
declare function maskBankCard(cardNumber: string): string;
|
|
229
242
|
declare function maskName(name: string): string;
|
|
243
|
+
declare function maskString(str: string, visibleChars?: number, mask?: string): string;
|
|
230
244
|
declare function isValidPhone(phone: string): boolean;
|
|
231
245
|
declare function isValidEmail(email: string): boolean;
|
|
232
246
|
declare function isValidUrl(url: string): boolean;
|
|
@@ -234,4 +248,4 @@ declare function isValidIdCard(idCard: string): boolean;
|
|
|
234
248
|
declare function toBase64(str: string): string;
|
|
235
249
|
declare function fromBase64(base64: string): string;
|
|
236
250
|
|
|
237
|
-
export { type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, aoaToSheet, camelCase, capitalize, ceil, clearAllCookies, countOccurrences, debounceFn as debounce, divide, endsWith, equals, escapeHtml, exportExcelFile, exportJSONToExcel, extractNumbers, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromBase64, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getSheet, getSheetNames, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, includes, isAfterDate, isAndroid, isBeforeDate, isDesktop, isEmpty, isIOS, isMobile, isNotEmpty, isSameDayDate, isTablet, isTouchDevice, isValidDate, isValidEmail, isValidIdCard, isValidPhone, isValidUrl, isWeChat, jsonToWorkbook, kebabCase, length, lessThan, lessThanOrEqual, maskBankCard, maskEmail, maskIdCard, maskName, maskPhone, multiply, negate, normalizeSpaces, padEnd, padStart, parseDate, parseMoney, pascalCase, randomString, read, readExcelFile, readExcelToJSON, readFile, removeCookie, removeSpaces, repeat, replaceAll, reverse, round, setCookie, sheetToAOA, sheetToCSV, sheetToHTML, snakeCase, split, startsWith, stripHtml, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, tableToSheet, throttleFn as throttle, titleCase, toBase64, toISOString, toLowerCase, toUTC, toUpperCase, trim, trimEnd, trimStart, truncate, unescapeHtml, unmaskEmail, utils, uuid, workbookToJSON, write, writeFile, writeFileXLSX };
|
|
251
|
+
export { type AmountNum, type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, aoaToSheet, camelCase, capitalize, ceil, clearAllCookies, countOccurrences, debounceFn as debounce, divide, endsWith, equals, escapeHtml, exportExcelFile, exportJSONToExcel, extractNumbers, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, formateAmount, formateFaitAmount, formatePrecision, fromBase64, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getSheet, getSheetNames, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, includes, integerTokenArr, isAfterDate, isAndroid, isBeforeDate, isDesktop, isEmpty, isIOS, isMobile, isNotEmpty, isSameDayDate, isTablet, isTouchDevice, isValidDate, isValidEmail, isValidIdCard, isValidPhone, isValidUrl, isWeChat, jsonToWorkbook, kebabCase, length, lessThan, lessThanOrEqual, maskBankCard, maskEmail, maskIdCard, maskName, maskPhone, maskString, multiply, negate, normalizeSpaces, padEnd, padStart, parseDate, parseMoney, pascalCase, randomString, read, readExcelFile, readExcelToJSON, readFile, removeCookie, removeInvalidZero, removeSpaces, repeat, replaceAll, reverse, round, setCookie, sheetToAOA, sheetToCSV, sheetToHTML, snakeCase, split, startsWith, stripHtml, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, tableToSheet, throttleFn as throttle, titleCase, toBase64, toISOString, toLocalString, toLowerCase, toUTC, toUpperCase, trim, trimEnd, trimStart, truncate, tryRun, unescapeHtml, unmaskEmail, utils, uuid, workbookToJSON, write, writeFile, writeFileXLSX };
|
package/dist/index.js
CHANGED
|
@@ -397,6 +397,111 @@ function unmaskEmail(maskedEmail, originalEmail) {
|
|
|
397
397
|
}
|
|
398
398
|
return maskedEmail;
|
|
399
399
|
}
|
|
400
|
+
function toLocalString(value) {
|
|
401
|
+
let result = value;
|
|
402
|
+
if (Number(value) >= 1e3) {
|
|
403
|
+
result = Number(value).toLocaleString("en-US");
|
|
404
|
+
} else {
|
|
405
|
+
result = value;
|
|
406
|
+
}
|
|
407
|
+
return result;
|
|
408
|
+
}
|
|
409
|
+
var integerTokenArr = ["SATS"];
|
|
410
|
+
function removeInvalidZero(num) {
|
|
411
|
+
let result = num;
|
|
412
|
+
if (num.includes(".")) {
|
|
413
|
+
result = result.replace(/\.?0+$/, "");
|
|
414
|
+
}
|
|
415
|
+
return result;
|
|
416
|
+
}
|
|
417
|
+
function formatePrecision(n, precision, tokenSymbol) {
|
|
418
|
+
if (!isNaN(Number(n))) {
|
|
419
|
+
let prec = precision && precision <= 8 ? precision : 8;
|
|
420
|
+
if (tokenSymbol && integerTokenArr.includes(tokenSymbol)) {
|
|
421
|
+
prec = 0;
|
|
422
|
+
}
|
|
423
|
+
return new Decimal(Number(n)).toFixed(prec, Decimal.ROUND_DOWN);
|
|
424
|
+
}
|
|
425
|
+
const num = new Decimal(n).toNumber();
|
|
426
|
+
return num;
|
|
427
|
+
}
|
|
428
|
+
function formateAmount({
|
|
429
|
+
num = 0,
|
|
430
|
+
precision = 8,
|
|
431
|
+
tokenSymbol = "",
|
|
432
|
+
type = "0"
|
|
433
|
+
}) {
|
|
434
|
+
return tryRun(() => {
|
|
435
|
+
const dec = new Decimal(num);
|
|
436
|
+
const { length: length2 } = dec.abs().floor().toString();
|
|
437
|
+
const pres = precision;
|
|
438
|
+
precision = precision > 8 ? 9 : precision + 1;
|
|
439
|
+
if (length2 >= precision) {
|
|
440
|
+
let value = "";
|
|
441
|
+
if (dec.greaterThan(1)) {
|
|
442
|
+
value = dec.toFixed(8, Decimal.ROUND_DOWN);
|
|
443
|
+
} else {
|
|
444
|
+
value = dec.toFixed(precision - length2, Decimal.ROUND_DOWN);
|
|
445
|
+
}
|
|
446
|
+
value = formatePrecision(value, pres).toString();
|
|
447
|
+
if (integerTokenArr.includes(tokenSymbol)) {
|
|
448
|
+
value = new Decimal(value).toFixed(0, Decimal.ROUND_CEIL);
|
|
449
|
+
}
|
|
450
|
+
let result = "";
|
|
451
|
+
if (typeof value === "string") {
|
|
452
|
+
if (value.includes(".")) {
|
|
453
|
+
result = value.replace(/\d(?=(\d{3})+\.)/g, "$&,");
|
|
454
|
+
} else {
|
|
455
|
+
result = toLocalString(value);
|
|
456
|
+
}
|
|
457
|
+
} else {
|
|
458
|
+
result = value;
|
|
459
|
+
}
|
|
460
|
+
if (Number(num) === 0) {
|
|
461
|
+
return result;
|
|
462
|
+
} else {
|
|
463
|
+
return dec.greaterThan(new Decimal(0)) ? removeInvalidZero(result) : type === "0" ? "-" + removeInvalidZero(result) : removeInvalidZero(result);
|
|
464
|
+
}
|
|
465
|
+
} else {
|
|
466
|
+
let value = "";
|
|
467
|
+
if (dec.greaterThan(1)) {
|
|
468
|
+
value = dec.toFixed(8, Decimal.ROUND_DOWN);
|
|
469
|
+
} else {
|
|
470
|
+
value = dec.toFixed(precision - length2, Decimal.ROUND_DOWN);
|
|
471
|
+
}
|
|
472
|
+
value = formatePrecision(value, pres).toString();
|
|
473
|
+
if (integerTokenArr.includes(tokenSymbol)) {
|
|
474
|
+
value = new Decimal(value).toFixed(0, Decimal.ROUND_CEIL);
|
|
475
|
+
}
|
|
476
|
+
let result = "";
|
|
477
|
+
if (typeof value === "string") {
|
|
478
|
+
if (value.includes(".")) {
|
|
479
|
+
result = value.replace(/\d(?=(\d{3})+\.)/g, "$&,");
|
|
480
|
+
} else {
|
|
481
|
+
result = toLocalString(value);
|
|
482
|
+
}
|
|
483
|
+
} else {
|
|
484
|
+
result = value;
|
|
485
|
+
}
|
|
486
|
+
return removeInvalidZero(result);
|
|
487
|
+
}
|
|
488
|
+
}) ?? "0.00";
|
|
489
|
+
}
|
|
490
|
+
function formateFaitAmount(num = 0) {
|
|
491
|
+
return tryRun(() => {
|
|
492
|
+
const dec = new Decimal(num);
|
|
493
|
+
const fnum = dec.toFixed(2, Decimal.ROUND_DOWN);
|
|
494
|
+
if (Number(num) >= 1e3) {
|
|
495
|
+
let result = toLocalString(fnum);
|
|
496
|
+
if (!result.includes(".")) {
|
|
497
|
+
result = result + ".00";
|
|
498
|
+
}
|
|
499
|
+
return result;
|
|
500
|
+
} else {
|
|
501
|
+
return fnum;
|
|
502
|
+
}
|
|
503
|
+
}) ?? "0.00";
|
|
504
|
+
}
|
|
400
505
|
function tryRun(fn) {
|
|
401
506
|
try {
|
|
402
507
|
return fn();
|
|
@@ -1101,6 +1206,15 @@ function maskName(name) {
|
|
|
1101
1206
|
}
|
|
1102
1207
|
return name.charAt(0) + "*".repeat(name.length - 2) + name.charAt(name.length - 1);
|
|
1103
1208
|
}
|
|
1209
|
+
function maskString(str, visibleChars = 3, mask = "...") {
|
|
1210
|
+
if (!str) return "";
|
|
1211
|
+
if (str.length <= visibleChars * 2) {
|
|
1212
|
+
return str;
|
|
1213
|
+
}
|
|
1214
|
+
const start = str.slice(0, visibleChars);
|
|
1215
|
+
const end = str.slice(-visibleChars);
|
|
1216
|
+
return `${start}${mask}${end}`;
|
|
1217
|
+
}
|
|
1104
1218
|
function isValidPhone(phone) {
|
|
1105
1219
|
if (!phone) return false;
|
|
1106
1220
|
return /^1[3-9]\d{9}$/.test(phone);
|
|
@@ -1196,6 +1310,9 @@ export {
|
|
|
1196
1310
|
formatUTC,
|
|
1197
1311
|
formatUTCDateOnly,
|
|
1198
1312
|
formatUTCTimeOnly,
|
|
1313
|
+
formateAmount,
|
|
1314
|
+
formateFaitAmount,
|
|
1315
|
+
formatePrecision,
|
|
1199
1316
|
fromBase64,
|
|
1200
1317
|
fromUTC,
|
|
1201
1318
|
getAllCookies,
|
|
@@ -1252,6 +1369,7 @@ export {
|
|
|
1252
1369
|
greaterThanOrEqual,
|
|
1253
1370
|
hasCookie,
|
|
1254
1371
|
includes,
|
|
1372
|
+
integerTokenArr,
|
|
1255
1373
|
isAfterDate,
|
|
1256
1374
|
isAndroid,
|
|
1257
1375
|
isBeforeDate,
|
|
@@ -1279,6 +1397,7 @@ export {
|
|
|
1279
1397
|
maskIdCard,
|
|
1280
1398
|
maskName,
|
|
1281
1399
|
maskPhone,
|
|
1400
|
+
maskString,
|
|
1282
1401
|
multiply,
|
|
1283
1402
|
negate,
|
|
1284
1403
|
normalizeSpaces,
|
|
@@ -1293,6 +1412,7 @@ export {
|
|
|
1293
1412
|
readExcelToJSON,
|
|
1294
1413
|
readFile,
|
|
1295
1414
|
removeCookie,
|
|
1415
|
+
removeInvalidZero,
|
|
1296
1416
|
removeSpaces,
|
|
1297
1417
|
repeat,
|
|
1298
1418
|
replaceAll,
|
|
@@ -1316,6 +1436,7 @@ export {
|
|
|
1316
1436
|
titleCase,
|
|
1317
1437
|
toBase64,
|
|
1318
1438
|
toISOString,
|
|
1439
|
+
toLocalString,
|
|
1319
1440
|
toLowerCase,
|
|
1320
1441
|
toUTC,
|
|
1321
1442
|
toUpperCase,
|
|
@@ -1323,6 +1444,7 @@ export {
|
|
|
1323
1444
|
trimEnd,
|
|
1324
1445
|
trimStart,
|
|
1325
1446
|
truncate,
|
|
1447
|
+
tryRun,
|
|
1326
1448
|
unescapeHtml,
|
|
1327
1449
|
unmaskEmail,
|
|
1328
1450
|
utils2 as utils,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-util-tools",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.29",
|
|
4
4
|
"description": "A collection of useful utilities: throttle, debounce, date formatting, device detection, money formatting, decimal calculations, Excel processing and more",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
package/src/index.ts
CHANGED
|
@@ -35,8 +35,16 @@ export {
|
|
|
35
35
|
formatMoneyToChinese,
|
|
36
36
|
formatPercent,
|
|
37
37
|
maskEmail,
|
|
38
|
-
unmaskEmail
|
|
38
|
+
unmaskEmail,
|
|
39
|
+
toLocalString,
|
|
40
|
+
integerTokenArr,
|
|
41
|
+
removeInvalidZero,
|
|
42
|
+
formatePrecision,
|
|
43
|
+
formateAmount,
|
|
44
|
+
formateFaitAmount,
|
|
45
|
+
tryRun
|
|
39
46
|
} from './format/index'
|
|
47
|
+
export type { AmountNum } from './format/index'
|
|
40
48
|
export {
|
|
41
49
|
formatDate,
|
|
42
50
|
formatDateOnly,
|
|
@@ -187,6 +195,7 @@ export {
|
|
|
187
195
|
maskIdCard,
|
|
188
196
|
maskBankCard,
|
|
189
197
|
maskName,
|
|
198
|
+
maskString,
|
|
190
199
|
isValidPhone,
|
|
191
200
|
isValidEmail,
|
|
192
201
|
isValidUrl,
|
package/src/string/README.md
CHANGED
|
@@ -90,6 +90,7 @@ import {
|
|
|
90
90
|
maskIdCard,
|
|
91
91
|
maskBankCard,
|
|
92
92
|
maskName,
|
|
93
|
+
maskString,
|
|
93
94
|
maskEmail
|
|
94
95
|
} from 'react-util-tools'
|
|
95
96
|
|
|
@@ -106,6 +107,22 @@ maskBankCard('6222021234567890') // '6222 **** **** 7890'
|
|
|
106
107
|
maskName('张三') // '张*'
|
|
107
108
|
maskName('李四光') // '李*光'
|
|
108
109
|
|
|
110
|
+
// 通用字符串脱敏
|
|
111
|
+
maskString('123wedwdwddwed567', 3) // '123...567'
|
|
112
|
+
maskString('abcdefghijk', 2) // 'ab...jk'
|
|
113
|
+
maskString('hello', 2, '***') // 'he***lo'
|
|
114
|
+
|
|
115
|
+
// 邮箱脱敏(在 format 模块)
|
|
116
|
+
maskEmail('test@example.com') // 'tes***@example.com'
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
// 银行卡号脱敏
|
|
120
|
+
maskBankCard('6222021234567890') // '6222 **** **** 7890'
|
|
121
|
+
|
|
122
|
+
// 姓名脱敏
|
|
123
|
+
maskName('张三') // '张*'
|
|
124
|
+
maskName('李四光') // '李*光'
|
|
125
|
+
|
|
109
126
|
// 邮箱脱敏(在 format 模块)
|
|
110
127
|
maskEmail('test@example.com') // 'tes***@example.com'
|
|
111
128
|
```
|
|
@@ -318,6 +335,7 @@ extractNumbers('abc123def456') // [123, 456]
|
|
|
318
335
|
- `maskIdCard(idCard)` - 身份证号脱敏
|
|
319
336
|
- `maskBankCard(cardNumber)` - 银行卡号脱敏
|
|
320
337
|
- `maskName(name)` - 姓名脱敏
|
|
338
|
+
- `maskString(str, visibleChars?, mask?)` - 通用字符串脱敏
|
|
321
339
|
|
|
322
340
|
### 格式验证
|
|
323
341
|
|
package/src/string/index.ts
CHANGED
|
@@ -428,6 +428,31 @@ export function maskName(name: string): string {
|
|
|
428
428
|
return name.charAt(0) + '*'.repeat(name.length - 2) + name.charAt(name.length - 1)
|
|
429
429
|
}
|
|
430
430
|
|
|
431
|
+
/**
|
|
432
|
+
* 通用字符串脱敏:保留前后指定位数,中间用省略号替换
|
|
433
|
+
* @param str 字符串
|
|
434
|
+
* @param visibleChars 前后各保留的字符数,默认 3
|
|
435
|
+
* @param mask 脱敏符号,默认 '...'
|
|
436
|
+
* @returns 脱敏后的字符串
|
|
437
|
+
* @example
|
|
438
|
+
* maskString('123wedwdwddwed567', 3) // '123...567'
|
|
439
|
+
* maskString('abcdefghijk', 2) // 'ab...jk'
|
|
440
|
+
* maskString('hello', 2, '***') // 'he***lo'
|
|
441
|
+
*/
|
|
442
|
+
export function maskString(str: string, visibleChars = 3, mask = '...'): string {
|
|
443
|
+
if (!str) return ''
|
|
444
|
+
|
|
445
|
+
// 如果字符串长度小于等于需要显示的字符数,直接返回原字符串
|
|
446
|
+
if (str.length <= visibleChars * 2) {
|
|
447
|
+
return str
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const start = str.slice(0, visibleChars)
|
|
451
|
+
const end = str.slice(-visibleChars)
|
|
452
|
+
|
|
453
|
+
return `${start}${mask}${end}`
|
|
454
|
+
}
|
|
455
|
+
|
|
431
456
|
/**
|
|
432
457
|
* 判断是否为有效的手机号(中国大陆)
|
|
433
458
|
* @param phone 手机号
|