@valbuild/core 0.26.0 → 0.27.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.
Files changed (92) hide show
  1. package/package.json +12 -3
  2. package/CHANGELOG.md +0 -0
  3. package/ROADMAP.md +0 -106
  4. package/jest.config.js +0 -4
  5. package/src/Json.ts +0 -4
  6. package/src/ValApi.ts +0 -81
  7. package/src/expr/README.md +0 -193
  8. package/src/expr/eval.test.ts +0 -198
  9. package/src/expr/eval.ts +0 -251
  10. package/src/expr/expr.ts +0 -91
  11. package/src/expr/index.ts +0 -3
  12. package/src/expr/parser.test.ts +0 -158
  13. package/src/expr/parser.ts +0 -229
  14. package/src/expr/repl.ts +0 -88
  15. package/src/expr/tokenizer.test.ts +0 -539
  16. package/src/expr/tokenizer.ts +0 -117
  17. package/src/fp/array.ts +0 -30
  18. package/src/fp/index.ts +0 -3
  19. package/src/fp/result.ts +0 -214
  20. package/src/fp/util.ts +0 -52
  21. package/src/future/fetchVal.test.ts +0 -164
  22. package/src/future/fetchVal.ts +0 -206
  23. package/src/getSha256.ts +0 -8
  24. package/src/index.ts +0 -132
  25. package/src/initSchema.ts +0 -50
  26. package/src/initVal.ts +0 -73
  27. package/src/module.test.ts +0 -170
  28. package/src/module.ts +0 -397
  29. package/src/patch/deref.test.ts +0 -298
  30. package/src/patch/deref.ts +0 -136
  31. package/src/patch/index.ts +0 -12
  32. package/src/patch/json.test.ts +0 -582
  33. package/src/patch/json.ts +0 -304
  34. package/src/patch/operation.ts +0 -86
  35. package/src/patch/ops.ts +0 -83
  36. package/src/patch/parse.test.ts +0 -202
  37. package/src/patch/parse.ts +0 -202
  38. package/src/patch/patch.ts +0 -49
  39. package/src/patch/util.ts +0 -74
  40. package/src/schema/array.ts +0 -93
  41. package/src/schema/boolean.ts +0 -49
  42. package/src/schema/future/i18n.ts +0 -69
  43. package/src/schema/future/oneOf.ts +0 -63
  44. package/src/schema/image.ts +0 -137
  45. package/src/schema/index.ts +0 -70
  46. package/src/schema/keyOf.ts +0 -167
  47. package/src/schema/literal.ts +0 -63
  48. package/src/schema/number.ts +0 -56
  49. package/src/schema/object.ts +0 -110
  50. package/src/schema/record.ts +0 -103
  51. package/src/schema/richtext.ts +0 -44
  52. package/src/schema/string.ts +0 -95
  53. package/src/schema/union.ts +0 -63
  54. package/src/schema/validation/ValidationError.ts +0 -16
  55. package/src/schema/validation/ValidationFix.ts +0 -6
  56. package/src/schema/validation.test.ts +0 -291
  57. package/src/selector/SelectorProxy.ts +0 -238
  58. package/src/selector/array.ts +0 -13
  59. package/src/selector/boolean.ts +0 -4
  60. package/src/selector/file.ts +0 -6
  61. package/src/selector/future/ExprProxy.test.ts +0 -203
  62. package/src/selector/future/ExprProxy.ts +0 -216
  63. package/src/selector/future/SelectorProxy.test.ts +0 -172
  64. package/src/selector/future/SelectorProxy.ts +0 -238
  65. package/src/selector/future/array.ts +0 -37
  66. package/src/selector/future/boolean.ts +0 -4
  67. package/src/selector/future/file.ts +0 -14
  68. package/src/selector/future/i18n.ts +0 -13
  69. package/src/selector/future/index.ts +0 -169
  70. package/src/selector/future/number.ts +0 -4
  71. package/src/selector/future/object.ts +0 -22
  72. package/src/selector/future/primitive.ts +0 -17
  73. package/src/selector/future/remote.ts +0 -9
  74. package/src/selector/future/selector.test.ts +0 -429
  75. package/src/selector/future/selectorOf.ts +0 -7
  76. package/src/selector/future/string.ts +0 -4
  77. package/src/selector/index.ts +0 -121
  78. package/src/selector/number.ts +0 -4
  79. package/src/selector/object.ts +0 -5
  80. package/src/selector/primitive.ts +0 -4
  81. package/src/selector/string.ts +0 -4
  82. package/src/source/file.ts +0 -45
  83. package/src/source/future/i18n.ts +0 -60
  84. package/src/source/future/remote.ts +0 -54
  85. package/src/source/index.ts +0 -53
  86. package/src/source/link.ts +0 -14
  87. package/src/source/richtext.ts +0 -178
  88. package/src/val/array.ts +0 -10
  89. package/src/val/index.ts +0 -100
  90. package/src/val/object.ts +0 -13
  91. package/src/val/primitive.ts +0 -8
  92. package/tsconfig.json +0 -8
