-
-
Save artursopelnik/04a36df3512015b3ac732bf9e2e8e473 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