Is there a way in TypeScript to get all the methods in a Class that has a particular parameter?
I am trying to write a UnitTest around this.
For example
class Foo {
constructor() { ... }
set blah(b) { ... }
get blah() { ... }
method1(yes : any) { ... }
method2(no : any) { ... }
method3(yes : any) { ... }
method4(no : any, not : any, this : any) { ... }
}
The idea should be get all the methods in Foo which has a single payload named 'yes'. Also i have a string form of the className.
So in this case it should an array of [method1, method3]
, in which i can loop against in my unit test.
This is what i got so far:
let className = 'Foo' // Assume that this is given
let methodNames = Object.getOwnPropertyNames(Object.getPrototypeOf(className))
methodNames.filter((x) => x !== <stuck here>).forEach((m) => { .. }
I don't want to do something like this. Curious if there was an easier way.
As well i have seen this page: How to get function parameter names/values dynamically?
But does not work in my case as my className is in a string formate.
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
function getParamNames(func) {
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if(result === null)
result = [];
return result;
}
let className = 'Foo' // Assume that this is given
let methodNames = Object.getPrototypeOf(className)
console.log(getParamNames(methodNames)) // Returns [Object, Object] also tried just className didn't work.