From 718a216797bea169b3a59f8034a487c8d8529385 Mon Sep 17 00:00:00 2001 From: Melvin Philips Date: Thu, 5 Oct 2017 14:17:39 -0700 Subject: [PATCH] Refactor is.domElement to declare constants outside the function --- index.js | 9 +++++---- test.js | 7 +++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 47b95f7..fab9230 100644 --- a/index.js +++ b/index.js @@ -161,11 +161,12 @@ is.inRange = (x, range) => { throw new TypeError(`Invalid range: ${util.inspect(range)}`); }; +const NODE_TYPE_ELEMENT_NODE = 1; +const DOM_PROPERTIES_TO_CHECK = ['innerHTML', 'ownerDocument', 'style', 'attributes', 'nodeValue']; + is.domElement = x => { - const ELEMENT_NODE = 1; - const propsToCheck = ['innerHTML', 'ownerDocument', 'style', 'attributes', 'nodeValue']; - return is.object(x) && x.nodeType === ELEMENT_NODE && is.string(x.nodeName) && - !is.plainObject(x) && propsToCheck.every(prop => prop in x); + return is.object(x) && x.nodeType === NODE_TYPE_ELEMENT_NODE && is.string(x.nodeName) && + !is.plainObject(x) && DOM_PROPERTIES_TO_CHECK.every(property => property in x); }; is.infinite = x => x === Infinity || x === -Infinity; diff --git a/test.js b/test.js index abadfa3..d026968 100644 --- a/test.js +++ b/test.js @@ -9,6 +9,7 @@ const PromiseSubclassFixture = class extends Promise {}; const ErrorSubclassFixture = class extends Error {}; const document = jsdom(); +const createDomElement = el => document.createElement(el); const types = new Map([ ['undefined', undefined], @@ -93,12 +94,14 @@ const types = new Map([ new Object() // eslint-disable-line no-new-object ]], ['integer', 6], - ['domElement', ['div', + ['domElement', [ + 'div', 'input', 'span', 'img', 'canvas', - 'script'].map(el => document.createElement(el))], + 'script' + ].map(createDomElement)], ['non-domElements', [ document.createTextNode('data'), document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),