Skip to content

Commit 9feec84

Browse files
committed
Migrate functional files to TS
1 parent 96bf1f9 commit 9feec84

File tree

11 files changed

+312
-131
lines changed

11 files changed

+312
-131
lines changed

src/functional/get.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { has } from './has';
1111
*
1212
* <!-- runkit:activate -->
1313
* ```js
14-
* const { get } = require('immutable')
14+
* import { get } from 'immutable';
15+
*
1516
* get([ 'dog', 'frog', 'cat' ], 1) // 'frog'
1617
* get({ x: 123, y: 456 }, 'x') // 123
1718
* get({ x: 123, y: 456 }, 'z', 'ifNotSet') // 'ifNotSet'

src/functional/getIn.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1-
import coerceKeyPath from '../utils/coerceKeyPath';
1+
import coerceKeyPath, { type KeyPath } from '../utils/coerceKeyPath';
22
import { NOT_SET } from '../TrieUtils';
33
import { get } from './get';
4-
import type { OrderedCollection } from '../../type-definitions/immutable';
54

65
type GetType = typeof get;
76
type GetTypeParameters = Parameters<GetType>;
87
type CollectionType = GetTypeParameters[0];
98
type Key = GetTypeParameters[1];
109

10+
/**
11+
* Returns the value at the provided key path starting at the provided
12+
* collection, or notSetValue if the key path is not defined.
13+
*
14+
* A functional alternative to `collection.getIn(keypath)` which will also
15+
* work with plain Objects and Arrays.
16+
*
17+
* <!-- runkit:activate -->
18+
* ```js
19+
* import { getIn } from 'immutable';
20+
*
21+
* getIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // 123
22+
* getIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p'], 'ifNotSet') // 'ifNotSet'
23+
* ```
24+
*/
1125
export function getIn(
1226
collection: CollectionType,
13-
searchKeyPath: OrderedCollection<Key> | ArrayLike<Key>,
27+
searchKeyPath: KeyPath<Key>,
1428
notSetValue?: GetTypeParameters[2]
1529
): ReturnType<GetType> {
1630
const keyPath = coerceKeyPath(searchKeyPath);

src/functional/has.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ import { isImmutable } from '../predicates/isImmutable';
22
import hasOwnProperty from '../utils/hasOwnProperty';
33
import isDataStructure from '../utils/isDataStructure';
44

5+
/**
6+
* Returns true if the key is defined in the provided collection.
7+
*
8+
* A functional alternative to `collection.has(key)` which will also work with
9+
* plain Objects and Arrays as an alternative for
10+
* `collection.hasOwnProperty(key)`.
11+
*
12+
* <!-- runkit:activate -->
13+
* ```js
14+
* import { has } from 'immutable';
15+
*
16+
* has([ 'dog', 'frog', 'cat' ], 2) // true
17+
* has([ 'dog', 'frog', 'cat' ], 5) // false
18+
* has({ x: 123, y: 456 }, 'x') // true
19+
* has({ x: 123, y: 456 }, 'z') // false
20+
* ```
21+
*/
522
export function has(collection: object, key: PropertyKey): boolean {
623
return isImmutable(collection)
724
? // @ts-expect-error key might be a number or symbol, which is not handled be Record key type

src/functional/hasIn.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import { NOT_SET } from '../TrieUtils';
33

44
type GetInParameters = Parameters<typeof getIn>;
55

6+
/**
7+
* Returns true if the key path is defined in the provided collection.
8+
*
9+
* A functional alternative to `collection.hasIn(keypath)` which will also
10+
* work with plain Objects and Arrays.
11+
*
12+
* <!-- runkit:activate -->
13+
* ```js
14+
* import { hasIn } from 'immutable';
15+
*
16+
* hasIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // true
17+
* hasIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p']) // false
18+
* ```
19+
*/
620
export function hasIn(
721
collection: GetInParameters[0],
822
keyPath: GetInParameters[1]

src/functional/remove.js

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

src/functional/remove.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import type { Collection, Record } from '../../type-definitions/immutable';
2+
import { isImmutable } from '../predicates/isImmutable';
3+
import hasOwnProperty from '../utils/hasOwnProperty';
4+
import isDataStructure from '../utils/isDataStructure';
5+
import shallowCopy from '../utils/shallowCopy';
6+
7+
/**
8+
* Returns a copy of the collection with the value at key removed.
9+
*
10+
* A functional alternative to `collection.remove(key)` which will also work
11+
* with plain Objects and Arrays as an alternative for
12+
* `delete collectionCopy[key]`.
13+
*
14+
* <!-- runkit:activate -->
15+
* ```js
16+
* import { remove } from 'immutable';
17+
*
18+
* const originalArray = [ 'dog', 'frog', 'cat' ]
19+
* remove(originalArray, 1) // [ 'dog', 'cat' ]
20+
* console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
21+
* const originalObject = { x: 123, y: 456 }
22+
* remove(originalObject, 'x') // { y: 456 }
23+
* console.log(originalObject) // { x: 123, y: 456 }
24+
* ```
25+
*/
26+
export function remove<K, C extends Collection<K, unknown>>(
27+
collection: C,
28+
key: K
29+
): C;
30+
export function remove<
31+
TProps extends object,
32+
C extends Record<TProps>,
33+
K extends keyof TProps,
34+
>(collection: C, key: K): C;
35+
export function remove<C extends Array<unknown>>(collection: C, key: number): C;
36+
export function remove<C, K extends keyof C>(collection: C, key: K): C;
37+
export function remove<
38+
C extends { [key: PropertyKey]: unknown },
39+
K extends keyof C,
40+
>(collection: C, key: K): C;
41+
export function remove<K extends PropertyKey>(
42+
collection:
43+
| Collection<K, unknown>
44+
| Array<unknown>
45+
| { [key: PropertyKey]: unknown },
46+
key: K
47+
) {
48+
if (!isDataStructure(collection)) {
49+
throw new TypeError(
50+
'Cannot update non-data-structure value: ' + collection
51+
);
52+
}
53+
if (isImmutable(collection)) {
54+
// @ts-expect-error weird "remove" here,
55+
if (!collection.remove) {
56+
throw new TypeError(
57+
'Cannot update immutable value without .remove() method: ' + collection
58+
);
59+
}
60+
// @ts-expect-error weird "remove" here,
61+
return collection.remove(key);
62+
}
63+
if (!hasOwnProperty.call(collection, key)) {
64+
return collection;
65+
}
66+
const collectionCopy = shallowCopy(collection);
67+
if (Array.isArray(collectionCopy)) {
68+
// @ts-expect-error assert that key is a number here
69+
collectionCopy.splice(key, 1);
70+
} else {
71+
delete collectionCopy[key];
72+
}
73+
return collectionCopy;
74+
}

src/functional/set.js

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

src/functional/set.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import type { Collection, Record } from '../../type-definitions/immutable';
2+
import { isImmutable } from '../predicates/isImmutable';
3+
import hasOwnProperty from '../utils/hasOwnProperty';
4+
import isDataStructure from '../utils/isDataStructure';
5+
import shallowCopy from '../utils/shallowCopy';
6+
7+
/**
8+
* Returns a copy of the collection with the value at key set to the provided
9+
* value.
10+
*
11+
* A functional alternative to `collection.set(key, value)` which will also
12+
* work with plain Objects and Arrays as an alternative for
13+
* `collectionCopy[key] = value`.
14+
*
15+
* <!-- runkit:activate -->
16+
* ```js
17+
* import { set } from 'immutable';
18+
*
19+
* const originalArray = [ 'dog', 'frog', 'cat' ]
20+
* set(originalArray, 1, 'cow') // [ 'dog', 'cow', 'cat' ]
21+
* console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
22+
* const originalObject = { x: 123, y: 456 }
23+
* set(originalObject, 'x', 789) // { x: 789, y: 456 }
24+
* console.log(originalObject) // { x: 123, y: 456 }
25+
* ```
26+
*/
27+
export function set<K, V, C extends Collection<K, V>>(
28+
collection: C,
29+
key: K,
30+
value: V
31+
): C;
32+
export function set<
33+
TProps extends object,
34+
C extends Record<TProps>,
35+
K extends keyof TProps,
36+
>(record: C, key: K, value: TProps[K]): C;
37+
export function set<V, C extends Array<V>>(
38+
collection: C,
39+
key: number,
40+
value: V
41+
): C;
42+
export function set<C, K extends keyof C>(object: C, key: K, value: C[K]): C;
43+
export function set<V, C extends { [key: string]: V }>(
44+
collection: C,
45+
key: string,
46+
value: V
47+
): C;
48+
export function set<K, V, C extends Collection<K, V> | { [key: string]: V }>(
49+
collection: C,
50+
key: K | string,
51+
value: V
52+
): C {
53+
if (!isDataStructure(collection)) {
54+
throw new TypeError(
55+
'Cannot update non-data-structure value: ' + collection
56+
);
57+
}
58+
if (isImmutable(collection)) {
59+
// @ts-expect-error weird "set" here,
60+
if (!collection.set) {
61+
throw new TypeError(
62+
'Cannot update immutable value without .set() method: ' + collection
63+
);
64+
}
65+
// @ts-expect-error weird "set" here,
66+
return collection.set(key, value);
67+
}
68+
// @ts-expect-error mix of key and string here. Probably need a more fine type here
69+
if (hasOwnProperty.call(collection, key) && value === collection[key]) {
70+
return collection;
71+
}
72+
const collectionCopy = shallowCopy(collection);
73+
// @ts-expect-error mix of key and string here. Probably need a more fine type here
74+
collectionCopy[key] = value;
75+
return collectionCopy;
76+
}

src/functional/updateIn.js

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

0 commit comments

Comments
 (0)