Last active
January 24, 2018 14:20
-
-
Save jankapunkt/e43405ab19fd939ff058be1e9d4c177e to your computer and use it in GitHub Desktop.
Approach to get a full working isObject test in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns true, if x is a pure Object (not a descendant of Object) | |
* Test cases: | |
* assert.isFalse(isObject('')); | |
* assert.isFalse(isObject('abc')); | |
* assert.isFalse(isObject([])); | |
* assert.isFalse(isObject([1,2,3])); | |
* assert.isFalse(isObject(1)); | |
* assert.isFalse(isObject(1.4)); | |
* assert.isFalse(isObject(function () {})); | |
* assert.isFalse(isObject(true)); | |
* assert.isFalse(isObject(false)); | |
* assert.isFalse(isObject(new Map())); | |
* assert.isFalse(isObject(new Set())); | |
* | |
* assert.isTrue(isObject(new Object())); | |
* assert.isTrue(isObject({})); | |
* assert.isTrue(isObject(Object.create({}))); | |
* assert.isTrue(isObject({ foo: 'bar' })); | |
**/ | |
export const isObject = x => !!x && | |
!Array.isArray(x) && | |
typeof x === 'object' && | |
x.constructor.name.indexOf('Object') > -1 && | |
x === Object(x); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment