0.1.4 โข Published 6 months ago
@eriveltonsilva/currency.js v0.1.4
๐ฐ Currency.js โ Currency Manipulation Library
A lightweight, robust JavaScript library for currency operations with precision and reliability. Designed to handle monetary values safely, avoiding floating point issues common in financial calculations. Inspired by the lib of the same name currency.js.
๐ Table of Contents
- 1. ๐ Why Currency.js?
- 2. ๐ฆ Installation
- 3. ๐ Quick Start
- 4. โจ Key Features
- 5. ๐ง Core APIs
- 6. ๐ Documentation
- 7. ๐งช Testing
- 8. ๐ค Contributing
- 9. ๐ License
- 10. ๐ Resources
- 11. ๐ฅ Contributors
1. ๐ Why Currency.js?
Financial operations demand precision. JavaScript's native floating-point math can lead to errors when handling currency values:
// Native JS floating-point issues
0.1 + 0.2 // 0.30000000000000004 โ
19.99 * 0.07 // 1.3993000000000002 โ
(10.25 * 3).toFixed(2) // "30.75" โ (close, but doesn't handle rounding properly)
// With Currency.js
Money(0.1).plus(0.2).value // 0.3 โ
Money(19.99).percentage(7) // 1.40 โ
(properly rounded)
Money(10.25).times(3).value // 30.75 โ
2. ๐ฆ Installation
npm install @eriveltonsilva/currency.js3. ๐ Quick Start
import Money from '@eriveltonsilva/currency.js'
// Create money instances
const price = Money(19.99)
const discount = Money(5)
// Basic operations
const finalPrice = price.minus(discount)
console.log(finalPrice.format()) // $14.99
// Chain operations
const total = Money(100)
.applyDiscount(15) // Apply 15% discount
.plus(4.99) // Add shipping
.format({ currencyCode: 'EUR' }) // Format as euros
console.log(total) // 89.99 โฌ
// Business calculations
const subtotal = Money(125.99)
const installments = subtotal.allocate(3) // [42.00, 42.00, 41.99]4. โจ Key Features
- โ Zero Dependencies: Lightweight implementation (< 5KB min+gzip)
- โ Type-Safe Operations: Full TypeScript support with comprehensive type definitions
- โ Immutable Values: All operations return new Money instances
- โ Precise Calculations: Uses integer-based math to eliminate floating-point errors
- โ Business Operations: Support for discounts, taxes, installments, and more
- โ Flexible Formatting: Internationalization support for 100+ currencies and locales
- โ Simple API: Intuitive method names and chainable operations
5. ๐ง Core APIs
5.1. Creating Money Instances
// Basic creation
const a = Money(10.50) // From number
const b = Money("10.50") // From string
const c = Money(a) // From another Money instance
// With format options
const price = Money(99.99, {
currencyCode: 'EUR',
locale: 'de-DE',
})
// Pre-configured currencies
import { Currency } from '@eriveltonsilva/currency.js'
const usd = Currency.USD(99.99) // $99.99
const eur = Currency.EUR(99.99) // 99.99 โฌ
const brl = Currency.BRL(99.99) // R$ 99,995.2. Essential Operations
// Arithmetic
const sum = price.plus(20) // Addition
const diff = price.minus(5.99) // Subtraction
const doubled = price.times(2) // Multiplication
const half = price.dividedBy(2) // Division
// Comparison
price.equals(99.99) // true
price.greaterThan(50) // true
price.lessThan(100) // false
price.isBetween(50, 150) // true
// Business operations
const tenPercent = price.percentage(10) // 10% of price
const discounted = price.applyDiscount(20) // 20% discount
const total = price.applySurcharge(15) // 15% surcharge6. ๐ Documentation
For comprehensive guides and examples, explore our documentation:
- API Reference - Complete method and property listing
- Formatting Guide - Currency formatting options
- Best Practices - Recommended usage patterns
- Advanced Examples - Real-world implementations
- Important Warnings - Limitations to be aware of
7. ๐งช Testing
# Run the test suite
npm test
# Run the test suite with coverage
npm run test:coverage8. ๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
9. ๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
10. ๐ Resources
11. ๐ฅ Contributors
Thanks to all the people who have contributed to this project:
- Erivelton Silva - Creator
To become a contributor, please see the ๐ค Contributing section.