allow javascript String and Number obj to be used as children

fixes #977
pull/979/head
tobymao 4 years ago
parent 27e9c4d5dc
commit a92d14e4ed

@ -42,7 +42,7 @@ export function h(sel: any, b?: any, c?: any): VNode {
if (is.array(c)) { if (is.array(c)) {
children = c; children = c;
} else if (is.primitive(c)) { } else if (is.primitive(c)) {
text = c; text = c.toString();
} else if (c && c.sel) { } else if (c && c.sel) {
children = [c]; children = [c];
} }
@ -50,7 +50,7 @@ export function h(sel: any, b?: any, c?: any): VNode {
if (is.array(b)) { if (is.array(b)) {
children = b; children = b;
} else if (is.primitive(b)) { } else if (is.primitive(b)) {
text = b; text = b.toString();
} else if (b && b.sel) { } else if (b && b.sel) {
children = [b]; children = [b];
} else { } else {

@ -1,4 +1,7 @@
export const array = Array.isArray; export const array = Array.isArray;
export function primitive(s: any): s is string | number { 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;
} }

@ -86,6 +86,22 @@ describe("snabbdom", function () {
const vnode = h("a", {}, "I am a string"); const vnode = h("a", {}, "I am a string");
assert.strictEqual(vnode.text, "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 () { it("can create vnode with null props", function () {
let vnode = h("a", null); let vnode = h("a", null);
assert.deepEqual(vnode.data, {}); assert.deepEqual(vnode.data, {});

Loading…
Cancel
Save