Created
October 1, 2015 22:10
-
-
Save dralletje/1784826c8c3d2e7e3a08 to your computer and use it in GitHub Desktop.
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
// asPairs :: (Object Any) -> [ (String, Any) ] | |
const asPairs = object => | |
Object.keys(object).map(key => [key, object[key]]) | |
// asObject :: [ (string, Any) ] -> Object | |
const asObject = pairs => | |
pairs.reduce((object, [key, value]) => { | |
object[key] = value | |
return object | |
}, {}) | |
// This is not a real haskell type signature, I know | |
// resolveStyles :: (Object (Or Callable (Object Any))) -> (Object (Object Any)) | |
const resolveStyles = styles => { | |
const result = {} | |
const functions = asObject(asPairs(styles).map( ([key, value]) => | |
[key, () => { | |
if(result[key]) return result[key] | |
const x = typeof(value) === 'function' | |
? value.call(functions) | |
: value | |
result[key] = x | |
}] | |
)) | |
Object.keys(styles).forEach(key => functions[key]()) | |
return result | |
} | |
export default resolveStyles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment