Carucel
Carucel

Reputation: 491

Function that returns a function that retrieves a membervariable

Consider:

const compose = <S, T, R>(func1: (inp2: T) => R, func2: (inp: S) => T): (inp: S) => R => ((arg: S) => func1(func2(arg)));
const not = (val: boolean): boolean => !val;
const equals = <T>(val1: T): (val2: T) => boolean => ((val2: T): boolean => (val1 == val2));

and the class

class MyClass {
  constructor(public readonly name: string) { }
}

The following are two ways to search in a list myarray for the MyClass with name "find me":

(1) Search using

myarray.filter(compose(equals("find me"), (c: MyClass): string => c.name));

(2) Add public getName(): string {return this.name; } to the class definition, and search using

myarray.filter(compose(equals("find me"), MyClass.prototype.getName.bind));

Question: Both options are verbose. Would it be possible to define a functor that takes a class and a membervariable and returns a 'getter' for this membervariable?

Upvotes: 1

Views: 38

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250116

We can use keyof to build such a function in a type-safe way:

const get = <T, K extends keyof T>(cls: new (...a: any[]) => T, key: K) => (c: T) => c[key];
var found = myarray.some(compose(equals("find me"), get(MyClass, 'name')));

A further simplification would be to create a class dedicated getter builder function. This could be kept on the class as a static member or in a sperate variable depending on how you want to split concerns in your code:

const getBuilder = <T>(cls: new (...a: any[]) => T) => <K extends keyof T>(key: K) => (c: T) => c[key];
class MyClass {
  constructor(public readonly name: string) { }
  static get = getBuilder(MyClass); // getter builder on class
}
let myarray: MyClass[] = []
const myClassGet = getBuilder(MyClass);// class dedicated builder 
var found = myarray.some(compose(equals("find me"), myClassGet('name')));
var found2 = myarray.some(compose(equals("find me"), MyClass.get('name')));

Upvotes: 1

Related Questions