Guest User

Untitled

a guest
Oct 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. interface Fruit {
  2. isRotten: boolean
  3. }
  4.  
  5. interface Apple extends Fruit {
  6. apple: true
  7. }
  8.  
  9. interface Banana extends Fruit {
  10. banana: true
  11. }
  12.  
  13. interface State {
  14. apples: Apple[]
  15. bananas: Banana[]
  16. selected: Set<'Apples' | 'Bananas'>
  17. }
  18.  
  19. const isRotten = <F extends Fruit>(fruit: F) => fruit.isRotten
  20. const edibleFruits = <F extends Fruit>(fruits: F[]) => fruits.filter(fruit => !isRotten(fruit))
  21.  
  22. const apples$ = (state: State) => state.apples
  23. const bananas$ = (state: State) => state.bananas
  24. const selectedFruits$ = (state: State) => state.selected
  25.  
  26. const edibleApples$ = map(apples$)(edibleFruits)
  27. const edibleBananas$ = map(bananas$)(edibleFruits)
  28. const selectedFruitsArray$ = map(selectedFruits$)(selected => Array.from(selected))
  29.  
  30. const fruitBasket$ = flatMap(selectedFruitsArray$)(fruits => {
  31. const selectors$: Selector<State, Fruit[]>[] = fruits.map(fruit => {
  32. switch (fruit) {
  33. case 'Apples':
  34. return apples$
  35. case 'Bananas':
  36. return bananas$
  37. }
  38. })
  39.  
  40. return createSelector(selectors$, (...allFruits) => ([] as Fruit[]).concat(...allFruits))
  41. })
Add Comment
Please, Sign In to add comment