Parse the variable declaration array, or null when absent/invalid.
(document: Document)
| 15 | |
| 16 | /** Parse the variable declaration array, or null when absent/invalid. */ |
| 17 | function readDecls(document: Document): { htmlEl: Element; arr: VariableDecl[] } | null { |
| 18 | const htmlEl = getHtmlEl(document); |
| 19 | if (!htmlEl) return null; |
| 20 | const raw = htmlEl.getAttribute("data-composition-variables"); |
| 21 | if (!raw) return null; |
| 22 | let parsed: unknown; |
| 23 | try { |
| 24 | parsed = JSON.parse(raw); |
| 25 | } catch { |
| 26 | return null; |
| 27 | } |
| 28 | if (!Array.isArray(parsed)) return null; |
| 29 | return { htmlEl, arr: parsed as VariableDecl[] }; |
| 30 | } |
| 31 | |
| 32 | function indexOfId(arr: VariableDecl[], id: string): number { |
| 33 | return arr.findIndex((v) => typeof v === "object" && v !== null && v.id === id); |
no test coverage detected