From c83937ed965a363ab13be1f1dbd36cd526e7f638 Mon Sep 17 00:00:00 2001 From: "M. George Hansen" Date: Tue, 26 May 2020 16:29:43 -0700 Subject: [PATCH] fix: target es2018 to support node 10+ --- examples/personal-site/tsconfig.json | 4 ++-- package-lock.json | 6 +++--- package.json | 4 ++-- src/create-element.ts | 3 ++- src/utils.ts | 23 +++++++++++++++++++++++ tsconfig-base.json | 4 ++-- 6 files changed, 34 insertions(+), 10 deletions(-) diff --git a/examples/personal-site/tsconfig.json b/examples/personal-site/tsconfig.json index f155692..1cfeaeb 100644 --- a/examples/personal-site/tsconfig.json +++ b/examples/personal-site/tsconfig.json @@ -5,8 +5,8 @@ "moduleResolution": "node", "jsx": "react", "jsxFactory": "createElement", - "target": "ES2019", - "lib": ["ES2019"], + "target": "ES2018", + "lib": ["ES2018"], "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, diff --git a/package-lock.json b/package-lock.json index a7e8feb..cd09d0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -62,9 +62,9 @@ "dev": true }, "@types/node": { - "version": "12.12.38", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.38.tgz", - "integrity": "sha512-75eLjX0pFuTcUXnnWmALMzzkYorjND0ezNEycaKesbUBg9eGZp4GHPuDmkRc4mQQvIpe29zrzATNRA6hkYqwmA==", + "version": "10.17.24", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.24.tgz", + "integrity": "sha512-5SCfvCxV74kzR3uWgTYiGxrd69TbT1I6+cMx1A5kEly/IVveJBimtAMlXiEyVFn5DvUFewQWxOOiJhlxeQwxgA==", "dev": true }, "@types/ws": { diff --git a/package.json b/package.json index e5085f0..a37ca0d 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "license": "MPL-2.0", "engines": { - "node": ">=12" + "node": ">=10" }, "main": "dist/index.js", "types": "types.d.ts", @@ -29,7 +29,7 @@ "test": "ts-node --project=test/tsconfig.json test/run-tests.ts" }, "devDependencies": { - "@types/node": "~12", + "@types/node": "~10", "@types/ws": "^7.2.4", "@typescript-eslint/eslint-plugin": "^3.0.2", "@typescript-eslint/parser": "^3.0.2", diff --git a/src/create-element.ts b/src/create-element.ts index 1163a9a..5927d75 100644 --- a/src/create-element.ts +++ b/src/create-element.ts @@ -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 }); } diff --git a/src/utils.ts b/src/utils.ts index c8ff29b..453c2bf 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -48,3 +48,26 @@ export const purgeModuleAndDepsFromCache = (modName: string): void => { } delete require.cache[modPath]; }; + +export type Flattenable = Array>; + +const flatDeepRec = (arr: Flattenable, 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 = (arr: Flattenable): T[] => flatDeepRec(arr, 1); diff --git a/tsconfig-base.json b/tsconfig-base.json index 84df7a5..821f547 100644 --- a/tsconfig-base.json +++ b/tsconfig-base.json @@ -3,8 +3,8 @@ "esModuleInterop": true, "module": "CommonJS", "moduleResolution": "node", - "target": "ES2019", - "lib": ["ES2019"], + "target": "ES2018", + "lib": ["ES2018"], "strict": true, "noUnusedLocals": true, "noUnusedParameters": true,