sonar 1 left. all regex fixed

This commit is contained in:
Cindy Chang
2024-08-13 13:52:05 +08:00
parent db8f8eefd6
commit 2c0d5fe8ee
3 changed files with 27 additions and 3 deletions

View File

@@ -1,3 +1,20 @@
// sonar-qube replace regex method
const formatNumberWithCommas = (numberStr) => {
// 反轉字符串
let reversedStr = numberStr.split('').reverse().join('');
// 將反轉後的字符串每 3 個字符為一組進行分割
let groupedStr = reversedStr.match(/.{1,3}/g);
// 用逗號將這些組連接起來
let joinedStr = groupedStr.join(',');
// 再次反轉回原來的順序
let finalStr = joinedStr.split('').reverse().join('');
return finalStr;
}
/**
* 將數字轉換成帶有逗號格式(ex: 1000 -> 1,000)
* 也可以改用原生 JS 方法 `.toLocaleString('en-US')`
@@ -6,6 +23,7 @@
*/
export default function numberLabel(num) {
let parts = num.toString().split('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
parts[0] = formatNumberWithCommas(parts[0]);
console.log(parts[0]);
return parts.join('.');
}

View File

@@ -228,7 +228,8 @@ export function formatTime(seconds) {
export function formatMaxTwo(times) {
const formattedTimes = [];
for (let time of times) {
let units = time.match(/\d+[dhms]/g); // 匹配數字和單位(天、小時、分鐘、秒)
// 匹配數字和單位(天、小時、分鐘、秒), 假設數字不可能大於10位數
let units = time.match(/\d{1,10}[dhms]/g);
let formattedTime = '';
let count = 0;