Skip to content

Instantly share code, notes, and snippets.

@zoxon
Created February 2, 2015 04:48
Show Gist options
  • Save zoxon/247a308b4ac3860566a1 to your computer and use it in GitHub Desktop.
Save zoxon/247a308b4ac3860566a1 to your computer and use it in GitHub Desktop.
Convert data-options attribute into an object of key/value pairs
/**
* Convert data-options attribute into an object of key/value pairs
* @private
* @param {String} options Item-specific options as a data attribute string
* @returns {Object}
*/
var getDataOptions = function ( options ) {
var settings = {};
// Trim whitespace from a string
var trim = function ( string ) {
return string.replace(/^s+|s+$/g, '');
};
// Create a key/value pair for each setting
if ( options ) {
options = options.split(';');
options.forEach( function(option) {
option = trim(option);
if ( option !== '' ) {
option = option.split(':');
settings[option[0]] = trim(option[1]);
}
});
}
return settings;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment