Millwork
    Preparing search index...

    Module @millwork-ts/dinero

    @millwork-ts/dinero

    Millwork codecs for money as dinero.js v2 objects.

    pnpm add @millwork-ts/dinero @millwork-ts/core dinero.js
    
    Codec Value Raw
    dinero Dinero<number> or Dinero<bigint> one of five wire forms

    The wire option selects the wire form. The names are the same as the money codec of @millwork-ts/core.

    wire Raw
    'minor' { "amount": 1999, "currency": "USD" } (the default)
    'decimal' { "amount": "19.99", "currency": "USD" }
    'snapshot' { "amount": 1999, "currency": { "code": "USD", "base": 10, "exponent": 2 }, "scale": 2 }
    'minor-scaled' { "amount": 19990, "currency": "USD", "scale": 3 }
    'amount' 1999, with the currency in the currency option
    • The codec reads a number amount, such as 19.99, on the 'decimal' wire. It never writes a float.
    • An amount that is not a safe integer is written as a string, such as "9007199254740993".
    • The 'minor' and 'amount' wires write the amount at the exponent of the currency. If digits are lost, serialize throws a RangeError.
    • The 'snapshot' wire keeps the currency object of the wire. The base must be 10.
    Option Type Use
    wire MoneyWire The wire form. The default is 'minor'.
    currency string The currency code for the 'amount' wire.
    currencies Record<string, Currency> More currencies, keyed by code. They replace a dinero.js currency.
    bigint boolean Make every amount a bigint.

    The codec finds a currency code in the ISO 4217 currency objects of dinero.js. A lower-case code is accepted.

    Without the bigint option, the amount is a number when it is a safe integer. Other amounts use dinero.js/bigint, and the value is a Dinero<bigint>. dinero.js cannot add a Dinero<number> to a Dinero<bigint>. If your amounts can be larger than Number.MAX_SAFE_INTEGER, set bigint: true. Then all values have one amount type.

    equals is true when the currency codes are the same and the amounts are equal at a common scale. 19.99 USD at scale 2 equals 19.990 USD at scale 3. It uses the dinero.js equal function.

    Register the codecs before the store calls transformations(), because transformations() reads the registry when it runs. See the @millwork-ts/warp-drive README.

    import { codecs } from '@millwork-ts/dinero'
    import { registerCodecs, valueField } from '@millwork-ts/warp-drive'
    import { withDefaults } from '@warp-drive/core/reactive'

    registerCodecs(codecs)

    export const OrderSchema = withDefaults({
    type: 'order',
    fields: [valueField('total', 'dinero', { wire: 'decimal' })],
    })
    DineroOptions
    AnyDinero
    Currency
    DineroRaw
    codecs
    dinero