Files
initiative/packages/domain/src/edit-combatant.ts
Lukas f4fb69dbc7
All checks were successful
CI / check (push) Successful in 1m13s
CI / build-image (push) Has been skipped
Add jsinspect-plus structural duplication gate, extract shared helpers
Add jsinspect-plus (AST-based structural duplication detector) to pnpm
check with threshold 50 / min 3 instances. Fix all findings:

- Extract condition icon/color maps to shared condition-styles.ts
- Extract useClickOutside hook (5 components)
- Extract dispatchAction + resolveAndRename in use-encounter
- Extract runEncounterAction in application layer (13 use cases)
- Extract findCombatant helper in domain (9 functions)
- Extract TraitSection in stat-block (4 trait rendering blocks)
- Extract DialogHeader in dialog.tsx (4 dialogs)

Net result: -263 lines across 40 files.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 02:16:54 +01:00

61 lines
1.4 KiB
TypeScript

import type { DomainEvent } from "./events.js";
import {
type CombatantId,
type DomainError,
type Encounter,
findCombatant,
isDomainError,
} from "./types.js";
export interface EditCombatantSuccess {
readonly encounter: Encounter;
readonly events: DomainEvent[];
}
/**
* Pure function that renames a combatant in an encounter by ID.
*
* FR-001: Accepts Encounter, CombatantId, and newName; returns next state + events.
* FR-002: Emits a CombatantUpdated event with combatantId, oldName, newName.
* FR-004: Rejects empty/whitespace-only names with DomainError.
* FR-005: Preserves activeIndex and roundNumber.
* FR-006: Preserves combatant list order.
*/
export function editCombatant(
encounter: Encounter,
id: CombatantId,
newName: string,
): EditCombatantSuccess | DomainError {
const trimmed = newName.trim();
if (trimmed === "") {
return {
kind: "domain-error",
code: "invalid-name",
message: "Combatant name must not be empty",
};
}
const found = findCombatant(encounter, id);
if (isDomainError(found)) return found;
const oldName = found.combatant.name;
return {
encounter: {
combatants: encounter.combatants.map((c) =>
c.id === id ? { ...c, name: trimmed } : c,
),
activeIndex: encounter.activeIndex,
roundNumber: encounter.roundNumber,
},
events: [
{
type: "CombatantUpdated",
combatantId: id,
oldName,
newName: trimmed,
},
],
};
}