Skip to content

Instantly share code, notes, and snippets.

@FQ400
Last active September 15, 2017 07:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FQ400/5dd971df3cab5ed55874a2a1ccb15bb5 to your computer and use it in GitHub Desktop.
Save FQ400/5dd971df3cab5ed55874a2a1ccb15bb5 to your computer and use it in GitHub Desktop.
Branching in functional javascript using Ramda
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