UNPKG

dev-utils-plus

Version:

Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math

180 lines 5.16 kB
"use strict"; /** * Format utility functions for common formatting tasks */ Object.defineProperty(exports, "__esModule", { value: true }); exports.formatNumber = formatNumber; exports.formatCurrency = formatCurrency; exports.formatPercentage = formatPercentage; exports.formatFileSize = formatFileSize; exports.formatDuration = formatDuration; exports.formatPhoneNumber = formatPhoneNumber; exports.formatCreditCard = formatCreditCard; exports.formatSSN = formatSSN; exports.formatPostalCode = formatPostalCode; exports.formatName = formatName; exports.formatSentence = formatSentence; exports.formatTitle = formatTitle; exports.formatSlug = formatSlug; exports.formatOrdinal = formatOrdinal; /** * Formats a number with commas as thousands separators */ function formatNumber(num, decimals = 0) { return num.toLocaleString('en-US', { minimumFractionDigits: decimals, maximumFractionDigits: decimals }); } /** * Formats a number as currency */ function formatCurrency(amount, currency = 'USD', locale = 'en-US') { return new Intl.NumberFormat(locale, { style: 'currency', currency: currency }).format(amount); } /** * Formats a number as a percentage */ function formatPercentage(value, decimals = 2, locale = 'en-US') { return new Intl.NumberFormat(locale, { style: 'percent', minimumFractionDigits: decimals, maximumFractionDigits: decimals }).format(value / 100); } /** * Formats a file size in human-readable format */ function formatFileSize(bytes) { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } /** * Formats a duration in human-readable format */ function formatDuration(seconds) { if (seconds < 60) { return `${Math.floor(seconds)}s`; } const minutes = Math.floor(seconds / 60); const remainingSeconds = seconds % 60; if (minutes < 60) { return `${minutes}m ${Math.floor(remainingSeconds)}s`; } const hours = Math.floor(minutes / 60); const remainingMinutes = minutes % 60; if (hours < 24) { return `${hours}h ${remainingMinutes}m`; } const days = Math.floor(hours / 24); const remainingHours = hours % 24; return `${days}d ${remainingHours}h`; } /** * Formats a phone number with dashes */ function formatPhoneNumber(phone) { const cleaned = phone.replace(/\D/g, ''); if (cleaned.length === 10) { return `${cleaned.slice(0, 3)}-${cleaned.slice(3, 6)}-${cleaned.slice(6)}`; } if (cleaned.length === 11 && cleaned.startsWith('1')) { return `+1 ${cleaned.slice(1, 4)}-${cleaned.slice(4, 7)}-${cleaned.slice(7)}`; } return phone; } /** * Formats a credit card number with spaces */ function formatCreditCard(cardNumber) { const cleaned = cardNumber.replace(/\D/g, ''); const groups = cleaned.match(/.{1,4}/g); return groups ? groups.join(' ') : cardNumber; } /** * Formats a social security number with dashes */ function formatSSN(ssn) { const cleaned = ssn.replace(/\D/g, ''); if (cleaned.length === 9) { return `${cleaned.slice(0, 3)}-${cleaned.slice(3, 5)}-${cleaned.slice(5)}`; } return ssn; } /** * Formats a postal code */ function formatPostalCode(postalCode) { const cleaned = postalCode.replace(/\D/g, ''); if (cleaned.length === 9) { return `${cleaned.slice(0, 5)}-${cleaned.slice(5)}`; } return cleaned; } /** * Formats a name with proper capitalization */ function formatName(name) { return name .toLowerCase() .split(' ') .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(' '); } /** * Formats a sentence with proper capitalization */ function formatSentence(sentence) { return sentence.charAt(0).toUpperCase() + sentence.slice(1).toLowerCase(); } /** * Formats a title with proper capitalization */ function formatTitle(title) { const smallWords = ['a', 'an', 'and', 'as', 'at', 'but', 'by', 'for', 'if', 'in', 'nor', 'of', 'on', 'or', 'so', 'the', 'to', 'up', 'yet']; return title .toLowerCase() .split(' ') .map((word, index) => { if (index === 0 || index === title.split(' ').length - 1 || !smallWords.includes(word)) { return word.charAt(0).toUpperCase() + word.slice(1); } return word; }) .join(' '); } /** * Formats a slug for URLs */ function formatSlug(text) { return text .toLowerCase() .replace(/[^\w\s-]/g, '') .replace(/[\s_-]+/g, '-') .replace(/^-+|-+$/g, ''); } /** * Formats a number with ordinal suffix */ function formatOrdinal(num) { const j = num % 10; const k = num % 100; if (j === 1 && k !== 11) { return num + 'st'; } if (j === 2 && k !== 12) { return num + 'nd'; } if (j === 3 && k !== 13) { return num + 'rd'; } return num + 'th'; } //# sourceMappingURL=index.js.map