Skip to content

Commit

Permalink
refactor: better heuristic for VNode detection
Browse files Browse the repository at this point in the history
  • Loading branch information
aotarola committed Oct 13, 2019
1 parent aeb93f0 commit 85e1eb0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/phy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@

const preact = require('preact');

// VNode props https://github.com/preactjs/preact/blob/master/src/index.d.ts#L14-L17
const VNODE_ATTRS = ['props', 'type', 'key', 'ref'];

// simplified from lodash
const objectCtorString = Object.toString();
function isPlainObject(obj) {
const objectCtoString = Object.toString();
function isAttributes(obj) {
return (
!obj.hasOwnProperty('props') &&
'object' === typeof obj &&
(!obj.constructor || obj.constructor.toString() === objectCtorString)
(!obj.constructor || obj.constructor.toString() === objectCtoString) &&
!VNODE_ATTRS.every(prop => Object.prototype.hasOwnProperty.call(obj, prop))
);
}

Expand All @@ -53,7 +56,7 @@ function h(createElement, selector, attrs) {
const kids = Array.from(arguments).slice(3);

if (attrs) {
if (!isPlainObject(attrs)) {
if (!isAttributes(attrs)) {
kids.unshift(attrs);
attrs = {};
}
Expand Down

0 comments on commit 85e1eb0

Please sign in to comment.