Skip to content

Instantly share code, notes, and snippets.

@jm2242
Created February 20, 2018 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jm2242/562fff394bc76b1f9c4d1e65dc454938 to your computer and use it in GitHub Desktop.
Save jm2242/562fff394bc76b1f9c4d1e65dc454938 to your computer and use it in GitHub Desktop.
Decode a JSON style object
/**
* Decode a JSON object of style key/value pairs
* Currently only cleans out the 'px' from values
* and ignores styles with spaces to allow for fonts
* @param {Object} styles - styles to decode
* @returns {Object} - decoded key/value pairs
*/
export const decodeStyles = styles => {
const decodedStyles = {}
Object.entries(styles).forEach(
([key, value]) => {
let decodedValue
// the value is a measurement in pixels
if (!value.includes(' ') && value.includes('px')) {
decodedValue = parseInt(value.replace('px',''))
} else {
decodedValue = value // do not change the value
}
decodedStyles[key] = decodedValue
}
)
return decodedStyles
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment