Created
February 20, 2018 20:27
-
-
Save jm2242/562fff394bc76b1f9c4d1e65dc454938 to your computer and use it in GitHub Desktop.
Decode a JSON style object
This file contains 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
/** | |
* 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