all files / app/components/errorsMapper/ errorsMapper.service.js

100% Statements 48/48
96.15% Branches 25/26
100% Functions 8/8
100% Lines 48/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                          207×                                     44× 44× 44×   44×     44× 44× 63× 63× 63× 56× 56×         63× 63×   44×   63× 63× 63×     63×     63× 63× 17× 18×     46×     56× 56× 56× 56× 25×   31× 31× 31× 31×       56×          
/**
 * @ngdoc service
 * @name app.components.errorsMapper
 * @description
 * Categorize error messages in a hash
 */
(function () {
  'use strict';
 
  angular
    .module('app.components')
    .factory('errorsMapper', factory);
 
  /** @ngInject */
  function factory() {
 
    return errorsMapper;
 
    /**
     * @ngdoc service
     * @name errorsMapper
     * @methodOf app.components.errorsMapper
     * @description
     * Given a hash, return a new hash that groups values together.  Useful for
     * processing the errors generated by Rails.
     * @param {Object} data
     * Input hash.  Values are expected to be strings or string arrays.
     * @param {Array} names
     * Key names to copy.
     * @param {Object} map
     * Map original key names to new key names.
     * @param {String} defaultKey
     * Key name to use for all of the errors that don't match a known key name (in names param).
     * Each value will be prefixed with the original key name.
     */
    function errorsMapper(data, names, map, defaultKey) {
      names = names || [];
      map = map || {};
      defaultKey = defaultKey || 'other';
 
      if (angular.isObject(data.errors))
        // handle nested errors object
        data = data.errors;
 
      var result = {};
      angular.forEach(data, function (value, key) {
        var newKey = matchKeyName(key);
        var messages = [];
        if (newKey == null) {
          newKey = defaultKey;
          appendMessages(messages, value, makePrefix(key));
        }
        else {
          appendMessages(messages, value);  // no prefix
        }
 
        var arr = result[newKey] || [];
        result[newKey] = arr.concat(messages);
      });
      return result;
 
      function matchKeyName(key) {
        var newKey = null;
        var i = names.indexOf(key);
        if (i >= 0) {
          var match = names[i];
          if (angular.isDefined(map[match]))
            newKey = map[match];
          else
            newKey = match;
        }
        return newKey;
      }
 
      function appendMessages(messages, value, prefix) {
        prefix = prefix || '';
        if (typeof value == "object")
          angular.forEach(value, function (el) {
            messages.push(prefix + el)
          });
        else
          messages.push(prefix + value);
      }
 
      function makePrefix(key) {
        var result = key;
        Eif (result.length > 0) {
          var notAPrefix = [defaultKey, 'errors', 'error'];
          if (notAPrefix.indexOf(key) >= 0)
            result = '';
          else {
            if (result.slice(-3) == '_id')
              result = result.slice(0, result.length - 3);
            result = result.replace(/_/g, ' ');
            result = result.charAt(0).toUpperCase() + result.slice(1);
            result = result + ' ';
          }
        }
 
        return result;
      }
    }
  }
})();