/
Field.ts
324 lines (283 loc) · 8.4 KB
/
Field.ts
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {AnyItem, Item} from "./Item";
import {AnyValue, Value} from "./Value";
import {Record} from "./Record";
import {AnyText} from "./Text";
import {AnyNum} from "./Num";
export type AnyField = Field
| {readonly $key: AnyValue, readonly $value: AnyValue}
| {[key: string]: AnyValue};
export abstract class Field extends Item {
/** @hidden */
constructor() {
super();
}
/**
* Always returns `true` because a `Field` can never be [[Absent]].
*/
isDefined(): boolean {
return true;
}
/**
* Always returns `true` because a `Field` can be neither [[Extant]] nor
* [[Absent]].
*/
isDistinct(): boolean {
return true;
}
/**
* Returns the key component of this `Field`.
*/
abstract get key(): Value;
/**
* Returns the value component of this `Field`.
*/
abstract get value(): Value;
/**
* Sets the value of this `Field` to the new `value`, returning the old value.
*
* @throws `Error` if this `Field` is immutable.
*/
abstract setValue(value: AnyValue): Value;
/**
* Returns a copy of this `Field` with the updated `value`.
*/
abstract updatedValue(value: AnyValue): Field;
/**
* Returns the value component of this `Field`.
*/
toValue(): Value {
return this.value;
}
/**
* Always returns `undefined` because a `Field` can't be a `Record`, so it
* can't have a first member `Attr` whose key string could be returned.
*/
tag(): string | undefined {
return void 0;
}
/**
* Always returns the value component of this `Field`.
*/
target(): Value {
return this.value;
}
/**
* Always returns [[Absent]] because a `Field` can't be flattened into a
* `Value`.
*/
flattened(): Value {
return Item.Value.absent();
}
/**
* Returns a `Record` containing just this `Field`.
*/
unflattened(): Record {
return Item.Record.of(this);
}
/**
* Always returns [[Absent]] because a `Field` can't be a `Record`, so it
* can't have a head `Attr` whose value could be returned if its key were
* equal to the `tag`.
*/
header(tag: string): Value {
return Item.Value.absent();
}
/**
* Always returns `undefined` because a `Field` can't be a `Record`, so it
* can't have a head `Attr` whose value could be returned as a `Record` if
* its key were equal to the `tag`.
*/
headers(tag: string): Record | undefined {
return void 0;
}
/**
* Always returns [[Absent]] because a `Field` can't be a `Record`, so it
* can't have a first member.
*/
head(): Item {
return Item.absent();
}
/**
* Always returns an empty `Record` because a `Field` can't itself be a
* `Record`, so it can't have any non-first members.
*/
tail(): Record {
return Item.Record.empty();
}
/**
* Always returns [[Absent]] because a `Field` can't be a `Record`, so it
* can't have any non-first members to flatten, and because a `Field` isn't
* a distinct `Value`, so it can't return `Extant`.
*/
body(): Value {
return Item.Value.absent();
}
/**
* Always returns `0` because a `Field` can't be a `Record`, so it can't
* contain any members.
*/
get length(): number {
return 0;
}
/**
* Always returns `false` because a `Field` can't be a `Record`, so it can't
* have a `Field` member whose key is equal to the given `key`.
*/
has(key: AnyValue): boolean {
return false;
}
/**
* Always returns [[Absent]] because a `Field` can't be a `Record`, so it
* can't have a `Field` member whose key is equal to the given `key`.
*/
get(key: AnyValue): Value {
return Item.Value.absent();
}
/**
* Always returns [[Absent]] because a `Field` can't be a `Record`, so it
* can't have an `Attr` member whose key is equal to the given `key`.
*/
getAttr(key: AnyText): Value {
return Item.Value.absent();
}
/**
* Always returns [[Absent]] because a `Field` can't be a `Record`, so it
* can't have a `Slot` member whose key is equal to the given `key`.
*/
getSlot(key: AnyValue): Value {
return Item.Value.absent();
}
/**
* Always returns `undefined` because a `Field` can't be a `Record`, so it
* can't have a `Field` member whose key is equal to the given `key`.
*/
getField(key: AnyValue): Field | undefined {
return void 0;
}
/**
* Always returns [[Absent]] because a `Field` can't be a `Record`, so it
* can't have a member at the given `index`.
*/
getItem(index: AnyNum): Item {
return Item.absent();
}
deleted(key: AnyValue): Field {
return this;
}
conditional(thenTerm: Field, elseTerm: Field): Field;
conditional(thenTerm: AnyItem, elseTerm: AnyItem): Item;
conditional(thenTerm: AnyItem, elseTerm: AnyItem): Item {
thenTerm = Item.fromAny(thenTerm);
return thenTerm;
}
or(that: Field): Field;
or(that: AnyItem): Item;
or(that: AnyItem): Item {
return this;
}
and(that: Field): Field;
and(that: AnyItem): Item;
and(that: AnyItem): Item {
that = Item.fromAny(that);
return that;
}
lambda(template: Value): Value {
return Item.Value.absent();
}
/**
* Converts the value of this `Field` into a `string` value, if possible.
*
* @throws `Error` if the value of this `Field` can't be converted into a
* `string` value.
*/
stringValue(): string | undefined;
/**
* Converts the value of this `Field` into a `string` value, if possible;
* otherwise returns `orElse` if the value of this `Field` can't be converted
* into a `string` value.
*/
stringValue<T>(orElse: T): string | T;
stringValue<T>(orElse?: T): string | T | undefined {
return this.value.stringValue(orElse);
}
/**
* Converts the value of this `Field` into a `number` value, if possible.
*
* @throws `Error` if the value of this `Field` can't be converted into a
* `number` value.
*/
numberValue(): number | undefined;
/**
* Converts the value of this `Field` into a `number` value, if possible;
* otherwise returns `orElse` if the value of this `Field` can't be converted
* into a `number` value.
*/
numberValue<T>(orElse: T): number | T;
numberValue<T>(orElse?: T): number | T | undefined {
return this.value.numberValue(orElse);
}
/**
* Converts the value of this `Field` into a `boolean` value, if possible.
*
* @throws `Error` if the value of this `Field` can't be converted into a
* `boolean` value.
*/
booleanValue(): boolean | undefined;
/**
* Converts the value of this `Field` into a `boolean` value, if possible;
* otherwise returns `orElse` if the value of this `Field` can't be converted
* into a `boolean` value.
*/
booleanValue<T>(orElse: T): boolean | T;
booleanValue<T>(orElse?: T): boolean | T | undefined {
return this.value.booleanValue(orElse);
}
abstract toAny(): AnyField;
abstract branch(): Field;
abstract clone(): Field;
abstract commit(): this;
/** @hidden */
static readonly IMMUTABLE: number = 1 << 0;
static of(key: AnyValue, value?: AnyValue): Field {
let name;
if (typeof key === "string") {
name = key;
} else if (key instanceof Item.Text) {
name = key.value;
}
if (name !== void 0 && name.charCodeAt(0) === 64/*'@'*/) {
arguments[0] = name.slice(1);
return Item.Attr.of.apply(undefined, arguments);
} else {
return Item.Slot.of.apply(undefined, arguments);
}
}
static fromAny(field: AnyField): Field {
if (field instanceof Field) {
return field;
} else if (field && typeof field === "object") {
if ((field as any).$key !== void 0) {
return Field.of((field as any).$key, (field as any).$value);
} else {
for (const key in field) {
return Field.of(key, (field as any)[key]);
}
}
}
throw new TypeError("" + field);
}
}
Item.Field = Field;