Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum FilterBy {
- COUNTRY = "country",
- AGE = "age",
- GENDER = "gender",
- FAVORITE = "favorite",
- }
- type AgeRange = {
- min: Number,
- max: Number,
- }
- type FilterType<F extends FilterBy> = F extends FilterBy.AGE ? AgeRange : (F extends FilterBy.GENDER ? Boolean : String)
- class Filter<F extends FilterBy> {
- filterBy: F;
- filterValue: FilterType<F>;
- constructor(filterBy: F, filterValue: FilterType<F>) {
- this.filterBy = filterBy;
- this.filterValue = filterValue;
- }
- public filter(users: Array<User>): Array<User> {
- return users.filter(user => user[this.filterBy.valueOf()] === this.filterValue );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement