Skip to content

Commit

Permalink
feat(atom): add deleteIn()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 17, 2018
1 parent fbc819e commit b593a9b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/atom/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,24 @@ export function updateIn(state: any, path: PropertyKey | PropertyKey[], fn: Swap
return setter(path)(state, fn.apply(null, args));
}

/**
* Uses `updateIn()` and returns updated state with key for given path removed.
* Does not modify original state.
*
* Returns `undefined` if `path` is an empty array.
*
* ```
* deleteIn({a:{b:{c: 23}}}, "a.b.c");
* // {a: {b: {}}}
* ```
*
* @param state
* @param path
*/
export function deleteIn(state: any, path: PropertyKey | PropertyKey[]) {
const ks = [...toPath(path)];
if (ks.length > 0) {
const k = ks.pop();
return updateIn(state, ks, (x) => { x = { ...x }; delete x[k]; return x; });
}
}

0 comments on commit b593a9b

Please sign in to comment.