Little annoying so with gitlab.com/zeen3/z3util-dom:
import { $cc, $cec, filter_map, noemp } from 'z3util-dom'; function createListWithTemplate(heroes: Hero[]) { return $cec( "ul", filter_map( heroes, hero => hero && $cec("li", [ $cc("name", [hero.name]), $cc("description", [hero.description]), ] ) ); }
though what's doing the work may end up a bit excessive:
/** Map an iterable list, filtering as necessary */ export function* filter_map<T, U>(list: T[] | IterableIterator<T>, f: ((t: T) => U | null | undefined)): IterableIterator<U> { let v, k for (k of list) if ((v = f(k)) != null) yield v; } export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>; type Appendable = HTMLElement | DocumentFragment | string; type Queryable = Document | DocumentFragment | Element; export type MaybeOr<T> = T | false | 0 | null | '' | undefined; export type IterableOrNot<T> = MaybeOr<Iterable<MaybeOr<T>>>; interface createelement<E> { <T extends keyof E>(el: T, o: Partial<Omit<E[T], "children" | "childNodes" | "classList" | "style">>): E[T]; } /** Create element (with attributes) */ export const $ce: createelement<HTMLElementTagNameMap> = (el, o) => Object["assign"](document["createElement"](el), o); /** Create element (with children, attributes) */ export const $cec = <T extends keyof HTMLElementTagNameMap>( name: T, children?: IterableOrNot<Appendable>, attrs?: Omit<Partial<HTMLElementTagNameMap[T]>, "children" | "childNodes" | "innerHTML" | "innerText" | "outerHTML">, ) => { let container = $ce(name, (attrs || {}) as unknown as any), el: MaybeOr<Appendable>; if (children) for (el of children) el && container["append"](el); return container }; /** Create container (with classlist) */ export const $cc = (n: string, c?: IterableOrNot<Appendable>) => $cec("div", c, {"className": n});
But then again it's there to be specifically generic.
seems excessive.
There's actually very little that's generated.
It's mostly typing information but even then...
Most of it is just expansion. If you expand all of it, it ends up being really short and roughly the same space as the template option.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Little annoying so with gitlab.com/zeen3/z3util-dom:
though what's doing the work may end up a bit excessive:
But then again it's there to be specifically generic.
seems excessive.
There's actually very little that's generated.
It's mostly typing information but even then...
Most of it is just expansion. If you expand all of it, it ends up being really short and roughly the same space as the template option.