@letanure/resend-cli
Version:
A command-line interface for Resend email API
67 lines • 2.44 kB
JavaScript
/**
* Generic function to format data using field configuration
* Returns array of formatted fields that can be displayed by CLI or TUI
*/
export function formatDataWithFields(data, fields, fieldsToShow) {
const fieldsToDisplay = fieldsToShow || Object.keys(data);
const formattedFields = [];
for (const fieldName of fieldsToDisplay) {
const field = fields.find((f) => f.name === fieldName);
const value = data[fieldName];
if (value === undefined || value === null || value === '') {
continue;
}
// Use field label or fallback to field name
const label = field?.label || fieldName;
// Format the value based on type and content
let displayValue;
if (Array.isArray(value)) {
if (value.length === 0) {
continue;
}
displayValue = value.join(', ');
}
else if (typeof value === 'string') {
// Handle date strings
if (fieldName === 'created_at') {
displayValue = new Date(value).toLocaleString();
}
else if (field?.type === 'textarea' && value.length > 100) {
// Truncate long content
displayValue = `${value.substring(0, 100)}...`;
}
else {
displayValue = value;
}
}
else if (value instanceof Date) {
displayValue = value.toLocaleString();
}
else if (typeof value === 'object') {
displayValue = JSON.stringify(value);
}
else {
displayValue = String(value);
}
formattedFields.push({ label, value: displayValue });
}
return formattedFields;
}
/**
* Format fields for CLI display with dynamic padding based on longest label
*/
export function formatForCLI(formattedFields, title) {
const lines = [];
if (title) {
lines.push(title);
lines.push('');
}
// Calculate the maximum label width for consistent alignment
const maxLabelWidth = formattedFields.length > 0 ? Math.max(...formattedFields.map((field) => field.label.length)) : 15; // fallback to original width
for (const field of formattedFields) {
lines.push(` ${field.label.padEnd(maxLabelWidth)}: ${field.value}`);
}
lines.push('');
return lines.join('\n');
}
//# sourceMappingURL=display-formatter.js.map