-
Notifications
You must be signed in to change notification settings - Fork 14
/
map.js
296 lines (238 loc) · 6.97 KB
/
map.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import {Typed, Type, Union, Any, typeOf, construct} from "./typed"
import * as Immutable from 'immutable'
const ImmutableMap = Immutable.Map
const {Keyed} = Immutable.Iterable
const $store = Typed.store
const $type = Typed.type
const $read = Typed.read
const $step = Typed.step
const $init = Typed.init
const $result = Typed.result
const $label = Typed.label
const $typeName = Typed.typeName
const $empty = Typed.empty
class EntryType extends Type {
constructor(key, value, label) {
super()
this.key = key
this.value = value
this.label = label
}
[Typed.typeName]() {
return this.label ||
`${this.key[$typeName]()}, ${this.value[$typeName]()}`
}
[Typed.read]([key, value]) {
const keyResult = this.key[$read](key)
if (keyResult instanceof TypeError) {
return TypeError(`Invalid key: ${keyResult.message}`)
}
const valueResult = this.value[$read](value)
if (valueResult instanceof TypeError) {
return TypeError(`Invalid value: ${valueResult.message}`)
}
return [keyResult, valueResult]
}
}
class InferredEntryType extends EntryType {
constructor() {
super(key, value)
}
toStatic() {
return new MapEntryType(this.key, this.value)
}
[Typed.typeName]() {
const key = this.key ? this.key[$typeName]() : "TypeInferred"
const value = this.value ? this.value[$typeName]() : "TypeInferred"
return `${key}, ${value}`
}
[Typed.read](entry) {
// typeOf usually creates type for the value with that
// value being a default. For type inference we should
// actually use a base type instead of type with default
// there for we use prototype of the constructor.
const key = typeOf(entry[0]).constructor.prototype
this.key = this.key ? Union(this.key, key) : key
const value = typeOf(entry[1]).constructor.prototype
this.value = this.value ? Union(this.value, value) : value
return entry
}
}
const BaseImmutableMap = function() {}
BaseImmutableMap.prototype = Immutable.Map.prototype
class TypedMap extends BaseImmutableMap {
constructor(value) {
super()
return TypedMap.prototype[$read](value)
}
advance(store) {
const result = store.__ownerID ? this : construct(this)
result[$store] = store
result.size = store.size
result.__ownerID = store.__ownerID
return result
}
[Typed.init]() {
return this.advance(ImmutableMap()).asMutable()
}
[Typed.step](state, entry) {
const result = this[$type][$read](entry)
if (result instanceof TypeError) {
throw result
}
const [key, value] = result
return state.advance(state[$store].set(key, value))
}
[Typed.result](state) {
return state.asImmutable()
}
[Typed.read](structure) {
const constructor = this.constructor
if (structure === null || structure === void(0)) {
if (!this[$empty]) {
this[$empty] = this.advance(ImmutableMap())
}
return this[$empty]
}
const isInstance = structure instanceof constructor &&
structure.constructor === constructor
if (isInstance) {
return structure
}
const entries = Keyed(structure).entries()
const type = this[$type]
let state = this[$init]()
while (true) {
const {done, value: entry} = entries.next()
if (done) {
break
}
const result = type[$read](entry)
if (result instanceof TypeError) {
return result
}
state = state[$step](state, result)
}
return this[$result](state)
}
[Typed.typeName]() {
return this[$label] || `Typed.Map(${this[$type][$typeName]()})`
}
toString() {
return this.__toString(this[$typeName]() + '({', '})')
}
has(key) {
return this[$store].has(key)
}
get(key, fallback) {
return this[$store].get(key, fallback)
}
clear() {
if (this.size === 0) {
return this
}
if (this.__ownerID) {
return this.advance(this[$store].clear())
}
return this[$empty] || this[$read]()
}
remove(key) {
return this.advance(this[$store].remove(key))
}
set(key, value) {
return this[$step](this, [key, value])
}
wasAltered() {
return this[$store].wasAltered()
}
__ensureOwner(ownerID) {
const result = this.__ownerID === ownerID ? this :
!ownerID ? this :
construct(this)
const store = this[$store].__ensureOwner(ownerID)
result[$store] = store
result.size = store.size
result.__ownerID = ownerID
return result
}
__iterator(type, reverse) {
return this[$store].__iterator(type, reverse)
}
__iterate(f, reverse) {
return this[$store].__iterate(f, reverse)
}
}
TypedMap.prototype[Typed.DELETE] = TypedMap.prototype.remove
class TypeInferredMap extends TypedMap {
constructor() {
super()
}
[Typed.init]() {
const result = this.advance(ImmutableMap()).asMutable()
result[$type] = new InferredEntryType()
return result
}
[Typed.result](state) {
const result = state.asImmutable()
result[$type] = state[$type].toStatic()
return result
}
}
export const Map = function(keyDescriptor, valueDescriptor, label) {
if (keyDescriptor === void(0)) {
throw TypeError("Typed.Map must be passed a key type descriptor")
}
if (valueDescriptor === void(0)) {
throw TypeError("Typed.Map must be passed a value type descriptor")
}
// If both key and value types are Any this is just a plain immutable map.
if (keyDescriptor === Any && valueDescriptor === Any) {
return ImmutableMap
}
const keyType = typeOf(keyDescriptor)
const valueType = typeOf(valueDescriptor)
if (keyType === Any && keyDescriptor !== Any) {
throw TypeError(`Typed.Map was passed an invalid key type descriptor: ${keyDescriptor}`)
}
if (valueType === Any && valueDescriptor !== Any) {
throw TypeError(`Typed.Map was passed an invalid value type descriptor: ${valueDescriptor}`)
}
const type = new EntryType(keyType, valueType, label)
const MapType = function(value) {
const isMapType = this instanceof MapType
const constructor = isMapType ? this.constructor : MapType
if (value instanceof constructor) {
return value
}
const result = constructor.prototype[$read](value)
if (result instanceof TypeError) {
throw result
}
if (isMapType && !this[$store]) {
this[$store] = result[$store]
this.size = result.size
} else {
return result
}
return this
}
MapType.of = (...keyValues) => {
return MapType().withMutations(map => {
for (var i = 0; i < keyValues.length; i += 2) {
if (i + 1 >= keyValues.length) {
throw new Error('Missing value for key: ' + keyValues[i]);
}
map.set(keyValues[i], keyValues[i + 1]);
}
});
}
MapType.prototype = Object.create(MapPrototype, {
constructor: {value: MapType},
[$type]: {value: type},
[$label]: {value: label}
})
return MapType
}
Map.Type = TypedMap
Map.prototype = TypedMap.prototype
const MapPrototype = Map.prototype