cleanup, patterns

This commit is contained in:
Oli Sturm
2026-04-27 13:08:40 +01:00
parent d0c9466170
commit 3194231879
8 changed files with 66 additions and 36 deletions
+4 -10
View File
@@ -1,17 +1,11 @@
import { randomUUID } from 'crypto';
import { catchResult, bind, tap, tapError, mapError, pipe } from './library/result.js';
import { output, outputError } from './library/logging.js';
import { catchResult, bind, mapError, pipe } from './library/result.js';
import { log, logWith, formatMoney } from './library/logging.js';
import { createInMemoryRepository, innerAccountError } from './infrastructure/repository.js';
import { openAccount } from './domain/account.js';
import { createMoney } from './domain/money.js';
import { createWithdrawMoney } from './application/accountApp.js';
const log = (src, msg) => (result) =>
tap((x) => output(src, msg)(x))(tapError((e) => outputError(src, e))(result));
const logWith = (src, renderText) => (result) =>
tap((x) => output(src, renderText(x))(x))(tapError((e) => outputError(src, e))(result));
console.log('[js-fp3] Starting withdraw money demo...');
const accountId = randomUUID();
@@ -30,11 +24,11 @@ pipe(
bind((withdrawMoney) =>
pipe(
openAccount(accountId, createMoney(200)),
logWith('js-fp3 seed', (account) => `Seeding account ${account.id} with opening balance ${account.balance.amount.toFixed(2)}`),
logWith('js-fp3 seed', (account) => `Seeding account ${account.id} with opening balance ${formatMoney(account.balance)}`),
bind(repo.saveAccount),
log('js-fp3 exec', `Executing withdrawal ${withdrawalAmount.toFixed(2)} from account ${accountId}`),
bind(() => withdrawMoney(accountId, withdrawalAmount)),
logWith('js-fp3 new balance', (account) => `New balance is ${account.balance.amount.toFixed(2)}`),
logWith('js-fp3 new balance', (account) => `New balance is ${formatMoney(account.balance)}`),
mapError((ae) => innerAccountError(ae))
)
)