@@ -1,172 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { Selector, GenericSelector, Path } from ".";
3
- import { number } from "../../schema/number";
4
- import { string } from "../../schema/string";
5
- import { Source } from "../../source";
6
- import { SourcePath } from "../../val";
7
- import { newSelectorProxy, selectorToVal } from "./SelectorProxy";
8
-
9
- describe("SelectorProxy", () => {
10
- test("string eq andThen", () => {
11
- const sourcePath = "/app/text" as SourcePath;
12
- const string1 = newSelectorProxy("foo", sourcePath) as Selector<string>;
13
- expectValOrExpr(string1.eq("foo")).toStrictEqual({
14
- val: true,
15
- [Path]: undefined,
16
- });
17
- expectValOrExpr(string1.eq("foo").andThen(() => string1)).toStrictEqual({
18
- val: "foo",
19
- [Path]: sourcePath,
20
- });
21
- });
22
-
23
- test("array filter eq", () => {
24
- const sourcePath = "/app/texts" as SourcePath;
25
- const string1 = newSelectorProxy(["foo", "bar"], sourcePath) as Selector<
26
- string[]
27
- >;
28
- expectValOrExpr(
29
- string1.map((a) => a).filter((a) => a.eq("foo"))
30
- ).toStrictEqual({
31
- val: ["foo"],
32
- [Path]: "/app/texts",
33
- });
34
- });
35
-
36
- test("array numbers", () => {
37
- const sourcePath = "/app/numbers" as SourcePath;
38
- const numbersVal = newSelectorProxy([1, 2, 3], sourcePath) as Selector<
39
- string[]
40
- >;
41
- expectValOrExpr(numbersVal).toStrictEqual({
42
- val: [1, 2, 3],
43
- [Path]: "/app/numbers",
44
- });
45
- });
46
-
47
- test("array length", () => {
48
- const sourcePath = "/app/numbers" as SourcePath;
49
- const numbersVal = newSelectorProxy([1, 2, 3], sourcePath) as Selector<
50
- string[]
51
- >;
52
- expectValOrExpr(numbersVal.length).toStrictEqual({
53
- val: 3,
54
- [Path]: undefined,
55
- });
56
- });
57
-
58
- test("array filter match number", () => {
59
- const sourcePath = "/app/numbers" as SourcePath;
60
- const numbersVal = newSelectorProxy([1, 2, undefined], sourcePath) as
61
- | Selector<string[]>
62
- | Selector<null[]>;
63
- expectValOrExpr(numbersVal.filter(number())).toStrictEqual({
64
- val: [1, 2],
65
- [Path]: "/app/numbers",
66
- });
67
- });
68
-
69
- test("array filter match string / undefined / null", () => {
70
- const sourcePath = "/app/numbers" as SourcePath;
71
- const numbersVal = newSelectorProxy(
72
- [1, 2, undefined, null, "test"],
73
- sourcePath
74
- ) as Selector<(number | string | null)[]>;
75
- expectValOrExpr(numbersVal.filter(string().optional())).toStrictEqual({
76
- val: [null, null, "test"],
77
- [Path]: "/app/numbers",
78
- });
79
- });
80
-
81
- test("object lookup", () => {
82
- const sourcePath = "/app/blog" as SourcePath;
83
- const blogsVal = newSelectorProxy(
84
- { title: "title1" },
85
- sourcePath
86
- ) as Selector<{ title: string }>;
87
- expectValOrExpr(blogsVal).toStrictEqual({
88
- val: { title: "title1" },
89
- [Path]: "/app/blog",
90
- });
91
- });
92
-
93
- test("object prop", () => {
94
- const sourcePath = "/app/blog" as SourcePath;
95
- const blogsVal = newSelectorProxy(
96
- { title: "title1" },
97
- sourcePath
98
- ) as Selector<{ title: string }>;
99
- expectValOrExpr(blogsVal.title).toStrictEqual({
100
- val: "title1",
101
- [Path]: '/app/blog."title"',
102
- });
103
- });
104
-
105
- test("array object index", () => {
106
- const sourcePath = "/app/blogs" as SourcePath;
107
- const blogsVal = newSelectorProxy(
108
- [{ title: "title1" }],
109
- sourcePath
110
- ) as Selector<{ title: string }[]>;
111
- expectValOrExpr(blogsVal[0]).toStrictEqual({
112
- val: {
113
- title: "title1",
114
- },
115
- [Path]: "/app/blogs.0",
116
- });
117
- });
118
-
119
- test("array map object index", () => {
120
- const sourcePath = "/app/blogs" as SourcePath;
121
- const blogsVal = newSelectorProxy(
122
- [{ title: "title1" }],
123
- sourcePath
124
- ) as Selector<{ title: string }[]>;
125
- expectValOrExpr(blogsVal.map((v) => v)[0]).toStrictEqual({
126
- val: {
127
- title: "title1",
128
- },
129
- [Path]: "/app/blogs.0",
130
- });
131
- });
132
-
133
- test("map object then index", () => {
134
- const sourcePath = "/app/blogs" as SourcePath;
135
- const blogsVal = newSelectorProxy(
136
- [{ title: "title1" }],
137
- sourcePath
138
- ) as Selector<{ title: string }[]>;
139
- expectValOrExpr(blogsVal.map((blog) => blog)).toStrictEqual({
140
- val: [{ title: "title1" }],
141
- [Path]: "/app/blogs",
142
- });
143
- });
144
-
145
- test("map object then index then prop", () => {
146
- const sourcePath = "/app/blogs" as SourcePath;
147
- const blogsVal = newSelectorProxy(
148
- [{ title: "title1" }, { title: "title2" }],
149
- sourcePath
150
- ) as Selector<{ title: string }[]>;
151
- expectValOrExpr(blogsVal.map((blog) => blog)[0].title).toStrictEqual({
152
- val: "title1",
153
- [Path]: '/app/blogs.0."title"',
154
- });
155
- });
156
-
157
- test("map object then prop then index", () => {
158
- const sourcePath = "/app/blogs" as SourcePath;
159
- const blogsVal = newSelectorProxy(
160
- [{ title: "title1" }, { title: "title2" }],
161
- sourcePath
162
- ) as Selector<{ title: string }[]>;
163
- expectValOrExpr(blogsVal.map((blog) => blog.title)[0]).toStrictEqual({
164
- val: "title1",
165
- [Path]: '/app/blogs.0."title"',
166
- });
167
- });
168
- });
169
-
170
- function expectValOrExpr<T extends Source>(selector: GenericSelector<T>) {
171
- return expect(selectorToVal(selector));
172
- }
@@ -1,238 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { Path, GenericSelector, SourceOrExpr, GetSchema } from ".";
3
- import { Expr } from "../../expr/expr";
4
- import { Schema } from "../../schema";
5
- import { convertFileSource } from "../../schema/image";
6
- import { Source, SourcePrimitive, VAL_EXTENSION } from "../../source";
7
- import { FILE_REF_PROP } from "../../source/file";
8
- import { isSerializedVal, SourcePath } from "../../val";
9
-
10
- function hasOwn<T extends PropertyKey>(obj: object, prop: T): boolean {
11
- return Object.prototype.hasOwnProperty.call(obj, prop);
12
- }
13
-
14
- function andThen(f: (...args: any[]) => any, source: any, path?: SourcePath) {
15
- if (source) {
16
- return newSelectorProxy(f(newSelectorProxy(source, path)));
17
- }
18
- return newSelectorProxy(source, path);
19
- }
20
-
21
- export function isSelector(source: any): source is GenericSelector<Source> {
22
- return (
23
- typeof source === "object" &&
24
- source !== null &&
25
- (SourceOrExpr in source || Path in source)
26
- );
27
- }
28
-
29
- export function newSelectorProxy(
30
- source: any,
31
- path?: SourcePath,
32
- moduleSchema?: any
33
- ): any {
34
- if (typeof source === "object") {
35
- if (isSelector(source)) {
36
- return source;
37
- } else if (isSerializedVal(source)) {
38
- return newSelectorProxy(source.val, source.valPath);
39
- }
40
- }
41
-
42
- if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
43
- const fileRef = source[FILE_REF_PROP];
44
- if (typeof fileRef !== "string") {
45
- throw Error("Invalid file ref: " + fileRef);
46
- }
47
- return newSelectorProxy(convertFileSource(source), path, moduleSchema);
48
- }
49
-
50
- switch (typeof source) {
51
- case "function":
52
- case "symbol":
53
- throw Error(`Invalid selector type: ${typeof source}: ${source}`);
54
- case "object":
55
- // Handles both objects and arrays!
56
- if (source !== null) {
57
- return new Proxy(source, {
58
- // TODO: see proxy docs if we want more traps
59
- has(target, prop: string | symbol) {
60
- if (prop === SourceOrExpr) {
61
- return true;
62
- }
63
- if (prop === Path) {
64
- return true;
65
- }
66
- if (prop === "andThen") {
67
- return true;
68
- }
69
- if (prop === GetSchema) {
70
- return true;
71
- }
72
- return prop in target;
73
- },
74
- get(target, prop: string | symbol) {
75
- if (prop === SourceOrExpr) {
76
- return source;
77
- }
78
- if (prop === Path) {
79
- return path;
80
- }
81
- if (prop === GetSchema) {
82
- return moduleSchema;
83
- }
84
- if (prop === "andThen") {
85
- return (f: any) => andThen(f, source, path);
86
- }
87
- if (Array.isArray(target)) {
88
- if (prop === "filter") {
89
- return (f: any) => {
90
- const filtered = target
91
- .map((a, i) =>
92
- newSelectorProxy(
93
- a,
94
- createValPathOfItem(path, i),
95
- moduleSchema?.item
96
- )
97
- )
98
- .filter((a) => {
99
- if (f && f instanceof Schema) {
100
- return f.assert(unValify(a));
101
- } else {
102
- return unValify(f(a));
103
- }
104
- });
105
- return newSelectorProxy(filtered, path, moduleSchema);
106
- };
107
- } else if (prop === "map") {
108
- return (f: any) => {
109
- const filtered = target.map((a, i) => {
110
- const valueOrSelector = f(
111
- newSelectorProxy(
112
- a,
113
- createValPathOfItem(path, i),
114
- moduleSchema?.item
115
- ),
116
- newSelectorProxy(i)
117
- );
118
- if (isSelector(valueOrSelector)) {
119
- return valueOrSelector;
120
- }
121
- return newSelectorProxy(valueOrSelector);
122
- });
123
- return newSelectorProxy(filtered, path, moduleSchema);
124
- };
125
- }
126
- }
127
- if (Array.isArray(target) && prop === "length") {
128
- return newSelectorProxy(target.length);
129
- }
130
- const reflectedValue = Reflect.get(target, prop);
131
-
132
- if (hasOwn(source, prop)) {
133
- if (!Number.isNaN(Number(prop))) {
134
- return newSelectorProxy(
135
- reflectedValue,
136
- createValPathOfItem(path, Number(prop)),
137
- moduleSchema?.item
138
- );
139
- }
140
- return newSelectorProxy(
141
- reflectedValue,
142
- createValPathOfItem(path, prop),
143
- moduleSchema?.items[prop]
144
- );
145
- }
146
- return reflectedValue;
147
- },
148
- });
149
- }
150
- // intentional fallthrough
151
- // eslint-disable-next-line no-fallthrough
152
- default:
153
- return {
154
- eq: (other: SourcePrimitive | GenericSelector<Source>) => {
155
- let otherValue: any = other;
156
- if (isSelector(other)) {
157
- otherValue = other[SourceOrExpr];
158
- if (otherValue instanceof Expr) {
159
- throw Error("TODO: Cannot evaluate equality with an Expr");
160
- }
161
- }
162
- return newSelectorProxy(source === otherValue, undefined);
163
- },
164
- andThen: (f: any) => {
165
- return andThen(f, source === undefined ? null : source, path);
166
- },
167
- [SourceOrExpr]: source === undefined ? null : source,
168
- [Path]: path,
169
- [GetSchema]: moduleSchema,
170
- };
171
- }
172
- }
173
-
174
- function selectorAsVal(sel: any): any {
175
- if (isSerializedVal(sel)) {
176
- // is a serialized val
177
- return selectorAsVal(newSelectorProxy(sel.val, sel.valPath));
178
- } else if (
179
- typeof sel === "object" &&
180
- sel &&
181
- !(SourceOrExpr in sel) &&
182
- !Array.isArray(sel)
183
- ) {
184
- // is object
185
- return Object.fromEntries(
186
- Object.entries(sel).map(([k, v]) => [k, selectorAsVal(v)])
187
- );
188
- } else if (
189
- typeof sel === "object" &&
190
- sel &&
191
- !(SourceOrExpr in sel) &&
192
- Array.isArray(sel)
193
- ) {
194
- // is array
195
- return sel.map((v) => selectorAsVal(v));
196
- } else if (
197
- typeof sel === "object" &&
198
- sel &&
199
- (SourceOrExpr in sel || Path in sel)
200
- ) {
201
- return selectorAsVal(sel?.[SourceOrExpr]);
202
- } else if (sel === undefined) {
203
- return null;
204
- }
205
- return sel;
206
- }
207
-
208
- export function createValPathOfItem(
209
- arrayPath: SourcePath | undefined,
210
- prop: string | number | symbol
211
- ) {
212
- if (typeof prop === "symbol") {
213
- throw Error(
214
- `Cannot create val path of array item with symbol prop: ${prop.toString()}`
215
- );
216
- }
217
- return arrayPath && (`${arrayPath}.${JSON.stringify(prop)}` as SourcePath);
218
- }
219
-
220
- export function selectorToVal(s: any): any {
221
- const v = selectorAsVal(s?.[SourceOrExpr]);
222
- return {
223
- val: v,
224
- [Path]: s?.[Path],
225
- };
226
- }
227
-
228
- // TODO: could we do .val on the objects instead?
229
- function unValify(valueOrSelector: any) {
230
- if (
231
- typeof valueOrSelector === "object" &&
232
- (SourceOrExpr in valueOrSelector || Path in valueOrSelector)
233
- ) {
234
- const selectorValue = valueOrSelector[SourceOrExpr];
235
- return selectorValue;
236
- }
237
- return valueOrSelector;
238
- }
@@ -1,37 +0,0 @@
1
- import {
2
- Selector as UnknownSelector,
3
- GenericSelector,
4
- SelectorOf,
5
- SelectorSource,
6
- } from ".";
7
- import { Schema } from "../../schema";
8
- import { Source, SourceArray } from "../../source";
9
- import { Selector as BooleanSelector } from "./boolean";
10
- import { Selector as NumberSelector } from "./number";
11
-
12
- export type UndistributedSourceArray<T extends SourceArray> = [T] extends [
13
- infer U // infer here to avoid Type instantiation is excessively deep and possibly infinite. See: https://github.com/microsoft/TypeScript/issues/30188#issuecomment-478938437. Avoiding infer extends to keep us below TS 4.9 compat
14
- ]
15
- ? U extends Source[]
16
- ? Selector<U>
17
- : never
18
- : never;
19
-
20
- // TODO: docs
21
- export type Selector<T extends SourceArray> = GenericSelector<T> & {
22
- readonly [key: number]: UnknownSelector<T[number]>;
23
- } & {
24
- length: NumberSelector<number>;
25
- filter(
26
- predicate: (
27
- v: UnknownSelector<T[number]>
28
- ) => BooleanSelector<boolean> | boolean
29
- ): Selector<T>;
30
- filter<U extends Source>(schema: Schema<U>): Selector<U[]>;
31
- map<U extends SelectorSource>(
32
- f: (v: UnknownSelector<T[number]>, i: UnknownSelector<number>) => U
33
- ): SelectorOf<U[]>; // TODO: this should be SelectorOf<ArraySelectorSourceBranded<U[]>>;
34
- andThen<U extends SelectorSource>(
35
- f: (v: UnknownSelector<NonNullable<T>>) => U
36
- ): SelectorOf<U | T>;
37
- };
@@ -1,4 +0,0 @@
1
- import { Selector as PrimitiveSelector } from "./primitive";
2
-
3
- // TODO: docs
4
- export type Selector<T extends boolean> = PrimitiveSelector<T>;
@@ -1,14 +0,0 @@
1
- import {
2
- Selector as UnknownSelector,
3
- GenericSelector,
4
- SelectorOf,
5
- SelectorSource,
6
- } from ".";
7
-
8
- // TODO: docs
9
- export type FileSelector = GenericSelector<{ url: string }> & {
10
- readonly url: UnknownSelector<string>;
11
- andThen<U extends SelectorSource>(
12
- f: (v: UnknownSelector<NonNullable<{ url: string }>>) => U
13
- ): SelectorOf<U> | UnknownSelector<boolean>;
14
- };
@@ -1,13 +0,0 @@
1
- import { SourceArray, SourceObject, SourcePrimitive } from "../../source";
2
- import { Selector as UnknownSelector } from ".";
3
- import { FileSource } from "../../source/file";
4
-
5
- declare const brand: unique symbol;
6
-
7
- export type I18nSelector<
8
- Locales extends readonly string[],
9
- T extends SourcePrimitive | SourceObject | SourceArray | FileSource
10
- > = UnknownSelector<T> & {
11
- readonly [brand]: "I18nSelector";
12
- all(): { [locale in Locales[number]]: UnknownSelector<T> };
13
- };
@@ -1,169 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
- import { I18nSelector } from "./i18n";
3
- import { Selector as ObjectSelector } from "./object";
4
- import { UndistributedSourceArray as ArraySelector } from "./array";
5
- import { Selector as NumberSelector } from "./number";
6
- import { Selector as StringSelector } from "./string";
7
- import { Selector as BooleanSelector } from "./boolean";
8
- import { Selector as PrimitiveSelector } from "./primitive";
9
- import { FileSelector } from "./file";
10
- import { SourcePath } from "../../val";
11
- import {
12
- Source,
13
- SourceArray,
14
- SourceObject,
15
- SourcePrimitive,
16
- } from "../../source";
17
- import { Schema } from "../../schema";
18
- import { Expr } from "../../expr/expr";
19
- import { RemoteSelector } from "./remote";
20
- import type { A } from "ts-toolbelt";
21
- import { I18nSource, I18nCompatibleSource } from "../../source/future/i18n";
22
- import {
23
- RemoteCompatibleSource,
24
- RemoteSource,
25
- } from "../../source/future/remote";
26
- import { FileSource } from "../../source/file";
27
- import {
28
- AnyRichTextOptions,
29
- RichText,
30
- RichTextSource,
31
- } from "../../source/richtext";
32
-
33
- /**
34
- * Selectors can be used to select parts of a Val module.
35
- * Unlike queries, joins, aggregates etc is and will not be supported.
36
- *
37
- * They are designed to be be used as if they were "normal" JSON data,
38
- * though some concessions had to be made because of TypeScript limitations.
39
- *
40
- * Selectors works equally on source content, defined in code, and remote content.
41
- *
42
- * @example
43
- * // Select the title of a document
44
- * const titles = useVal(docsVal.map((doc) => doc.title));
45
- *
46
- * @example
47
- * // Match on a union type
48
- * const titles = useVal(docsVal.map((doc) => doc.fold("type")({
49
- * newsletter: (newsletter) => newsletter.title,
50
- * email: (email) => email.subject,
51
- * }));
52
- *
53
- */
54
- export type Selector<T extends Source> = Source extends T
55
- ? GenericSelector<T>
56
- : T extends I18nSource<infer L, infer S>
57
- ? I18nSelector<L, S>
58
- : T extends RemoteSource<infer S>
59
- ? S extends RemoteCompatibleSource
60
- ? RemoteSelector<S>
61
- : GenericSelector<Source, "Could not determine remote source">
62
- : T extends FileSource
63
- ? FileSelector
64
- : T extends RichTextSource<infer O>
65
- ? RichText<O>
66
- : T extends SourceObject
67
- ? ObjectSelector<T>
68
- : T extends SourceArray
69
- ? ArraySelector<T>
70
- : T extends string
71
- ? StringSelector<T>
72
- : T extends number
73
- ? NumberSelector<T>
74
- : T extends boolean
75
- ? BooleanSelector<T>
76
- : T extends null
77
- ? PrimitiveSelector<null>
78
- : never;
79
-
80
- export type SelectorSource =
81
- | SourcePrimitive
82
- | undefined
83
- | readonly SelectorSource[]
84
- | {
85
- [key: string]: SelectorSource;
86
- }
87
- | I18nSource<readonly string[], I18nCompatibleSource>
88
- | RemoteSource<RemoteCompatibleSource>
89
- | FileSource
90
- | RichTextSource<AnyRichTextOptions>
91
- | GenericSelector<Source>;
92
-
93
- /**
94
- * @internal
95
- */
96
- export const GetSchema = Symbol("GetSchema");
97
- /**
98
- /**
99
- * @internal
100
- */
101
- export const Path = Symbol("Path");
102
- /**
103
- * @internal
104
- */
105
- export const SourceOrExpr = Symbol("SourceOrExpr");
106
- /**
107
- * @internal
108
- */
109
- export const ValError = Symbol("ValError");
110
- export abstract class GenericSelector<
111
- out T extends Source,
112
- Error extends string | undefined = undefined
113
- > {
114
- readonly [Path]: SourcePath | undefined;
115
- readonly [SourceOrExpr]: T | Expr;
116
- readonly [ValError]: Error | undefined;
117
- readonly [GetSchema]: Schema<T> | undefined;
118
- constructor(
119
- valOrExpr: T,
120
- path: SourcePath | undefined,
121
- schema?: Schema<T>,
122
- error?: Error
123
- ) {
124
- this[Path] = path;
125
- this[SourceOrExpr] = valOrExpr;
126
- this[ValError] = error;
127
- this[GetSchema] = schema;
128
- }
129
-
130
- assert<U extends Source, E extends Source = null>(
131
- schema: Schema<U>,
132
- other?: () => E
133
- ): SelectorOf<U | E> {
134
- throw new Error("Not implemented");
135
- }
136
- }
137
-
138
- export type SourceOf<T extends SelectorSource> = Source extends T
139
- ? Source
140
- : T extends Source
141
- ? T
142
- : T extends undefined
143
- ? null
144
- : T extends GenericSelector<infer S>
145
- ? S
146
- : T extends readonly (infer S)[] // NOTE: the infer S instead of Selector Source here, is to avoid infinite recursion
147
- ? S extends SelectorSource
148
- ? {
149
- [key in keyof T]: SourceOf<A.Try<T[key], SelectorSource>>;
150
- }
151
- : never
152
- : T extends { [key: string]: SelectorSource }
153
- ? {
154
- [key in keyof T]: SourceOf<A.Try<T[key], SelectorSource>>;
155
- }
156
- : never;
157
-
158
- /**
159
- * Use this type to convert types that accepts both Source and Selectors
160
- *
161
- * An example would be where literals are supported like in most higher order functions (e.g. map in array)
162
- **/
163
- export type SelectorOf<U extends SelectorSource> = Source extends U
164
- ? GenericSelector<Source>
165
- : SourceOf<U> extends infer S // we need this to avoid infinite recursion
166
- ? S extends Source
167
- ? Selector<S>
168
- : GenericSelector<Source, "Could not determine selector of source">
169
- : GenericSelector<Source, "Could not determine source">;
@@ -1,4 +0,0 @@
1
- import { Selector as PrimitiveSelector } from "./primitive";
2
-
3
- // TODO:
4
- export type Selector<T extends number> = PrimitiveSelector<T>;
@@ -1,22 +0,0 @@
1
- import {
2
- Selector as UnknownSelector,
3
- GenericSelector,
4
- SelectorOf,
5
- SelectorSource,
6
- } from ".";
7
- import { SourceObject } from "../../source";
8
-
9
- // TODO: docs
10
- export type Selector<T extends SourceObject> = GenericSelector<T> & {
11
- fold<Tag extends string>(
12
- key: Tag
13
- ): <U extends SelectorSource>(cases: {
14
- [key in T[Tag & keyof T] & string]: (v: UnknownSelector<T>) => U;
15
- }) => SelectorOf<U>;
16
-
17
- andThen<U extends SelectorSource>(
18
- f: (v: UnknownSelector<NonNullable<T>>) => U
19
- ): SelectorOf<U>;
20
- } & {
21
- readonly [key in keyof T]: UnknownSelector<T[key]>;
22
- };
@@ -1,17 +0,0 @@
1
- import {
2
- Selector as UnknownSelector,
3
- GenericSelector,
4
- SelectorOf,
5
- SelectorSource,
6
- } from ".";
7
- import { Source, SourcePrimitive } from "../../source";
8
- import { Selector as BooleanSelector } from "./boolean";
9
-
10
- export type Selector<T extends SourcePrimitive> = GenericSelector<T> & {
11
- eq(other: Source): BooleanSelector<boolean>;
12
- andThen<U extends SelectorSource>(
13
- f: (v: UnknownSelector<NonNullable<T>>) => U
14
- ): SelectorOf<U | NullableOf<T>>;
15
- };
16
-
17
- type NullableOf<T extends Source> = T extends null ? null : never;