Skip to content

Instantly share code, notes, and snippets.

@voratham
Created August 17, 2019 07:24
Show Gist options
  • Select an option

  • Save voratham/c90d2c8eec6b39534508ec64245443ed to your computer and use it in GitHub Desktop.

Select an option

Save voratham/c90d2c8eec6b39534508ec64245443ed to your computer and use it in GitHub Desktop.
JavaScript if-else และ try-catch ให้เป็นรูปแบบ Functional
const condition = (funcExec) => (props) => {
return funcExec.if(props) ? funcExec.then(props) : funcExec.else(props)
}
const priceChangeUpdate = (rating) => rating > 3;
const dataRatingCar = (car) => {
const carDB = {
'honda' : () => 5,
'toyota' : () => 3,
'nissan' : () => 2.5
}
return carDB[car]()
}
const calculatePriceFromRating = condition({
if : priceChangeUpdate,
then : rating => 1000000 * rating,
else : () => 890000
})
const getCarConfig = (car) => {
return {
newPrice : calculatePriceFromRating(dataRatingCar(car))
}
}
const carListUpdate = ['honda' , 'toyota' , 'nissan'].map( car => getCarConfig(car) )
console.log("carListUpdate :: " , carListUpdate)
ref : https://www.techstarthailand.com/blog/detail/JavaScript-if-else-and-try-catch-as-functional-constructs/1005?fbclid=IwAR1wG-N0SRRNVD-9JkloDLJifY6TnWV-kmb08_Nmlyv21cWBSQJ5BLLJVxE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment