'use strict';
/* A newable factory to handle the Morphology service
 *
 * The constructor functions takes a configuration object (that typically
 * contains a resource object for this service).
 *
 */
angular.module('arethusa.morph').factory('BspMorphRetriever', [
  'configurator',
  function (configurator) {
    function deleteUnwantedKeys(obj, keys) {
      keys.forEach(function (el) {
        delete obj[el];
      });
    }

    function flattenAttributes(form, toFlatten) {
      toFlatten.forEach(function (el) {
        var attr = form[el];
        if (attr) {
          form[el] = attr.$;
        }
      });
    }

    function renameAttributes(form, renamers) {
      for (var oldName in renamers) {
        var newName = renamers[oldName];
        var val = form[oldName];
        delete form[oldName];
        form[newName] = val;
      }
    }

    function renameValues(form, renamers) {
      for (var category in renamers) {
        var val = form[category];
        var actions = renamers[category];
        for (var orig in actions) {
          var actual = actions[orig];
          if (val === orig) {
            form[category] = actual;
          }
        }
      }
    }

    function formatLexInvData(uri) {
      if (uri) {
        return {
          uri: uri,
          urn: uri.slice(uri.indexOf('urn:'))
        };
      }
    }

    return function (conf) {
      var self = this;
      var resource = configurator.provideResource(conf.resource);
      this.getWord = function (word) {
        return resource.get({ 'word': word });
      };
      this.abort = resource.abort;
      this.getData = function (string, callback) {
        self.getWord(string).then(function (res) {
          try {
            // The body can contain a single object or an array of objects.
            // Can also be undefined, in that case we will just throw an exception
            // eventually - and we will end up in catch path and just return
            // an empty array.
            var entries = arethusaUtil.toAry(res.data.RDF.Annotation.Body);
            var results = arethusaUtil.inject([], entries, function (results, el) {
                var entry = el.rest.entry;
                var lemma = entry.dict.hdwd.$;
                // We might have multiple inflections for each entry and need to wrap
                // the array vs. object problem again.
                arethusaUtil.toAry(entry.infl).forEach(function (form) {
                  // form is an object with some key/val pairs we have no use for right
                  // now - we just ditch them. The rest we take and form another object,
                  // which will wrap up the morphological attributes and contain lemma
                  // information.
                  // There are actually more than these original 3 - we might want to do
                  // this differently at some point.
                  deleteUnwantedKeys(form, [
                    'term',
                    'stemtype'
                  ]);
                  // If the form has a case attribute, it wrapped in another object we
                  // don't want and need. Flatten it to a plain expression.
                  // The same goes for part of speech.
                  flattenAttributes(form, [
                    'case',
                    'pofs'
                  ]);
                  // These renaming stuff could probably be configurable...
                  renameAttributes(form, { 'pofs': 'pos' });
                  renameValues(form, {
                    'pos': {
                      'verb\nparticiple': 'verb'
                    }
                  });
                  results.push({
                    lexInvLocation: formatLexInvData(entry.uri),
                    lemma: lemma,
                    attributes: form,
                    origin: 'bsp/morpheus'
                  });
                });
              });
            callback(results);
          } catch (err) {
            return [];
          }
        });
        return [];
      };
    };
  }
]);

@@ -1017,46 +1017,8 @@
 y%5D;%0A
-        for (var orig in actions) %7B%0A  
     
@@ -1046,17 +1046,14 @@
 ons%5B
-orig
+val
 %5D;%0A
-  
     
@@ -1064,26 +1064,18 @@
 if (
-val === orig
+actual
 ) %7B%0A
-  
     
@@ -1105,28 +1105,16 @@
 actual;%0A
-          %7D%0A
         

Refactor again for more efficiency