separate types

This commit is contained in:
Oli Sturm
2026-04-24 18:15:22 +01:00
parent b1ac745d70
commit d0c9466170
13 changed files with 178 additions and 221 deletions
+53
View File
@@ -0,0 +1,53 @@
import { Result } from '../library/result.js';
import { Money } from './money.js';
export type Account = {
id: string;
balance: Money;
};
export type OpeningBalanceMustBeNonNegative = {
type: 'OpeningBalanceMustBeNonNegative';
};
export type AmountMustBePositive = {
type: 'AmountMustBePositive';
};
export type InsufficientBalance = {
type: 'InsufficientBalance';
balance: Money;
amount: Money;
};
export type AccountNotFound = {
type: 'AccountNotFound';
accountId: string;
};
export type AccountError =
| OpeningBalanceMustBeNonNegative
| AmountMustBePositive
| InsufficientBalance
| AccountNotFound;
export declare const openingBalanceMustBeNonNegative: OpeningBalanceMustBeNonNegative;
export declare const amountMustBePositive: AmountMustBePositive;
export declare const insufficientBalance: (
balance: Money,
amount: Money
) => InsufficientBalance;
export declare const accountNotFound: (accountId: string) => AccountNotFound;
export declare const openAccount: (
id: string,
openingBalance: Money
) => Result<Account, AccountError>;
export declare const withdraw: (
account: Account,
amount: Money
) => Result<Account, AccountError>;
-38
View File
@@ -1,63 +1,25 @@
import { ok, fail } from '../library/result.js';
/**
* @typedef {Object} Account
* @property {string} id
* @property {import('./money.js').Money} balance
*/
/** @typedef {{ type: 'OpeningBalanceMustBeNonNegative' }} OpeningBalanceMustBeNonNegative */
/** @typedef {{ type: 'AmountMustBePositive' }} AmountMustBePositive */
/** @typedef {{ type: 'InsufficientBalance', balance: import('./money.js').Money, amount: import('./money.js').Money }} InsufficientBalance */
/** @typedef {{ type: 'AccountNotFound', accountId: string }} AccountNotFound */
/** @typedef {OpeningBalanceMustBeNonNegative | AmountMustBePositive | InsufficientBalance | AccountNotFound} AccountError */
/**
* @type {OpeningBalanceMustBeNonNegative}
*/
export const openingBalanceMustBeNonNegative = { type: 'OpeningBalanceMustBeNonNegative' };
/**
* @type {AmountMustBePositive}
*/
export const amountMustBePositive = { type: 'AmountMustBePositive' };
/**
* @param {import('./money.js').Money} balance
* @param {import('./money.js').Money} amount
* @returns {InsufficientBalance}
*/
export const insufficientBalance = (balance, amount) => ({
type: 'InsufficientBalance',
balance,
amount,
});
/**
* @param {string} accountId
* @returns {AccountNotFound}
*/
export const accountNotFound = (accountId) => ({
type: 'AccountNotFound',
accountId,
});
/**
* @param {string} id
* @param {import('./money.js').Money} openingBalance
* @returns {import('../library/result.js').Result<Account, AccountError>}
*/
export const openAccount = (id, openingBalance) =>
openingBalance.amount < 0
? fail(openingBalanceMustBeNonNegative)
: ok({ id, balance: openingBalance });
/**
* @param {Account} account
* @param {import('./money.js').Money} amount
* @returns {import('../library/result.js').Result<Account, AccountError>}
*/
export const withdraw = (account, amount) => {
if (amount.amount <= 0) {
return fail(amountMustBePositive);
+7
View File
@@ -0,0 +1,7 @@
export type Money = {
amount: number;
};
export declare const createMoney: (amount: number) => Money;
export declare const formatMoney: (money: Money) => string;
-13
View File
@@ -1,16 +1,3 @@
/**
* @typedef {Object} Money
* @property {number} amount
*/
/**
* @param {number} amount
* @returns {Money}
*/
export const createMoney = (amount) => ({ amount });
/**
* @param {Money} money
* @returns {string}
*/
export const formatMoney = (money) => money.amount.toFixed(2);