react-util-tools 1.0.27 → 1.0.28
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 +119 -0
- package/dist/index.d.cts +14 -1
- package/dist/index.d.ts +14 -1
- package/dist/index.js +112 -0
- package/package.json +1 -1
- package/src/index.ts +9 -1
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,
|
|
@@ -161,6 +165,7 @@ __export(index_exports, {
|
|
|
161
165
|
readExcelToJSON: () => readExcelToJSON,
|
|
162
166
|
readFile: () => readFile,
|
|
163
167
|
removeCookie: () => removeCookie,
|
|
168
|
+
removeInvalidZero: () => removeInvalidZero,
|
|
164
169
|
removeSpaces: () => removeSpaces,
|
|
165
170
|
repeat: () => repeat,
|
|
166
171
|
replaceAll: () => replaceAll,
|
|
@@ -184,6 +189,7 @@ __export(index_exports, {
|
|
|
184
189
|
titleCase: () => titleCase,
|
|
185
190
|
toBase64: () => toBase64,
|
|
186
191
|
toISOString: () => toISOString,
|
|
192
|
+
toLocalString: () => toLocalString,
|
|
187
193
|
toLowerCase: () => toLowerCase,
|
|
188
194
|
toUTC: () => toUTC,
|
|
189
195
|
toUpperCase: () => toUpperCase,
|
|
@@ -191,6 +197,7 @@ __export(index_exports, {
|
|
|
191
197
|
trimEnd: () => trimEnd,
|
|
192
198
|
trimStart: () => trimStart,
|
|
193
199
|
truncate: () => truncate,
|
|
200
|
+
tryRun: () => tryRun,
|
|
194
201
|
unescapeHtml: () => unescapeHtml,
|
|
195
202
|
unmaskEmail: () => unmaskEmail,
|
|
196
203
|
utils: () => utils2,
|
|
@@ -601,6 +608,111 @@ function unmaskEmail(maskedEmail, originalEmail) {
|
|
|
601
608
|
}
|
|
602
609
|
return maskedEmail;
|
|
603
610
|
}
|
|
611
|
+
function toLocalString(value) {
|
|
612
|
+
let result = value;
|
|
613
|
+
if (Number(value) >= 1e3) {
|
|
614
|
+
result = Number(value).toLocaleString("en-US");
|
|
615
|
+
} else {
|
|
616
|
+
result = value;
|
|
617
|
+
}
|
|
618
|
+
return result;
|
|
619
|
+
}
|
|
620
|
+
var integerTokenArr = ["SATS"];
|
|
621
|
+
function removeInvalidZero(num) {
|
|
622
|
+
let result = num;
|
|
623
|
+
if (num.includes(".")) {
|
|
624
|
+
result = result.replace(/\.?0+$/, "");
|
|
625
|
+
}
|
|
626
|
+
return result;
|
|
627
|
+
}
|
|
628
|
+
function formatePrecision(n, precision, tokenSymbol) {
|
|
629
|
+
if (!isNaN(Number(n))) {
|
|
630
|
+
let prec = precision && precision <= 8 ? precision : 8;
|
|
631
|
+
if (tokenSymbol && integerTokenArr.includes(tokenSymbol)) {
|
|
632
|
+
prec = 0;
|
|
633
|
+
}
|
|
634
|
+
return new import_decimal.default(Number(n)).toFixed(prec, import_decimal.default.ROUND_DOWN);
|
|
635
|
+
}
|
|
636
|
+
const num = new import_decimal.default(n).toNumber();
|
|
637
|
+
return num;
|
|
638
|
+
}
|
|
639
|
+
function formateAmount({
|
|
640
|
+
num = 0,
|
|
641
|
+
precision = 8,
|
|
642
|
+
tokenSymbol = "",
|
|
643
|
+
type = "0"
|
|
644
|
+
}) {
|
|
645
|
+
return tryRun(() => {
|
|
646
|
+
const dec = new import_decimal.default(num);
|
|
647
|
+
const { length: length2 } = dec.abs().floor().toString();
|
|
648
|
+
const pres = precision;
|
|
649
|
+
precision = precision > 8 ? 9 : precision + 1;
|
|
650
|
+
if (length2 >= precision) {
|
|
651
|
+
let value = "";
|
|
652
|
+
if (dec.greaterThan(1)) {
|
|
653
|
+
value = dec.toFixed(8, import_decimal.default.ROUND_DOWN);
|
|
654
|
+
} else {
|
|
655
|
+
value = dec.toFixed(precision - length2, import_decimal.default.ROUND_DOWN);
|
|
656
|
+
}
|
|
657
|
+
value = formatePrecision(value, pres).toString();
|
|
658
|
+
if (integerTokenArr.includes(tokenSymbol)) {
|
|
659
|
+
value = new import_decimal.default(value).toFixed(0, import_decimal.default.ROUND_CEIL);
|
|
660
|
+
}
|
|
661
|
+
let result = "";
|
|
662
|
+
if (typeof value === "string") {
|
|
663
|
+
if (value.includes(".")) {
|
|
664
|
+
result = value.replace(/\d(?=(\d{3})+\.)/g, "$&,");
|
|
665
|
+
} else {
|
|
666
|
+
result = toLocalString(value);
|
|
667
|
+
}
|
|
668
|
+
} else {
|
|
669
|
+
result = value;
|
|
670
|
+
}
|
|
671
|
+
if (Number(num) === 0) {
|
|
672
|
+
return result;
|
|
673
|
+
} else {
|
|
674
|
+
return dec.greaterThan(new import_decimal.default(0)) ? removeInvalidZero(result) : type === "0" ? "-" + removeInvalidZero(result) : removeInvalidZero(result);
|
|
675
|
+
}
|
|
676
|
+
} else {
|
|
677
|
+
let value = "";
|
|
678
|
+
if (dec.greaterThan(1)) {
|
|
679
|
+
value = dec.toFixed(8, import_decimal.default.ROUND_DOWN);
|
|
680
|
+
} else {
|
|
681
|
+
value = dec.toFixed(precision - length2, import_decimal.default.ROUND_DOWN);
|
|
682
|
+
}
|
|
683
|
+
value = formatePrecision(value, pres).toString();
|
|
684
|
+
if (integerTokenArr.includes(tokenSymbol)) {
|
|
685
|
+
value = new import_decimal.default(value).toFixed(0, import_decimal.default.ROUND_CEIL);
|
|
686
|
+
}
|
|
687
|
+
let result = "";
|
|
688
|
+
if (typeof value === "string") {
|
|
689
|
+
if (value.includes(".")) {
|
|
690
|
+
result = value.replace(/\d(?=(\d{3})+\.)/g, "$&,");
|
|
691
|
+
} else {
|
|
692
|
+
result = toLocalString(value);
|
|
693
|
+
}
|
|
694
|
+
} else {
|
|
695
|
+
result = value;
|
|
696
|
+
}
|
|
697
|
+
return removeInvalidZero(result);
|
|
698
|
+
}
|
|
699
|
+
}) ?? "0.00";
|
|
700
|
+
}
|
|
701
|
+
function formateFaitAmount(num = 0) {
|
|
702
|
+
return tryRun(() => {
|
|
703
|
+
const dec = new import_decimal.default(num);
|
|
704
|
+
const fnum = dec.toFixed(2, import_decimal.default.ROUND_DOWN);
|
|
705
|
+
if (Number(num) >= 1e3) {
|
|
706
|
+
let result = toLocalString(fnum);
|
|
707
|
+
if (!result.includes(".")) {
|
|
708
|
+
result = result + ".00";
|
|
709
|
+
}
|
|
710
|
+
return result;
|
|
711
|
+
} else {
|
|
712
|
+
return fnum;
|
|
713
|
+
}
|
|
714
|
+
}) ?? "0.00";
|
|
715
|
+
}
|
|
604
716
|
function tryRun(fn) {
|
|
605
717
|
try {
|
|
606
718
|
return fn();
|
|
@@ -1358,6 +1470,9 @@ function fromBase64(base64) {
|
|
|
1358
1470
|
formatUTC,
|
|
1359
1471
|
formatUTCDateOnly,
|
|
1360
1472
|
formatUTCTimeOnly,
|
|
1473
|
+
formateAmount,
|
|
1474
|
+
formateFaitAmount,
|
|
1475
|
+
formatePrecision,
|
|
1361
1476
|
fromBase64,
|
|
1362
1477
|
fromUTC,
|
|
1363
1478
|
getAllCookies,
|
|
@@ -1414,6 +1529,7 @@ function fromBase64(base64) {
|
|
|
1414
1529
|
greaterThanOrEqual,
|
|
1415
1530
|
hasCookie,
|
|
1416
1531
|
includes,
|
|
1532
|
+
integerTokenArr,
|
|
1417
1533
|
isAfterDate,
|
|
1418
1534
|
isAndroid,
|
|
1419
1535
|
isBeforeDate,
|
|
@@ -1455,6 +1571,7 @@ function fromBase64(base64) {
|
|
|
1455
1571
|
readExcelToJSON,
|
|
1456
1572
|
readFile,
|
|
1457
1573
|
removeCookie,
|
|
1574
|
+
removeInvalidZero,
|
|
1458
1575
|
removeSpaces,
|
|
1459
1576
|
repeat,
|
|
1460
1577
|
replaceAll,
|
|
@@ -1478,6 +1595,7 @@ function fromBase64(base64) {
|
|
|
1478
1595
|
titleCase,
|
|
1479
1596
|
toBase64,
|
|
1480
1597
|
toISOString,
|
|
1598
|
+
toLocalString,
|
|
1481
1599
|
toLowerCase,
|
|
1482
1600
|
toUTC,
|
|
1483
1601
|
toUpperCase,
|
|
@@ -1485,6 +1603,7 @@ function fromBase64(base64) {
|
|
|
1485
1603
|
trimEnd,
|
|
1486
1604
|
trimStart,
|
|
1487
1605
|
truncate,
|
|
1606
|
+
tryRun,
|
|
1488
1607
|
unescapeHtml,
|
|
1489
1608
|
unmaskEmail,
|
|
1490
1609
|
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;
|
|
@@ -234,4 +247,4 @@ declare function isValidIdCard(idCard: string): boolean;
|
|
|
234
247
|
declare function toBase64(str: string): string;
|
|
235
248
|
declare function fromBase64(base64: string): string;
|
|
236
249
|
|
|
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 };
|
|
250
|
+
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, 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;
|
|
@@ -234,4 +247,4 @@ declare function isValidIdCard(idCard: string): boolean;
|
|
|
234
247
|
declare function toBase64(str: string): string;
|
|
235
248
|
declare function fromBase64(base64: string): string;
|
|
236
249
|
|
|
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 };
|
|
250
|
+
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, 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();
|
|
@@ -1196,6 +1301,9 @@ export {
|
|
|
1196
1301
|
formatUTC,
|
|
1197
1302
|
formatUTCDateOnly,
|
|
1198
1303
|
formatUTCTimeOnly,
|
|
1304
|
+
formateAmount,
|
|
1305
|
+
formateFaitAmount,
|
|
1306
|
+
formatePrecision,
|
|
1199
1307
|
fromBase64,
|
|
1200
1308
|
fromUTC,
|
|
1201
1309
|
getAllCookies,
|
|
@@ -1252,6 +1360,7 @@ export {
|
|
|
1252
1360
|
greaterThanOrEqual,
|
|
1253
1361
|
hasCookie,
|
|
1254
1362
|
includes,
|
|
1363
|
+
integerTokenArr,
|
|
1255
1364
|
isAfterDate,
|
|
1256
1365
|
isAndroid,
|
|
1257
1366
|
isBeforeDate,
|
|
@@ -1293,6 +1402,7 @@ export {
|
|
|
1293
1402
|
readExcelToJSON,
|
|
1294
1403
|
readFile,
|
|
1295
1404
|
removeCookie,
|
|
1405
|
+
removeInvalidZero,
|
|
1296
1406
|
removeSpaces,
|
|
1297
1407
|
repeat,
|
|
1298
1408
|
replaceAll,
|
|
@@ -1316,6 +1426,7 @@ export {
|
|
|
1316
1426
|
titleCase,
|
|
1317
1427
|
toBase64,
|
|
1318
1428
|
toISOString,
|
|
1429
|
+
toLocalString,
|
|
1319
1430
|
toLowerCase,
|
|
1320
1431
|
toUTC,
|
|
1321
1432
|
toUpperCase,
|
|
@@ -1323,6 +1434,7 @@ export {
|
|
|
1323
1434
|
trimEnd,
|
|
1324
1435
|
trimStart,
|
|
1325
1436
|
truncate,
|
|
1437
|
+
tryRun,
|
|
1326
1438
|
unescapeHtml,
|
|
1327
1439
|
unmaskEmail,
|
|
1328
1440
|
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.28",
|
|
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,
|