All checks were successful
CI Monitor MIflow / build-image (push) Successful in 45s
64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
/**
|
|
* 计算百分比变化
|
|
* @param {number} cur - 当前值。
|
|
* @param {number} prev - 之前的值。
|
|
* @returns {{ diff: number, percentage: string }} 包含差值和百分比变化的对象。
|
|
*/
|
|
export const calculatePercentageChange = (cur: number, prev: number) => {
|
|
// 计算差值
|
|
const diff = cur - prev
|
|
|
|
if (diff === 0)
|
|
return {
|
|
diff,
|
|
percentage: "0",
|
|
}
|
|
|
|
// 计算百分比
|
|
const percentage = Math.abs((diff / prev) * 100).toFixed(1)
|
|
|
|
return {
|
|
diff,
|
|
percentage,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 计算周同比
|
|
* @param {string | number} cur - 当前值。
|
|
* @param {string | number} prev - 之前的值。
|
|
* @param {boolean} [needCN=true] - 是否需要中文描述。
|
|
* @returns {{ text: string, diff: number, percentage: string }} 包含描述文本、差值和百分比变化的对象。
|
|
*/
|
|
export const calculateWeeklyRate = (
|
|
cur: string | number,
|
|
prev: string | number,
|
|
needCN = true
|
|
) => {
|
|
const { diff, percentage } = calculatePercentageChange(
|
|
Number(cur),
|
|
Number(prev)
|
|
)
|
|
if (diff > 0)
|
|
return {
|
|
text: `<font color='red'>${
|
|
needCN ? "较上周 " : ""
|
|
}↑${percentage}%</font>`,
|
|
diff,
|
|
percentage,
|
|
}
|
|
if (diff < 0)
|
|
return {
|
|
text: `<font color='green'>${
|
|
needCN ? "较上周 " : ""
|
|
}↓${percentage}%</font>`,
|
|
diff,
|
|
percentage,
|
|
}
|
|
return {
|
|
text: `<font color='gray'>${needCN ? "较上周 " : ""}0%</font>`,
|
|
diff,
|
|
percentage,
|
|
}
|
|
}
|