formatting-utils
v1.0.1
Published
A utility package to format various data types such as dates, numbers, strings, phone numbers, and URLs.
Downloads
1
Maintainers
Readme
Formatting Utils
A utility package to format various data types such as dates, numbers, and strings.
Installation
npm install formatting-utils
Usage
const { formatDate, customFormatDate, formatTime, formatNumber, capitalize, decapitalize, formatPhoneNumber, dateDifference, trimString, truncateString, formatURL, pluralize } = require('formatting-utils');
// Test formatDate
const date = new Date();
console.log('Short date format:', formatDate(date, 'short')); // Example: Jan 1, 2024
console.log('Long date format:', formatDate(date, 'long')); // Example: Monday, January 1, 2024
console.log('Custom date format (DD-YYYY-MM):', customFormatDate(date, 'DD-YYYY-MM')); // Example: 01-2024-01
console.log('Custom date format (MM-YYYY-DD):', customFormatDate(date, 'MM-YYYY-DD')); // Example: 01-2024-01
// Test formatTime
console.log('Short time format (12-hour):', formatTime(date, 'short', true)); // Example: 12:34 PM
console.log('Short time format (24-hour):', formatTime(date, 'short', false)); // Example: 12:34
console.log('Medium time format (12-hour):', formatTime(date, 'medium', true)); // Example: 12:34:56 PM
console.log('Medium time format (24-hour):', formatTime(date, 'medium', false)); // Example: 12:34:56
console.log('Long time format (12-hour):', formatTime(date, 'long', true)); // Example: 12:34:56 PM GMT
console.log('Long time format (24-hour):', formatTime(date, 'long', false)); // Example: 12:34:56 GMT
// Test formatNumber
console.log('Decimal number format:', formatNumber(1234567.89)); // Example: 1,234,567.89
console.log('Currency number format:', formatNumber(1234567.89, 'currency')); // Default currency: $1,234,567.89
console.log('Currency number format with EUR:', formatNumber(1234567.89, 'currency', 'EUR')); // Example: €1,234,567.89
// Test capitalize
console.log('Capitalized string:', capitalize('hello world')); // Example: Hello World
// Test decapitalize
console.log('Decapitalized string:', decapitalize('Hello World')); // Example: hello world
// Test formatPhoneNumber
console.log('Formatted phone number (US):', formatPhoneNumber('1234567890', 'US')); // Example: (123) 456-7890
// Test dateDifference
const date1 = new Date('2024-01-01');
const date2 = new Date('2024-12-31');
console.log('Date difference in days:', dateDifference(date1, date2, 'days')); // Example: 365
console.log('Date difference in months:', dateDifference(date1, date2, 'months')); // Example: 12
console.log('Date difference in years:', dateDifference(date1, date2, 'years')); // Example: 1
// Test trimString
console.log('Trimmed string:', trimString(' Hello World ')); // Example: Hello World
// Test truncateString
console.log('Truncated string:', truncateString('Hello World', 5)); // Example: Hello...
// Test formatURL
console.log('Formatted URL (default https):', formatURL('example.com')); // Example: https://example.com
console.log('Formatted URL (http):', formatURL('example.com', 'http')); // Example: http://example.com
// Test pluralize
console.log('Pluralized word (1 apple):', pluralize('apple', 1)); // Example: apple
console.log('Pluralized word (2 apples):', pluralize('apple', 2)); // Example: apples
Functions
- formatDate(date, format): Formats a date according to the specified format ('short', 'long'). For custom formats, use
customFormatDate
. - customFormatDate(date, format): Custom date formatting using 'DD', 'MM', 'YYYY' placeholders.
- formatTime(date, format, hour12): Formats a time according to the specified format ('short', 'medium', 'long') and whether to use 12-hour clock (default
false
for 24-hour). - formatNumber(number, style, currency, locale): Formats a number according to the specified style ('decimal', 'currency', 'percent') and locale (default 'en-US').
- capitalize(str): Capitalizes the first letter of each word in a string.
- decapitalize(str): Decapitalizes the first letter of each word in a string.
- formatPhoneNumber(number, country): Formats a phone number based on the specified country (default 'US'). (RIGHT NOW ONLY THERE IS US)
- dateDifference(date1, date2, unit): Calculates the difference between two dates in the specified unit ('days', 'months', 'years').
- trimString(str): Trims whitespace from both ends of a string.
- truncateString(str, length): Truncates a string to the specified length, appending '...' if truncated.
- formatURL(url, protocol): Ensures a URL starts with the specified protocol (default 'https').
- pluralize(word, count): Returns the plural form of a word based on the count.
- formatNumber(number, style): Formats a number according to the specified style ('decimal', 'currency', 'percent').