@valbuild/core 0.16.0 → 0.17.0
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/ROADMAP.md +106 -0
- package/dist/declarations/src/ValApi.d.ts +26 -0
- package/dist/declarations/src/expr/eval.d.ts +1 -1
- package/dist/declarations/src/future/fetchVal.d.ts +5 -0
- package/dist/declarations/src/index.d.ts +36 -9
- package/dist/declarations/src/initSchema.d.ts +2 -10
- package/dist/declarations/src/initVal.d.ts +3 -21
- package/dist/declarations/src/module.d.ts +2 -14
- package/dist/declarations/src/schema/image.d.ts +1 -1
- package/dist/declarations/src/schema/index.d.ts +3 -5
- package/dist/declarations/src/schema/keyOf.d.ts +23 -0
- package/dist/declarations/src/schema/record.d.ts +19 -0
- package/dist/declarations/src/schema/union.d.ts +1 -0
- package/dist/declarations/src/selector/SelectorProxy.d.ts +7 -0
- package/dist/declarations/src/selector/array.d.ts +2 -13
- package/dist/declarations/src/selector/file.d.ts +1 -4
- package/dist/declarations/src/selector/future/array.d.ts +17 -0
- package/dist/declarations/src/selector/future/boolean.d.ts +2 -0
- package/dist/declarations/src/selector/future/file.d.ts +9 -0
- package/dist/declarations/src/selector/{i18n.d.ts → future/i18n.d.ts} +2 -2
- package/dist/declarations/src/selector/future/index.d.ts +81 -0
- package/dist/declarations/src/selector/future/number.d.ts +2 -0
- package/dist/declarations/src/selector/future/object.d.ts +10 -0
- package/dist/declarations/src/selector/future/primitive.d.ts +9 -0
- package/dist/declarations/src/selector/{remote.d.ts → future/remote.d.ts} +1 -1
- package/dist/declarations/src/selector/future/string.d.ts +2 -0
- package/dist/declarations/src/selector/index.d.ts +5 -32
- package/dist/declarations/src/selector/object.d.ts +2 -9
- package/dist/declarations/src/selector/primitive.d.ts +3 -9
- package/dist/declarations/src/source/{i18n.d.ts → future/i18n.d.ts} +2 -2
- package/dist/declarations/src/source/{remote.d.ts → future/remote.d.ts} +3 -3
- package/dist/declarations/src/source/index.d.ts +4 -3
- package/dist/declarations/src/val/index.d.ts +2 -2
- package/dist/{SelectorProxy-63c2d0e2.esm.js → index-369caccf.esm.js} +23 -195
- package/dist/{index-af761363.esm.js → index-3e3e839e.esm.js} +189 -1
- package/dist/{index-2fff5ca8.cjs.dev.js → index-486c7fbf.cjs.dev.js} +242 -54
- package/dist/{index-cac9ecbd.cjs.prod.js → index-601a7d73.cjs.prod.js} +242 -54
- package/dist/{SelectorProxy-2af1b2b8.cjs.prod.js → index-8706c87e.cjs.prod.js} +25 -199
- package/dist/{SelectorProxy-873782a5.cjs.dev.js → index-a6e642dd.cjs.dev.js} +25 -199
- package/dist/{ops-1b6e0e35.cjs.prod.js → ops-0d09f8ee.cjs.prod.js} +258 -126
- package/dist/{ops-74661336.esm.js → ops-23a5abb2.esm.js} +195 -65
- package/dist/{ops-ea4827fc.cjs.dev.js → ops-f3015423.cjs.dev.js} +258 -126
- package/dist/valbuild-core.cjs.dev.js +585 -198
- package/dist/valbuild-core.cjs.prod.js +585 -198
- package/dist/valbuild-core.esm.js +520 -134
- package/expr/dist/valbuild-core-expr.cjs.dev.js +8 -8
- package/expr/dist/valbuild-core-expr.cjs.prod.js +8 -8
- package/expr/dist/valbuild-core-expr.esm.js +2 -2
- package/package.json +2 -3
- package/patch/dist/valbuild-core-patch.cjs.dev.js +24 -24
- package/patch/dist/valbuild-core-patch.cjs.prod.js +24 -24
- package/patch/dist/valbuild-core-patch.esm.js +3 -3
- package/src/ValApi.ts +85 -0
- package/src/expr/eval.test.ts +2 -2
- package/src/expr/eval.ts +2 -2
- package/src/{fetchVal.test.ts → future/fetchVal.test.ts} +57 -57
- package/src/{fetchVal.ts → future/fetchVal.ts} +17 -22
- package/src/index.ts +46 -14
- package/src/initSchema.ts +11 -12
- package/src/initVal.ts +42 -52
- package/src/module.test.ts +40 -40
- package/src/module.ts +53 -43
- package/src/patch/deref.test.ts +1 -1
- package/src/patch/deref.ts +1 -1
- package/src/patch/json.test.ts +0 -1
- package/src/schema/{i18n.ts → future/i18n.ts} +12 -10
- package/src/schema/{oneOf.ts → future/oneOf.ts} +19 -17
- package/src/schema/image.ts +1 -1
- package/src/schema/index.ts +16 -13
- package/src/schema/keyOf.ts +167 -0
- package/src/schema/record.ts +103 -0
- package/src/schema/union.ts +1 -1
- package/src/schema/validation.test.ts +195 -130
- package/src/selector/SelectorProxy.ts +15 -15
- package/src/selector/array.ts +2 -26
- package/src/selector/file.ts +1 -9
- package/src/selector/{ExprProxy.test.ts → future/ExprProxy.test.ts} +2 -2
- package/src/selector/{ExprProxy.ts → future/ExprProxy.ts} +9 -2
- package/src/selector/{SelectorProxy.test.ts → future/SelectorProxy.test.ts} +4 -4
- package/src/selector/future/SelectorProxy.ts +238 -0
- package/src/selector/future/array.ts +37 -0
- package/src/selector/future/boolean.ts +4 -0
- package/src/selector/future/file.ts +14 -0
- package/src/selector/{i18n.ts → future/i18n.ts} +2 -2
- package/src/selector/future/index.ts +165 -0
- package/src/selector/future/number.ts +4 -0
- package/src/selector/future/object.ts +22 -0
- package/src/selector/future/primitive.ts +17 -0
- package/src/selector/{remote.ts → future/remote.ts} +1 -1
- package/src/selector/{selector.test.ts → future/selector.test.ts} +8 -28
- package/src/selector/{selectorOf.ts → future/selectorOf.ts} +1 -1
- package/src/selector/future/string.ts +4 -0
- package/src/selector/index.ts +4 -46
- package/src/selector/object.ts +2 -19
- package/src/selector/primitive.ts +3 -16
- package/src/source/{i18n.ts → future/i18n.ts} +2 -2
- package/src/source/{remote.ts → future/remote.ts} +3 -3
- package/src/source/index.ts +3 -2
- package/src/val/array.ts +1 -1
- package/src/val/index.ts +2 -2
- package/src/val/object.ts +1 -1
- package/dist/declarations/src/fetchVal.d.ts +0 -5
- package/dist/declarations/src/schema/i18n.d.ts +0 -22
- package/dist/declarations/src/schema/oneOf.d.ts +0 -23
@@ -0,0 +1,9 @@
|
|
1
|
+
import { Selector as UnknownSelector, GenericSelector, SelectorOf, SelectorSource } from "./index.js";
|
2
|
+
import { Source, SourcePrimitive } from "../../source/index.js";
|
3
|
+
import { Selector as BooleanSelector } from "./boolean.js";
|
4
|
+
export type Selector<T extends SourcePrimitive> = GenericSelector<T> & {
|
5
|
+
eq(other: Source): BooleanSelector<boolean>;
|
6
|
+
andThen<U extends SelectorSource>(f: (v: UnknownSelector<NonNullable<T>>) => U): SelectorOf<U | NullableOf<T>>;
|
7
|
+
};
|
8
|
+
type NullableOf<T extends Source> = T extends null ? null : never;
|
9
|
+
export {};
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { Selector as UnknownSelector } from "./index.js";
|
2
|
-
import { RemoteCompatibleSource } from "
|
2
|
+
import { RemoteCompatibleSource } from "../../source/future/remote.js";
|
3
3
|
declare const brand: unique symbol;
|
4
4
|
export type RemoteSelector<T extends RemoteCompatibleSource> = UnknownSelector<T> & {
|
5
5
|
readonly [brand]: "RemoteSelector";
|
@@ -1,4 +1,3 @@
|
|
1
|
-
import { I18nSelector } from "./i18n.js";
|
2
1
|
import { Selector as ObjectSelector } from "./object.js";
|
3
2
|
import { UndistributedSourceArray as ArraySelector } from "./array.js";
|
4
3
|
import { Selector as NumberSelector } from "./number.js";
|
@@ -9,38 +8,13 @@ import { FileSelector } from "./file.js";
|
|
9
8
|
import { SourcePath } from "../val/index.js";
|
10
9
|
import { Source, SourceArray, SourceObject, SourcePrimitive } from "../source/index.js";
|
11
10
|
import { Schema } from "../schema/index.js";
|
12
|
-
import {
|
13
|
-
import { RemoteSelector } from "./remote.js";
|
14
|
-
import { A } from "ts-toolbelt";
|
15
|
-
import { I18nSource, I18nCompatibleSource } from "../source/i18n.js";
|
16
|
-
import { RemoteCompatibleSource, RemoteSource } from "../source/remote.js";
|
11
|
+
import type { A } from "ts-toolbelt";
|
17
12
|
import { FileSource } from "../source/file.js";
|
18
13
|
import { RichText, RichTextSource } from "../source/richtext.js";
|
19
|
-
|
20
|
-
* Selectors can be used to select parts of a Val module.
|
21
|
-
* Unlike queries, joins, aggregates etc is and will not be supported.
|
22
|
-
*
|
23
|
-
* They are designed to be be used as if they were "normal" JSON data,
|
24
|
-
* though some concessions had to be made because of TypeScript limitations.
|
25
|
-
*
|
26
|
-
* Selectors works equally on source content, defined in code, and remote content.
|
27
|
-
*
|
28
|
-
* @example
|
29
|
-
* // Select the title of a document
|
30
|
-
* const titles = useVal(docsVal.map((doc) => doc.title));
|
31
|
-
*
|
32
|
-
* @example
|
33
|
-
* // Match on a union type
|
34
|
-
* const titles = useVal(docsVal.map((doc) => doc.fold("type")({
|
35
|
-
* newsletter: (newsletter) => newsletter.title,
|
36
|
-
* email: (email) => email.subject,
|
37
|
-
* }));
|
38
|
-
*
|
39
|
-
*/
|
40
|
-
export type Selector<T extends Source> = Source extends T ? GenericSelector<T> : T extends I18nSource<infer L, infer S> ? I18nSelector<L, S> : T extends RemoteSource<infer S> ? S extends RemoteCompatibleSource ? RemoteSelector<S> : GenericSelector<Source, "Could not determine remote source"> : T extends FileSource ? FileSelector : T extends RichTextSource ? RichText : T extends SourceObject ? ObjectSelector<T> : T extends SourceArray ? ArraySelector<T> : T extends string ? StringSelector<T> : T extends number ? NumberSelector<T> : T extends boolean ? BooleanSelector<T> : T extends null ? PrimitiveSelector<null> : never;
|
14
|
+
export type Selector<T extends Source> = Source extends T ? GenericSelector<T> : T extends FileSource ? FileSelector : T extends RichTextSource ? RichText : T extends SourceObject ? ObjectSelector<T> : T extends SourceArray ? ArraySelector<T> : T extends string ? StringSelector<T> : T extends number ? NumberSelector<T> : T extends boolean ? BooleanSelector<T> : T extends null ? PrimitiveSelector<null> : never;
|
41
15
|
export type SelectorSource = SourcePrimitive | undefined | readonly SelectorSource[] | {
|
42
16
|
[key: string]: SelectorSource;
|
43
|
-
} |
|
17
|
+
} | FileSource | RichTextSource | GenericSelector<Source>;
|
44
18
|
/**
|
45
19
|
* @internal
|
46
20
|
*/
|
@@ -53,18 +27,17 @@ export declare const Path: unique symbol;
|
|
53
27
|
/**
|
54
28
|
* @internal
|
55
29
|
*/
|
56
|
-
export declare const
|
30
|
+
export declare const GetSource: unique symbol;
|
57
31
|
/**
|
58
32
|
* @internal
|
59
33
|
*/
|
60
34
|
export declare const ValError: unique symbol;
|
61
35
|
export declare abstract class GenericSelector<out T extends Source, Error extends string | undefined = undefined> {
|
62
36
|
readonly [Path]: SourcePath | undefined;
|
63
|
-
readonly [
|
37
|
+
readonly [GetSource]: T;
|
64
38
|
readonly [ValError]: Error | undefined;
|
65
39
|
readonly [GetSchema]: Schema<T> | undefined;
|
66
40
|
constructor(valOrExpr: T, path: SourcePath | undefined, schema?: Schema<T>, error?: Error);
|
67
|
-
assert<U extends Source, E extends Source = null>(schema: Schema<U>, other?: () => E): SelectorOf<U | E>;
|
68
41
|
}
|
69
42
|
export type SourceOf<T extends SelectorSource> = Source extends T ? Source : T extends Source ? T : T extends undefined ? null : T extends GenericSelector<infer S> ? S : T extends readonly (infer S)[] ? S extends SelectorSource ? {
|
70
43
|
[key in keyof T]: SourceOf<A.Try<T[key], SelectorSource>>;
|
@@ -1,10 +1,3 @@
|
|
1
|
-
import {
|
1
|
+
import { GenericSelector } from "./index.js";
|
2
2
|
import { SourceObject } from "../source/index.js";
|
3
|
-
export type Selector<T extends SourceObject> = GenericSelector<T
|
4
|
-
fold<Tag extends string>(key: Tag): <U extends SelectorSource>(cases: {
|
5
|
-
[key in T[Tag & keyof T] & string]: (v: UnknownSelector<T>) => U;
|
6
|
-
}) => SelectorOf<U>;
|
7
|
-
andThen<U extends SelectorSource>(f: (v: UnknownSelector<NonNullable<T>>) => U): SelectorOf<U>;
|
8
|
-
} & {
|
9
|
-
readonly [key in keyof T]: UnknownSelector<T[key]>;
|
10
|
-
};
|
3
|
+
export type Selector<T extends SourceObject> = GenericSelector<T>;
|
@@ -1,9 +1,3 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
|
4
|
-
export type Selector<T extends SourcePrimitive> = GenericSelector<T> & {
|
5
|
-
eq(other: Source): BooleanSelector<boolean>;
|
6
|
-
andThen<U extends SelectorSource>(f: (v: UnknownSelector<NonNullable<T>>) => U): SelectorOf<U | NullableOf<T>>;
|
7
|
-
};
|
8
|
-
type NullableOf<T extends Source> = T extends null ? null : never;
|
9
|
-
export {};
|
1
|
+
import { GenericSelector } from "./index.js";
|
2
|
+
import { SourcePrimitive } from "../source/index.js";
|
3
|
+
export type Selector<T extends SourcePrimitive> = GenericSelector<T>;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { F } from "ts-toolbelt";
|
2
|
-
import { SourcePrimitive, VAL_EXTENSION } from "
|
3
|
-
import { FileSource } from "
|
2
|
+
import { SourcePrimitive, VAL_EXTENSION } from "../index.js";
|
3
|
+
import { FileSource } from "../file.js";
|
4
4
|
/**
|
5
5
|
* I18n sources cannot have nested remote sources.
|
6
6
|
*/
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { SourcePrimitive, VAL_EXTENSION, PhantomType } from "
|
2
|
-
import { FileSource } from "
|
1
|
+
import { SourcePrimitive, VAL_EXTENSION, PhantomType } from "../index.js";
|
2
|
+
import { FileSource } from "../file.js";
|
3
3
|
import { I18nCompatibleSource, I18nSource } from "./i18n.js";
|
4
|
-
import { RichTextSource } from "
|
4
|
+
import { RichTextSource } from "../richtext.js";
|
5
5
|
/**
|
6
6
|
* Remote sources cannot include other remote sources.
|
7
7
|
*/
|
@@ -1,12 +1,13 @@
|
|
1
1
|
import { FileSource } from "./file.js";
|
2
|
-
import { I18nSource, I18nCompatibleSource } from "./i18n.js";
|
3
|
-
import { RemoteSource, RemoteCompatibleSource } from "./remote.js";
|
2
|
+
import { I18nSource, I18nCompatibleSource } from "./future/i18n.js";
|
3
|
+
import { RemoteSource, RemoteCompatibleSource } from "./future/remote.js";
|
4
4
|
import { RichTextSource } from "./richtext.js";
|
5
5
|
export type Source = SourcePrimitive | SourceObject | SourceArray | I18nSource<string[], I18nCompatibleSource> | RemoteSource<RemoteCompatibleSource> | FileSource | RichTextSource;
|
6
6
|
export type SourceObject = {
|
7
7
|
[key in string]: Source;
|
8
8
|
} & {
|
9
9
|
fold?: never;
|
10
|
+
assert?: never;
|
10
11
|
andThen?: never;
|
11
12
|
_ref?: never;
|
12
13
|
_type?: never;
|
@@ -16,7 +17,7 @@ export type SourceObject = {
|
|
16
17
|
export type SourceArray = readonly Source[];
|
17
18
|
export type SourcePrimitive = string | number | boolean | null;
|
18
19
|
export declare const VAL_EXTENSION: "_type";
|
19
|
-
export declare function getValExtension(source: Source): false | "" | 0 | "richtext" | "
|
20
|
+
export declare function getValExtension(source: Source): false | "" | 0 | "richtext" | "remote" | "file" | "i18n" | null | undefined;
|
20
21
|
/**
|
21
22
|
* A phantom type parameter is one that doesn't show up at runtime, but is checked statically (and only) at compile time.
|
22
23
|
*
|
@@ -4,8 +4,8 @@ import { Val as ArrayVal } from "./array.js";
|
|
4
4
|
import { Val as PrimitiveVal } from "./primitive.js";
|
5
5
|
import { Json, JsonArray, JsonObject, JsonPrimitive } from "../Json.js";
|
6
6
|
import { Path, Selector } from "../selector/index.js";
|
7
|
-
import { I18nSource } from "../source/i18n.js";
|
8
|
-
import { RemoteSource } from "../source/remote.js";
|
7
|
+
import { I18nSource } from "../source/future/i18n.js";
|
8
|
+
import { RemoteSource } from "../source/future/remote.js";
|
9
9
|
import { FileSource } from "../source/file.js";
|
10
10
|
export type SerializedVal = {
|
11
11
|
val: SerializedVal | Json;
|
@@ -218,21 +218,26 @@ function _createSuper(Derived) {
|
|
218
218
|
};
|
219
219
|
}
|
220
220
|
|
221
|
+
// import { RemoteCompatibleSource, RemoteSource } from "../source/remote";
|
222
|
+
|
223
|
+
// import { SerializedI18nSchema } from "./future/i18n";
|
224
|
+
// import { SerializedOneOfSchema } from "./future/oneOf";
|
221
225
|
var Schema = /*#__PURE__*/function () {
|
222
226
|
function Schema() {
|
223
227
|
_classCallCheck(this, Schema);
|
224
228
|
}
|
225
229
|
_createClass(Schema, [{
|
226
|
-
key: "
|
227
|
-
value:
|
228
|
-
|
229
|
-
|
230
|
-
|
230
|
+
key: "appendValidationError",
|
231
|
+
value:
|
232
|
+
// remote(): Src extends RemoteCompatibleSource
|
233
|
+
// ? Schema<RemoteSource<Src>>
|
234
|
+
// : never {
|
235
|
+
// // TODO: Schema<never, "Cannot create remote schema from non-remote source.">
|
236
|
+
// throw new Error("You need Val Ultra to use .remote()");
|
237
|
+
// }
|
231
238
|
|
232
239
|
/** MUTATES! since internal and perf sensitive */
|
233
|
-
|
234
|
-
key: "appendValidationError",
|
235
|
-
value: function appendValidationError(current, path, message, value) {
|
240
|
+
function appendValidationError(current, path, message, value) {
|
236
241
|
if (current) {
|
237
242
|
if (current[path]) {
|
238
243
|
current[path].push({
|
@@ -259,30 +264,6 @@ var Schema = /*#__PURE__*/function () {
|
|
259
264
|
|
260
265
|
// TODO: SourceError<"Could not determine type of Schema">
|
261
266
|
|
262
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
263
|
-
|
264
|
-
/**
|
265
|
-
* Selectors can be used to select parts of a Val module.
|
266
|
-
* Unlike queries, joins, aggregates etc is and will not be supported.
|
267
|
-
*
|
268
|
-
* They are designed to be be used as if they were "normal" JSON data,
|
269
|
-
* though some concessions had to be made because of TypeScript limitations.
|
270
|
-
*
|
271
|
-
* Selectors works equally on source content, defined in code, and remote content.
|
272
|
-
*
|
273
|
-
* @example
|
274
|
-
* // Select the title of a document
|
275
|
-
* const titles = useVal(docsVal.map((doc) => doc.title));
|
276
|
-
*
|
277
|
-
* @example
|
278
|
-
* // Match on a union type
|
279
|
-
* const titles = useVal(docsVal.map((doc) => doc.fold("type")({
|
280
|
-
* newsletter: (newsletter) => newsletter.title,
|
281
|
-
* email: (email) => email.subject,
|
282
|
-
* }));
|
283
|
-
*
|
284
|
-
*/
|
285
|
-
|
286
267
|
/**
|
287
268
|
* @internal
|
288
269
|
*/
|
@@ -295,27 +276,18 @@ var Path = Symbol("Path");
|
|
295
276
|
/**
|
296
277
|
* @internal
|
297
278
|
*/
|
298
|
-
var
|
279
|
+
var GetSource = Symbol("GetSource");
|
299
280
|
/**
|
300
281
|
* @internal
|
301
282
|
*/
|
302
283
|
var ValError = Symbol("ValError");
|
303
|
-
var GenericSelector = /*#__PURE__*/function () {
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
}
|
311
|
-
_createClass(GenericSelector, [{
|
312
|
-
key: "assert",
|
313
|
-
value: function assert(schema, other) {
|
314
|
-
throw new Error("Not implemented");
|
315
|
-
}
|
316
|
-
}]);
|
317
|
-
return GenericSelector;
|
318
|
-
}();
|
284
|
+
var GenericSelector = /*#__PURE__*/_createClass(function GenericSelector(valOrExpr, path, schema, error) {
|
285
|
+
_classCallCheck(this, GenericSelector);
|
286
|
+
this[Path] = path;
|
287
|
+
this[GetSource] = valOrExpr;
|
288
|
+
this[ValError] = error;
|
289
|
+
this[GetSchema] = schema;
|
290
|
+
});
|
319
291
|
|
320
292
|
/**
|
321
293
|
* Use this type to convert types that accepts both Source and Selectors
|
@@ -534,7 +506,7 @@ var ImageSchema = /*#__PURE__*/function (_Schema) {
|
|
534
506
|
var image = function image(options) {
|
535
507
|
return new ImageSchema(options);
|
536
508
|
};
|
537
|
-
var
|
509
|
+
var convertFileSource = function convertFileSource(src) {
|
538
510
|
var _src$metadata;
|
539
511
|
// TODO: /public should be configurable
|
540
512
|
return {
|
@@ -575,148 +547,4 @@ function getValPath(valOrSelector) {
|
|
575
547
|
return valOrSelector[Path];
|
576
548
|
}
|
577
549
|
|
578
|
-
|
579
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
580
|
-
}
|
581
|
-
function _andThen(f, source, path) {
|
582
|
-
if (source) {
|
583
|
-
return newSelectorProxy(f(newSelectorProxy(source, path)));
|
584
|
-
}
|
585
|
-
return newSelectorProxy(source, path);
|
586
|
-
}
|
587
|
-
function isSelector(source) {
|
588
|
-
return _typeof(source) === "object" && source !== null && (SourceOrExpr in source || Path in source);
|
589
|
-
}
|
590
|
-
function newSelectorProxy(source, path, moduleSchema) {
|
591
|
-
var _ref;
|
592
|
-
if (_typeof(source) === "object") {
|
593
|
-
if (isSelector(source)) {
|
594
|
-
return source;
|
595
|
-
} else if (isSerializedVal(source)) {
|
596
|
-
return newSelectorProxy(source.val, source.valPath);
|
597
|
-
}
|
598
|
-
}
|
599
|
-
if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
|
600
|
-
var fileRef = source[FILE_REF_PROP];
|
601
|
-
if (typeof fileRef !== "string") {
|
602
|
-
throw Error("Invalid file ref: " + fileRef);
|
603
|
-
}
|
604
|
-
return newSelectorProxy(convertImageSource(source), path, moduleSchema);
|
605
|
-
}
|
606
|
-
switch (_typeof(source)) {
|
607
|
-
case "function":
|
608
|
-
case "symbol":
|
609
|
-
throw Error("Invalid selector type: ".concat(_typeof(source), ": ").concat(source));
|
610
|
-
case "object":
|
611
|
-
// Handles both objects and arrays!
|
612
|
-
if (source !== null) {
|
613
|
-
return new Proxy(source, {
|
614
|
-
// TODO: see proxy docs if we want more traps
|
615
|
-
has: function has(target, prop) {
|
616
|
-
if (prop === SourceOrExpr) {
|
617
|
-
return true;
|
618
|
-
}
|
619
|
-
if (prop === Path) {
|
620
|
-
return true;
|
621
|
-
}
|
622
|
-
if (prop === "andThen") {
|
623
|
-
return true;
|
624
|
-
}
|
625
|
-
if (prop === GetSchema) {
|
626
|
-
return true;
|
627
|
-
}
|
628
|
-
return prop in target;
|
629
|
-
},
|
630
|
-
get: function get(target, prop) {
|
631
|
-
if (prop === SourceOrExpr) {
|
632
|
-
return source;
|
633
|
-
}
|
634
|
-
if (prop === Path) {
|
635
|
-
return path;
|
636
|
-
}
|
637
|
-
if (prop === GetSchema) {
|
638
|
-
return moduleSchema;
|
639
|
-
}
|
640
|
-
if (prop === "andThen") {
|
641
|
-
return function (f) {
|
642
|
-
return _andThen(f, source, path);
|
643
|
-
};
|
644
|
-
}
|
645
|
-
if (Array.isArray(target)) {
|
646
|
-
if (prop === "filter") {
|
647
|
-
return function (f) {
|
648
|
-
var filtered = target.map(function (a, i) {
|
649
|
-
return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
650
|
-
}).filter(function (a) {
|
651
|
-
if (f && f instanceof Schema) {
|
652
|
-
return f.assert(unValify(a));
|
653
|
-
} else {
|
654
|
-
return unValify(f(a));
|
655
|
-
}
|
656
|
-
});
|
657
|
-
return newSelectorProxy(filtered, path, moduleSchema);
|
658
|
-
};
|
659
|
-
} else if (prop === "map") {
|
660
|
-
return function (f) {
|
661
|
-
var filtered = target.map(function (a, i) {
|
662
|
-
var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
|
663
|
-
if (isSelector(valueOrSelector)) {
|
664
|
-
return valueOrSelector;
|
665
|
-
}
|
666
|
-
return newSelectorProxy(valueOrSelector);
|
667
|
-
});
|
668
|
-
return newSelectorProxy(filtered, path, moduleSchema);
|
669
|
-
};
|
670
|
-
}
|
671
|
-
}
|
672
|
-
if (Array.isArray(target) && prop === "length") {
|
673
|
-
return newSelectorProxy(target.length);
|
674
|
-
}
|
675
|
-
var reflectedValue = Reflect.get(target, prop);
|
676
|
-
if (hasOwn(source, prop)) {
|
677
|
-
if (!Number.isNaN(Number(prop))) {
|
678
|
-
return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
679
|
-
}
|
680
|
-
return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
|
681
|
-
}
|
682
|
-
return reflectedValue;
|
683
|
-
}
|
684
|
-
});
|
685
|
-
}
|
686
|
-
// intentional fallthrough
|
687
|
-
// eslint-disable-next-line no-fallthrough
|
688
|
-
default:
|
689
|
-
return _ref = {
|
690
|
-
eq: function eq(other) {
|
691
|
-
var otherValue = other;
|
692
|
-
if (isSelector(other)) {
|
693
|
-
otherValue = other[SourceOrExpr];
|
694
|
-
if (otherValue instanceof Expr) {
|
695
|
-
throw Error("TODO: Cannot evaluate equality with an Expr");
|
696
|
-
}
|
697
|
-
}
|
698
|
-
return newSelectorProxy(source === otherValue, undefined);
|
699
|
-
},
|
700
|
-
andThen: function andThen(f) {
|
701
|
-
return _andThen(f, source === undefined ? null : source, path);
|
702
|
-
}
|
703
|
-
}, _defineProperty(_ref, SourceOrExpr, source === undefined ? null : source), _defineProperty(_ref, Path, path), _defineProperty(_ref, GetSchema, moduleSchema), _ref;
|
704
|
-
}
|
705
|
-
}
|
706
|
-
function createValPathOfItem(arrayPath, prop) {
|
707
|
-
if (_typeof(prop) === "symbol") {
|
708
|
-
throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
|
709
|
-
}
|
710
|
-
return arrayPath && "".concat(arrayPath, ".").concat(JSON.stringify(prop));
|
711
|
-
}
|
712
|
-
|
713
|
-
// TODO: could we do .val on the objects instead?
|
714
|
-
function unValify(valueOrSelector) {
|
715
|
-
if (_typeof(valueOrSelector) === "object" && (SourceOrExpr in valueOrSelector || Path in valueOrSelector)) {
|
716
|
-
var selectorValue = valueOrSelector[SourceOrExpr];
|
717
|
-
return selectorValue;
|
718
|
-
}
|
719
|
-
return valueOrSelector;
|
720
|
-
}
|
721
|
-
|
722
|
-
export { Call as C, Expr as E, FILE_REF_PROP as F, GenericSelector as G, ImageSchema as I, NilSym as N, Path as P, Schema as S, VAL_EXTENSION as V, _inherits as _, _createSuper as a, _classCallCheck as b, _createClass as c, _objectSpread2 as d, _defineProperty as e, _typeof as f, getValPath as g, file as h, image as i, _slicedToArray as j, isFile as k, createValPathOfItem as l, SourceOrExpr as m, newSelectorProxy as n, isSelector as o, isSerializedVal as p, convertImageSource as q, getSchema as r, isVal as s, Sym as t, StringLiteral as u, StringTemplate as v, _toConsumableArray as w };
|
550
|
+
export { Call as C, Expr as E, FILE_REF_PROP as F, GetSource as G, ImageSchema as I, NilSym as N, Path as P, Schema as S, VAL_EXTENSION as V, _inherits as _, _createSuper as a, _classCallCheck as b, _createClass as c, _defineProperty as d, _typeof as e, _objectSpread2 as f, getValPath as g, file as h, image as i, _slicedToArray as j, isFile as k, isSerializedVal as l, convertFileSource as m, getSchema as n, isVal as o, GenericSelector as p, Sym as q, StringLiteral as r, StringTemplate as s, _toConsumableArray as t, GetSchema as u };
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { f as _objectSpread2, j as _slicedToArray, c as _createClass, b as _classCallCheck, q as Sym, C as Call, r as StringLiteral, s as StringTemplate, e as _typeof, l as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, m as convertFileSource, d as _defineProperty, S as Schema, E as Expr, t as _toConsumableArray, N as NilSym } from './index-369caccf.esm.js';
|
2
2
|
import { i as isErr, e as err, o as ok, a as isOk } from './result-b96df128.esm.js';
|
3
3
|
|
4
4
|
var WHITE_SPACE = ["\n", "\r", "\t", " "];
|
@@ -260,6 +260,194 @@ function parse(input) {
|
|
260
260
|
return parseTokens(tokens);
|
261
261
|
}
|
262
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
|
+
var _ref;
|
321
|
+
if (_typeof(source) === "object") {
|
322
|
+
if (isSelector(source)) {
|
323
|
+
return source;
|
324
|
+
} else if (isSerializedVal(source)) {
|
325
|
+
return newSelectorProxy(source.val, source.valPath);
|
326
|
+
}
|
327
|
+
}
|
328
|
+
if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
|
329
|
+
var fileRef = source[FILE_REF_PROP];
|
330
|
+
if (typeof fileRef !== "string") {
|
331
|
+
throw Error("Invalid file ref: " + fileRef);
|
332
|
+
}
|
333
|
+
return newSelectorProxy(convertFileSource(source), path, moduleSchema);
|
334
|
+
}
|
335
|
+
switch (_typeof(source)) {
|
336
|
+
case "function":
|
337
|
+
case "symbol":
|
338
|
+
throw Error("Invalid selector type: ".concat(_typeof(source), ": ").concat(source));
|
339
|
+
case "object":
|
340
|
+
// Handles both objects and arrays!
|
341
|
+
if (source !== null) {
|
342
|
+
return new Proxy(source, {
|
343
|
+
// TODO: see proxy docs if we want more traps
|
344
|
+
has: function has(target, prop) {
|
345
|
+
if (prop === SourceOrExpr) {
|
346
|
+
return true;
|
347
|
+
}
|
348
|
+
if (prop === Path) {
|
349
|
+
return true;
|
350
|
+
}
|
351
|
+
if (prop === "andThen") {
|
352
|
+
return true;
|
353
|
+
}
|
354
|
+
if (prop === GetSchema) {
|
355
|
+
return true;
|
356
|
+
}
|
357
|
+
return prop in target;
|
358
|
+
},
|
359
|
+
get: function get(target, prop) {
|
360
|
+
if (prop === SourceOrExpr) {
|
361
|
+
return source;
|
362
|
+
}
|
363
|
+
if (prop === Path) {
|
364
|
+
return path;
|
365
|
+
}
|
366
|
+
if (prop === GetSchema) {
|
367
|
+
return moduleSchema;
|
368
|
+
}
|
369
|
+
if (prop === "andThen") {
|
370
|
+
return function (f) {
|
371
|
+
return _andThen(f, source, path);
|
372
|
+
};
|
373
|
+
}
|
374
|
+
if (Array.isArray(target)) {
|
375
|
+
if (prop === "filter") {
|
376
|
+
return function (f) {
|
377
|
+
var filtered = target.map(function (a, i) {
|
378
|
+
return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
379
|
+
}).filter(function (a) {
|
380
|
+
if (f && f instanceof Schema) {
|
381
|
+
return f.assert(unValify(a));
|
382
|
+
} else {
|
383
|
+
return unValify(f(a));
|
384
|
+
}
|
385
|
+
});
|
386
|
+
return newSelectorProxy(filtered, path, moduleSchema);
|
387
|
+
};
|
388
|
+
} else if (prop === "map") {
|
389
|
+
return function (f) {
|
390
|
+
var filtered = target.map(function (a, i) {
|
391
|
+
var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
|
392
|
+
if (isSelector(valueOrSelector)) {
|
393
|
+
return valueOrSelector;
|
394
|
+
}
|
395
|
+
return newSelectorProxy(valueOrSelector);
|
396
|
+
});
|
397
|
+
return newSelectorProxy(filtered, path, moduleSchema);
|
398
|
+
};
|
399
|
+
}
|
400
|
+
}
|
401
|
+
if (Array.isArray(target) && prop === "length") {
|
402
|
+
return newSelectorProxy(target.length);
|
403
|
+
}
|
404
|
+
var reflectedValue = Reflect.get(target, prop);
|
405
|
+
if (hasOwn(source, prop)) {
|
406
|
+
if (!Number.isNaN(Number(prop))) {
|
407
|
+
return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
408
|
+
}
|
409
|
+
return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
|
410
|
+
}
|
411
|
+
return reflectedValue;
|
412
|
+
}
|
413
|
+
});
|
414
|
+
}
|
415
|
+
// intentional fallthrough
|
416
|
+
// eslint-disable-next-line no-fallthrough
|
417
|
+
default:
|
418
|
+
return _ref = {
|
419
|
+
eq: function eq(other) {
|
420
|
+
var otherValue = other;
|
421
|
+
if (isSelector(other)) {
|
422
|
+
otherValue = other[SourceOrExpr];
|
423
|
+
if (otherValue instanceof Expr) {
|
424
|
+
throw Error("TODO: Cannot evaluate equality with an Expr");
|
425
|
+
}
|
426
|
+
}
|
427
|
+
return newSelectorProxy(source === otherValue, undefined);
|
428
|
+
},
|
429
|
+
andThen: function andThen(f) {
|
430
|
+
return _andThen(f, source === undefined ? null : source, path);
|
431
|
+
}
|
432
|
+
}, _defineProperty(_ref, SourceOrExpr, source === undefined ? null : source), _defineProperty(_ref, Path, path), _defineProperty(_ref, GetSchema, moduleSchema), _ref;
|
433
|
+
}
|
434
|
+
}
|
435
|
+
function createValPathOfItem(arrayPath, prop) {
|
436
|
+
if (_typeof(prop) === "symbol") {
|
437
|
+
throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
|
438
|
+
}
|
439
|
+
return arrayPath && "".concat(arrayPath, ".").concat(JSON.stringify(prop));
|
440
|
+
}
|
441
|
+
|
442
|
+
// TODO: could we do .val on the objects instead?
|
443
|
+
function unValify(valueOrSelector) {
|
444
|
+
if (_typeof(valueOrSelector) === "object" && (SourceOrExpr in valueOrSelector || Path in valueOrSelector)) {
|
445
|
+
var selectorValue = valueOrSelector[SourceOrExpr];
|
446
|
+
return selectorValue;
|
447
|
+
}
|
448
|
+
return valueOrSelector;
|
449
|
+
}
|
450
|
+
|
263
451
|
var EvalError = /*#__PURE__*/function () {
|
264
452
|
function EvalError(message, expr) {
|
265
453
|
_classCallCheck(this, EvalError);
|