Advertisement
CLooker

Untitled

Apr 12th, 2018
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // which is better
  2.  
  3. // For each case, read code from bottom to top
  4.  
  5. // CASE 1
  6. // less refactoring, but fn's aren't necessarily following DO ONE THING
  7. const searchResultsDiffer = (searchResults, kvs) =>
  8.   searchResults.map(
  9.     book =>
  10.       kvs[book.id]
  11.         ? kvs[book.id]
  12.         : book.shelf ? { ...book, shelf: false } : book
  13.   );
  14.  
  15. const SearchViewDiffer = (nextProps, prevState) => {
  16.   return {
  17.     query: prevState.query,
  18.     searchResults: searchResultsDiffer(
  19.       prevState.searchResults,
  20.       nextProps.shelfKeyValueStore
  21.     )
  22.   };
  23. };
  24. //---------------------------------------------------------------------
  25.  
  26. // CASE 2
  27. // fn's do less, but more code
  28. const returnFormattedBook = book => ({ ...book, shelf: false });
  29.  
  30. const bookShelfChecker = book =>
  31.   book.shelf ? returnFormattedBook(book) : book;
  32.  
  33. const bookRecordExists = (kvs, book) => (kvs[book.id] ? true : false);
  34.  
  35. const searchResultsDiffer = (searchResults, kvs) =>
  36.   searchResults.map(
  37.     book =>
  38.       bookRecordExists(kvs, book) ? kvs[book.id] : bookShelfChecker(book)
  39.   );
  40.  
  41. const SearchViewDiffer = (nextProps, prevState) => ({
  42.   query: prevState.query,
  43.   searchResults: searchResultsDiffer(
  44.     prevState.searchResults,
  45.     nextProps.shelfKeyValueStore
  46.   )
  47. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement