refactor: Update byId function for typed object returns (#1)

* updated byId with typing and optional null check

* apply new byId
This commit is contained in:
kruschen 2024-09-01 22:12:56 +02:00 committed by GitHub
parent 2dd6935135
commit 50fe5ae2e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 41 deletions

View file

@ -1,4 +1,40 @@
import {byId} from "./shorthands";
export type ElementMap = {
a: HTMLAnchorElement;
button: HTMLButtonElement;
div: HTMLDivElement;
img: HTMLImageElement;
input: HTMLInputElement;
output: HTMLOutputElement;
select: HTMLSelectElement;
// add more types as needed
};
type ElementMapKeys = keyof ElementMap;
interface ByIdOptions {
throwOnNull?: boolean;
// add more options as needed
}
// function definition with overloads to account for different options
export function byId<K extends ElementMapKeys>(id: string, options?: ByIdOptions & {throwOnNull: true}): ElementMap[K];
export function byId<K extends ElementMapKeys>(id: string, options: ByIdOptions & {throwOnNull: boolean}): ElementMap[K]|null;
/**
* Retrieves an element from the DOM by its ID.
* @template K - The key of the element in the ElementMap.
* @param {string} id - The ID of the element to retrieve.
* @param {ByIdOptions} [options] - The options for retrieving the element.
* @param {boolean} [options.throwOnNull=true] - Whether to throw an error if the element is not found.
* @returns {ElementMap[K] | null} The retrieved element or null if not found.
* @throws {Error} If the element is not found and options.throwOnNull is true.
*/
export function byId<K extends ElementMapKeys>(id: string, options: ByIdOptions = {throwOnNull: true}) {
const element = document.getElementById(id);
if (!element && options.throwOnNull) {
throw new Error(`Element ${id} not found`);
}
return element as ElementMap[K] | null;
}
// get next unused id
export function getNextId(core: string, index = 1) {
@ -42,4 +78,4 @@ export function applyDropdownOption($select: HTMLSelectElement, value: string, n
const isExisting = Array.from($select.options).some(o => o.value === value);
if (!isExisting) $select.options.add(new Option(name, value));
$select.value = value;
}
}