Advertisement
greedydev

Untitled

Nov 4th, 2022
1,822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum FilterBy {
  2.   COUNTRY = "country",
  3.   AGE = "age",
  4.   GENDER = "gender",
  5.   FAVORITE = "favorite",
  6. }
  7.  
  8. type FilterType<F extends FilterBy> = F extends FilterBy.AGE ? Number : (F extends FilterBy.GENDER ? Boolean : String)
  9.  
  10. class Filter {
  11.   filterBy: FilterBy;
  12.   filterValue: FilterType<FilterBy>;
  13.  
  14.   constructor(filterBy: FilterBy, filterValue: FilterType<FilterBy>) {
  15.     this.filterBy = filterBy;
  16.     this.filterValue = filterValue;
  17.   }
  18.  
  19.   public filter(users: Array<User>): Array<User> {
  20.     return users.filter(user => user[this.filterBy.valueOf()] === this.filterValue );
  21.   }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement