Simple function wrapper over Object.getPrototypeOf()
.
Additionally provides functions to getClassName()
and getParentClass()
.
Last active
October 6, 2018 21:25
-
-
Save burdiuz/4d3a39f8e8e926a226a210d5beab4592 to your computer and use it in GitHub Desktop.
getClass() - Simple function wrapper over `Object.getPrototypeOf()`.
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
'use strict'; | |
Object.defineProperty(exports, '__esModule', { value: true }); | |
const getProtoConstructor = (value) => { | |
const proto = Object.getPrototypeOf(value); | |
if (proto && typeof proto === 'object') { | |
return proto.constructor; | |
} | |
return proto || Object; | |
} | |
const getClass = (value) => { | |
if(value === null || value === undefined) { | |
return undefined; | |
} | |
const constructor = value.constructor; | |
if( | |
typeof constructor === 'function' | |
&& value instanceof constructor | |
) { | |
return value.constructor; | |
} | |
return getProtoConstructor(value); | |
}; | |
const getParentClass = (value) => getProtoConstructor(getClass(value)); | |
const getClassName = (value) => { | |
if (!value) return ''; | |
const def = getClass(value); | |
return def ? def.name : ''; | |
}; | |
exports.getClassName = getClassName; | |
exports.getParentClass = getParentClass; | |
exports.getClass = getClass; | |
exports.default = getClass; |
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
(function(global, factory) { | |
typeof exports === 'object' && typeof module !== 'undefined' | |
? factory(exports) | |
: typeof define === 'function' && define.amd | |
? define(['exports'], factory) | |
: factory((global.GetClass = {})); | |
})(this, function(exports) { | |
'use strict'; | |
const getProtoConstructor = (value) => { | |
const proto = Object.getPrototypeOf(value); | |
if (proto && typeof proto === 'object') { | |
return proto.constructor; | |
} | |
return proto || Object; | |
} | |
const getClass = (value) => { | |
if (value === null || value === undefined) { | |
return undefined; | |
} | |
const constructor = value.constructor; | |
if (typeof constructor === 'function' && value instanceof constructor) { | |
return value.constructor; | |
} | |
return getProtoConstructor(value); | |
}; | |
const getParentClass = (value) => getProtoConstructor(getClass(value)); | |
const getClassName = (value) => { | |
if (!value) return ''; | |
const def = getClass(value); | |
return def ? def.name : ''; | |
}; | |
exports.getClassName = getClassName; | |
exports.getParentClass = getParentClass; | |
exports.getClass = getClass; | |
exports.default = getClass; | |
Object.defineProperty(exports, '__esModule', { value: true }); | |
}); |
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
{ | |
"author": { | |
"name": "Oleg Galaburda", | |
"email": "burdiuz@gmail.com", | |
"url": "http://actualwave.com/" | |
}, | |
"bugs": { | |
"url": "https://gist.github.com/burdiuz/4d3a39f8e8e926a226a210d5beab4592", | |
"email": "burdiuz@gmail.com" | |
}, | |
"description": "Returns object constructor", | |
"homepage": "https://gist.github.com/burdiuz/4d3a39f8e8e926a226a210d5beab4592", | |
"keywords": [ | |
"js", | |
"javascript", | |
"Object", | |
"constructor", | |
"class", | |
"getClass" | |
], | |
"license": "MIT", | |
"main": "get-class.js", | |
"name": "@actualwave/get-class", | |
"version": "0.0.6" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment