Advertisement
greedydev

Untitled

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