Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinh/9b56cbdebe5e03715040c2b5278d1fc1 to your computer and use it in GitHub Desktop.
Save dinh/9b56cbdebe5e03715040c2b5278d1fc1 to your computer and use it in GitHub Desktop.
Convert HTML data attribute name to JS dataset name in camel case
function data2prop( sDset ){ // Convert HTML data attrib name to JS dataset name
sDset = sDset.replace("data-", "").toLowerCase();
let aDset = sDset.split(""), aDret = [], bUpper = false;
aDset.forEach( (sVal, nIx) => {
if( sVal == "-" ){
bUpper = true;
}else{
aDret.push( ( bUpper ) ? sVal.toUpperCase() : sVal );
bUpper = false;
}
});
return aDret.join("");
}
// data-is-whatever will be converted to isWhatever
// So you could do e.dataset[ data2prop("data-is-whatever") ]
// I think this was originally written in COBOL, then transpiled to Advanced Revelation, before is was ported to Smalltalk.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment