Skip to content

Instantly share code, notes, and snippets.

@dralletje
Created October 1, 2015 22:10
Show Gist options
  • Save dralletje/1784826c8c3d2e7e3a08 to your computer and use it in GitHub Desktop.
Save dralletje/1784826c8c3d2e7e3a08 to your computer and use it in GitHub Desktop.
// 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