3
\$\begingroup\$

The following code works but I am wondering if it can be improved both readability and performance.
I am using jquery and underscore.
Any suggestions, comments or feedbacks will be appreciate. thanks.

var getThemeName = function (theme_name, transparency) {
    if (theme_name === 'light') {
        if (transparency) {
            return translator.get('messages:theme.info.light_with_transparency', {
                percentage: transparency
            });
        }
        return translator.get('messages:theme.info.light');
    } else if (theme_name === 'dark') {
        if (transparency) {
            return translator.get('messages:theme.info.dark_with_transparency', {
                percentage: transparency
            });
        }
        return translator.get('messages:theme.info.dark');
    }
};

var THEME_ITEMS = {
    themeLight: getThemeName('light'),
    themeLightTransparency75: getThemeName('light', 75),
    themeLightTransparency50: getThemeName('light', 50),
    themeLightTransparency25: getThemeName('light', 25),
    themeDark: getThemeName('dark'),
    themeDarkTransparency75: getThemeName('dark', 75),
    themeDarkTransparency50: getThemeName('dark', 50),
    themeDarkTransparency25: getThemeName('dark', 25)
};
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

If you keep the naming consistent, you could probably do something like:

function getThemeName(themeName, transparency) {
  var name = "messages:theme.info." + themeName;
  if( transparency ) {
    name += "_with_transparency";
    return translator.get(name, { percentage: transparency });
  } else {
    return translator.get(name);
  }
}
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.