@halospv3/hce.shared-config
Version:
Automate commit message quality, changelogs, and CI/CD releases. Its `main` entry point is a Semantic Release config. Functions and classes are exposed for customization. An ESLint config, a Commitlint config, and addl. resources for .NET projects are als
31 lines (30 loc) • 1.56 kB
JavaScript
//#region src/utils/reflection/isConstructor.ts
/**
* A very jank function to determine if an object can be the target of Reflect.construct.
* Unfortunately, many functions have a constructor in their prototype. These
* functions are treated like classes due to JavaScript's poor distinction between
* classes and functions.\
* Typescript can enforce "new" keyword usage, but overriding the type
* allows you to `new isConstructor()` despite this function not intended to be
* used with the `new` keyword.
* #### NOTE: Only works when targeting ES6/ES2015 or later.
* > If your project or a dependent project is compiled to < ES6, this function will always return `false`; classes and constructors were introduced in ES6/ES2015.
* @param object Anything.
* @returns `true` if the {@link object} is a constructor. Else, `false`.
* @since 3.0.0
* @see https://stackoverflow.com/a/49510834
*/
function isConstructor(object) {
if (typeof object !== "function") return false;
if (/^class\s/.test(object.toString())) return true;
const prototype = object.prototype;
if (prototype != void 0 && (typeof prototype === "function" || typeof prototype === "object") && "constructor" in prototype && typeof prototype.constructor === "function") {
const _ctor = prototype.constructor;
const _name = Reflect.getOwnPropertyDescriptor(_ctor, "name");
return _ctor === object && _name?.writable === false && _name.enumerable === false && _name.configurable === true;
}
return false;
}
//#endregion
export { isConstructor };
//# sourceMappingURL=isConstructor.mjs.map