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

Suggest ommiting sub keys with dot notation #11

Open
andyjamesn opened this issue Feb 26, 2024 · 0 comments
Open

Suggest ommiting sub keys with dot notation #11

andyjamesn opened this issue Feb 26, 2024 · 0 comments

Comments

@andyjamesn
Copy link

andyjamesn commented Feb 26, 2024

This is a suggestion to be able to omit nested values in the state.

Example omit: ['editor.color'] instead of just omit: ['editor']

I tested this out and this seems to work, but I am not completely sure if this is all that is needed or if there is something else I may have missed.

function removeOmittedKeys(
  options: Options,
  store: Store,
  serializer: Serializer = { serialize: JSON.stringify, deserialize: JSON.parse },
): Store['$state'] {
  const clone = serializer.deserialize(serializer.serialize(store.$state))
  if (options.undo && options.undo.omit) {
    options.undo.omit.forEach((key) => {
      if (key.includes('.')) {
        const keys = key.split('.')
        let obj = clone
        for (let i = 0; i < keys.length - 1; i++) {
          obj = obj[keys[i]]
        }
        delete obj[keys[keys.length - 1]]
        return
      }
      delete clone[key]
    })
    return clone
  }
  return clone
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant