0.1.4 โ€ข Published 6 months ago

@eriveltonsilva/currency.js v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

๐Ÿ’ฐ 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.

๐Ÿ“– 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.

0.1.4

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago

0.0.6

6 months ago

0.0.5

6 months ago

0.0.4

6 months ago

0.0.3

6 months ago

0.0.2

6 months ago

0.0.1

6 months ago