Add is.domElement()

This commit is contained in:
Melvin Philips 2017-10-03 13:25:58 -07:00
parent 27d15f40bd
commit b3903e8ec5
4 changed files with 22 additions and 0 deletions

View file

@ -161,4 +161,6 @@ is.inRange = (x, range) => {
throw new TypeError(`Invalid range: ${util.inspect(range)}`);
};
is.domElement = x => is.object(x) && x.nodeType === 1;
module.exports = is;

View file

@ -44,6 +44,7 @@
],
"devDependencies": {
"ava": "*",
"jsdom": "^11.3.0",
"xo": "*"
}
}

View file

@ -145,6 +145,10 @@ Check if `value` (number) is in the range of `0` to `upperBound`.
is.inRange(3, 10);
```
##### .domElement(value)
Returns `true` if `value` is a domElement.
## FAQ

15
test.js
View file

@ -1,5 +1,6 @@
import util from 'util';
import test from 'ava';
import {JSDOM} from 'jsdom';
import m from '.';
const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
@ -358,3 +359,17 @@ test('is.inRange', t => {
m.inRange(0, [1, 2, 3]);
});
});
test('is.domElement', t => {
const {document} = (new JSDOM(`...`)).window;
t.true(m.domElement(document.createElement('div')));
t.false(m.domElement('hello world'));
t.false(m.domElement([]));
t.false(m.domElement(new Map()));
t.false(m.domElement(null));
t.false(m.domElement(undefined));
t.false(m.domElement(0));
t.false(m.domElement(NaN));
t.false(m.domElement(Infinity));
t.false(m.domElement({}));
});