classLooksLike
Available since v0.9
Determines if a target class "looks like" the provided class "blueprint".
Arguments
classLooksLike()
accepts the following arguments:
target: object
- the target class object.blueprint: ClassBlueprint
- a blueprint that defines the expected members of a class (see Class Blueprint for details.).
import { classLooksLike } from '@aedart/support/reflections';
class A {}
class B {
foo() {}
}
const blueprint = { members: [ 'foo' ] };
classLooksLike(A, blueprint); // false
classLooksLike(B, blueprint); // true
Class Blueprint
The class "blueprint" is an object that defines the expected members (property keys) of a target class. All defined members must exist in target class' prototype, before the classLooksLike()
returns true
.
You can specify either or both of the following properties in a class blueprint object:
members: PropertyKey[]
- (optional) Properties or methods expected to exist in class' prototype.staticMembers: PropertyKey[]
- (optional) Properties or methods expected to exist in class as static members.
Note: If you do not specify either members
or staticMembers
, then a TypeError
is thrown.
class A {
foo() {}
bar() {}
}
class B {
foo() {}
static bar() {}
}
const blueprint = { members: [ 'foo' ], staticMembers: [ 'bar' ] };
classLooksLike(A, blueprint); // false
classLooksLike(B, blueprint); // true
Recursive
classLooksLike()
traverses target class' prototype chain. This means that you can compare a subclass against a blueprint and inherited members will automatically be included in the check.
class A {
foo() {}
}
class B extends A {
bar() {}
}
const blueprint = { members: [ 'foo', 'bar' ]};
classLooksLike(A, blueprint); // false
classLooksLike(B, blueprint); // true