This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const transformCage = () => { /* ... */ }; | |
| const handleError = e => "Invalid or Corrupted User Data"; | |
| const handleSuccess = data => "Success"; | |
| const doSomethingWithUserData = () => | |
| getUserFromLocalStorage('nickCage') | |
| .map(transformCage) | |
| .fold(handleError, handleSuccess); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const getUserFromLocalStorage = keyName => { | |
| const str = localStorage.getItem(keyName); | |
| try { | |
| return Right(JSON.parse(str)); | |
| } catch (e) { | |
| return Left("Invalid or Corrupted User Data"); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const getUserFromLocalStorage = keyName => { | |
| const str = localStorage.getItem(keyName); | |
| try { | |
| return JSON.parse(str); | |
| } catch (e) { | |
| return "Invalid or Corrupted User Data"; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const doSomethingWithUserData = () => { | |
| const cage = getUserFromLocalStorage('cage'); | |
| // do wonderful things | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const getUserFromLocalStorage = keyName => { | |
| const str = localStorage.getItem(keyName); | |
| return JSON.parse(str); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Left = x => ({ | |
| map: f => Left(x), | |
| fold: (ifLeft, ifRight) => ifLeft(x) | |
| }); | |
| const Right = x => ({ | |
| map: f => Right(f(x)), | |
| fold: (ifLeft, ifRight) => ifRight(x) | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const getBedrooms = data => | |
| Option(data.listing) | |
| .flatMap(listing => Option(listing.features)) | |
| .flatMap(features => Option(features.bedrooms)) | |
| .map(bedrooms => bedrooms.value) | |
| .fold(() => 0, v => v); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Option = x => (x === undefined || x === null) ? None : Some(x); | |
| const Some = x => ({ | |
| map: f => Some(f(x)), | |
| flatMap: f => f(x), | |
| fold: (ifEmpty, f) => f(x) | |
| }); | |
| const None = { | |
| map: f => None, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ComponentThatWrapsFeatures = ({ data }) => { | |
| const features = getFeatures(data); | |
| return features && <Features {...features} /> | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Features = ({ bedrooms, bathrooms }) => ( | |
| <div> | |
| <span>Beds: </span><span>{ bedrooms }</span> | |
| <span>Baths: </span><span>{ bathrooms }</span> | |
| </div> | |
| ); |