# tools/core/resolvers/board.resolver.ts

> 64 lines of code and 23 definitions.

Tree: Coordination tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/coordination#file-coordination-tools-core-resolvers-board-resolver-ts
Source text: https://banes-lab.com/assets/sources/source.863c47564fd0844710b58f8804a3e48e5d97124d29ba5702ee3beb95601848af.generated.txt

## Definitions

- `stateOf` (lexical_declaration, line 9, exported)
- `activeLetters` (lexical_declaration, line 7, exported)
- `INACTIVE` (lexical_declaration, line 4)
- `AGENT_HEADING` (lexical_declaration, line 5)
- `active` (lexical_declaration, line 8, exported)
- `trimmed` (lexical_declaration, line 12, exported)
- `rest` (lexical_declaration, line 17, exported)
- `space` (lexical_declaration, line 18, exported)
- `letter` (lexical_declaration, line 19, exported)
- `marker` (lexical_declaration, line 24, exported)
- `SpanConflict` (interface_declaration, line 35, exported)
- `spanLines` (lexical_declaration, line 41)
- `difference` (lexical_declaration, line 50)
- `held` (lexical_declaration, line 51)
- `dropItemSpan` (lexical_declaration, line 55, exported)
- `span` (lexical_declaration, line 59, exported)
- `lines` (lexical_declaration, line 64, exported)
- `kept` (lexical_declaration, line 65, exported)
- `compareSpans` (lexical_declaration, line 70, exported)
- `was` (lexical_declaration, line 71, exported)
- `now` (lexical_declaration, line 72, exported)
- `added` (lexical_declaration, line 78, exported)
- `removed` (lexical_declaration, line 79, exported)

## Source

```typescript
import { ACTIVE_STATE, seatState } from "../analyzers/board.analyzer.ts";
import { blockOf } from "../transformers/board.transformer.ts";

const INACTIVE = "INACTIVE";
const AGENT_HEADING = "Agent ";

export const activeLetters = function activeLetters(source: string, index: string): ReadonlySet<string> {
    const active = new Set<string>();
    const stateOf = seatState(index);

    for (const line of source.split("\n")) {
        const trimmed = line.trim();
        if (!trimmed.startsWith(AGENT_HEADING)) {
            continue;
        }

        const rest = trimmed.slice(AGENT_HEADING.length).trim();
        const space = rest.indexOf(" ");
        const letter = space === -1 ? rest : rest.slice(0, space);
        if (letter.length === 0) {
            continue;
        }

        const marker = trimmed.includes(INACTIVE) ? INACTIVE : ACTIVE_STATE;
        if (stateOf(letter, marker) !== ACTIVE_STATE) {
            continue;
        }

        active.add(letter);
    }

    return active;
};

export interface SpanConflict {
    readonly overlapping: boolean;
    readonly added: readonly string[];
    readonly removed: readonly string[];
}

const spanLines = function spanLines(source: string, agent: string): string[] | null {
    const span = blockOf(source, agent);
    if (span === null) {
        return null;
    }

    return source.split("\n").slice(span.from - 1, span.to);
};

const difference = function difference(from: readonly string[], against: readonly string[]): string[] {
    const held = new Set(against);
    return from.filter((line) => !held.has(line) && line.trim().length > 0);
};

export const dropItemSpan = function dropItemSpan(
    source: string,
    key: string,
): { text: string; removed: number } | null {
    const span = blockOf(source, key);
    if (span === null) {
        return null;
    }

    const lines = source.split("\n");
    const kept = [...lines.slice(0, span.from - 1), ...lines.slice(span.to)];

    return { removed: lines.slice(span.from - 1, span.to).join("\n").length, text: kept.join("\n") };
};

export const compareSpans = function compareSpans(before: string, witness: string, agent: string): SpanConflict {
    const was = spanLines(before, agent);
    const now = spanLines(witness, agent);

    if (was === null || now === null) {
        return { added: [], overlapping: true, removed: [] };
    }

    const added = difference(now, was);
    const removed = difference(was, now);

    return { added, overlapping: added.length > 0 || removed.length > 0, removed };
};
```
