title: How to create a utils library in angular
body: <p>I'm pretty new to Angular. Sorry for the basic question but I couldn't find info about how to write a library in Angular the proper way.</p>
  
  <p>The reason is to benefit from Angular modules as well as testability/mocks.</p>
  
  <h2>The library</h2>
  
  <p>I am transferring my code to Angular but a big part of my code is written based on a library that I've developed over a couple of months. Imagine something like <em>underscore</em>.</p>
  
  <p>So my initial thought was to wrap it in an angular module like:</p>
  
  <pre><code>angular.module('mylib', [])
         .factory('map', map)
         .factory('reduce', reduce)
         .factory('find', find);
  
  function map (...
  </code></pre>
  
  <p>Each factory function is testable and stuff. And then use it like:</p>
  
  <pre><code>angular.module('app', ['mylib'])
         .controller('MainCtrl', ['$scope', 'map', MainCtrl]);
  
  function MainCtrl($scope, map) {
      ... do stuff with map ...
  </code></pre>
  
  <p>This is fine for a couple of functions I guess but my library has over 20 functions and it's just too much boilerplate to define every one of them as a factory and then inject them into my controller.</p>
  
  <h2>Second solution</h2>
  
  <p>Another way is to have just one factory that returns an object. Kinda like this:</p>
  
  <pre><code>angular.module('mylib', [])
         .factory('mylib', mylib)
  
  function mylib () {
    var exports = {};
  
    exports.map = function (...
    exports.reduce = function (...
    exports.find = function (...
  
    return exports;
  }
  </code></pre>
  
  <p>And then use it like this:</p>
  
  <pre><code>angular.module('app', ['mylib'])
         .controller('MainCtrl', ['$scope', 'mylib', MainCtrl]);
  
  function MainCtrl($scope, mylib) {
      ... do stuff with mylib.map ...
  </code></pre>
  
  <h2>But...</h2>
  
  <p>Is this a good approach? What would a good way to write a utility library in Angular and use it? Readability and maintainability are important but I prefer not to have so much boilerplate code.</p>