Created
March 28, 2020 15:48
-
-
Save jsmanifest/98de67c4958d886ca3553c00bfd1b81f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 transformStyleObject(styleObj) { | |
const result = {} | |
const keys = Object.keys(styleObj) | |
keys.forEach((key) => { | |
if (key === 'border') { | |
const { color, width, style } = styleObj.border | |
if (color) result.borderColor = color | |
if (width) result.borderWidth = width | |
if (style) result.borderStyle = style | |
} else if (key === 'textColor') { | |
result['color'] = styleObj.textColor | |
} else if (key === 'width') { | |
result['width'] = `${Number(styleObj.width) * 100}vw` | |
} else if (key === 'height') { | |
result['height'] = `${Number(styleObj.height) * 100}vh` | |
} else { | |
result[key] = styleObj[key] | |
} | |
}) | |
return result | |
} | |
const result = transformStyleObject({ | |
border: { | |
width: '2px', | |
style: 'dashed', | |
}, | |
height: '0.42', | |
}) | |
console.log(result) // result: { borderWidth: '2px', borderStyle: 'dashed', height: '42vh' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment