Javascript/Angular: Where to put small helper functions

Published

I’m working on an angular app, which I kind of inherited. I saw that there was a bug with unescaped regex so I wanted to add a function for escaping regex like so:

function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[]]/g, '$&'); // $& means the whole matched string
}

No big deal, right? But where to put this? The regex is used inside a controller. But does it make sense to have this escapeRegExp function inside the scope like so:

                    $scope.escapeRegExp = function(string) {
                      return string.replace(/[.*+?^${}()|[]]/g, '$&'); // $& means the whole matched string
                    }

I’m a little confused where best to put these little functions. What’s the best practice?

Source: AngularJS Questions

Published
Categorized as angularjs, helper, javascript Tagged , ,

Answers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq