I'm trying to test if the following strings are arrow functions by regex testing it for the presence of =>. Here are my test cases. I've bolded the cases that should find a match - in other words, they're valid arrow functions.
name => '=>'(name) => {...}name => ({..})function() { return '=>'; }function(name) { return () => {}}- while this function has a valid arrow function inside of it, the function expression itself is not an arrow function(name, age) => doSomething('ff')name => doSomething(() => {})
My attempt is partially working, but needs a more knowledgeable eye.
(=>)\s*(?![^\{]*\})
Why am I doing this?
Arrow functions don't have their own context, the context is inherited from where they're defined, and so I can't set the context manually as I would with a normal function. I'm building an api, where a part of the api acts as an initializer for an object being instantiated. Just as a constructor gives one access to this, this initializer function will do the same and will depend on the initializer being a function expressions rather than an arrow function. I'd like to throw an exception and notify the user accordingly if they pass an arrow function as an argument.