Skip to content

Commit

Permalink
feat(core): Add methodsOf function to retrieve all methods for a give…
Browse files Browse the repository at this point in the history
…n class
  • Loading branch information
Romakita committed Apr 17, 2019
1 parent c425bc4 commit 3bfff57
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
41 changes: 36 additions & 5 deletions packages/core/src/utils/ObjectUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
/**
* Get the provide constructor.
* @param targetClass
* Get the class constructor
* @param target
*/
export const getConstructor = (targetClass: any): Function => (typeof targetClass === "function" ? targetClass : targetClass.constructor);
export function getConstructor(target: any): Function {
return typeof target === "function" ? target : target.constructor;
}

/**
* Get the class constructor
* @param target
*/
export function constructorOf(target: any): Function {
return getConstructor(target);
}

/**
* Get the provide constructor if target is an instance.
Expand Down Expand Up @@ -269,7 +279,7 @@ export const nameOfSymbol = (sym: symbol): string =>
.replace(")", "");

/**
*
* Return the descriptor for a given class and propertyKey
* @param target
* @param {string} propertyKey
* @returns {PropertyDescriptor}
Expand All @@ -279,10 +289,31 @@ export function descriptorOf(target: any, propertyKey: string): PropertyDescript
}

/**
*
* Return the prototype of the given class.
* @param target
* @returns {any}
*/
export function prototypeOf(target: any) {
return classOf(target) === target ? target.prototype : target;
}

/**
* Return all methods for a given class.
* @param target
*/
export function methodsOf(target: any) {
const methods = new Map();
target = classOf(target);

ancestorsOf(target).forEach(target => {
const keys = Reflect.ownKeys(prototypeOf(target));

keys.forEach((propertyKey: string) => {
if (propertyKey !== "constructor") {
methods.set(propertyKey, {target, propertyKey});
}
});
});

return Array.from(methods.values());
}
54 changes: 53 additions & 1 deletion packages/core/test/utils/ObjectUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {expect} from "chai";
import {
classOf,
constructorOf,
getClass,
getClassOrSymbol,
getConstructor,
Expand All @@ -8,12 +10,28 @@ import {
isCollection,
isEmpty,
isPrimitiveOrPrimitiveClass,
methodsOf,
nameOf,
nameOfClass,
primitiveOf
} from "../../src";

class Test {
class Base {
test1() {

}

test3() {
}
}

class Test extends Base {
test1() {

}

test2() {
}
}

const sym = Symbol("test2");
Expand Down Expand Up @@ -222,4 +240,38 @@ describe("ObjectUtils", () => {
expect(primitiveOf(Object)).to.eq("any");
});
});

describe("constructorOf()", () => {
it("should return the constructor when class is given", () => {
expect(constructorOf(Test)).to.eq(Test);
});
it("should return the constructor when instance is given", () => {
expect(constructorOf(new Test())).to.eq(Test);
});
});

describe("classOf()", () => {
it("should return the class when class is given", () => {
expect(classOf(Test)).to.eq(Test);
});

it("should return the class when instance is given", () => {
expect(classOf(new Test())).to.eq(Test);
});

it("should return the class when prototype is given", () => {
expect(classOf(Test.prototype)).to.eq(Test);
});
});

describe("methodsOf", () => {
it("should return all methods", () => {
const methods = methodsOf(Test);
expect(methods).to.deep.eq([
{propertyKey: "test1", target: Test},
{propertyKey: "test3", target: Base},
{propertyKey: "test2", target: Test}
]);
});
});
});

0 comments on commit 3bfff57

Please sign in to comment.