Skip to content

Commit

Permalink
allow javascript String and Number obj to be used as children
Browse files Browse the repository at this point in the history
fixes #977
  • Loading branch information
tobymao committed Aug 27, 2021
1 parent 27e9c4d commit a92d14e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/h.ts
Expand Up @@ -42,15 +42,15 @@ 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];
}
} else if (b !== undefined && b !== null) {
if (is.array(b)) {
children = b;
} else if (is.primitive(b)) {
text = b;
text = b.toString();
} else if (b && b.sel) {
children = [b];
} else {
Expand Down
5 changes: 4 additions & 1 deletion 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;
}
16 changes: 16 additions & 0 deletions test/unit/core.ts
Expand Up @@ -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, {});
Expand Down

0 comments on commit a92d14e

Please sign in to comment.