data-formatters
v0.1.3
Published
Reusable TypeScript formatters for numbers, currency, percent and text.
Readme
data-formatters
Utility library for formatting numbers, currency, percentages, dates, time, bytes, and text using modern Intl APIs.
This project provides small, reusable helpers to standardize value formatting in frontend applications.
Features
- Currency formatting
- Compact currency formatting
- Number formatting
- Compact numbers
- Percent formatting
- Compact percent formatting
- Date formatting
- Date/time formatting
- Relative time formatting
- Duration formatting
- Bytes formatting
- Name normalization (Title Case)
- Locale support (
Intl) - Lightweight and dependency-free
Installation
npm install data-formattersor
yarn add data-formattersUsage
import {
formatBytes,
formatCurrency,
formatDate,
formatDateTime,
formatDuration,
formatCompactCurrency,
formatNumber,
formatCompactNumber,
formatPercent,
formatCompactPercent,
formatName,
formatRelativeTime
} from "data-formatters"Currency
formatCurrency(1200, {
currency: "USD",
locale: "en-US"
})Result:
$1,200.00Compact Currency
formatCompactCurrency(1200000, {
currency: "USD",
locale: "en-US"
})Result:
$1.2MNumbers
formatNumber(1200, { locale: "en-US" })Result:
1,200Compact Numbers
formatCompactNumber(1500, { locale: "en-US" })Result:
1.5KPercent
formatPercent(0.25, { locale: "en-US" })Result:
25%Compact Percent
formatCompactPercent(0.1234, { locale: "en-US" })Result:
12.3%Name Formatting
formatName("victor hugo lima")Result:
Victor Hugo LimaBytes
formatBytes(1536)Result:
1.5 KBDate
formatDate("2026-03-09T00:00:00Z", {
locale: "en-US",
timeZone: "UTC"
})Result:
03/09/2026DateTime
formatDateTime("2026-03-09T14:30:00Z", {
locale: "en-US",
timeZone: "UTC",
dateStyle: "medium",
timeStyle: "short"
})Result:
Mar 9, 2026, 2:30 PMDuration
formatDuration(3665)Result:
1h 1m 5sRelative Time
formatRelativeTime(-1, "day", "en-US")Result:
yesterdayLocale Support
The library uses the native Intl API, so formatting behavior depends on the provided locale.
Examples:
| Locale | Number Example |
| ------- | -------------- |
| en-US | 1,000 |
| pt-BR | 1.000 |
| de-DE | 1.000 |
Testing
Tests are written using Vitest. Run locally:
npm testRun in watch mode:
npm run test:watchDevelopment
Clone the repository:
git clone https://github.com/vhlima1008/data-formatters.gitInstall dependencies:
npm installBuild:
npm run buildContributing
Contributions are welcome! Steps:
- Fork the repository
- Create a branch
git checkout -b feature/my-feature- Commit your changes
- Push the branch
- Open a Pull Request
License
Distributed under the MIT License.
See LICENSE for more information.
Author
Victor Hugo Lima Monteiro GitHub: https://github.com/vhlima1008
