export function normalizeIncoming(msg: unknown): { type: string | null, data: unknown } { if (!msg) return { type: null, data: null }; let parsed: unknown = null; try { if (typeof msg === 'string') { parsed = JSON.parse(msg); } else { parsed = msg; } } catch (e) { return { type: null, data: null }; } if (!parsed) return { type: null, data: null }; const type = (parsed as any).type || (parsed as any).action || null; const data = (parsed as any).data || (Object.keys(parsed as any).length ? Object.assign({}, parsed as any) : null); return { type, data }; } export function shuffleArray(array: T[]): T[] { let currentIndex = array.length, temporaryValue: T | undefined, randomIndex: number; while (0 !== currentIndex) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // use non-null assertions because we're swapping indices that exist temporaryValue = array[currentIndex] as T; array[currentIndex] = array[randomIndex] as T; array[randomIndex] = temporaryValue as T; } return array; }