str.toDatasetPropName()

convert (kebab-cased) "data-xxx" attribute name to (camel-cased) `dataset` property name.

// ⭐️ string.toDatasetPropName()
//    string is assumed to be in "data-xxx" format (kebab case)
String.prototype.toDatasetPropName = function(){                     
  return this                       // 'data-xx-yy-zz'
    .split('-')                     // ['data', 'xx', 'yy', 'zz']
    .slice(1)                       // remove 'data'
    .map((str, i) => (i === 0 ? str : str.capitalize()) ) // ['xx', 'Yy', 'Zz']
    .join('');                      // xxYyZz
};

Last updated