angular.js – best practice for defining a utility library

All we need is an easy explanation of the problem, so here it is.

I’m giving my first steps with angular, and I need to define a utility helper library, with a couple of function methods, something like

dataHelper.parse, dataHelper.sanitize, etc

What would be the recommended way to organize it in angular?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

For utility methods, I would organize them in a library outside of the Angular framework, but with a small dependency on angular.extend. If you want your library completely independent of Angular, you can substitute extend with your own implementation.

Wrap your utility API in a JavaScript enclosure and expose your library as a property of window (in the example below, I’ve named my library myLibrary, but you’re free to choose whichever name you like). By attaching your library as a property of window, you will be able to reference ‘myLibrary’ without qualification. If you are curious, this is exactly how the angular library is exposed.

 (function (window, document) {
     'use strict';

     // attach myLibrary as a property of window
     var myLibrary = window.myLibrary || (window.myLibrary = {});

     // BEGIN API
     function helloWorld() {
         alert('hello world!');
     }

     function utilityMethod1() {
         alert('Utility Method 1');
     }

     function utilityMethod2() {
         alert('Utility Method 2');
     }
     // END API

     // publish external API by extending myLibrary
     function publishExternalAPI(myLibrary) {
         angular.extend(myLibrary, {
             'helloWorld': helloWorld,
             'utilityMethod1': utilityMethod1,
             'utilityMethod2': utilityMethod2
         });
     }


     publishExternalAPI(myLibrary);

 })(window, document);

Usage

Once the script is added, you can use your library from anywhere – services, factories, providers, controllers, directives etc.

<script type='text/javascript' src='angular.js'></script>
<script type='text/javascript' src='myLibrary.js'></script>

<script>
      myLibrary.helloWorld();
      myLibrary.utilityMethod1();
      myLibrary.utilityMethod2();
</script>

Utility Library vs Angular Services

The reason that I prefer utility functions to reside in their own library rather than in angular services is because I think of them as being separate from the angular ecosystem. These utility functions are pretty much stand-alone and are module agnostic, while angular services/factories work closely with other parts of angular, including controllers and directives, and are very much closely tied to the module.

Extending Angular

If you really wanted, you could even extend the angular library itself. Just substitute myLibrary with angular in the script (Note: this may not be advisable as it would make your scripts dependent on the Angular implementation).

Method 2

Create a custom service

globalModule.service('toolkit',function(){

return{
   
    getDate: function(altFormat){
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!

        var yyyy = today.getFullYear();
        if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = yyyy +'-'+ mm+'-'+dd;

        if(altFormat){
            today = today.replace(/-/g, '.');
        }

        return today;
    },

    /**
     *  Returns a 32 character ID [ XXXXXXXX - XXXXXXXXXXXXXXXXXXXXXXXX ]
     * @returns {string}
     */

    newGUID: function(){

        // ...
    }

  };
});

You can inject it wherever it is needed. If you can bundle some logic into seperate services (e.g parseService) I think you should do so.

If you need something more flexible or configurable, have a look at providers.

Method 3

To extend @pixelbits’ answer, if you’d like to keep your external dependencies explicit, you can fairly easily wrap existing libraries for use with Angular, like this for momentjs:

angular.module('testApp')
.constant( 'moment', moment)

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply