@devt110103/utils-js
v1.3.2
Published
A simple utility library for JavaScript and TypeScript
Maintainers
Readme
@devt110103/utils-js
A comprehensive utility library for JavaScript and TypeScript with full type support.
Installation
npm install @devt110103/utils-jsFeatures
- ✅ Full TypeScript Support - Complete type definitions for perfect IntelliSense
- 🌍 Number Formatting - Locale-aware number formatting with currency, percent, and custom options
- 📅 Date Formatting - Comprehensive date formatting with dayjs integration
- 📦 Zero Configuration - Works out of the box with sensible defaults
- 🔧 Highly Customizable - Extensive options for advanced use cases
Usage
Number Formatting
import { formatNumber } from '@devt110103/utils-js';
// Basic usage with locale
formatNumber(1234.56, 'vi-VN'); // "1.234,56"
formatNumber(1234.56, 'en-US'); // "1,234.56"
// Currency formatting
formatNumber(1234.56, {
style: 'currency',
currency: 'VND',
locale: 'vi-VN'
}); // "1.235 ₫"
formatNumber(1234.56, {
style: 'currency',
currency: 'USD',
locale: 'en-US'
}); // "$1,234.56"
// Percentage formatting
formatNumber(0.1234, {
style: 'percent',
minimumFractionDigits: 2
}); // "12.34%"
// Custom decimal places
formatNumber(1234.56789, {
minimumFractionDigits: 2,
maximumFractionDigits: 3
}); // "1,234.568"Date Formatting
import { formatDate } from '@devt110103/utils-js';
const date = new Date('2025-08-01T14:30:00');
// Basic formats
formatDate(date, 'YYYY-MM-DD'); // "2025-08-01"
formatDate(date, 'DD/MM/YYYY'); // "01/08/2025"
formatDate(date, 'MM/DD/YYYY'); // "08/01/2025"
// With time
formatDate(date, 'YYYY-MM-DD HH:mm:ss'); // "2025-08-01 14:30:00"
formatDate(date, 'DD/MM/YYYY h:mm A'); // "01/08/2025 2:30 PM"
// Long formats
formatDate(date, 'MMMM D, YYYY'); // "August 1, 2025"
formatDate(date, 'dddd, MMMM D, YYYY'); // "Friday, August 1, 2025"
// ISO formats
formatDate(date, 'YYYY-MM-DDTHH:mm:ss'); // "2025-08-01T14:30:00"
// Custom format (any dayjs format)
formatDate(date, 'YYYY年MM月DD日'); // "2025年08月01日"TypeScript Support
This library is written in TypeScript and provides full type definitions. When you install the package, you'll get:
- IntelliSense - Auto-completion for all function parameters
- Type Safety - Compile-time checking for correct usage
- Documentation - Hover tooltips with parameter descriptions
- Format Suggestions - Auto-complete for date format strings
Example with TypeScript
import { formatNumber, formatDate } from '@devt110103/utils-js';
// TypeScript will provide auto-completion for format strings
const formatted = formatDate(new Date(), 'YYYY-MM-DD'); // ✅ Suggested formats
const number = formatNumber(1234.56, {
style: 'currency', // ✅ Auto-complete: 'decimal' | 'currency' | 'percent' | 'unit'
currency: 'USD' // ✅ Type-safe
});API Reference
formatNumber(value, options?)
Formats a number with locale and style options.
Parameters:
value: number- The number to formatoptions?: string | object- Locale string or formatting options
Options Object:
locale?: string- BCP 47 language tag (e.g., 'en-US', 'vi-VN')style?: 'decimal' | 'currency' | 'percent' | 'unit'- Number formatting stylecurrency?: string- Currency code (e.g., 'USD', 'VND') - required when style is 'currency'unit?: string- Unit identifier (e.g., 'kilometer') - required when style is 'unit'minimumFractionDigits?: number- Minimum number of decimal placesmaximumFractionDigits?: number- Maximum number of decimal placesuseGrouping?: boolean- Whether to use grouping separators
formatDate(date, format)
Formats a date using dayjs with comprehensive format options.
Parameters:
date: Date- The date to formatformat: string- Format string (with auto-completion for common patterns)
Common Format Patterns:
YYYY-MM-DD- ISO date formatDD/MM/YYYY- European date formatMM/DD/YYYY- US date formatMMMM D, YYYY- Long format with month namedddd, MMMM D, YYYY- Full format with day name- And many more...
License
MIT
Author
DevT (@devt110103)
