8000 Migrate functional dir to TypeScript · immutable-js/immutable-js@f5f4a2a · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5f4a2a

Browse files
committed
Migrate functional dir to TypeScript
1 parent ec7cb81 commit f5f4a2a

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

src/functional/get.js

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

src/functional/get.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type { Collection, Record } from '../../type-definitions/immutable';
2+
import { isImmutable } from '../predicates/isImmutable';
3+
import { has } from './has';
4+
5+
/**
6+
* Returns the value within the provided collection associated with the
7+
* provided key, or notSetValue if the key is not defined in the collection.
8+
*
9+
* A functional alternative to `collection.get(key)` which will also work on
10+
* plain Objects and Arrays as an alternative for `collection[key]`.
11+
*
12+
* <!-- runkit:activate -->
13+
* ```js
14+
* const { get } = require('immutable')
15+
* get([ 'dog', 'frog', 'cat' ], 1) // 'frog'
16+
* get({ x: 123, y: 456 }, 'x') // 123
17+
* get({ x: 123, y: 456 }, 'z', 'ifNotSet') // 'ifNotSet'
18+
* ```
19+
*/
20+
export function get<K, V>(collection: Collection<K, V>, key: K): V | undefined;
21+
export function get<K, V, NSV>(
22+
collection: Collection<K, V>,
23+
key: K,
24+
notSetValue: NSV
25+
): V | NSV;
26+
export function get<TProps extends object, K extends keyof TProps>(
27+
record: Record<TProps>,
28+
key: K,
29+
notSetValue: unknown
30+
): TProps[K];
31+
export function get<V>(collection: Array<V>, key: number): V | undefined;
32+
export function get<V, NSV>(
33+
collection: Array<V>,
34+
key: number,
35+
notSetValue: NSV
36+
): V | NSV;
37+
export function get<C extends object, K extends keyof C>(
38+
object: C,
39+
key: K,
40+
notSetValue: unknown
41+
): C[K];
42+
export function get<V>(
43+
collection: { [key: string]: V },
44+
key: string
45+
): V | undefined;
46+
export function get<V, NSV>(
47+
collection: { [key: string]: V },
48+
key: string,
49+
notSetValue: NSV
50+
): V | NSV;
51+
export function get<K extends PropertyKey, V, NSV>(
52+
collection: Collection<K, V> | Array<V> | { [key: string]: V },
53+
key: K,
54+
notSetValue?: NSV
55+
): V | NSV {
56+
return isImmutable(collection)
57+
? collection.get(key, notSetValue)
58+
: !has(collection, key)
59+
? notSetValue
60+
: // @ts-expect-error weird "get" here,
61+
typeof collection.get === 'function'
62+
? // @ts-expect-error weird "get" here,
63+
collection.get(key)
64+
: // @ts-expect-error key is unknown here,
65+
collection[key];
66+
}

src/functional/has.js renamed to src/functional/has.ts

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

5-
export function has(collection, key) {
5+
export function has(collection: object, key: PropertyKey): boolean {
66
return isImmutable(collection)
7-
? collection.has(key)
7+
? // @ts-expect-error key might be a number or symbol, which is not handled be Record key type
8+
collection.has(key)
89
: isDataStructure(collection) && hasOwnProperty.call(collection, key);
910
}

0 commit comments

Comments
 (0)
0