Pattern matching is a pretty common action performed on entities in order to check if they follow some pattern or not.
For example, an object repr...
For further actions, you may consider blocking this person and/or reporting abuse
You could do something like this:
Yes indeed! The point is that we have to be careful about this technique because those aren't branches and are eagerly executed.
Wrapping it in a function solves the problem but it's not something we do with
ifstatements, for example 😄Not sure about performance, but you can swap the immediate invocation for a
bind. Eg:This way none of your functions get invoked except the one that you explicitly match.
You would probably want to throw a guard condition in there so you don't try to invoke
undefined, but this would be my standard approach for anything that needs a dynamic list of functions that have external dependencies.I'd wager it would be pretty straightforward to build a library to do this. I built something similar to verify json against user-defined schemas. You could extend that idea to check conditions or pass arguments into a callback on success.
There's something about in Lodash:
lodash.com/docs/4.17.11#cond
Neat! I've never actually used lodash, but it looks like it has some really nice utility methods.
Coming soon to babel: github.com/babel/babel/pull/9318
Oh I hope so!
Alas, it won't be for production projects for a while, but a working Babel plugin is important to make things move forward.