Skip to content

Instantly share code, notes, and snippets.

function findValue(arr, callback) {
for (let index = 0; index < arr.length; index++) {
const value = arr[index]
if (callback(value)) {
return value
}
}
}
{
"type": "div",
"style": {
"height": 300,
"fontSize": 14,
"fontWeight": "bold",
"textAlign": "center"
},
"children": [
{
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
const getBaseStyles = () => ({ baseStyles: { color: '#333' } })
const baseStyles = getBaseStyles()
const injectContext = makeInjectContext({
baseStyles,
})
function resolveTimestampInjection(component) {
return function ({ displayTimestamp }) {
if (displayTimestamp === true) {
function makeInjectContext(context) {
return function (callback) {
return function (...args) {
let result = callback(...args)
if (typeof result === 'function') {
// Call it again and inject additional options
result = result(context)
}
return result
}
function callResolvers(component, resolvers) {
let result
for (let index = 0; index < resolvers.length; index++) {
const resolver = resolvers[index]
const resolved = resolver(component)
if (resolved) {
result = { ...result, ...resolved }
}
function resolveTimestampInjection(component) {
return function ({ displayTimestamp }) {
if (displayTimestamp === true) {
return {
time: new Date(currentDate).toLocaleTimeString(),
}
}
}
}
function callResolvers(component, resolvers) {
let result
for (let index = 0; index < resolvers.length; index++) {
const resolver = resolvers[index]
const resolved = resolver(component)
if (resolved) {
result = { ...result, ...resolved }
}
}
function resolveStyles(component) {
let result = {}
// Restrict it from displaying in a smaller size
if (component.style.height < 300) {
result['height'] = 300
}
if (component.type === 'button') {
// Give all button components a dashed teal border
TypeError: Cannot read property 'children' of undefined