Skip to content

Instantly share code, notes, and snippets.

@artursopelnik
Forked from zoxon/data-options.js
Created October 6, 2016 11:05
Show Gist options
  • Save artursopelnik/04a36df3512015b3ac732bf9e2e8e473 to your computer and use it in GitHub Desktop.
Save artursopelnik/04a36df3512015b3ac732bf9e2e8e473 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