Skip to content

Instantly share code, notes, and snippets.

@neodigm
Last active July 15, 2020 16:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save neodigm/8b7a8ff1026f04149a99877aad5d85e6 to your computer and use it in GitHub Desktop.
Save neodigm/8b7a8ff1026f04149a99877aad5d85e6 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.
@neodigm
Copy link
Author

neodigm commented Feb 5, 2020

I think this was originally written in COBOL, then transpiled to Advanced Revelation, before is was ported to Smalltalk. Whatever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment