This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
26 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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; |