@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
39 lines (38 loc) • 1.76 kB
JavaScript
import { getPrototypeOf } from "./getPrototypeOf.mjs";
import { baseClassProto } from "./inheritance.mjs";
import { isConstructor } from "./isConstructor.mjs";
//#region src/utils/reflection/getPrototypeChainOf.ts
/**
* Iterate through the class and its base/super classes until an anonymous function is reached. This is the default superclass all classes extend.
* @template Class Any {@link ClassLike} type.
* @template ClassesOrInstances 'classes' or 'classInstances'
* @param classDefinition Any class type satisfying {@link Class}. This class (or its instance type) is included in the return value.
* @param [returnType='classes'] Determines return type. If 'classInstances', the return type is an array of the classes' `.prototype`. Else, the classes themselves are returned.
* @since 3.0.0
* @returns
* `returnType extends 'classInstances' ? ClassLike<T>[] : ClassLike<T>[].map(c => c.prototype)`
* Excludes default superclasses e.g. anonymous functions, native code.
*/
function getPrototypesChainOf(classDefinition, returnType) {
let current = classDefinition;
const returnValue = [];
let index = 0;
while (baseClassProto !== current) {
const parent = getPrototypeOf(current);
if (!isConstructor(current)) break;
if (returnType === "classInstances") returnValue[index] = current.prototype;
else returnValue[index] = current;
/**
* Assign the super class to current.
* If the argument is a class, Reflect.getPrototypeOf method returns the
* superclass.
*/
if (isConstructor(parent) && "name" in parent && typeof parent.name === "string" && "" !== parent.name) current = parent;
else break;
index++;
}
return returnValue;
}
//#endregion
export { getPrototypesChainOf };
//# sourceMappingURL=getPrototypeChainOf.mjs.map