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)
};
asked Oct 17, 2012 at 11:44
\$\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);
 }
}
answered Oct 17, 2012 at 12:51
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.