Skip to content

eriveltondasilva/currency.js

Repository files navigation

πŸ’° Currency.js β€” Currency Manipulation Library

npm Node Typescript Zero Dependencies Size
CI Tests License

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.

Currency.js Banner

πŸ“– Table of Contents

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.js

3. πŸ” 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,99

5.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% surcharge

6. πŸ“š Documentation

For comprehensive guides and examples, explore our documentation:

7. πŸ§ͺ Testing

# Run the test suite
npm test

# Run the test suite with coverage
npm run test:coverage

8. 🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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:

To become a contributor, please see the 🀝 Contributing section.

About

A lightweight and reliable JavaScript library for precise currency operations, built to safely handle monetary values without floating point errors.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors