Functional Weave
Code in Rust

money.compare@2.0.0

impl/python/clamp_money.py

614 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from .math_clamp import clamp  ← from math.clamp ^1.0.0 · built alongside by fune
from .money_amount import Money, assert_same_currency, money  ← from money.amount ^1.0.0 · built alongside by fune


def clamp_money(amount: Money, min: Money, max: Money) -> Money:  # noqa: A002 - the manifest names them min and max
    """``amount`` held within ``min..max``, all three in one currency.

    Bounds the wrong way round are an error (from math.clamp), not an answer:
    capping a refund at the original charge with the bounds swapped is a bug
    to surface.
    """
    assert_same_currency(amount, min)
    assert_same_currency(amount, max)
    return money(clamp(amount.minor, min.minor, max.minor), amount.currency)