CyanSalt / notebook

3 stars 0 forks source link

史上最差的 Case 转化实现 #28

Open CyanSalt opened 4 years ago

CyanSalt commented 4 years ago

path: worst-casing-convertion


let transformer = document.createElement('span')

function camel2kebab(name) {
  const dataset = transformer.dataset
  dataset[name] = '1'
  const key = transformer.attributes[0].name
  delete dataset[name]
  return key.substring(5)
}

function kebab2camel(name) {
  transformer.setAttribute(`data-${name}`, '1')
  const dataset = transformer.dataset
  const key = Object.keys(dataset)[0]
  delete dataset[key]
  return key
}