Skip to content

Instantly share code, notes, and snippets.

@simontegg
Last active January 20, 2022 02:47
Show Gist options
  • Select an option

  • Save simontegg/6df19267be79b38409e52720402e8b7d to your computer and use it in GitHub Desktop.

Select an option

Save simontegg/6df19267be79b38409e52720402e8b7d to your computer and use it in GitHub Desktop.
A map function with an error
// inspired by https://rescript-lang.org/docs/manual/v8.0.0/api/belt/map#getexn
function makeErroringMap (array, byIdFunc, defaultValue) {
const wm = new WeakMap()
array.forEach(item => {
wm.set(byIdFunc(item), item)
})
return {
get(id) {
const item = wm.get(id)
if (defaultValue && !item) {
return defaultValue
}
if (!defaultValue && !item) {
throw new Error(`item with id: ${id} not found in ${JSON.stringify(array)}`
}
return item
},
has(id) {
return wm.has(id)
}
}
}
export const getWorkflowsMap = createSelector(
getWorkflows,
workflows => makeErroringMap(workflows, workflow => workflow.id)
)
/**
* @returns {Workflow}
*/
export const getWorkFlow = createSelector(
getWorkflowsMap,
getId,
(workflowsMap, id) => workflowsMap.get(id)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment