Forked from neodigm/html_data_attrib_2_javascript_dataset.js
Created
July 12, 2020 07:33
-
-
Save dinh/9b56cbdebe5e03715040c2b5278d1fc1 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