Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created June 9, 2020 15:02
Show Gist options
  • Save jsmanifest/8e3eeb3a30849f7583d6e4af0b73a3fa to your computer and use it in GitHub Desktop.
Save jsmanifest/8e3eeb3a30849f7583d6e4af0b73a3fa to your computer and use it in GitHub Desktop.
const getBaseStyles = () => ({ baseStyles: { color: '#333' } })
function start(component, { resolvers = {}, displayTimestamp }) {
const baseStyles = getBaseStyles()
// This is what will be injected in the returned function from the higher order function
const context = { baseStyles, displayTimestamp }
// This will replace each original resolver and maintain the behavior of the program to behave the same by calling the original resolver inside it
const enhancedResolve = makeInjectContext(context)
let baseResolvers
let styleResolvers
// Ensure base resolvers is the correct data type
if (Array.isArray(resolvers.base)) baseResolvers = resolvers.base
else baseResolvers = [resolvers.base]
// Ensure style resolvers is the correct data type
if (Array.isArray(resolvers.styles)) styleResolvers = resolvers.styles
else styleResolvers = [resolvers.styles]
return {
...component,
...callResolvers(component, baseResolvers.map(enhancedResolve)),
style: {
...component.style,
...callResolvers(component, styleResolvers.map(enhancedResolve)),
},
}
}
const component = {
type: 'div',
style: {
height: 250,
fontSize: 14,
fontWeight: 'bold',
textAlign: 'center',
},
children: [
{
type: 'input',
inputType: 'email',
placeholder: 'Enter your email',
style: {
border: '1px solid magenta',
},
},
],
}
const result = start(component, {
resolvers: {
base: [resolveTimestampInjection, resolveChildren],
styles: [resolveStyles],
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment