@valbuild/core 0.61.0 → 0.62.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/declarations/src/ValApi.d.ts +18 -15
- package/dist/declarations/src/index.d.ts +36 -42
- package/dist/declarations/src/initVal.d.ts +1 -1
- package/dist/declarations/src/module.d.ts +3 -2
- package/dist/declarations/src/source/file.d.ts +1 -0
- package/dist/declarations/src/val/index.d.ts +4 -4
- package/dist/index-316f5dd8.esm.js +4016 -0
- package/dist/index-3193efd5.cjs.dev.js +4057 -0
- package/dist/index-596f28bb.cjs.prod.js +4057 -0
- package/dist/{result-b96df128.esm.js → result-a8316efa.esm.js} +1 -1
- package/dist/valbuild-core.cjs.dev.js +29 -1844
- package/dist/valbuild-core.cjs.prod.js +29 -1844
- package/dist/valbuild-core.esm.js +2 -1823
- package/expr/dist/valbuild-core-expr.cjs.dev.js +7 -8
- package/expr/dist/valbuild-core-expr.cjs.prod.js +7 -8
- package/expr/dist/valbuild-core-expr.esm.js +2 -3
- package/fp/dist/valbuild-core-fp.esm.js +1 -1
- package/package.json +1 -1
- package/patch/dist/valbuild-core-patch.cjs.dev.js +46 -50
- package/patch/dist/valbuild-core-patch.cjs.prod.js +46 -50
- package/patch/dist/valbuild-core-patch.esm.js +6 -10
- package/dist/index-051c34f3.esm.js +0 -539
- package/dist/index-0df230ef.cjs.dev.js +0 -572
- package/dist/index-2dfaf8f7.cjs.prod.js +0 -643
- package/dist/index-e8ab2166.cjs.dev.js +0 -643
- package/dist/index-ed5767a3.esm.js +0 -639
- package/dist/index-f2552460.cjs.prod.js +0 -572
- package/dist/ops-34737ef0.cjs.dev.js +0 -1068
- package/dist/ops-92570725.cjs.prod.js +0 -1068
- package/dist/ops-9262cf01.esm.js +0 -1044
@@ -1,639 +0,0 @@
|
|
1
|
-
import { f as _objectSpread2, k as _slicedToArray, c as _createClass, a as _classCallCheck, t as Sym, C as Call, u as StringLiteral, v as StringTemplate, e as _typeof, n as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, p as convertFileSource, d as _defineProperty, E as Expr, S as Schema, w as _toConsumableArray, N as NilSym } from './index-051c34f3.esm.js';
|
2
|
-
import { i as isErr, e as err, o as ok, a as isOk } from './result-b96df128.esm.js';
|
3
|
-
|
4
|
-
var WHITE_SPACE = ["\n", "\r", "\t", " "];
|
5
|
-
function tokenize(input) {
|
6
|
-
var tokens = [];
|
7
|
-
var cursor = 0;
|
8
|
-
while (cursor < input.length) {
|
9
|
-
var _char = input[cursor];
|
10
|
-
var peek = input[cursor + 1];
|
11
|
-
// TODO: remove this not used any more
|
12
|
-
if (_char === "!" && peek === "(") {
|
13
|
-
tokens.push({
|
14
|
-
type: "!(",
|
15
|
-
span: [cursor, cursor + 1]
|
16
|
-
});
|
17
|
-
cursor += 2;
|
18
|
-
} else if (_char === "(") {
|
19
|
-
tokens.push({
|
20
|
-
type: "(",
|
21
|
-
span: [cursor, cursor]
|
22
|
-
});
|
23
|
-
cursor++;
|
24
|
-
} else if (_char === ")") {
|
25
|
-
tokens.push({
|
26
|
-
type: ")",
|
27
|
-
span: [cursor, cursor]
|
28
|
-
});
|
29
|
-
cursor++;
|
30
|
-
} else if (_char === "'" || _char === "}") {
|
31
|
-
var _start = cursor;
|
32
|
-
var value = "";
|
33
|
-
var unescapedValue = "";
|
34
|
-
var escaped = false;
|
35
|
-
if (_char === "}") {
|
36
|
-
tokens.push({
|
37
|
-
type: "}",
|
38
|
-
span: [cursor, cursor]
|
39
|
-
});
|
40
|
-
} else if (_char === "'") {
|
41
|
-
tokens.push({
|
42
|
-
type: "'",
|
43
|
-
span: [cursor, cursor]
|
44
|
-
});
|
45
|
-
}
|
46
|
-
while (cursor < input.length) {
|
47
|
-
if (_char === "\\") {
|
48
|
-
escaped = !escaped;
|
49
|
-
} else {
|
50
|
-
escaped = false;
|
51
|
-
}
|
52
|
-
if (peek === "'" && !escaped) {
|
53
|
-
cursor += 2;
|
54
|
-
break;
|
55
|
-
} else if (_char === "$" && peek === "{") {
|
56
|
-
cursor += 2;
|
57
|
-
break;
|
58
|
-
}
|
59
|
-
cursor++;
|
60
|
-
_char = input[cursor];
|
61
|
-
peek = input[cursor + 1];
|
62
|
-
if (!(_char === "$" && peek === "{") && cursor < input.length) {
|
63
|
-
if (!(_char === "\\" && !escaped // counter-intuitive, but escape just became false if this was a backslash we want to escape
|
64
|
-
)) {
|
65
|
-
value += _char;
|
66
|
-
}
|
67
|
-
unescapedValue += _char;
|
68
|
-
}
|
69
|
-
}
|
70
|
-
var cursorOffset = peek === "'" && !escaped ? 2 : _char === "$" && peek === "{" ? 3 : 1;
|
71
|
-
if (value) {
|
72
|
-
tokens.push(_objectSpread2({
|
73
|
-
type: "string",
|
74
|
-
span: [_start + 1, cursor - cursorOffset],
|
75
|
-
value: value
|
76
|
-
}, unescapedValue !== value && {
|
77
|
-
unescapedValue: unescapedValue
|
78
|
-
}));
|
79
|
-
}
|
80
|
-
if (peek === "'" && !escaped) {
|
81
|
-
tokens.push({
|
82
|
-
type: "'",
|
83
|
-
span: [cursor - 1, cursor - 1]
|
84
|
-
});
|
85
|
-
} else if (_char === "$" && peek === "{") {
|
86
|
-
tokens.push({
|
87
|
-
type: "${",
|
88
|
-
span: [cursor - cursorOffset + 1, cursor - 1]
|
89
|
-
});
|
90
|
-
}
|
91
|
-
} else if (WHITE_SPACE.includes(_char)) {
|
92
|
-
var _start2 = cursor;
|
93
|
-
while (WHITE_SPACE.includes(input[cursor]) && cursor < input.length) {
|
94
|
-
cursor++;
|
95
|
-
}
|
96
|
-
tokens.push({
|
97
|
-
type: "ws",
|
98
|
-
span: [_start2, cursor - 1]
|
99
|
-
});
|
100
|
-
} else {
|
101
|
-
var _value = "";
|
102
|
-
var _start3 = cursor;
|
103
|
-
do {
|
104
|
-
_char = input[cursor];
|
105
|
-
peek = input[cursor + 1];
|
106
|
-
_value += _char;
|
107
|
-
cursor++;
|
108
|
-
} while (!WHITE_SPACE.includes(peek) && peek !== ")" && peek !== "'" && cursor < input.length);
|
109
|
-
tokens.push({
|
110
|
-
type: "token",
|
111
|
-
span: [_start3, cursor - 1],
|
112
|
-
value: _value
|
113
|
-
});
|
114
|
-
}
|
115
|
-
}
|
116
|
-
return [tokens, cursor];
|
117
|
-
}
|
118
|
-
|
119
|
-
var ParserError = /*#__PURE__*/_createClass(function ParserError(message, span) {
|
120
|
-
_classCallCheck(this, ParserError);
|
121
|
-
this.message = message;
|
122
|
-
this.span = span;
|
123
|
-
});
|
124
|
-
function parseTokens(inputTokens) {
|
125
|
-
var tokens = inputTokens.slice();
|
126
|
-
function slurpCall(first, isAnon) {
|
127
|
-
var _tokens$, _tokens$2, _tokens$3, _tokens$7, _tokens$8, _args$slice$0$span;
|
128
|
-
// peek
|
129
|
-
if (((_tokens$ = tokens[0]) === null || _tokens$ === void 0 ? void 0 : _tokens$.type) === "ws" && ((_tokens$2 = tokens[1]) === null || _tokens$2 === void 0 ? void 0 : _tokens$2.type) === ")" || ((_tokens$3 = tokens[0]) === null || _tokens$3 === void 0 ? void 0 : _tokens$3.type) === ")") {
|
130
|
-
slurpWs();
|
131
|
-
tokens.shift();
|
132
|
-
return ok(new Sym("()", [first.span[0], first.span[1] + 1]));
|
133
|
-
}
|
134
|
-
var args = [];
|
135
|
-
var completed = false;
|
136
|
-
while (!completed) {
|
137
|
-
var _res = slurp();
|
138
|
-
if (isOk(_res)) {
|
139
|
-
var _tokens$4, _tokens$5, _tokens$6;
|
140
|
-
args.push(_res.value);
|
141
|
-
completed = ((_tokens$4 = tokens[0]) === null || _tokens$4 === void 0 ? void 0 : _tokens$4.type) !== "ws" || ((_tokens$5 = tokens[0]) === null || _tokens$5 === void 0 ? void 0 : _tokens$5.type) === "ws" && ((_tokens$6 = tokens[1]) === null || _tokens$6 === void 0 ? void 0 : _tokens$6.type) === ")";
|
142
|
-
} else {
|
143
|
-
return _res;
|
144
|
-
}
|
145
|
-
}
|
146
|
-
if (((_tokens$7 = tokens[0]) === null || _tokens$7 === void 0 ? void 0 : _tokens$7.type) === "ws" && ((_tokens$8 = tokens[1]) === null || _tokens$8 === void 0 ? void 0 : _tokens$8.type) === ")") {
|
147
|
-
tokens.shift();
|
148
|
-
}
|
149
|
-
var last = tokens.shift();
|
150
|
-
if ((last === null || last === void 0 ? void 0 : last.type) !== ")") {
|
151
|
-
return err(new ParserError("unbalanced parens: missing a ')'", [first.span[0], first.span[1] + 1]));
|
152
|
-
}
|
153
|
-
return ok(new Call(args, isAnon, [first.span[0], ((_args$slice$0$span = args.slice(-1)[0].span) === null || _args$slice$0$span === void 0 ? void 0 : _args$slice$0$span[1]) || -1]));
|
154
|
-
}
|
155
|
-
function slurpWs() {
|
156
|
-
while (((_tokens$9 = tokens[0]) === null || _tokens$9 === void 0 ? void 0 : _tokens$9.type) === "ws") {
|
157
|
-
var _tokens$9;
|
158
|
-
tokens.shift();
|
159
|
-
}
|
160
|
-
}
|
161
|
-
function slurpTemplate(first) {
|
162
|
-
var children = [];
|
163
|
-
while (tokens.length > 0) {
|
164
|
-
var _tokens$10;
|
165
|
-
if (((_tokens$10 = tokens[0]) === null || _tokens$10 === void 0 ? void 0 : _tokens$10.type) === "'") {
|
166
|
-
break;
|
167
|
-
}
|
168
|
-
var nextToken = tokens.shift();
|
169
|
-
if ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.type) === "${") {
|
170
|
-
var _res2 = slurp();
|
171
|
-
if (isOk(_res2)) {
|
172
|
-
children.push(_res2.value);
|
173
|
-
var _last = tokens.shift();
|
174
|
-
if (!_last) {
|
175
|
-
var _children$slice$0$spa;
|
176
|
-
return err(new ParserError("unbalanced string template: missing a '}'", [first.span[0], (_children$slice$0$spa = children.slice(-1)[0].span) === null || _children$slice$0$spa === void 0 ? void 0 : _children$slice$0$spa[1]]));
|
177
|
-
} else if (_last.type !== "}") {
|
178
|
-
return err(new ParserError("unbalanced string template: expected '}'", _last.span));
|
179
|
-
}
|
180
|
-
} else {
|
181
|
-
return _res2;
|
182
|
-
}
|
183
|
-
} else if ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.type) === "string") {
|
184
|
-
children.push(new StringLiteral(nextToken.unescapedValue || nextToken.value || "", nextToken.span));
|
185
|
-
}
|
186
|
-
}
|
187
|
-
var last = tokens.shift();
|
188
|
-
if (!last) {
|
189
|
-
var _children$slice$0$spa2;
|
190
|
-
return err(new ParserError("unbalanced string template: missing a '''", [first.span[0], (_children$slice$0$spa2 = children.slice(-1)[0].span) === null || _children$slice$0$spa2 === void 0 ? void 0 : _children$slice$0$spa2[1]]));
|
191
|
-
} else if (last.type !== "'") {
|
192
|
-
return err(new ParserError("unbalanced string template: expected '''", last.span));
|
193
|
-
}
|
194
|
-
return ok(new StringTemplate(children, [first.span[0], last.span[1]]));
|
195
|
-
}
|
196
|
-
function slurpString(first) {
|
197
|
-
var _tokens$11, _tokens$12, _tokens$13;
|
198
|
-
if (((_tokens$11 = tokens[0]) === null || _tokens$11 === void 0 ? void 0 : _tokens$11.type) === "string" && ((_tokens$12 = tokens[1]) === null || _tokens$12 === void 0 ? void 0 : _tokens$12.type) === "'") {
|
199
|
-
var stringToken = tokens.shift();
|
200
|
-
var last = tokens.shift();
|
201
|
-
if (!last || !stringToken) {
|
202
|
-
throw Error("Unexpected error: stringToken or last is undefined");
|
203
|
-
}
|
204
|
-
return ok(new StringLiteral(stringToken.unescapedValue || stringToken.value || "", [first.span[0], last.span[1]]));
|
205
|
-
} else if (((_tokens$13 = tokens[0]) === null || _tokens$13 === void 0 ? void 0 : _tokens$13.type) === "'") {
|
206
|
-
var _last2 = tokens.shift();
|
207
|
-
if (!_last2) {
|
208
|
-
throw Error("Unexpected error: last is undefined");
|
209
|
-
}
|
210
|
-
return ok(new StringLiteral("", [first.span[0], _last2.span[1]]));
|
211
|
-
} else {
|
212
|
-
return slurpTemplate(first);
|
213
|
-
}
|
214
|
-
}
|
215
|
-
function slurp() {
|
216
|
-
slurpWs();
|
217
|
-
var first = tokens.shift();
|
218
|
-
if (!first) {
|
219
|
-
return err(new ParserError("expected '(', '!(', string or literal", [0, 0]));
|
220
|
-
}
|
221
|
-
if (first.type === "(" || first.type === "!(") {
|
222
|
-
return slurpCall(first, first.type === "!(");
|
223
|
-
} else if (first.type === "'") {
|
224
|
-
return slurpString(first);
|
225
|
-
} else if (first.type === "token") {
|
226
|
-
var _first$value, _first$value2, _first$value3, _first$value4, _first$value5, _first$value6;
|
227
|
-
if ((_first$value = first.value) !== null && _first$value !== void 0 && _first$value.includes("(") || (_first$value2 = first.value) !== null && _first$value2 !== void 0 && _first$value2.includes(")")) {
|
228
|
-
return err(new ParserError("unexpected token: '(' and ')' are not allowed in tokens", first.span));
|
229
|
-
}
|
230
|
-
if ((_first$value3 = first.value) !== null && _first$value3 !== void 0 && _first$value3.includes("'")) {
|
231
|
-
return err(new ParserError('unexpected token: "\'" is not allowed in tokens', first.span));
|
232
|
-
}
|
233
|
-
if ((_first$value4 = first.value) !== null && _first$value4 !== void 0 && _first$value4.includes(".")) {
|
234
|
-
return err(new ParserError('unexpected token: "." is not allowed in tokens', first.span));
|
235
|
-
}
|
236
|
-
if ((_first$value5 = first.value) !== null && _first$value5 !== void 0 && _first$value5.includes("{") || (_first$value6 = first.value) !== null && _first$value6 !== void 0 && _first$value6.includes("}")) {
|
237
|
-
return err(new ParserError("unexpected token: '{' and '}' are not allowed in tokens", first.span));
|
238
|
-
}
|
239
|
-
return ok(new Sym(first.value || "", first.span));
|
240
|
-
} else {
|
241
|
-
return err(new ParserError("expected '(', '!(' or literal or token".concat(first.value || first.type ? ", got: '".concat(first.value || first.type, "'") : ""), first.span));
|
242
|
-
}
|
243
|
-
}
|
244
|
-
var res = slurp();
|
245
|
-
slurpWs();
|
246
|
-
if (isErr(res)) {
|
247
|
-
return res;
|
248
|
-
}
|
249
|
-
if (tokens.length > 0) {
|
250
|
-
return err(new ParserError("expected end of input, superfluous tokens", [tokens[0].span[0], tokens.slice(-1)[0].span[1]]));
|
251
|
-
}
|
252
|
-
return res;
|
253
|
-
}
|
254
|
-
function parse(input) {
|
255
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
256
|
-
var _tokenize = tokenize(input),
|
257
|
-
_tokenize2 = _slicedToArray(_tokenize, 2),
|
258
|
-
tokens = _tokenize2[0];
|
259
|
-
_tokenize2[1]; // TODO: we can use cursor to improve error messages / spans
|
260
|
-
return parseTokens(tokens);
|
261
|
-
}
|
262
|
-
|
263
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
264
|
-
|
265
|
-
/**
|
266
|
-
* Selectors can be used to select parts of a Val module.
|
267
|
-
* Unlike queries, joins, aggregates etc is and will not be supported.
|
268
|
-
*
|
269
|
-
* They are designed to be be used as if they were "normal" JSON data,
|
270
|
-
* though some concessions had to be made because of TypeScript limitations.
|
271
|
-
*
|
272
|
-
* Selectors works equally on source content, defined in code, and remote content.
|
273
|
-
*
|
274
|
-
* @example
|
275
|
-
* // Select the title of a document
|
276
|
-
* const titles = useVal(docsVal.map((doc) => doc.title));
|
277
|
-
*
|
278
|
-
* @example
|
279
|
-
* // Match on a union type
|
280
|
-
* const titles = useVal(docsVal.map((doc) => doc.fold("type")({
|
281
|
-
* newsletter: (newsletter) => newsletter.title,
|
282
|
-
* email: (email) => email.subject,
|
283
|
-
* }));
|
284
|
-
*
|
285
|
-
*/
|
286
|
-
|
287
|
-
/**
|
288
|
-
* @internal
|
289
|
-
*/
|
290
|
-
var GetSchema = Symbol("GetSchema");
|
291
|
-
/**
|
292
|
-
/**
|
293
|
-
* @internal
|
294
|
-
*/
|
295
|
-
var Path = Symbol("Path");
|
296
|
-
/**
|
297
|
-
* @internal
|
298
|
-
*/
|
299
|
-
var SourceOrExpr = Symbol("SourceOrExpr");
|
300
|
-
|
301
|
-
/**
|
302
|
-
* Use this type to convert types that accepts both Source and Selectors
|
303
|
-
*
|
304
|
-
* An example would be where literals are supported like in most higher order functions (e.g. map in array)
|
305
|
-
**/
|
306
|
-
|
307
|
-
function hasOwn(obj, prop) {
|
308
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
309
|
-
}
|
310
|
-
function _andThen(f, source, path) {
|
311
|
-
if (source) {
|
312
|
-
return newSelectorProxy(f(newSelectorProxy(source, path)));
|
313
|
-
}
|
314
|
-
return newSelectorProxy(source, path);
|
315
|
-
}
|
316
|
-
function isSelector(source) {
|
317
|
-
return _typeof(source) === "object" && source !== null && (SourceOrExpr in source || Path in source);
|
318
|
-
}
|
319
|
-
function newSelectorProxy(source, path, moduleSchema) {
|
320
|
-
if (_typeof(source) === "object") {
|
321
|
-
if (isSelector(source)) {
|
322
|
-
return source;
|
323
|
-
} else if (isSerializedVal(source)) {
|
324
|
-
return newSelectorProxy(source.val, source.valPath);
|
325
|
-
}
|
326
|
-
}
|
327
|
-
if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
|
328
|
-
var fileRef = source[FILE_REF_PROP];
|
329
|
-
if (typeof fileRef !== "string") {
|
330
|
-
throw Error("Invalid file ref: " + fileRef);
|
331
|
-
}
|
332
|
-
return newSelectorProxy(convertFileSource(source), path, moduleSchema);
|
333
|
-
}
|
334
|
-
switch (_typeof(source)) {
|
335
|
-
case "function":
|
336
|
-
case "symbol":
|
337
|
-
throw Error("Invalid selector type: ".concat(_typeof(source), ": ").concat(source));
|
338
|
-
case "object":
|
339
|
-
// Handles both objects and arrays!
|
340
|
-
if (source !== null) {
|
341
|
-
return new Proxy(source, {
|
342
|
-
// TODO: see proxy docs if we want more traps
|
343
|
-
has: function has(target, prop) {
|
344
|
-
if (prop === SourceOrExpr) {
|
345
|
-
return true;
|
346
|
-
}
|
347
|
-
if (prop === Path) {
|
348
|
-
return true;
|
349
|
-
}
|
350
|
-
if (prop === "andThen") {
|
351
|
-
return true;
|
352
|
-
}
|
353
|
-
if (prop === GetSchema) {
|
354
|
-
return true;
|
355
|
-
}
|
356
|
-
return prop in target;
|
357
|
-
},
|
358
|
-
get: function get(target, prop) {
|
359
|
-
if (prop === SourceOrExpr) {
|
360
|
-
return source;
|
361
|
-
}
|
362
|
-
if (prop === Path) {
|
363
|
-
return path;
|
364
|
-
}
|
365
|
-
if (prop === GetSchema) {
|
366
|
-
return moduleSchema;
|
367
|
-
}
|
368
|
-
if (prop === "andThen") {
|
369
|
-
return function (f) {
|
370
|
-
return _andThen(f, source, path);
|
371
|
-
};
|
372
|
-
}
|
373
|
-
if (Array.isArray(target)) {
|
374
|
-
if (prop === "filter") {
|
375
|
-
return function (f) {
|
376
|
-
var filtered = target.map(function (a, i) {
|
377
|
-
return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
378
|
-
}).filter(function (a) {
|
379
|
-
if (f && f instanceof Schema) {
|
380
|
-
return f.assert(unValify(a));
|
381
|
-
} else {
|
382
|
-
return unValify(f(a));
|
383
|
-
}
|
384
|
-
});
|
385
|
-
return newSelectorProxy(filtered, path, moduleSchema);
|
386
|
-
};
|
387
|
-
} else if (prop === "map") {
|
388
|
-
return function (f) {
|
389
|
-
var filtered = target.map(function (a, i) {
|
390
|
-
var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
|
391
|
-
if (isSelector(valueOrSelector)) {
|
392
|
-
return valueOrSelector;
|
393
|
-
}
|
394
|
-
return newSelectorProxy(valueOrSelector);
|
395
|
-
});
|
396
|
-
return newSelectorProxy(filtered, path, moduleSchema);
|
397
|
-
};
|
398
|
-
}
|
399
|
-
}
|
400
|
-
if (Array.isArray(target) && prop === "length") {
|
401
|
-
return newSelectorProxy(target.length);
|
402
|
-
}
|
403
|
-
var reflectedValue = Reflect.get(target, prop);
|
404
|
-
if (hasOwn(source, prop)) {
|
405
|
-
if (!Number.isNaN(Number(prop))) {
|
406
|
-
return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
407
|
-
}
|
408
|
-
return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
|
409
|
-
}
|
410
|
-
return reflectedValue;
|
411
|
-
}
|
412
|
-
});
|
413
|
-
}
|
414
|
-
// intentional fallthrough
|
415
|
-
// eslint-disable-next-line no-fallthrough
|
416
|
-
default:
|
417
|
-
return _defineProperty(_defineProperty(_defineProperty({
|
418
|
-
eq: function eq(other) {
|
419
|
-
var otherValue = other;
|
420
|
-
if (isSelector(other)) {
|
421
|
-
otherValue = other[SourceOrExpr];
|
422
|
-
if (otherValue instanceof Expr) {
|
423
|
-
throw Error("TODO: Cannot evaluate equality with an Expr");
|
424
|
-
}
|
425
|
-
}
|
426
|
-
return newSelectorProxy(source === otherValue, undefined);
|
427
|
-
},
|
428
|
-
andThen: function andThen(f) {
|
429
|
-
return _andThen(f, source === undefined ? null : source, path);
|
430
|
-
}
|
431
|
-
}, SourceOrExpr, source === undefined ? null : source), Path, path), GetSchema, moduleSchema);
|
432
|
-
}
|
433
|
-
}
|
434
|
-
function createValPathOfItem(arrayPath, prop) {
|
435
|
-
if (_typeof(prop) === "symbol") {
|
436
|
-
throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
|
437
|
-
}
|
438
|
-
return arrayPath && "".concat(arrayPath, ".").concat(JSON.stringify(prop));
|
439
|
-
}
|
440
|
-
|
441
|
-
// TODO: could we do .val on the objects instead?
|
442
|
-
function unValify(valueOrSelector) {
|
443
|
-
if (_typeof(valueOrSelector) === "object" && (SourceOrExpr in valueOrSelector || Path in valueOrSelector)) {
|
444
|
-
var selectorValue = valueOrSelector[SourceOrExpr];
|
445
|
-
return selectorValue;
|
446
|
-
}
|
447
|
-
return valueOrSelector;
|
448
|
-
}
|
449
|
-
|
450
|
-
var EvalError = /*#__PURE__*/function () {
|
451
|
-
function EvalError(message, expr) {
|
452
|
-
_classCallCheck(this, EvalError);
|
453
|
-
this.message = message;
|
454
|
-
this.expr = expr;
|
455
|
-
}
|
456
|
-
_createClass(EvalError, [{
|
457
|
-
key: "toString",
|
458
|
-
value: function toString() {
|
459
|
-
return "".concat(this.message, " in: ").concat(this.expr.transpile());
|
460
|
-
}
|
461
|
-
}]);
|
462
|
-
return EvalError;
|
463
|
-
}();
|
464
|
-
var MAX_STACK_SIZE = 100; // an arbitrary semi-large number
|
465
|
-
function evaluateSync(expr, getSource, stack) {
|
466
|
-
// TODO: amount of evaluates should be limited?
|
467
|
-
if (stack.length > MAX_STACK_SIZE) {
|
468
|
-
throw new EvalError("Stack overflow. Final frames: ".concat(stack.slice(-10).map(function (frame, i) {
|
469
|
-
return frame.map(function (s, j) {
|
470
|
-
return "@[".concat(i, ",").concat(j, "]: ").concat(JSON.stringify(s));
|
471
|
-
}).join(", ");
|
472
|
-
}).join(" -> ")), expr);
|
473
|
-
}
|
474
|
-
if (expr instanceof Call) {
|
475
|
-
if (expr.children[0] instanceof Sym) {
|
476
|
-
if (expr.children[0].value === "val") {
|
477
|
-
if (expr.isAnon) {
|
478
|
-
throw new EvalError("cannot call 'val' as anonymous function", expr);
|
479
|
-
}
|
480
|
-
if (expr.children[1] instanceof StringLiteral) {
|
481
|
-
var _path = expr.children[1].value;
|
482
|
-
return newSelectorProxy(getSource(_path), _path);
|
483
|
-
} else {
|
484
|
-
throw new EvalError("argument of 'val' must be a string literal", expr);
|
485
|
-
}
|
486
|
-
} else if (expr.children[0].value === "json") {
|
487
|
-
if (expr.children.length !== 2) {
|
488
|
-
throw new EvalError("must call 'json' with exactly one argument", expr);
|
489
|
-
}
|
490
|
-
var value = evaluateSync(expr.children[1], getSource, stack);
|
491
|
-
var valObj = value[SourceOrExpr];
|
492
|
-
var valPath = value[Path];
|
493
|
-
if (typeof valObj !== "string") {
|
494
|
-
throw new EvalError("cannot parse JSON: ".concat(JSON.stringify(valObj), ", expected string"), expr.children[1]);
|
495
|
-
}
|
496
|
-
try {
|
497
|
-
var serialized = JSON.parse(valObj);
|
498
|
-
if (isSerializedVal(serialized)) {
|
499
|
-
return newSelectorProxy(serialized.val, serialized.valPath);
|
500
|
-
}
|
501
|
-
var parsedValue = newSelectorProxy(JSON.parse(valObj), valPath);
|
502
|
-
return parsedValue;
|
503
|
-
} catch (e) {
|
504
|
-
if (e instanceof SyntaxError) {
|
505
|
-
throw new EvalError("cannot parse JSON: ".concat(valObj, ", ").concat(e.message, " - value: ").concat(JSON.stringify(value)), expr.children[1]);
|
506
|
-
}
|
507
|
-
throw e;
|
508
|
-
}
|
509
|
-
} else if (expr.children[0].value === "stringify") {
|
510
|
-
// TODO: remove stringify
|
511
|
-
if (expr.children.length !== 2) {
|
512
|
-
throw new EvalError("must call 'stringify' with exactly one argument", expr);
|
513
|
-
}
|
514
|
-
var res = evaluateSync(expr.children[1], getSource, stack);
|
515
|
-
return newSelectorProxy(JSON.stringify(res[SourceOrExpr]));
|
516
|
-
}
|
517
|
-
}
|
518
|
-
var prop = evaluateSync(expr.children[0], getSource, stack)[SourceOrExpr];
|
519
|
-
if (expr.children.length === 1) {
|
520
|
-
// TODO: return if literal only?
|
521
|
-
return newSelectorProxy(prop);
|
522
|
-
}
|
523
|
-
var obj = evaluateSync(expr.children[1], getSource, stack);
|
524
|
-
if (typeof prop !== "string" && typeof prop !== "number") {
|
525
|
-
throw new EvalError("cannot access ".concat(JSON.stringify(obj), " with property ").concat(JSON.stringify(prop), ": is not a string or number"), expr);
|
526
|
-
}
|
527
|
-
if (prop in obj) {
|
528
|
-
if (expr.isAnon) {
|
529
|
-
// anon functions:
|
530
|
-
var maybeFunction = obj[prop];
|
531
|
-
if (typeof maybeFunction !== "function") {
|
532
|
-
throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required higher ordered function got ").concat(_typeof(obj[prop])), expr);
|
533
|
-
}
|
534
|
-
if (expr.children[0] instanceof Sym) {
|
535
|
-
return maybeFunction(function () {
|
536
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
537
|
-
args[_key] = arguments[_key];
|
538
|
-
}
|
539
|
-
return evaluateSync(expr.children[2], getSource, stack.concat([args]));
|
540
|
-
});
|
541
|
-
} else {
|
542
|
-
throw new EvalError("cannot call an expression that is not a symbol, got: '".concat(expr.children[0].type, "'"), expr);
|
543
|
-
}
|
544
|
-
} else {
|
545
|
-
// non-anon functions:
|
546
|
-
if (expr.children[0] instanceof Sym) {
|
547
|
-
if (expr.children[0].value === "val") {
|
548
|
-
if (expr.children[1] instanceof StringLiteral) {
|
549
|
-
var _path2 = expr.children[1].value;
|
550
|
-
return newSelectorProxy(getSource(_path2), _path2);
|
551
|
-
} else {
|
552
|
-
throw new EvalError("argument of 'val' must be a string literal", expr);
|
553
|
-
}
|
554
|
-
}
|
555
|
-
}
|
556
|
-
var args = expr.children.slice(2);
|
557
|
-
if (args.length > 0) {
|
558
|
-
var _maybeFunction = obj[prop];
|
559
|
-
if (typeof _maybeFunction !== "function") {
|
560
|
-
throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required function got ").concat(_typeof(obj[prop])), expr);
|
561
|
-
}
|
562
|
-
return _maybeFunction.apply(void 0, _toConsumableArray(args.map(function (arg) {
|
563
|
-
return evaluateSync(arg, getSource, stack);
|
564
|
-
})));
|
565
|
-
}
|
566
|
-
var maybeValue = obj[prop];
|
567
|
-
if (typeof maybeValue === "function") {
|
568
|
-
throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required value got ").concat(_typeof(obj[prop])), expr);
|
569
|
-
}
|
570
|
-
return maybeValue;
|
571
|
-
}
|
572
|
-
}
|
573
|
-
} else if (expr instanceof Sym) {
|
574
|
-
if (expr.value.startsWith("@")) {
|
575
|
-
var _stack$Number;
|
576
|
-
var _expr$value$slice$spl = expr.value.slice(2, -1).split(","),
|
577
|
-
_expr$value$slice$spl2 = _slicedToArray(_expr$value$slice$spl, 3),
|
578
|
-
i = _expr$value$slice$spl2[0],
|
579
|
-
j = _expr$value$slice$spl2[1],
|
580
|
-
rest = _expr$value$slice$spl2[2];
|
581
|
-
if (rest) {
|
582
|
-
throw new EvalError("cannot access stack: too many indices", expr);
|
583
|
-
}
|
584
|
-
var stackValue = (_stack$Number = stack[Number(i)]) === null || _stack$Number === void 0 ? void 0 : _stack$Number[Number(j)];
|
585
|
-
if (stackValue === undefined) {
|
586
|
-
throw new EvalError("cannot access stack: out of bounds", expr);
|
587
|
-
}
|
588
|
-
return stackValue;
|
589
|
-
} else if (expr.value === "()") {
|
590
|
-
return newSelectorProxy(null);
|
591
|
-
}
|
592
|
-
return newSelectorProxy(expr.value);
|
593
|
-
} else if (expr instanceof StringLiteral) {
|
594
|
-
return newSelectorProxy(expr.value);
|
595
|
-
} else if (expr instanceof StringTemplate) {
|
596
|
-
return newSelectorProxy(expr.children.map(function (child) {
|
597
|
-
if (child instanceof Sym && child.value === "()") {
|
598
|
-
return "null";
|
599
|
-
}
|
600
|
-
var evalRes = evaluateSync(child, getSource, stack);
|
601
|
-
if (child.type === "StringLiteral" || child.type === "StringTemplate") {
|
602
|
-
return evalRes[SourceOrExpr];
|
603
|
-
}
|
604
|
-
if (Path in evalRes) {
|
605
|
-
// a selector, so serialize to Val
|
606
|
-
return JSON.stringify({
|
607
|
-
val: evalRes[SourceOrExpr],
|
608
|
-
valPath: evalRes[Path]
|
609
|
-
});
|
610
|
-
}
|
611
|
-
return JSON.stringify(evalRes[SourceOrExpr]);
|
612
|
-
}).join(""));
|
613
|
-
}
|
614
|
-
throw new EvalError("could not evaluate", expr);
|
615
|
-
}
|
616
|
-
function evaluate(expr, source, stack) {
|
617
|
-
try {
|
618
|
-
return ok(evaluateSync(expr, source, stack));
|
619
|
-
} catch (err$1) {
|
620
|
-
if (err$1 instanceof EvalError) {
|
621
|
-
return err(err$1);
|
622
|
-
}
|
623
|
-
throw err$1;
|
624
|
-
}
|
625
|
-
}
|
626
|
-
|
627
|
-
var index = /*#__PURE__*/Object.freeze({
|
628
|
-
__proto__: null,
|
629
|
-
parse: parse,
|
630
|
-
Call: Call,
|
631
|
-
Expr: Expr,
|
632
|
-
NilSym: NilSym,
|
633
|
-
StringLiteral: StringLiteral,
|
634
|
-
StringTemplate: StringTemplate,
|
635
|
-
Sym: Sym,
|
636
|
-
evaluate: evaluate
|
637
|
-
});
|
638
|
-
|
639
|
-
export { evaluate as e, index as i, parse as p };
|