Replace x instanceof Array with Array.isArray(x)

This commit is contained in:
Barney Carroll 2016-11-24 09:21:27 +00:00
parent 106a9720d1
commit d82d337569
10 changed files with 32 additions and 34 deletions

View file

@ -41,20 +41,20 @@ function hyperscript(selector) {
break
}
}
if (children instanceof Array && children.length == 1 && children[0] != null && children[0].tag === "#") text = children[0].children
if (Array.isArray(children) && children.length == 1 && children[0] != null && children[0].tag === "#") text = children[0].children
else childList = children
return Vnode(tag || "div", attrs.key, hasAttrs ? attrs : undefined, childList, text, undefined)
}
}
var attrs, children, childrenIndex
if (arguments[1] == null || typeof arguments[1] === "object" && arguments[1].tag === undefined && !(arguments[1] instanceof Array)) {
if (arguments[1] == null || typeof arguments[1] === "object" && arguments[1].tag === undefined && !Array.isArray(arguments[1])) {
attrs = arguments[1]
childrenIndex = 2
}
else childrenIndex = 1
if (arguments.length === childrenIndex + 1) {
children = arguments[childrenIndex] instanceof Array ? arguments[childrenIndex] : [arguments[childrenIndex]]
children = Array.isArray(arguments[childrenIndex]) ? arguments[childrenIndex] : [arguments[childrenIndex]]
}
else {
children = []

View file

@ -135,7 +135,7 @@ module.exports = function($window) {
else {
var recycling = isRecyclable(old, vnodes)
if (recycling) old = old.concat(old.pool)
var oldStart = 0, start = 0, oldEnd = old.length - 1, end = vnodes.length - 1, map
while (oldEnd >= oldStart && end >= start) {
var o = old[oldStart], v = vnodes[start]
@ -410,7 +410,7 @@ module.exports = function($window) {
if (vnode.instance != null) onremove(vnode.instance)
else {
var children = vnode.children
if (children instanceof Array) {
if (Array.isArray(children)) {
for (var i = 0; i < children.length; i++) {
var child = children[i]
if (child != null) onremove(child)
@ -559,7 +559,7 @@ module.exports = function($window) {
// First time rendering into a node clears it out
if (dom.vnodes == null) dom.textContent = ""
if (!(vnodes instanceof Array)) vnodes = [vnodes]
if (!Array.isArray(vnodes)) vnodes = [vnodes]
updateNodes(dom, dom.vnodes, Vnode.normalizeChildren(vnodes), hooks, null, undefined)
dom.vnodes = vnodes
for (var i = 0; i < hooks.length; i++) hooks[i]()

View file

@ -11,7 +11,7 @@ o.spec("fragment", function() {
o(frag.tag).equals("[")
o(frag.children instanceof Array).equals(true)
o(Array.isArray(frag.children)).equals(true)
o(frag.children.length).equals(1)
o(frag.children[0]).equals(child)
@ -24,7 +24,7 @@ o.spec("fragment", function() {
var frag = fragment(attrs, [])
o(frag.tag).equals("[")
o(frag.children instanceof Array).equals(true)
o(Array.isArray(frag.children)).equals(true)
o(frag.children.length).equals(0)
o(frag.attrs).equals(attrs)

View file

@ -2,7 +2,7 @@ function Vnode(tag, key, attrs, children, text, dom) {
return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom: dom, domSize: undefined, state: {}, events: undefined, instance: undefined, skip: false}
}
Vnode.normalize = function(node) {
if (node instanceof Array) return Vnode("[", undefined, undefined, Vnode.normalizeChildren(node), undefined, undefined)
if (Array.isArray(node)) return Vnode("[", undefined, undefined, Vnode.normalizeChildren(node), undefined, undefined)
if (node != null && typeof node !== "object") return Vnode("#", undefined, undefined, node, undefined, undefined)
return node
}