nr revisó este gist 1 month ago. Ir a la revisión
1 file changed, 20 insertions
expectEl.ts(archivo creado)
| @@ -0,0 +1,20 @@ | |||
| 1 | + | export function expectEl<T extends HTMLElement = HTMLElement>( | |
| 2 | + | query: string, | |
| 3 | + | rootEl: Element | Document = document, | |
| 4 | + | ): T { | |
| 5 | + | const el = rootEl.querySelector<T>(query); | |
| 6 | + | ||
| 7 | + | if (!el) { | |
| 8 | + | throw new Error(`[expectEl] Can't find \`${query}\` element`); | |
| 9 | + | } | |
| 10 | + | ||
| 11 | + | return el; | |
| 12 | + | } | |
| 13 | + | ||
| 14 | + | export function expectEls<T extends HTMLElement = HTMLElement>( | |
| 15 | + | query: string, | |
| 16 | + | rootEl: Element | Document = document, | |
| 17 | + | ): T[] { | |
| 18 | + | const els = rootEl.querySelectorAll<T>(query); | |
| 19 | + | return Array.from(els); | |
| 20 | + | } | |
Siguiente
Anterior