Represents a monetary amount with a specific currency.

Money is immutable and all operations return new Money instances. Operations between Money objects with different currencies will fail.

const price = Money.USD(99.99);
const quantity = 3;
const total = price.multiply(quantity);

console.log(total.format()); // "$299.97"

const discount = Money.USD(10);
const finalPrice = total.subtract(discount);
if (finalPrice.isSuccess) {
console.log(finalPrice.value.format()); // "$289.97"
}

Hierarchy (View Summary)

Methods

  • Compares this Value Object with another for equality. Value Objects are equal if they are of the same type and all equality components match.

    Parameters

    • other: ValueObject

      The other Value Object to compare with

    Returns boolean

    true if the Value Objects are equal, false otherwise

  • Creates a Money instance in Chilean Pesos.

    Parameters

    • amount: number

      The amount in Chilean Pesos

    Returns Money

    A Money instance

    const price = Money.CLP(5000);
    
  • Creates a Money instance in Colombian Pesos.

    Parameters

    • amount: number

      The amount in Colombian Pesos.

    Returns Money

    A Money instance

    const price = Money.COP(300);
    
  • Creates a Money instance in Peruvian Sol.

    Parameters

    • amount: number

      The amount in Peruvian Sol.

    Returns Money

    A Money instance

    const price = Money.PEN(5000);
    
  • Creates a Money instance in Argentine Pesos.

    Parameters

    • amount: number

      The amount in Argentine Pesos

    Returns Money

    A Money instance

    const price = Money.ARS(1000);
    
  • Creates a Money instance from a currency code.

    Parameters

    • amount: number

      The monetary amount

    • currencyCode: string

      The ISO 4217 currency code

    Returns Result<Money, DomainError>

    Success with Money or Failure with DomainError

    const result = Money.fromCurrencyCode(100, 'MXN');
    if (result.isSuccess) {
    console.log(result.value.format()); // "$100.00"
    }
  • Creates a Money instance in US Dollars.

    Parameters

    • amount: number

      The amount in USD

    Returns Money

    A Money instance

    const price = Money.USD(99.99);
    
  • Subtracts another Money amount from this one. Both Money objects must have the same currency. Result cannot be negative.

    Parameters

    • other: Money

      The Money to subtract

    Returns Result<Money, DomainError>

    Success with new Money or Failure with DomainError

    const a = Money.USD(100);
    const b = Money.USD(30);
    const result = a.subtract(b);
    // result.value.amount === 70
  • Multiplies this Money by a factor.

    Parameters

    • factor: number

      The multiplication factor

    Returns Money

    A new Money instance

    const price = Money.USD(10);
    const total = price.multiply(3);
    // total.amount === 30
  • Formats this Money as a string according to the locale.

    Parameters

    • locale: string = 'en-US'

      The locale to use for formatting (default: 'en-US')

    Returns string

    A formatted string representation

    const price = Money.USD(1234.56);
    console.log(price.format()); // "$1,234.56"
    console.log(price.format('de-DE')); // "1.234,56 $"
  • Gets all countries that use this money's currency.

    Returns CountryInfo[]

    Array of country information

    const euros = Money.EUR(100);
    const countries = euros.getCountries();
    console.log(countries.map(c => c.name)); // ['France', 'Germany', ...]
  • Checks if this money's currency is used in a specific country.

    Parameters

    • countryCode: string

      The ISO2 country code

    Returns boolean

    true if the currency is used in the country

    const euros = Money.EUR(100);
    console.log(euros.isValidForCountry('FR')); // true
    console.log(euros.isValidForCountry('US')); // false

Properties

amount: number
currency: Currency