expectEl.ts
· 506 B · TypeScript
Неформатований
export function expectEl<T extends HTMLElement = HTMLElement>(
query: string,
rootEl: Element | Document = document,
): T {
const el = rootEl.querySelector<T>(query);
if (!el) {
throw new Error(`[expectEl] Can't find \`${query}\` element`);
}
return el;
}
export function expectEls<T extends HTMLElement = HTMLElement>(
query: string,
rootEl: Element | Document = document,
): T[] {
const els = rootEl.querySelectorAll<T>(query);
return Array.from(els);
}
| 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 | } |