Created
February 2, 2015 04:48
-
-
Save zoxon/247a308b4ac3860566a1 to your computer and use it in GitHub Desktop.
Convert data-options attribute into an object of key/value pairs
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
/** | |
* 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