all repos — h3 @ 6ae1da260a48c3d4b21835869f0711816791cf38

A tiny, extremely minimalist JavaScript microframework.

Null/false/undefined children are now automatically removed.
h3rald h3rald@h3rald.com
Sun, 26 Apr 2020 14:56:14 +0200
commit

6ae1da260a48c3d4b21835869f0711816791cf38

parent

70209a2cd7756c72b065050561d7c9901a996a96

4 files changed, 48 insertions(+), 31 deletions(-)

jump to
M __tests__/h3.js__tests__/h3.js

@@ -2,9 +2,9 @@ const h3 = require("../h3.js").default;

describe("h3", () => { it("should support a way to discriminate functions and objects", () => { - const v1 = h3("div", {onclick: () => true}); - const v2 = h3("div", {onclick: () => true}); - const v3 = h3("div", {onclick: () => false}); + const v1 = h3("div", { onclick: () => true }); + const v2 = h3("div", { onclick: () => true }); + const v3 = h3("div", { onclick: () => false }); const v4 = h3("div"); expect(v1.equal(v2)).toEqual(true); expect(v1.equal(v3)).toEqual(false);

@@ -40,7 +40,7 @@ const invalid2nd = () => h3("div", 1);

const invalid2nd2 = () => h3("div", true, []); const invalid2nd3 = () => h3("div", null, []); const invalidChildren = () => h3("div", ["test", 1, 2]); - const tooManyArgs = () => h3("div", {id: "test"}, "test", "aaa"); + const tooManyArgs = () => h3("div", { id: "test" }, "test", "aaa"); expect(empty).toThrowError(/No arguments passed/); expect(invalid1st).toThrowError(/Invalid first argument/); expect(invalid1st2).toThrowError(/Invalid first argument/);

@@ -59,6 +59,11 @@ const vnode1 = h3("div", () => "test");

const vnode2 = h3("div", () => h3("span")); expect(vnode1.children[0].value).toEqual("test"); expect(vnode2.children[0].type).toEqual("span"); + }); + + it("should remove null/false/undefined children", () => { + const v1 = h3("div", [false, "test", undefined, null, ""]); + expect(v1.children).toEqual([h3({ type: "#text", value: "test" }), h3({ type: "#text", value: "" })]); }); it("should support the creation of nodes with a single child node", () => {
M docs/example/assets/js/h3.jsdocs/example/assets/js/h3.js

@@ -236,15 +236,19 @@ }

processChildren(arg) { const children = Array.isArray(arg) ? arg : [arg]; - this.children = children.map((c) => { - if (typeof c === "string") { - return new VNode({ type: "#text", value: c }); - } - if (typeof c === "function" || (typeof c === "object" && c !== null)) { - return this.processVNodeObject(c); - } - throw new Error(`[VNode] Specified child is not a VNode: ${c}`); - }); + this.children = children + .map((c) => { + if (typeof c === "string") { + return new VNode({ type: "#text", value: c }); + } + if (typeof c === "function" || (typeof c === "object" && c !== null)) { + return this.processVNodeObject(c); + } + if (c) { + throw new Error(`[VNode] Specified child is not a VNode: ${c}`); + } + }) + .filter((c) => c); } // Renders the actual DOM Node corresponding to the current Virtual Node
M docs/js/h3.jsdocs/js/h3.js

@@ -236,15 +236,19 @@ }

processChildren(arg) { const children = Array.isArray(arg) ? arg : [arg]; - this.children = children.map((c) => { - if (typeof c === "string") { - return new VNode({ type: "#text", value: c }); - } - if (typeof c === "function" || (typeof c === "object" && c !== null)) { - return this.processVNodeObject(c); - } - throw new Error(`[VNode] Specified child is not a VNode: ${c}`); - }); + this.children = children + .map((c) => { + if (typeof c === "string") { + return new VNode({ type: "#text", value: c }); + } + if (typeof c === "function" || (typeof c === "object" && c !== null)) { + return this.processVNodeObject(c); + } + if (c) { + throw new Error(`[VNode] Specified child is not a VNode: ${c}`); + } + }) + .filter((c) => c); } // Renders the actual DOM Node corresponding to the current Virtual Node
M h3.jsh3.js

@@ -236,15 +236,19 @@ }

processChildren(arg) { const children = Array.isArray(arg) ? arg : [arg]; - this.children = children.map((c) => { - if (typeof c === "string") { - return new VNode({ type: "#text", value: c }); - } - if (typeof c === "function" || (typeof c === "object" && c !== null)) { - return this.processVNodeObject(c); - } - throw new Error(`[VNode] Specified child is not a VNode: ${c}`); - }); + this.children = children + .map((c) => { + if (typeof c === "string") { + return new VNode({ type: "#text", value: c }); + } + if (typeof c === "function" || (typeof c === "object" && c !== null)) { + return this.processVNodeObject(c); + } + if (c) { + throw new Error(`[VNode] Specified child is not a VNode: ${c}`); + } + }) + .filter((c) => c); } // Renders the actual DOM Node corresponding to the current Virtual Node