samedi 16 juillet 2022

Javascript, log what causes a boolean expression to fail

I have a complex predicate in JavaScript that chains various tests against a value.

I'd love to log where in the expression it fails, if it fails. Sort of like testing libraries that log out where an exception fails.

Is there some sort of reflection I can do to extract some pretty description of what caused an expression to fail?

e.g.,

const predicate = a.foo == 1 && a.bar < 3 && someOtherPredicate(a.bar)

// if predicate == false, console.log ->
// "error: b.bar < 3 evaluated to false"

(I'm actually interested in doing this in Typescript, but want to see what features JavaScript has that could enable this)




Aucun commentaire:

Enregistrer un commentaire