Skip to content

Instantly share code, notes, and snippets.

@jimbol
Created March 28, 2017 21:34
Show Gist options
  • Save jimbol/edbf0d4a7be235bf9befb7dc58d9481c to your computer and use it in GitHub Desktop.
Save jimbol/edbf0d4a7be235bf9befb7dc58d9481c to your computer and use it in GitHub Desktop.
Helpers for hash reducers
const update = (id, hash, changes) => {
const newHash = hash;
const updateEntity = { ...newHash[id], ...changes };
newHash[id] = updateEntity;
return newHash;
};
const replace = (id, nextEntity, hash) => {
let newHash = hash;
newHash = remove(id, newHash);
newHash = add(nextEntity, newHash);
return newHash;
};
const remove = (id, hash) => {
const newHash = hash;
delete newHash[id];
return newHash;
};
const add = (entity, hash) => {
const newHash = hash;
newHash[entity.id] = entity;
return newHash;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment