Skip to content

Amount

max

1.0.0

Finds the maximum amount from an AmountArray by comparing the values of all amounts in the array.

amountArray.max();
  • Returns a new Amount instance with the largest value
  • Requires all amounts to have the same currency
  • Maintains immutability - original array remains unchanged
  • Compares amounts based on their cent values
  • Throws error for empty arrays or mixed currencies
  • Simple and intuitive API
  • Comprehensive testing
ArgTypeDefault ValueRequired
No arguments required

From an AmountArray instance, use the max() method to find the amount with the largest value.

import { amount, AmountArray } from '@inpulse-ui/utils';
const amounts = new AmountArray(
amount(100, 'USD'),
amount(50, 'USD'),
amount(200, 'USD'), // Maximum value
amount(75, 'USD')
);
const result = amounts.max();
console.log(result); // Results in Amount instance with value 200 USD

It requires all amounts to have the same currency:

const validAmounts = new AmountArray(
amount(33.33, 'EUR'),
amount(33.34, 'EUR'), // Maximum value
amount(33.32, 'EUR')
);
const result = validAmounts.max();
console.log(result); // Results in Amount instance with value 33.34 EUR

It throws an error for mixed currencies:

const mixedAmounts = new AmountArray(
amount(100, 'USD'),
amount(150, 'EUR') // Different currency
);
// This will throw: "All amounts must have the same currency for min/max operations."
const result = mixedAmounts.max();

It throws an error for empty arrays:

const emptyArray = new AmountArray();
// This will throw: "AmountArray must be non-empty."
const result = emptyArray.max();

Works with negative values:

const amountsWithNegatives = new AmountArray(
amount(-100, 'GBP'),
amount(-50, 'GBP'), // Maximum value (least negative)
amount(-75, 'GBP')
);
const result = amountsWithNegatives.max();
console.log(result); // Results in Amount instance with value -50 GBP

Imagine a scenario in an e-commerce application. Based on store settings, you need to find the highest installment amount to present at checkout.

import { amount } from '@inpulse-ui/utils';
// Customer's shopping cart total
const cartTotal = amount(1000, 'USD');
// Split into 6 installments using split() method
const installments = cartTotal.split(6);
console.log(installments); // AmountArray with 6 installment amounts
// Store setting: Apply discount to the lowest installment
// Need to find the highest installment to maintain profit margins
const highestInstallment = installments.max();
console.log(`Highest installment: ${highestInstallment}`);
// Use this value to ensure discount doesn't exceed acceptable limits

Project

Built with by Jo Santana in Brazil.

© 2026 Inpulse. All rights reserved.