Rethinking JavaScript: Eliminate the switch statement for better code
Joel Thoms
52122

Loved it. I’m struggling to get my head around FP and this helps a lot. I did my own mix with ramda:

const executeIfFunction = f =>
  f instanceof Function ? f() : f;
const switchcase = defaultCase => key =>
  R.ifElse(R.has(key), R.view(R.lensProp(key)), R.always(defaultCase));
const switchcaseF = cases => defaultCase => key =>
  executeIfFunction(switchcase(defaultCase)(key)(cases));

I noticed cases in the switchcase function doesn’t have to be passed as argument using this method, so it got even cleaner.

Thanks and keep doing these amazing articles.