Skip to content

Instantly share code, notes, and snippets.

@turingmachine
Last active January 14, 2021 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turingmachine/6939690f3ec1e11d621c5230c01ae6e4 to your computer and use it in GitHub Desktop.
Save turingmachine/6939690f3ec1e11d621c5230c01ae6e4 to your computer and use it in GitHub Desktop.
// storeinventory module
const storeinventoryLoader = async productIds => {
return await fetch(productIds)
}
const storeinventoryTransformer = (storeinventory, product) => {
return { ...product, ...storeinventory[product.id] }
}
// excludedProducts module
const exludedProductsLoader = async productIds => {
return await fetch(productIds)
}
const exludedProductsFilter = (excludedProductIds, product) => {
return excludedProductIds.includes(excludedProductIds[product.id])
}
const REGISTRY = {
default: {
loaders: [],
transformers: [],
filters: [],
},
storeinventory: {
loaders: [storeinventoryLoader],
transformers: [storeinventoryTransformer],
},
excludedProducts: {
loaders: [exludedProductsLoader],
filters: [exludedProductsFilter],
},
}
const transform = async (registry, products) => {
// call loders with productIds and create loaderData object
const productIds = _.map(products, 'id')
const loaderData = await _.zipObject(
_.keys(registry),
await Promise.all(
_.map(_.values(registry), 'loaders').map(loaders =>
Promise.all(_.over(loaders)(productIds))
)
)
)
// curry transformer functions with loaderData
const transformer: Array<any> = _.keys(registry).flatMap(key => {
const transformers = registry[key].transformers ?? []
return _.over(transformers.map(_.curry))(loaderData[key])
})
// curry transformer functions with loaderData
const transformers: Array<any> = _.keys(registry).flatMap(key => {
const transformers = registry[key].transformers ?? []
return _.over(transformers.map(_.curry))(loaderData[key])
})
// curry filter functions with loaderData
const filters: Array<any> = _.keys(registry).flatMap(key => {
const filters = registry[key].filters ?? []
return _.over(filters.map(_.curry))(loaderData[key])
})
// push product through transformer and filter functions
return products.map(_.flow(transformers)).filter(_.flow(filters))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment