MCPcopy
hub / github.com/immutable-js/immutable-js / set

Function set

src/functional/set.ts:37–69  ·  view source on GitHub ↗
(
  collection: C,
  key: K | string,
  value: V
)

Source from the content-addressed store, hash-verified

35 value: V
36): C;
37export function set<K, V, C extends Collection<K, V> | { [key: string]: V }>(
38 collection: C,
39 key: K | string,
40 value: V
41): C {
42 if (isProtoKey(key)) {
43 return collection;
44 }
45
46 if (!isDataStructure(collection)) {
47 throw new TypeError(
48 'Cannot update non-data-structure value: ' + collection
49 );
50 }
51 if (isImmutable(collection)) {
52 // @ts-expect-error weird "set" here,
53 if (!collection.set) {
54 throw new TypeError(
55 'Cannot update immutable value without .set() method: ' + collection
56 );
57 }
58 // @ts-expect-error weird "set" here,
59 return collection.set(key, value);
60 }
61 // @ts-expect-error mix of key and string here. Probably need a more fine type here
62 if (hasOwnProperty.call(collection, key) && value === collection[key]) {
63 return collection;
64 }
65 const collectionCopy = shallowCopy(collection);
66 // @ts-expect-error mix of key and string here. Probably need a more fine type here
67 collectionCopy[key] = value;
68 return collectionCopy;
69}

Callers 5

updateInDeeplyFunction · 0.90
immutable-flow.jsFile · 0.85
list.tsFile · 0.85
functional.tsFile · 0.85
set.tsFile · 0.85

Calls 5

isProtoKeyFunction · 0.90
isImmutableFunction · 0.90
isDataStructureFunction · 0.85
shallowCopyFunction · 0.85
setMethod · 0.65

Tested by

no test coverage detected