Skip to content

Commit 20a9940

Browse files
committed
setIn / removeIn
1 parent e89e7c9 commit 20a9940

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

src/functional/removeIn.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/functional/removeIn.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { updateIn, type PossibleCollection } from './updateIn';
2+
import { NOT_SET } from '../TrieUtils';
3+
import type { KeyPath } from '../../type-definitions/immutable';
4+
5+
/**
6+
* Returns a copy of the collection with the value at the key path removed.
7+
*
8+
* A functional alternative to `collection.removeIn(keypath)` which will also
9+
* work with plain Objects and Arrays.
10+
*
11+
* <!-- runkit:activate -->
12+
* ```js
13+
* import { removeIn } from 'immutable';
14+
*
15+
* const original = { x: { y: { z: 123 }}}
16+
* removeIn(original, ['x', 'y', 'z']) // { x: { y: {}}}
17+
* console.log(original) // { x: { y: { z: 123 }}}
18+
* ```
19+
*/
20+
export function removeIn<
21+
K extends PropertyKey,
22+
V,
23+
TProps extends object,
24+
C extends PossibleCollection<K, V, TProps>,
25+
>(collection: C, keyPath: KeyPath<K>): C {
26+
return updateIn(collection, keyPath, () => NOT_SET);
27+
}

src/functional/setIn.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/functional/setIn.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { updateIn, type PossibleCollection } from './updateIn';
2+
import { NOT_SET } from '../TrieUtils';
3+
import type { KeyPath } from '../../type-definitions/immutable';
4+
5+
/**
6+
* Returns a copy of the collection with the value at the key path set to the
7+
* provided value.
8+
*
9+
* A functional alternative to `collection.setIn(keypath)` which will also
10+
* work with plain Objects and Arrays.
11+
*
12+
* <!-- runkit:activate -->
13+
* ```js
14+
* import { setIn } from 'immutable';
15+
*
16+
* const original = { x: { y: { z: 123 }}}
17+
* setIn(original, ['x', 'y', 'z'], 456) // { x: { y: { z: 456 }}}
18+
* console.log(original) // { x: { y: { z: 123 }}}
19+
* ```
20+
*/
21+
export function setIn<
22+
K extends PropertyKey,
23+
V,
24+
TProps extends object,
25+
C extends PossibleCollection<K, V, TProps>,
26+
>(collection: C, keyPath: KeyPath<K>, value: unknown): C {
27+
return updateIn(collection, keyPath, NOT_SET, () => value);
28+
}

0 commit comments

Comments
 (0)