-
Notifications
You must be signed in to change notification settings - Fork 14
/
record.js
291 lines (234 loc) · 7.49 KB
/
record.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
import {Typed, typeOf, construct} from "./typed"
import {Seq, Iterable, Map} from 'immutable'
const {Keyed} = Iterable
const Getter = key => function() {
return this.get(key)
}
const Setter = key => function(value) {
if (!this.__ownerID) {
throw TypeError("Cannot set on an immutable record.")
}
this.set(key, value)
}
const $store = Typed.store
const $type = Typed.type
const $step = Typed.step
const $init = Typed.init
const $result = Typed.result
const $read = Typed.read
const $label = Typed.label
const $empty = Typed.empty
const $typeName = Typed.typeName
const $typeSignature = Typed.typeSignature
const IterableKeyedBase = function() {}
IterableKeyedBase.prototype = Iterable.Keyed.prototype
class TypedRecord extends IterableKeyedBase {
constructor() {
super()
}
[Typed.init]() {
return construct(this).asMutable()
}
[Typed.result](result) {
return result.asImmutable()
}
[Typed.read](structure) {
const Type = this.constructor
if (structure && structure instanceof Type) {
return structure
}
if (structure === null || (structure && typeof(structure) !== "object")) {
return TypeError(`Invalid data structure "${structure}" was passed to ${this[$typeName]()}`)
}
const seq = Seq(structure)
const type = this[$type]
const isEmpty = seq.size === 0
if (isEmpty && this[$empty]) {
return this[$empty]
}
let record
for (let key in type) {
const fieldType = type[key]
const value = seq.has(key) ? seq.get(key) : this.get(key)
const result = fieldType[$read](value)
if (result instanceof TypeError) {
return TypeError(`Invalid value for "${key}" field:\n ${result.message}`)
}
record = this[$step](record || this[$init](), [key, result])
}
record = this[$result](record)
if (isEmpty) {
this[$empty] = record
}
return record
}
[Typed.step](result, [key, value]) {
const store = result[$store] ? result[$store].set(key, value) :
new Map([[key, value]])
if (result[$store] === store) {
return result;
}
const record = result.__ownerID ? result : construct(result)
record[$store] = store
return record
}
[Typed.typeSignature]() {
const type = this[$type]
const body = []
for (let key in type) {
body.push(`${key}: ${type[key][$typeName]()}`)
}
return `Typed.Record({${body.join(', ')}})`
}
[Typed.typeName]() {
return this[$label] || this[$typeSignature]()
}
toString() {
return this.__toString(this[$typeName]() + '({', '})')
}
has(key) {
return !!this[$type][key]
}
get(key, defaultValue) {
return !this[$type][key] ? defaultValue :
!this[$store] ? defaultValue :
this[$store].get(key, defaultValue);
}
clear() {
if (this.__ownerID) {
this[$store] && this[$store].clear()
return this
}
return this[$empty] ||
(this[$empty] = new this.constructor())
}
remove(key) {
return this[$type][key] ? this.set(key, void(0)) : this
}
set(key, value) {
const fieldType = this[$type][key]
if (!fieldType) {
throw TypeError(`Cannot set unknown field "${key}" on "${this[$typeName]()}"`)
}
const result = fieldType[$read](value)
if (result instanceof TypeError) {
throw TypeError(`Invalid value for ${key} field: ${result.message}`)
}
return this[$step](this, [key, result])
}
__iterator(type, reverse) {
return Keyed(this[$type]).map((_, key) => this.get(key)).__iterator(type, reverse);
}
__iterate(f, reverse) {
return Keyed(this[$type]).map((_, key) => this.get(key)).__iterate(f, reverse);
}
__ensureOwner(ownerID) {
if (ownerID === this.__ownerID) {
return this
}
const store = this[$store] && this[$store].__ensureOwner(ownerID)
const result = !ownerID ? this : construct(this)
result.__ownerID = ownerID
result[$store] = store
return result
}
wasAltered() {
return this[$store].wasAltered()
}
}
export const Record = function(descriptor, label) {
if (descriptor && typeof(descriptor) === "object") {
const type = Object.create(null)
const keys = Object.keys(descriptor)
const size = keys.length
if (size > 0) {
const properties = {
size: {value: size},
[$type]: {value: type},
[$label]: {value: label}
}
let index = 0
while (index < size) {
const key = keys[index]
const fieldType = typeOf(descriptor[key])
if (fieldType) {
type[key] = fieldType
properties[key] = {get:Getter(key), set:Setter(key), enumerable: true}
} else {
throw TypeError(`Invalid field descriptor provided for a "${key}" field`)
}
index = index + 1
}
const RecordType = function(structure) {
const isNew = this instanceof RecordType
const constructor = isNew ? this.constructor : RecordType
if (structure instanceof constructor) {
return structure
}
const type = constructor.prototype
const result = type[$read](structure)
if (result instanceof TypeError) {
throw result
}
if (isNew) {
this[$store] = result[$store]
} else {
return result
}
}
properties.constructor = {value: RecordType}
RecordType.prototype = Object.create(RecordPrototype, properties)
const prototype = RecordType.prototype
return RecordType
} else {
throw TypeError(`Typed.Record descriptor must define at least one field`)
}
} else {
throw TypeError(`Typed.Record must be passed a descriptor of fields`)
}
}
Record.Type = TypedRecord
Record.prototype = TypedRecord.prototype
const RecordPrototype = TypedRecord.prototype
RecordPrototype[Typed.DELETE] = RecordPrototype.remove
// Large part of the Record API is implemented by Immutabel.Map
// and is just copied over.
RecordPrototype.deleteIn = Map.prototype.deleteIn
RecordPrototype.removeIn = Map.prototype.removeIn
RecordPrototype.merge = Map.prototype.merge
RecordPrototype.mergeWith = Map.prototype.mergeWith
RecordPrototype.mergeIn = Map.prototype.mergeIn
RecordPrototype.mergeDeep = Map.prototype.mergeDeep
RecordPrototype.mergeDeepWith = Map.prototype.mergeDeepWith
RecordPrototype.mergeDeepIn = Map.prototype.mergeDeepIn
RecordPrototype.setIn = Map.prototype.setIn
RecordPrototype.update = Map.prototype.update
RecordPrototype.updateIn = Map.prototype.updateIn
RecordPrototype.withMutations = Map.prototype.withMutations
RecordPrototype.asMutable = Map.prototype.asMutable
RecordPrototype.asImmutable = Map.prototype.asImmutable
// Large chuck of API inherited from Iterable does not makes
// much sense in the context of records so we undefine it.
RecordPrototype.map = void(0)
RecordPrototype.filter = void(0)
RecordPrototype.filterNot = void(0)
RecordPrototype.flip = void(0)
RecordPrototype.mapKeys = void(0)
RecordPrototype.mapEntries = void(0)
RecordPrototype.sort = void(0)
RecordPrototype.sortBy = void(0)
RecordPrototype.reverse = void(0)
RecordPrototype.slice = void(0)
RecordPrototype.butLast = void(0)
RecordPrototype.flatMap = void(0)
RecordPrototype.flatten = void(0)
RecordPrototype.rest = void(0)
RecordPrototype.skip = void(0)
RecordPrototype.skipLast = void(0)
RecordPrototype.skipWhile = void(0)
RecordPrototype.skipUntil = void(0)
RecordPrototype.sortBy = void(0)
RecordPrototype.take = void(0)
RecordPrototype.takeLast = void(0)
RecordPrototype.takeWhile = void(0)
RecordPrototype.takeUntil = void(0)