Files
monkey-ai-web/src/helpers/safeDisplay.js
T
zhanjunhao 9760e28cb4 fix(*): 全量 UI 审查与 undefined 展示异常修复
分析模块:补齐看板、排行、趋势及报表数值兜底
企业与用户中心:修复统计、成员、技能及资金字段展示
充值与交易:修复充值、发票、订单、订阅及支付字段拼接
平台页面:修复渠道、模型、服务监控与状态空值展示
通用能力:新增安全展示工具,补齐九语言文案与回归测试
文档记录:更新修复范围、影响及回滚说明
2026-11-20 10:18:17 +08:00

69 lines
2.3 KiB
JavaScript

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
export const toFiniteNumber = (value) => {
if (value === null || value === undefined) return null;
if (typeof value === 'string' && value.trim() === '') return null;
const number = Number(value);
return Number.isFinite(number) ? number : null;
};
export const formatPercent = (value, digits = 1, fallback = '-') => {
const number = toFiniteNumber(value);
return number === null ? fallback : `${number.toFixed(digits)}%`;
};
export const formatNumber = (value, digits = 2, fallback = '-') => {
const number = toFiniteNumber(value);
return number === null ? fallback : number.toFixed(digits);
};
export const formatCurrency = (
value,
symbol = '¥',
digits = 2,
fallback = '-',
) => {
const formatted = formatNumber(value, digits, null);
return formatted === null ? fallback : symbol + formatted;
};
export const formatRatioPercent = (part, total, digits = 1, fallback = '-') => {
const numerator = toFiniteNumber(part);
const denominator = toFiniteNumber(total);
if (numerator === null || denominator === null || denominator <= 0) {
return fallback;
}
return formatPercent((numerator / denominator) * 100, digits, fallback);
};
export const formatUserReference = (name, id, fallback = '-') => {
const displayName = typeof name === 'string' ? name.trim() : name;
if (displayName) return String(displayName);
const userId = toFiniteNumber(id);
return userId === null ? fallback : `User#${userId}`;
};
export const formatIdReference = (id, fallback = '-') => {
if (id === null || id === undefined || id === '') return fallback;
const value = String(id).trim();
return value ? `#${value}` : fallback;
};