1

I have a method that takes a language abbreviation and matches it using a .constant dictionary, and returns the matching language name.

How can I do an evaluation with .filter to check whether the passed isoCode/language abbreviation exists?

Here is my method:

angular.module('portalDashboardApp')

  .service('ISOtoLanguageService', ['Languages', function(Languages) {

    this.returnLanguage = function(isoCode) {

      var categoryObject = Languages.filter(function ( categoryObject ) {
        return categoryObject.code === isoCode;
      })[0];

      return categoryObject.name;

    };

  }]);

Here is the method with some error catching I have tried:

angular.module('portalDashboardApp')

  .service('ISOtoLanguageService', ['Languages', function(Languages) {

    this.returnLanguage = function(isoCode) {

        var categoryObject = Languages.filter(function (categoryObject) {
            if (isoCode != null || isoCode != undefined) {
                return categoryObject.code === isoCode;
            }
            else {
                return categoryObject.code === 'und';
            }        
      })[0];

      if (categoryObject.name != undefined || categoryObject.name != null) {
          return categoryObject.name;
      }
      else {
          return "undefined";
      }
    };
  }]);

Thank you!

3
  • Languages is your service-dictionary or any external? Commented Feb 16, 2017 at 8:40
  • 1
    off-topic: in JavaScript "isoCode != undefined" and "isoCode != null" both do the exact same. So you're safe to drop one of them. This only works with != and not with !== Commented Feb 16, 2017 at 8:41
  • @Antonio it is an internal .constant dictionary. Commented Feb 16, 2017 at 10:12

1 Answer 1

1

I would recommend you organize your data at Languagesin an object or map, it'll be much faster and simpler when you fetch your translation by an abbreviation. A short example:

angular.module('portalDashboardApp')
  .factory('Languages', function(){
    var dictionary = {
      ISO: {name: 'International Organization for Standardization'}
    };
    return {
       get: function(abbr){
           return dict[abbr];
       }
    };
  }).service('ISOtoLanguageService', ['Languages', function(Languages) {
    this.returnLanguage = function(isoCode) {
       if(!isoCode) {
         return "Answer for empty isoCode";
       }
       var categoryObject = Languages.get(isoCode);
       return (categoryObject || {}).name || "I don't know this abbr";
  };

}]);

I'm not sure that this JS works without any syntax error (I've not try to launch it) but idea is that you don't need array and filter on big dictionaries and you are able to get any abbreviation from dict with O(1) complexity even with huge dictionary.

If you don't want to have a refactoring with your code you can do something like this:

angular.module('portalDashboardApp')

  .service('ISOtoLanguageService', ['Languages', function(Languages) {

    this.returnLanguage = function(isoCode) {
        if (!isoCode) {
            return;
        }

        var resultAbbrs = Languages.filter(function (categoryObject) {
            return categoryObject.code === isoCode;
        });

        if (resultAbbrs.length > 0) {
            return resultAbbrs[0].name;
        }
    };
  }]);

In this case if isoCode is null, undefined or empty string or this key is not found in dictionary return undefined will be by default. Outside you should check a result of this function with if (result === undefined) ...

I hope it helped you)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the input and feedback and great suggestion. But is there perhaps a way for me to do an evaluation on .filter with my code as is?
@onmyway I changed my answer, try it please
Thank you, works like a charm. Also, just an FYI; although I have my language dictionary set up as .constant dictionary, and I use the method as shown above, to export a file of 1000 records to csv, doing the language conversion and other stuff, takes a fraction of a second.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.