Metakit / @doeixd/metakit/domain

Money

namespace · packages/metakit/src/domain/money.ts:9

export * as Money from './domain/money.js'

Fixed-scale integer-backed Money and Currency Schemas + symbolic ops.

Amounts are stored as exact integer minor units (e.g. cents for scale 2), so Tier 3 Zone and Tier 4 Z3 prove financial invariants in exact linear integer arithmetic without JS float drift. Currency & scale mismatches are compile errors. Money shares its implementation and its operations with Decimal.

In the guides

Members

Money.add

const · packages/metakit/src/domain/money.ts:103

const add: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, ValueOfInput<L>>

Add two Money amounts of the same currency and scale.

Money.between

const · packages/metakit/src/domain/money.ts:121

const between: <
  X extends ExprInput<never, MoneyValue<string, number>>,
  Lo extends Same<X>,
  Hi extends Same<X>,
>(
  value: X,
  lo: Lo,
  hi: Hi,
) => Expr<EnvOfInput<X> & EnvOfInput<Lo> & EnvOfInput<Hi>, boolean>

lo <= value <= hi, all of one currency and scale.

Money.currency

function · packages/metakit/src/domain/money.ts:25

function currency<const Codes extends readonly [string, ...Array<string>]>(
  codes: Codes,
): Schema.Literals<Codes>

Construct a Currency literal union Schema with @doeixd/metakit/domain metadata.

Money.format

function · packages/metakit/src/domain/money.ts:92

function format<C extends string, S extends number>(
  money: Schema.Codec<MoneyValue<C, S>, number>,
  amount: NoInfer<MoneyValue<C, S>>,
): string

Format minor units as exact fixed-decimal text with the currency code (1250 USD at scale 2 is "12.50 USD"), both read from money. An amount of another currency or scale does not compile. Throws on an amount that is not a safe integer.

Money.gt

const · packages/metakit/src/domain/money.ts:116

const gt: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Money.gte

const · packages/metakit/src/domain/money.ts:117

const gte: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Money.is

const · packages/metakit/src/domain/money.ts:118

const is: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Money.isNot

const · packages/metakit/src/domain/money.ts:119

const isNot: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Money.lt

const · packages/metakit/src/domain/money.ts:114

const lt: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Money.lte

const · packages/metakit/src/domain/money.ts:115

const lte: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, boolean>

Money.make

function · packages/metakit/src/domain/money.ts:36

function make<const C extends string, const S extends number = 2>(
  code: C,
  options?: {
    readonly scale?: S
  },
): Schema.Codec<MoneyValue<C, S>, number>

Construct an integer-backed Money Schema for currency C and decimal scale (default 2, i.e. minor units like cents).

Money.max

const · packages/metakit/src/domain/money.ts:113

const max: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, ValueOfInput<L>>

The larger of two amounts of the same currency and scale.

Money.min

const · packages/metakit/src/domain/money.ts:111

const min: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, ValueOfInput<L>>

The smaller of two amounts of the same currency and scale.

Money.minor

function · packages/metakit/src/domain/money.ts:64

function minor<C extends string, S extends number>(
  money: Schema.Codec<MoneyValue<C, S>, number>,
  amount: number,
): MoneyValue<C, S>

Construct a typed MoneyValue<C, S> from a safe integer of minor units, with the currency and scale of money. It reads nothing else from money, so an amount is not checked against a refinement of it.

Money.MoneyBrand

const · packages/metakit/src/domain/money.ts:17

const MoneyBrand: unique symbol

Money.MoneyValue

type · packages/metakit/src/domain/money.ts:20

type MoneyValue<C extends string, S extends number = 2> = number & {
  readonly [MoneyBrand]: readonly [currency: C, scale: S]
}

Integer minor-unit amount branded by currency C and decimal scale S.

Money.mul

function · packages/metakit/src/domain/money.ts:133

function mul<
  M extends ExprInput<never, MoneyValue<string, number> | null>,
  R extends ExprInput<never, DecimalValue<number> | null>,
>(
  amount: M,
  rate: R,
  options?: {
    readonly rateScale?: NoInfer<RateScale<R>>
  },
): Expr<
  EnvOfInput<M> & EnvOfInput<R>,
  NonNullable<ValueOfInput<M>> | Fixed.NullIn<M | R>
>

Multiply an amount by a Decimal rate (a tax rate, an exchange rate) into an amount of the same currency and scale, rounding half-even. rateScale must be the rate's own scale. Exact in integers: it builds num.mulScaled, which evaluation, SQL (SQLite and Postgres), the checkers and Z3 read or refuse by name. A missing operand evaluates to null (typed so when an operand may be null); a product outside the safe integer range is refused.

Money.mulInt

const · packages/metakit/src/domain/money.ts:109

const mulInt: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  F extends ExprInput<never, number>,
>(
  amount: L,
  factor: F,
) => Expr<Env2<L, F>, ValueOfInput<L>>

Multiply a Money amount by an integer scalar.

Money.neg

const · packages/metakit/src/domain/money.ts:107

const neg: <X extends ExprInput<never, MoneyValue<string, number>>>(
  value: X,
) => Expr<EnvOfInput<X>, ValueOfInput<X>>

Negate a Money amount.

Money.parse

function · packages/metakit/src/domain/money.ts:77

function parse<C extends string, S extends number>(
  money: Schema.Codec<MoneyValue<C, S>, number>,
  text: string,
): Result.Result<MoneyValue<C, S>, Fixed.InvalidDecimalError>

Parse exact base-10 text ("12.50") into minor units at the scale of money. Refuses text with more fractional digits than the scale and a result outside the safe integer range.

Money.sub

const · packages/metakit/src/domain/money.ts:105

const sub: <
  L extends ExprInput<never, MoneyValue<string, number>>,
  R extends Same<L>,
>(
  left: L,
  right: R,
) => Expr<Env2<L, R>, ValueOfInput<L>>

Subtract two Money amounts of the same currency and scale.