import { isNullish, Nullable } from 'vest-utils'; import { TIsolate } from './IsolateTypes'; export function createHistoryIndex( children: Nullable, ): Map { const index = new Map(); if (!children) { return index; } for (const child of children) { addToIndex(index, child); } return index; } function addToIndex( index: Map, isolate: TIsolate, ): void { const { key } = isolate; if (isNullish(key)) { return; } const existing = index.get(key); if (existing) { if (Array.isArray(existing)) { existing.push(isolate); } else { index.set(key, [existing, isolate]); } } else { index.set(key, isolate); } }