util: Add classExtends function

Javascript doesn't have a builtin operator for checking if one class is
a subclass of another (ie. instanceof needs an instance, not a class).

This commit adds a new function, classExtends, that loops through the
class inheritance hierarchy to find if a relationship exists.
This commit is contained in:
Ray Strode 2023-09-15 22:56:18 -04:00
parent b41f2ed98b
commit c9827cdaa6

View file

@ -279,6 +279,29 @@ export function lerp(start, end, progress) {
return start + progress * (end - start);
}
/**
* @typedef {Function} Class
* @description A class
*
* Note: A class (as defined by the ES6 class keyword)
*/
/**
* @param {Class} subClass
* @param {Class} superClass
* @returns {boolean}
*
* Returns whether `subClass` is a subclass of `superClass` or not
*/
export function classExtends(subClass, superClass) {
for (let prototype = subClass?.prototype; prototype; prototype = Object.getPrototypeOf(prototype)) {
if (prototype === superClass?.prototype)
return true;
}
return false;
}
/**
* _GNOMEversionToNumber:
*