Last active
September 15, 2017 07:42
-
-
Save FQ400/5dd971df3cab5ed55874a2a1ccb15bb5 to your computer and use it in GitHub Desktop.
Branching in functional javascript using Ramda
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
import R from 'ramda'; | |
const payloadSizeWithinLimit = (payload, limit) => limit > payload.size; | |
const errorState = { name: 'can not be empty' }; | |
const sizeInBytes = 50000; | |
let errorObj; | |
errorObj = R.cond([ | |
[R.equals(true), R.always({})], | |
[R.equals(false), R.always(errorState)], | |
])(payloadSizeWithinLimit(payload, sizeInBytes)); | |
errorObj = R.ifElse( | |
R.equals(true), | |
R.always({}), | |
R.always(errorState) | |
)(payloadSizeWithinLimit(payload, sizeInBytes)); | |
// using arrow functions | |
errorObj = R.ifElse( | |
R.equals(true), | |
() => {}, | |
() => errorState | |
)(payloadSizeWithinLimit(payload, sizeInBytes)); | |
// maybe something like | |
const ifTrue = (arg) => {}; // could be implemented | |
errorObj = ifTrue( | |
R.always({}), | |
R.always(errorState) | |
)(payloadSizeWithinLimit(payload, sizeInBytes)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment