Skip to content
This repository has been archived by the owner on Sep 16, 2020. It is now read-only.

Commit

Permalink
Refactored isObject.js
Browse files Browse the repository at this point in the history
  • Loading branch information
cn0047 committed Mar 19, 2018
1 parent 192bbd8 commit b0597b1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "is-it-object",
"version": "1.1.6",
"version": "1.1.7",
"license": "MIT",
"author": "Vladimir Kovpak",
"homepage": "https://github.com/cn007b/is-it-object#readme",
Expand Down
41 changes: 25 additions & 16 deletions src/isObject.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
/**
* Check that value is an object using strict strategy.
*
* @param o Value for inspection is it an object.
* @returns {boolean} Is object.
*/
function checkByStrictStrategy(o) {
return (o instanceof Object || typeof o === 'object')
&& (o.constructor === undefined || o.constructor === Object);
}

/**
* Check that value is an object using weak (non-strict) strategy.
*
* @param o Value for inspection is it an object.
* @returns {boolean} Is object.
*/
function checkByWeakStrategy(o) {
return o.constructor === undefined || typeof o.constructor === 'function';
}

/**
* Check that value is an object.
*
* @param o Value for inspection is it an object.
* @param {boolean} strict Strict comparison strategy.
* @returns {boolean} Is object.
*/
module.exports = function isObject(o, strict = true) {
function isObject(o, strict = true) {
if (o === null || o === undefined) {
return false;
}

const instanceOfObject = o instanceof Object;
const typeOfObject = typeof o === 'object';
const constructorUndefined = o.constructor === undefined;
const constructorObject = o.constructor === Object;
const typeOfConstructorObject = typeof o.constructor === 'function';

let r;

if (strict === true) {
r = (instanceOfObject || typeOfObject) && (constructorUndefined || constructorObject);
} else {
r = (constructorUndefined || typeOfConstructorObject);
}
return strict === true ? checkByStrictStrategy(o) : checkByWeakStrategy(o);
}

return r;
};
module.exports = isObject;

0 comments on commit b0597b1

Please sign in to comment.