Appearance
Formatting
Economy returns strings and UI-agnostic view models. It does not render DOM, choose icons or own layout.
ts
import { createEconomy, describeFailure, stateKey } from "@idlekitjs/economy";Amounts
economy.formatAmount(resourceId, amount) formats one amount using the resource formatter, then the economy-wide formatter, then String.
ts
const economy = createEconomy<State>({
format: (amount) => Math.floor(amount).toLocaleString("en-US"),
}).resource({
id: "currency:coins",
label: "Coins",
accessor: stateKey("coins"),
});
economy.formatAmount("currency:coins", 1200); // "1,200"Costs
economy.formatCost(state, cost) returns one FormattedCostLine per normalized line.
ts
const lines = economy.formatCost(state, [["currency:coins", 100]]);
for (const line of lines) {
line.resourceId;
line.label;
line.amount; // formatted text
line.rawAmount;
line.available; // spendable balance
line.affordable;
}Rewards
economy.formatReward(reward) returns FormattedAmount view models without affordability fields.
ts
const rewards = economy.formatReward([["currency:coins", 25]]);Failures
describeFailure(failure, options?) returns a default English sentence for a transaction failure.
ts
const preview = economy.preview(state, transaction);
const text = preview.failures
.map((failure) =>
describeFailure(failure, {
label: (id) => economy.resource(id).label,
}),
)
.join("\n");For localized or styled UIs, switch on failure.kind directly.