File: merge.js

package info (click to toggle)
node-immutable 4.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,640 kB
  • sloc: javascript: 8,266; sh: 17; makefile: 6
file content (138 lines) | stat: -rw-r--r-- 4,659 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// @flow
import {
  List,
  Map,
  Record,
  type RecordOf,
  type RecordFactory,
  get,
  getIn,
  has,
  hasIn,
  merge,
  mergeDeep,
  mergeWith,
  mergeDeepWith,
  remove,
  removeIn,
  set,
  setIn,
  update,
  updateIn,
} from 'immutable';

// merge: Objects as Maps

type ObjMap<T> = { [key: string]: T };
const objMap: ObjMap<number> = { x: 12, y: 34 };
(merge(objMap, { x: 321 }): ObjMap<number>);
(merge(objMap, { z: 321 }): ObjMap<number>);
// $FlowExpectedError[incompatible-call]
(merge(objMap, { x: 'abc' }): ObjMap<number>);
(merge(objMap, [['x', 321]]): ObjMap<number>);
(merge(objMap, [['z', 321]]): ObjMap<number>);
// $FlowExpectedError[incompatible-call]
(merge(objMap, [['x', 'abc']]): ObjMap<number>);
// $FlowExpectedError[incompatible-call]
(merge(objMap, [321]): ObjMap<number>);
(merge(objMap, Map({ x: 123 })): ObjMap<number>);
(merge(objMap, Map({ z: 123 })): ObjMap<number>);
(merge(objMap, Map([['x', 123]])): ObjMap<number>);
(merge(objMap, Map([['z', 123]])): ObjMap<number>);
// $FlowExpectedError[incompatible-call]
(merge(objMap, List([123])): ObjMap<number>);

// merge: Maps

const map = Map({ key: 'value' });
(merge(map, { key: 'alternate' }): Map<string, string>);
(merge(map, { otherKey: 'value' }): Map<string, string>);
(merge(map, Map({ key: 'alternate' })): Map<string, string>);
(merge(map, Map({ otherKey: 'value' })): Map<string, string>);
(merge(map, [['otherKey', 'value']]): Map<string, string>);
// $FlowExpectedError[incompatible-call] (functional merge cannot return union value types)
(merge(map, Map({ otherKey: 123 })): Map<string, string | number>);
// $FlowExpectedError[incompatible-call]
(merge(map, [4, 5, 6]): Map<string, string>);
// $FlowExpectedError[incompatible-call]
(merge(map, 123): Map<string, string>);
// $FlowExpectedError[incompatible-call]
(merge(map, { '0': 123 }): Map<string, string>);
// $FlowExpectedError[incompatible-call]
(merge(map, [
  [0, 4],
  [1, 5],
  [1, 6],
]): Map<string, string>);

// merge: Lists

const list = List([1, 2, 3]);
(merge(list, [4, 5, 6]): List<number>);
(merge(list, List([4, 5, 6])): List<number>);
// $FlowExpectedError[incompatible-call] (functional merge cannot return union value types)
(merge(list, ['a', 'b', 'c']): List<number | string>);
// $FlowExpectedError[incompatible-call] (functional merge cannot return union value types)
(merge(list, List(['a', 'b', 'c'])): List<number | string>);
// $FlowExpectedError[incompatible-call]
(merge(list, 123): List<number>);
// $FlowExpectedError[incompatible-call]
(merge(list, { '0': 123 }): List<number>);
// $FlowExpectedError[incompatible-call]
(merge(list, Map({ '0': 123 })): List<number>);
// $FlowExpectedError[incompatible-call]
(merge(list, [
  [0, 4],
  [1, 5],
  [1, 6],
]): List<number>);

// merge: Objects as Records

type XYPoint = { x: number, y: number };
const objRecord: XYPoint = { x: 12, y: 34 };
(merge(objRecord, { x: 321 }): XYPoint);
(merge(objRecord, [['x', 321]]): XYPoint);
(merge(objRecord, Map({ x: 123 })): XYPoint);
(merge(objRecord, Map([['x', 123]])): XYPoint);
const xyPointRecord = Record({ x: 0, y: 0 });
(merge(objRecord, xyPointRecord({ x: 321 })): XYPoint);
// $FlowExpectedError[incompatible-call]
(merge(objRecord, { x: 'abc' }): XYPoint);
// $FlowExpectedError[incompatible-call]
(merge({ x: 12, y: 34 }, [['x', 'abc']]): XYPoint);
// $FlowExpectedError[incompatible-call]
(merge(objRecord, { z: 321 }): XYPoint);
// $FlowExpectedError[prop-missing]]
// $FlowExpectedError[invalid-call-util]]
(merge(objRecord, [['z', 321]]): XYPoint);
// $FlowExpectedError[incompatible-call]
(merge(objRecord, Map({ z: 123 })): XYPoint);
// $FlowExpectedError[incompatible-call]
(merge(objRecord, Map([['z', 123]])): XYPoint);
// $FlowExpectedError[incompatible-call]
(merge(objRecord, [321]): XYPoint);
// $FlowExpectedError[incompatible-call]
(merge(objRecord, List([123])): XYPoint);

// merge: Arrays

const arr = [1, 2, 3];
(merge(arr, [4, 5, 6]): Array<number>);
(merge(arr, List([4, 5, 6])): Array<number>);
// $FlowExpectedError[incompatible-call] (functional merge cannot return union value types)
(merge(arr, ['a', 'b', 'c']): Array<number | string>);
// $FlowExpectedError[incompatible-call] (functional merge cannot return union value types)
(merge(arr, List(['a', 'b', 'c'])): Array<number | string>);
// $FlowExpectedError[incompatible-call]
(merge(arr, 123): Array<number>);
// $FlowExpectedError[incompatible-call]
(merge(arr, { '0': 123 }): Array<number>);
// $FlowExpectedError[incompatible-call]
(merge(arr, Map({ '0': 123 })): Array<number>);
// $FlowExpectedError[incompatible-call]
(merge(arr, [
  [0, 4],
  [1, 5],
  [1, 6],
]): Array<number>);