Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: add something like a save() or as() function #800

Open
josephjclark opened this issue Oct 25, 2024 · 1 comment
Open

common: add something like a save() or as() function #800

josephjclark opened this issue Oct 25, 2024 · 1 comment

Comments

@josephjclark
Copy link
Collaborator

A common use case is to run an operation and then to save the result back to state on a key other than data

Like this:

get('fixture/?fixture_type=option_set_mapping', {}, state => {
  state.lookup_table.option_set = state.data;
  return state;
});

This is really quite painful.

What if we had a wrapper operation that was able to take any state.data object and proxy/alias/push it to some other key:

save('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))

The implementation is something like this:

function save(key, op) {
  return async (state) => {
       const data = state.data // save the incoming state
       const result  = await(op(state))
       state[key]=result;
       state.data = data;       
       return state;
  }
}

I don't like name though. Some ideas:

keyby('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))
key('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))
alias('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))
proxy('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))
@github-project-automation github-project-automation bot moved this to New Issues in v2 Oct 25, 2024
@josephjclark
Copy link
Collaborator Author

Ooh maybe as

as('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))

I like this because it's quite natural language-y. It would be better to do like get(something).as(key), but the as function would have no means to restore the prior state. The get would just blat it. References would be a workaround but we can't guarantee it will have written.

@josephjclark josephjclark changed the title common: add something like a save() function common: add something like a save() or as() function Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New Issues
Development

No branches or pull requests

1 participant