Last active
July 15, 2020 16:04
-
-
Save neodigm/8b7a8ff1026f04149a99877aad5d85e6 to your computer and use it in GitHub Desktop.
Convert HTML data attribute name to JS dataset name in camel case
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
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
I think this was originally written in COBOL, then transpiled to Advanced Revelation, before is was ported to Smalltalk. Whatever.