mirror of
https://github.com/catlog22/Claude-Code-Workflow.git
synced 2026-02-12 02:37:45 +08:00
refactor(notifications): 优化JSON格式化函数,增强可读性和错误处理
This commit is contained in:
@@ -5,37 +5,80 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Format JSON object for display in notifications
|
* Format JSON object for display in notifications
|
||||||
* @param {Object} obj - Object to format
|
* Parses JSON strings and formats objects into readable key-value pairs
|
||||||
* @param {number} maxLen - Max string length
|
* @param {Object|string} obj - Object or JSON string to format
|
||||||
* @returns {string} Formatted string
|
* @param {number} maxLen - Max string length (unused, kept for compatibility)
|
||||||
|
* @returns {string} Formatted string with key: value pairs
|
||||||
*/
|
*/
|
||||||
function formatJsonDetails(obj, maxLen = 150) {
|
function formatJsonDetails(obj, maxLen = 150) {
|
||||||
if (!obj || typeof obj !== 'object') return String(obj);
|
// Handle null/undefined
|
||||||
|
if (obj === null || obj === undefined) return '';
|
||||||
|
|
||||||
// Try pretty format first
|
// If it is a string, try to parse as JSON
|
||||||
try {
|
if (typeof obj === 'string') {
|
||||||
const formatted = JSON.stringify(obj, null, 2);
|
// Check if it looks like JSON
|
||||||
if (formatted.length <= maxLen) {
|
const trimmed = obj.trim();
|
||||||
return formatted;
|
if ((trimmed.startsWith('{') && trimmed.endsWith('}')) ||
|
||||||
|
(trimmed.startsWith('[') && trimmed.endsWith(']'))) {
|
||||||
|
try {
|
||||||
|
obj = JSON.parse(trimmed);
|
||||||
|
} catch (e) {
|
||||||
|
// Not valid JSON, return as-is
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Plain string, return as-is
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// For longer content, show key-value pairs on separate lines
|
// Handle non-objects (numbers, booleans, etc.)
|
||||||
|
if (typeof obj !== 'object') return String(obj);
|
||||||
|
|
||||||
|
// Handle arrays
|
||||||
|
if (Array.isArray(obj)) {
|
||||||
|
if (obj.length === 0) return '(empty array)';
|
||||||
|
return obj.slice(0, 5).map((item, i) => {
|
||||||
|
const itemStr = typeof item === 'object' ? JSON.stringify(item) : String(item);
|
||||||
|
return `[${i}] ${itemStr.length > 50 ? itemStr.substring(0, 47) + '...' : itemStr}`;
|
||||||
|
}).join('\n') + (obj.length > 5 ? `\n... +${obj.length - 5} more` : '');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle objects - format as readable key: value pairs
|
||||||
|
try {
|
||||||
const entries = Object.entries(obj);
|
const entries = Object.entries(obj);
|
||||||
if (entries.length === 0) return '{}';
|
if (entries.length === 0) return '(empty object)';
|
||||||
|
|
||||||
const lines = entries.slice(0, 5).map(([key, val]) => {
|
// Format each entry with proper value display
|
||||||
let valStr = typeof val === 'object' ? JSON.stringify(val) : String(val);
|
const lines = entries.slice(0, 8).map(([key, val]) => {
|
||||||
if (valStr.length > 50) valStr = valStr.substring(0, 47) + '...';
|
let valStr;
|
||||||
|
if (val === null) {
|
||||||
|
valStr = 'null';
|
||||||
|
} else if (val === undefined) {
|
||||||
|
valStr = 'undefined';
|
||||||
|
} else if (typeof val === 'boolean') {
|
||||||
|
valStr = val ? 'true' : 'false';
|
||||||
|
} else if (typeof val === 'number') {
|
||||||
|
valStr = String(val);
|
||||||
|
} else if (typeof val === 'object') {
|
||||||
|
valStr = JSON.stringify(val);
|
||||||
|
if (valStr.length > 40) valStr = valStr.substring(0, 37) + '...';
|
||||||
|
} else {
|
||||||
|
valStr = String(val);
|
||||||
|
if (valStr.length > 50) valStr = valStr.substring(0, 47) + '...';
|
||||||
|
}
|
||||||
return `${key}: ${valStr}`;
|
return `${key}: ${valStr}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (entries.length > 5) {
|
if (entries.length > 8) {
|
||||||
lines.push(`... +${entries.length - 5} more`);
|
lines.push(`... +${entries.length - 8} more fields`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return lines.join('\n');
|
return lines.join('\n');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return JSON.stringify(obj).substring(0, maxLen) + '...';
|
// Fallback to stringified version
|
||||||
|
const str = JSON.stringify(obj);
|
||||||
|
return str.length > 200 ? str.substring(0, 197) + '...' : str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user