fix: target es2018 to support node 10+
This commit is contained in:
parent
ac8b798b2f
commit
7092f608c2
6 changed files with 34 additions and 10 deletions
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
import { Component, Element, HTMLElement } from "./component";
|
||||
import { HTMLAttributes } from "./jsx";
|
||||
import { flatDeep } from "./utils";
|
||||
|
||||
/**
|
||||
* Create an HTMLElement from a custom Component.
|
||||
|
|
@ -42,7 +43,7 @@ export function createElement(
|
|||
...children: Element[]
|
||||
): HTMLElement {
|
||||
// Flatten the children array so we can accept arrays as children.
|
||||
const normalizedChildren = children.flat();
|
||||
const normalizedChildren = flatDeep(children);
|
||||
if (type instanceof Function) {
|
||||
return type({ ...props, children: normalizedChildren });
|
||||
}
|
||||
|
|
|
|||
23
src/utils.ts
23
src/utils.ts
|
|
@ -48,3 +48,26 @@ export const purgeModuleAndDepsFromCache = (modName: string): void => {
|
|||
}
|
||||
delete require.cache[modPath];
|
||||
};
|
||||
|
||||
export type Flattenable<T> = Array<T | Flattenable<T>>;
|
||||
|
||||
const flatDeepRec = <T>(arr: Flattenable<T>, d: number): T[] => {
|
||||
if (d <= 0) {
|
||||
return arr.slice() as T[];
|
||||
}
|
||||
|
||||
let acc = [] as T[];
|
||||
for (const val of arr) {
|
||||
acc = acc.concat(Array.isArray(val) ? flatDeepRec(arr, d - 1) : val);
|
||||
}
|
||||
return acc;
|
||||
};
|
||||
|
||||
/**
|
||||
* Flatten an arbitrarily-deeply nested array into a flat array.
|
||||
*
|
||||
* @param arr Array to flatten.
|
||||
*
|
||||
* @return Flattened array.
|
||||
*/
|
||||
export const flatDeep = <T>(arr: Flattenable<T>): T[] => flatDeepRec(arr, 1);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue