From a92d14e4ed0181c6d67f21b19ca61a83d9f88376 Mon Sep 17 00:00:00 2001 From: tobymao Date: Mon, 16 Aug 2021 21:11:41 -0700 Subject: [PATCH] allow javascript String and Number obj to be used as children fixes #977 --- src/h.ts | 4 ++-- src/is.ts | 5 ++++- test/unit/core.ts | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/h.ts b/src/h.ts index 736b012..3535168 100644 --- a/src/h.ts +++ b/src/h.ts @@ -42,7 +42,7 @@ export function h(sel: any, b?: any, c?: any): VNode { if (is.array(c)) { children = c; } else if (is.primitive(c)) { - text = c; + text = c.toString(); } else if (c && c.sel) { children = [c]; } @@ -50,7 +50,7 @@ export function h(sel: any, b?: any, c?: any): VNode { if (is.array(b)) { children = b; } else if (is.primitive(b)) { - text = b; + text = b.toString(); } else if (b && b.sel) { children = [b]; } else { diff --git a/src/is.ts b/src/is.ts index be93b62..04739af 100644 --- a/src/is.ts +++ b/src/is.ts @@ -1,4 +1,7 @@ export const array = Array.isArray; export function primitive(s: any): s is string | number { - return typeof s === "string" || typeof s === "number"; + return typeof s === "string" || + typeof s === "number" || + s instanceof String || + s instanceof Number; } diff --git a/test/unit/core.ts b/test/unit/core.ts index ea392b5..a201830 100644 --- a/test/unit/core.ts +++ b/test/unit/core.ts @@ -86,6 +86,22 @@ describe("snabbdom", function () { const vnode = h("a", {}, "I am a string"); assert.strictEqual(vnode.text, "I am a string"); }); + it("can create vnode with String obj content", function() { + var vnode = h("a", new String("b")); + assert.equal(vnode.text, "b"); + }); + it("can create vnode with props and String obj content", function() { + var vnode = h("a", {}, new String("b")); + assert.equal(vnode.text, "b"); + }); + it("can create vnode with array String obj content", function() { + var vnode = h("a", ["b", new String("c")]); + assert.equal(vnode.text, "bc"); + }); + it("can create vnode with Number obj content", function() { + var vnode = h("a", new Number(1)); + assert.equal(vnode.text, "1"); + }); it("can create vnode with null props", function () { let vnode = h("a", null); assert.deepEqual(vnode.data, {});