Segments model
retab-ui/segmentsFreeUtility
Shared model + helpers for page-segmented results (partition/split): Segment type, Tableau palette, color map, page-range and confidence helpers.
Install it
npx shadcn@latest add https://ui.retab.com/r/segments.jsonSource
1/**2 * Shared model for page-segmented document results — Retab "partition" (keyed3 * chunks) and "split" (named subdocuments). Both reduce to the same `Segment[]`:4 * a label, the pages it owns, a deterministic color, and optional confidence.5 *6 * The legend, sidebar, and timeline primitives all consume `Segment[]`, so a7 * partition and a split render through the exact same components.8 */910// Tableau 20 — the palette Retab's dashboard uses, so colors match.11export const SEGMENT_PALETTE = [12 "#4E79A7",13 "#A0CBE8",14 "#F28E2B",15 "#FFBE7D",16 "#59A14F",17 "#8CD17D",18 "#B6992D",19 "#F1CE63",20 "#499894",21 "#86BCB6",22 "#E15759",23 "#FF9D9A",24 "#79706E",25 "#BAB0AC",26 "#D37295",27 "#FABFD2",28 "#B07AA1",29 "#D4A6C8",30 "#9D7660",31 "#D7B5A6",32] as const;3334export interface Segment {35 /** Stable id (label + occurrence, since a split name can repeat). */36 id: string;37 /** Display label — the partition key or the subdocument name. */38 label: string;39 /** Sorted, de-duplicated 1-based pages owned by this segment. */40 pages: number[];41 /** Deterministic color (by label, so the same label is always one color). */42 color: string;43 /** Render order. */44 index: number;45 /** Optional consensus confidence in [0, 1]. */46 confidence?: number | null;47}4849export interface SegmentChunk {50 key?: string;51 name?: string;52 pages: number[];53}5455export function segmentDisplayLabel(label: string): string {56 const trimmed = typeof label === "string" ? label.trim() : "";57 return trimmed || "unnamed";58}5960export function normalizePageCount(value: number): number {61 return Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;62}6364/** Assign one palette color per distinct display label, ordered by sorted label. */65export function buildColorMap(labels: string[]): Map<string, string> {66 const distinct = Array.from(new Set(labels.map(segmentDisplayLabel))).sort(67 (a, b) => a.localeCompare(b),68 );69 const map = new Map<string, string>();70 distinct.forEach((label, i) => {71 map.set(label, SEGMENT_PALETTE[i % SEGMENT_PALETTE.length]);72 });73 return map;74}7576function normalizePages(pages: number[]): number[] {77 const rawPages = Array.isArray(pages) ? pages : [];78 return Array.from(79 new Set(rawPages.filter((p) => Number.isInteger(p) && p > 0)),80 ).sort((a, b) => a - b);81}8283export function segmentPageCount(pages: number[]): number {84 return normalizePages(pages).length;85}8687export function firstSegmentPage(pages: number[]): number | null {88// … truncated
Files it writes
More from retab-ui
All 130 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| CSV parsercsv | retab-ui | Utilities | Free | no deps · 4 files | |
| Document source modeldocument-source | retab-ui | Utilities | Free | no deps | |
| DOCX Document Resourcedocx-document-resource | retab-ui | Utilities | Free | no deps · 5 files | |
| Effect Keyeffect-key | retab-ui | Utilities | Free | no deps | |
| PDF Document Resourcepdf-document-resource | retab-ui | Utilities | Free | 1 dep · 6 files | |
| Segment Interactionsegment-interaction | retab-ui | Utilities | Free | no deps | |
| Utilsutils | retab-ui | Utilities | Free | 2 deps | |
| XLSX SheetJS flattenerxlsx-sheetjs-flattener | retab-ui | Utilities | Free | 1 dep |
