UNPKG

reblendjs

Version:

ReblendJs uses Reactjs pradigm to build UI components, with isolated state for each components.

59 lines (52 loc) 1.42 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Deeply flattens an array or set of elements. * * @template T * @param {T[] | Set<T>} data - The data to flatten. * @returns {T[]} A flattened array of elements. */ export function deepFlat<T>(data: T[] | Set<T>): T[] { if (!data) { return [] } if (!(data instanceof Set) && !Array.isArray(data)) { data = [data] } const ts: T[] = [] for (const t of data) { if (t instanceof Set || Array.isArray(t)) { ts.push(...deepFlat(t)) } else { ts.push(t) } } return ts } /** * Flattens a nested array or set of virtual nodes (VNode) and adds the result to the `containerArr`. * * @template T * @param {(T | T[])[] | Set<T | T[]>} arr - The array or set of VNode children to flatten. * @param {T[]} [containerArr=[]] - The container array to store flattened nodes. * @returns {T[]} The flattened array of VNode children. */ export function flattenVNodeChildren<T>(arr: (T | T[])[] | Set<T | T[]>, containerArr: T[] = []): T[] { if (!arr) { return [] } if (!Array.isArray(arr)) { arr = [arr as any] } if ((arr.length || (arr as any).size) < 1) { return [] } for (const item of arr) { if (item instanceof Set || Array.isArray(item)) { flattenVNodeChildren(item as T[], containerArr) } else { containerArr.push(item as T) } } return containerArr }