benjaminvr

Untitled

Jun 27th, 2022
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. Intro:
  4.  
  5.     Time to filter the data! In order to be flexible
  6.     we filter users using a number of criteria and
  7.     return only those matching all of the criteria.
  8.     We don't need Admins yet, we only filter Users.
  9.  
  10. Exercise:
  11.  
  12.     Without duplicating type structures, modify
  13.     filterUsers function definition so that we can
  14.     pass only those criteria which are needed,
  15.     and not the whole User information as it is
  16.     required now according to typing.
  17.  
  18. Higher difficulty bonus exercise:
  19.  
  20.     Exclude "type" from filter criterias.
  21.  
  22. */
  23.  
  24. interface User {
  25.     name: string;
  26.     age: number;
  27.     occupation: string;
  28. }
  29.  
  30. interface Admin {
  31.     name: string;
  32.     age: number;
  33.     role: string;
  34. }
  35.  
  36. export type Person = User | Admin;
  37.  
  38. export const persons: Person[] = [
  39.     { name: 'Max Mustermann', age: 25, occupation: 'Chimney sweep' },
  40.     {
  41.         name: 'Jane Doe',
  42.         age: 32,
  43.         role: 'Administrator'
  44.     },
  45.     {
  46.         name: 'Kate Müller',
  47.         age: 23,
  48.         occupation: 'Astronaut'
  49.     },
  50.     {
  51.         name: 'Bruce Willis',
  52.         age: 64,
  53.         role: 'World saver'
  54.     },
  55.     {
  56.         name: 'Wilson',
  57.         age: 23,
  58.         occupation: 'Ball'
  59.     },
  60.     {
  61.         name: 'Agent Smith',
  62.         age: 23,
  63.         role: 'Administrator'
  64.     }
  65. ];
  66.  
  67. export const isAdmin = (person: Person): person is Admin => !!Object.keys(person).find(x => x === "role");
  68. export const isUser = (person: Person): person is User => !!Object.keys(person).find(x => x === "occupation");
  69.  
  70. export function logPerson(person: Person) {
  71.     let additionalInformation = '';
  72.     if (isAdmin(person)) {
  73.         additionalInformation = person.role;
  74.     }
  75.     if (isUser(person)) {
  76.         additionalInformation = person.occupation;
  77.     }
  78.     console.log(` - ${person.name}, ${person.age}, ${additionalInformation}`);
  79. }
  80.  
  81. export function filterUsers(persons: Person[], criteria: Partial<User>): User[] {
  82.     return persons.filter(isUser).filter((user) => {
  83.         const criteriaKeys = Object.keys(criteria) as (keyof User)[];
  84.         return criteriaKeys.every((fieldName) => {
  85.             return user[fieldName] === criteria[fieldName];
  86.         });
  87.     });
  88. }
  89.  
  90. console.log('Users of age 23:');
  91.  
  92. filterUsers(
  93.     persons,
  94.     {
  95.         age: 23
  96.     }
  97. ).forEach(logPerson);
  98.  
  99. // In case if you are stuck:
  100. // https://www.typescriptlang.org/docs/handbook/utility-types.html
  101. // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#predefined-conditional-types
  102.  
Advertisement
Add Comment
Please, Sign In to add comment