marmar-context / web / context / src / review.ts
review.ts
Raw
import type { Fact } from "./types";
const unknown = /^(unknown|not known|not sure|not recorded)$/i;
const none = /^(none|no known (drug )?allergies|nkda)$/i;

export function conflicts(facts: Fact[]): string[] {
  const groups = new Map<string, Fact[]>();
  for (const fact of facts)
    groups.set(fact.key, [...(groups.get(fact.key) ?? []), fact]);
  return [...groups.values()]
    .filter((group) => {
      if (group.length < 2) return false;
      if (group[0].kind === "allergy") {
        // Several positive allergies can coexist, but none/unknown cannot erase a positive entry.
        const special = group.filter(
          (f) => unknown.test(f.label) || none.test(f.label),
        );
        return (
          special.length > 0 &&
          new Set(group.map((f) => f.label.toLowerCase())).size > 1
        );
      }
      if (group[0].kind === "condition") return false;
      return (
        new Set(group.map((f) => `${f.label}|${f.detail}`.toLowerCase())).size >
        1
      );
    })
    .map((group) => group[0].key);
}