Last active
January 20, 2022 02:47
-
-
Save simontegg/6df19267be79b38409e52720402e8b7d to your computer and use it in GitHub Desktop.
A map function with an error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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