Last active
August 29, 2015 14:02
-
-
Save GonchuB/cfa7b641f04ee6286626 to your computer and use it in GitHub Desktop.
parseResponseSettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
_ = require('underscore'), | |
Utils = { | |
string: require('../utils/string') | |
}, | |
whereToValues = { | |
notification: ['inApp', 'email'], | |
sharing: ['facebook', 'twitter'] | |
}, | |
categoryValues = { | |
notification: ['favoriteTour', 'favoriteMedia', 'facebookFriend'], | |
sharing: ['favoriteArtist', 'favoriteVideo', 'playlistView'] | |
} | |
function parseResponseSettings (settings, type) { | |
var parsedSettings = {}, | |
_whereToValues = whereToValues[type]; | |
settings = settings[type]; | |
// Iterate through possible category share values. | |
_.each(categoryValues[type], function(action) { | |
// Front-End expects 'notificationFavoriteMedia', and we have just 'favoriteMedia'. | |
var category = type + Utils.string.capitalize(action); | |
// Initialize empty category. | |
parsedSettings[category] = {}; | |
// Find user settings for given category. | |
var foundSetting = _.findWhere(settings, {category: action}); | |
// Iterate through possible whereToValues to set category to true or false. | |
_.each(_whereToValues, function(whereToValue) { | |
// notificationFavoriteMedia.facebook = true if foundSetting.whereTo (array) contains facebook | |
parsedSettings[category][whereToValue] = !!(foundSetting && _.contains(foundSetting.whereTo, whereToValue)); | |
}); | |
}); | |
return parsedSettings; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment