127 lines
4.0 KiB
JavaScript
127 lines
4.0 KiB
JavaScript
/**
|
|
* Formatters - Number and text formatting utilities
|
|
*/
|
|
|
|
/**
|
|
* Format number with decimal places
|
|
* @param {number} value - Number to format
|
|
* @param {number} decimals - Number of decimal places
|
|
* @returns {string} Formatted number
|
|
*/
|
|
export function formatNumber(value, decimals = 2) {
|
|
if (value === null || value === undefined || isNaN(value)) {
|
|
return '0.00';
|
|
}
|
|
return parseFloat(value).toFixed(decimals);
|
|
}
|
|
|
|
/**
|
|
* Format number as percentage
|
|
* @param {number} value - Number to format (0-1 or 0-100)
|
|
* @param {number} decimals - Number of decimal places
|
|
* @param {boolean} isDecimal - If true, value is 0-1, else 0-100
|
|
* @returns {string} Formatted percentage
|
|
*/
|
|
export function formatPercentage(value, decimals = 1, isDecimal = true) {
|
|
if (value === null || value === undefined || isNaN(value)) {
|
|
return '0%';
|
|
}
|
|
const percent = isDecimal ? value * 100 : value;
|
|
return `${percent.toFixed(decimals)}%`;
|
|
}
|
|
|
|
/**
|
|
* Format number with thousands separator
|
|
* @param {number} value - Number to format
|
|
* @param {string} separator - Thousands separator (default: '.')
|
|
* @returns {string} Formatted number
|
|
*/
|
|
export function formatThousands(value, separator = '.') {
|
|
if (value === null || value === undefined || isNaN(value)) {
|
|
return '0';
|
|
}
|
|
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, separator);
|
|
}
|
|
|
|
/**
|
|
* Format currency (BRL)
|
|
* @param {number} value - Value to format
|
|
* @param {boolean} showSymbol - Show R$ symbol
|
|
* @returns {string} Formatted currency
|
|
*/
|
|
export function formatCurrency(value, showSymbol = true) {
|
|
if (value === null || value === undefined || isNaN(value)) {
|
|
return showSymbol ? 'R$ 0,00' : '0,00';
|
|
}
|
|
const formatted = value.toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, '.');
|
|
return showSymbol ? `R$ ${formatted}` : formatted;
|
|
}
|
|
|
|
/**
|
|
* Parse formatted number back to float
|
|
* @param {string} value - Formatted number string
|
|
* @returns {number} Parsed number
|
|
*/
|
|
export function parseFormattedNumber(value) {
|
|
if (!value) return 0;
|
|
// Remove thousands separators and replace comma with dot
|
|
return parseFloat(value.toString().replace(/\./g, '').replace(',', '.')) || 0;
|
|
}
|
|
|
|
/**
|
|
* Truncate text with ellipsis
|
|
* @param {string} text - Text to truncate
|
|
* @param {number} maxLength - Maximum length
|
|
* @returns {string} Truncated text
|
|
*/
|
|
export function truncateText(text, maxLength = 50) {
|
|
if (!text || text.length <= maxLength) return text;
|
|
return text.substring(0, maxLength - 3) + '...';
|
|
}
|
|
|
|
/**
|
|
* Capitalize first letter
|
|
* @param {string} text - Text to capitalize
|
|
* @returns {string} Capitalized text
|
|
*/
|
|
export function capitalize(text) {
|
|
if (!text) return '';
|
|
return text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
|
|
}
|
|
|
|
/**
|
|
* Format date to Brazilian format
|
|
* @param {Date|string} date - Date to format
|
|
* @returns {string} Formatted date (DD/MM/YYYY)
|
|
*/
|
|
export function formatDate(date) {
|
|
if (!date) return '';
|
|
const d = new Date(date);
|
|
if (isNaN(d.getTime())) return '';
|
|
|
|
const day = String(d.getDate()).padStart(2, '0');
|
|
const month = String(d.getMonth() + 1).padStart(2, '0');
|
|
const year = d.getFullYear();
|
|
|
|
return `${day}/${month}/${year}`;
|
|
}
|
|
|
|
/**
|
|
* Format date and time to Brazilian format
|
|
* @param {Date|string} date - Date to format
|
|
* @returns {string} Formatted date and time (DD/MM/YYYY HH:MM)
|
|
*/
|
|
export function formatDateTime(date) {
|
|
if (!date) return '';
|
|
const d = new Date(date);
|
|
if (isNaN(d.getTime())) return '';
|
|
|
|
const day = String(d.getDate()).padStart(2, '0');
|
|
const month = String(d.getMonth() + 1).padStart(2, '0');
|
|
const year = d.getFullYear();
|
|
const hours = String(d.getHours()).padStart(2, '0');
|
|
const minutes = String(d.getMinutes()).padStart(2, '0');
|
|
|
|
return `${day}/${month}/${year} ${hours}:${minutes}`;
|
|
}
|