DEV Community

Discussion on: ✔||🤢 Commit or Vomit | Switch(true)

Collapse
 
disgustingdev profile image
disgusting-dev • Edited

How about that (pre-enterprise example :))

//allocate in one place binding of all rules with results
const conditionsEnum = {
  nope: ignoringConditions,
  hire: hiringConditions,
  maybe: mbConditions,
}

let result = '';

//find first truth in there
for (let field in conditionsEnum) {
  if (conditionsEnum[field].some(condition => !!condition())) {
    result = field;

    return result;
  }
}
Enter fullscreen mode Exit fullscreen mode

values in object are just arrays of functions:

const ignoringConditions = [
  () => false,
  () => false,
  () => false,
]

const hiringConditions = [
  () => true,
  () => false,
  () => false,
]

const mbConditions = [
  () => false,
  () => false,
  () => false,
]
Enter fullscreen mode Exit fullscreen mode

Hope my nickname speaks for itself

Collapse
 
jmdejager profile image
🐤🥇 Jasper de Jager

Hope my nickname speaks for itself

🤣🤣