@chain1/format-utils
v1.0.0
Published
Number, currency, and date formatting utilities for blockchain applications
Maintainers
Readme
@chain1/format-utils
Number, currency, and date formatting utilities for blockchain applications.
Installation
npm install @chain1/format-utilsFeatures
- 📊 Number Formatting: Thousand separators, compact notation (K/M/B)
- 💰 Currency Formatting: Multi-currency support (KRW, USD, CNY)
- 📅 Date Formatting: Multiple date formats, timezone handling
- 🚀 Zero Dependencies: Lightweight and fast
- 📝 TypeScript Support: Full type definitions included
Usage
Number Formatting
import { numberWithCommas, formatCompactNumber, formatDecimal } from '@chain1/format-utils';
// Add thousand separators
numberWithCommas(1000000); // "1,000,000"
numberWithCommas(1234.56); // "1,234.56"
// Compact notation
formatCompactNumber(1500); // "1.5K"
formatCompactNumber(1500000); // "1.5M"
formatCompactNumber(1500000000); // "1.5B"
// Decimal formatting
formatDecimal(1.23456, 2); // "1.23"
formatDecimal(1.23456, 4); // "1.2346"Currency Formatting
import { formatDisplayAmount, formatAmount } from '@chain1/format-utils';
// Korean Won (no decimals)
formatDisplayAmount(1000000, 'krw'); // "1,000,000"
// US Dollar (2 decimals)
formatDisplayAmount(1000.5, 'usd'); // "1,000.50"
// Chinese Yuan
formatDisplayAmount(1000.5, 'cny'); // "1,000.50"
// Small amounts (6 decimals for values < 1)
formatDisplayAmount(0.000123, 'usd'); // "0.000123"Date Formatting
import { formatDate, getTodayTimeRange, isToday } from '@chain1/format-utils';
// Default format
formatDate('2024-11-10T12:30:00Z'); // "2024-11-10 12:30"
// Korean format
formatDate('2024-11-10T12:30:00Z', 'korean'); // "2024년 11월 10일 12:30"
// Short format
formatDate('2024-11-10T12:30:00Z', 'short'); // "11/10 12:30"
// Time only
formatDate('2024-11-10T12:30:00Z', 'time'); // "12:30"
// Date only
formatDate('2024-11-10T12:30:00Z', 'date'); // "2024-11-10"
// Get today's time range
const { startTime, endTime } = getTodayTimeRange();
// Check if date is today
isToday(new Date()); // trueAPI Reference
Number Utils
numberWithCommas(number: number | string): string
Add thousand separators to a number.
formatDecimal(number: number | string, decimals: number = 2): string
Format number with limited decimal places.
formatCompactNumber(number: number | string): string
Format number in compact notation (K/M/B).
Currency Utils
formatDisplayAmount(amount: number | string, currency: string = 'usd'): string
Format amount based on currency type.
formatAmount(amount: number | string, currency: string = 'usd'): string
Alias for formatDisplayAmount.
Date Utils
formatDate(date: string | Date, format: DateFormat = 'default'): string
Format date to specified format.
Formats:
'default': "YYYY-MM-DD HH:MM"'korean': "YYYY년 MM월 DD일 HH:MM"'short': "MM/DD HH:MM"'time': "HH:MM"'date': "YYYY-MM-DD"'korean-date': "YYYY년 MM월 DD일"
getTodayTimeRange(): { startTime: string; endTime: string }
Get today's start and end time as ISO strings.
isToday(date: string | Date): boolean
Check if a date is today.
License
MIT
Author
Stochain
