Skip to content

Instantly share code, notes, and snippets.

@kistaaa
Last active April 29, 2023 22:20
Show Gist options
  • Save kistaaa/7ba71f3500bfc3bdcdaf44e0359ee11a to your computer and use it in GitHub Desktop.
Save kistaaa/7ba71f3500bfc3bdcdaf44e0359ee11a to your computer and use it in GitHub Desktop.
Object and CSS conversion, for using on "style=" in html elements
// object to css
Object.prototype.css = function() {return Object.entries(this).map(([k, v]) => `${k}:${v}`).join(';')}
// css to object
String.prototype.obj = function() {
let obj = {}
let parts = this.split(";")
for(let part of parts){
let prop = part.split(':')
obj[prop[0]] = prop[1]
}
return obj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment