@player-tools/dsl 0.11.0 → 0.12.1--canary.213.4811
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/cjs/index.cjs +102 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.legacy-esm.js +92 -1
- package/dist/index.mjs +92 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/helpers/mock-data-refs.ts +1 -1
- package/src/__tests__/util.test.tsx +196 -8
- package/src/compiler/schema.ts +15 -7
- package/src/expressions/__tests__/native.test.ts +159 -0
- package/src/expressions/__tests__/testing.test.ts +38 -0
- package/src/expressions/native.ts +141 -0
- package/src/expressions/testing.ts +40 -0
- package/src/index.ts +1 -0
- package/src/string-templates/index.ts +20 -10
- package/src/types.ts +27 -1
- package/src/utils.tsx +80 -6
- package/types/compiler/schema.d.ts +5 -2
- package/types/expressions/native.d.ts +50 -0
- package/types/expressions/testing.d.ts +13 -0
- package/types/index.d.ts +1 -0
- package/types/string-templates/index.d.ts +9 -7
- package/types/types.d.ts +11 -1
- package/types/utils.d.ts +22 -3
|
@@ -22,9 +22,11 @@ export interface TemplateInstanceRefStringOptions {
|
|
|
22
22
|
) => string;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
const OpaqueIdentifier = Symbol("TemplateStringType");
|
|
25
|
+
const OpaqueIdentifier: unique symbol = Symbol("TemplateStringType");
|
|
26
26
|
|
|
27
|
-
export type TemplateStringType
|
|
27
|
+
export type TemplateStringType<
|
|
28
|
+
T extends string | number | boolean | unknown = any,
|
|
29
|
+
> = React.ReactElement & {
|
|
28
30
|
/** An identifier to show that this is a template type */
|
|
29
31
|
[OpaqueIdentifier]: true;
|
|
30
32
|
/** The value of the template string when in another string */
|
|
@@ -32,15 +34,21 @@ export type TemplateStringType = React.ReactElement & {
|
|
|
32
34
|
/** the raw value of the template string */
|
|
33
35
|
toValue: () => string;
|
|
34
36
|
/** the dereferenced value when used in another */
|
|
35
|
-
toRefString: (options?: TemplateRefStringOptions) =>
|
|
37
|
+
toRefString: (options?: TemplateRefStringOptions) => T;
|
|
38
|
+
/** Underlying type of this binding */
|
|
39
|
+
type: T;
|
|
36
40
|
};
|
|
37
41
|
|
|
38
|
-
export type BindingTemplateInstance
|
|
42
|
+
export type BindingTemplateInstance<
|
|
43
|
+
DataType extends string | number | boolean | unknown = any,
|
|
44
|
+
> = TemplateStringType<DataType> & {
|
|
39
45
|
/** An identifier for a binding instance */
|
|
40
46
|
__type: "binding";
|
|
41
47
|
};
|
|
42
48
|
|
|
43
|
-
export type ExpressionTemplateInstance
|
|
49
|
+
export type ExpressionTemplateInstance<
|
|
50
|
+
ReturnType extends string | number | boolean | unknown = any,
|
|
51
|
+
> = TemplateStringType<ReturnType> & {
|
|
44
52
|
/** The identifier for an expression instance */
|
|
45
53
|
__type: "expression";
|
|
46
54
|
};
|
|
@@ -49,7 +57,9 @@ export type ExpressionTemplateInstance = TemplateStringType & {
|
|
|
49
57
|
export const TemplateStringComponent = (props: {
|
|
50
58
|
/** The string value of the child template string */
|
|
51
59
|
value: string;
|
|
52
|
-
})
|
|
60
|
+
}): React.ReactElement<{
|
|
61
|
+
value: string;
|
|
62
|
+
}> => {
|
|
53
63
|
return React.createElement(
|
|
54
64
|
"value",
|
|
55
65
|
{
|
|
@@ -179,10 +189,10 @@ const createExpressionTemplateInstance = (
|
|
|
179
189
|
};
|
|
180
190
|
|
|
181
191
|
/** A tagged-template constructor for a binding */
|
|
182
|
-
export const binding = (
|
|
192
|
+
export const binding = <T>(
|
|
183
193
|
strings: TemplateStringsArray,
|
|
184
194
|
...nested: Array<TemplateStringType | string>
|
|
185
|
-
): BindingTemplateInstance => {
|
|
195
|
+
): BindingTemplateInstance<T> => {
|
|
186
196
|
return createBindingTemplateInstance({
|
|
187
197
|
strings,
|
|
188
198
|
other: nested,
|
|
@@ -191,12 +201,12 @@ export const binding = (
|
|
|
191
201
|
};
|
|
192
202
|
|
|
193
203
|
/** A tagged-template constructor for an expression */
|
|
194
|
-
export const expression = (
|
|
204
|
+
export const expression = <T>(
|
|
195
205
|
strings: TemplateStringsArray,
|
|
196
206
|
...nested: Array<
|
|
197
207
|
ExpressionTemplateInstance | BindingTemplateInstance | string
|
|
198
208
|
>
|
|
199
|
-
): ExpressionTemplateInstance => {
|
|
209
|
+
): ExpressionTemplateInstance<T> => {
|
|
200
210
|
return createExpressionTemplateInstance({
|
|
201
211
|
strings,
|
|
202
212
|
other: nested,
|
package/src/types.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
BindingTemplateInstance,
|
|
10
10
|
ExpressionTemplateInstance,
|
|
11
11
|
} from "./string-templates";
|
|
12
|
+
import { ExpressionHandler } from "@player-ui/player";
|
|
12
13
|
|
|
13
14
|
export type WithChildren<T = Record<string, unknown>> = T & {
|
|
14
15
|
/** child nodes */
|
|
@@ -56,12 +57,21 @@ export type SwapKeysToType<T, K extends keyof T, NewType> = {
|
|
|
56
57
|
[P in keyof T]: P extends K ? NewType : T[P];
|
|
57
58
|
};
|
|
58
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Note: have to explicitly handle boolean cases, otherwise boolean gets distributed to
|
|
62
|
+
* the union as true | false breaking the mapping.
|
|
63
|
+
*/
|
|
59
64
|
export type WithTemplateTypes<T> =
|
|
60
65
|
T extends Record<any, any>
|
|
61
66
|
? {
|
|
62
67
|
[P in keyof T]: WithTemplateTypes<T[P]>;
|
|
63
68
|
}
|
|
64
|
-
: T
|
|
69
|
+
: T extends boolean
|
|
70
|
+
?
|
|
71
|
+
| boolean
|
|
72
|
+
| BindingTemplateInstance<boolean>
|
|
73
|
+
| ExpressionTemplateInstance<boolean>
|
|
74
|
+
: T | BindingTemplateInstance<T> | ExpressionTemplateInstance<T>;
|
|
65
75
|
|
|
66
76
|
type ValidKeys = "exp" | "onStart" | "onEnd";
|
|
67
77
|
|
|
@@ -115,6 +125,10 @@ export type DataTypeRefs<
|
|
|
115
125
|
[Property in Extract<keyof DataTypeObjects, string> as `${Property}Ref`]: {
|
|
116
126
|
/** DataType name */
|
|
117
127
|
type: Property;
|
|
128
|
+
|
|
129
|
+
default?: DataTypeObjects[Property] extends Schema.DataType<infer BaseType>
|
|
130
|
+
? BaseType
|
|
131
|
+
: unknown;
|
|
118
132
|
};
|
|
119
133
|
};
|
|
120
134
|
|
|
@@ -149,3 +163,15 @@ export interface DSLSchema<DataTypeRef = DataTypeReference> {
|
|
|
149
163
|
| [DSLSchema<DataTypeRef>]
|
|
150
164
|
| DSLSchema<DataTypeRef>;
|
|
151
165
|
}
|
|
166
|
+
|
|
167
|
+
type ExpressionHandlerToFunction<T extends ExpressionHandler> =
|
|
168
|
+
T extends ExpressionHandler<infer A, infer B>
|
|
169
|
+
? (...args: WithTemplateTypes<A>) => ExpressionTemplateInstance<B>
|
|
170
|
+
: undefined;
|
|
171
|
+
|
|
172
|
+
export type ExpressionArray<T> =
|
|
173
|
+
T extends Record<any, any>
|
|
174
|
+
? {
|
|
175
|
+
[P in keyof T]: ExpressionHandlerToFunction<T[P]>;
|
|
176
|
+
}
|
|
177
|
+
: undefined;
|
package/src/utils.tsx
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import {
|
|
3
|
+
expression,
|
|
4
|
+
ExpressionTemplateInstance,
|
|
3
5
|
isTemplateStringInstance,
|
|
4
6
|
TemplateStringComponent,
|
|
5
7
|
} from "./string-templates";
|
|
6
|
-
import type {
|
|
8
|
+
import type {
|
|
9
|
+
ExpressionArray,
|
|
10
|
+
toJsonOptions,
|
|
11
|
+
WithTemplateTypes,
|
|
12
|
+
} from "./types";
|
|
13
|
+
import { Schema } from "@player-ui/types";
|
|
14
|
+
import { ExpressionHandler } from "@player-ui/player";
|
|
7
15
|
|
|
8
16
|
/** Get an array version of the value */
|
|
9
17
|
export function toArray<T>(val: T | Array<T>): Array<T> {
|
|
@@ -57,7 +65,7 @@ export function toJsonElement(
|
|
|
57
65
|
export function toJsonProperties(
|
|
58
66
|
value: Record<string, any>,
|
|
59
67
|
options: toJsonOptions = { propertiesToSkip: ["applicability"] },
|
|
60
|
-
) {
|
|
68
|
+
): React.JSX.Element[] {
|
|
61
69
|
return Object.keys(value).map((key) => {
|
|
62
70
|
return (
|
|
63
71
|
<property key={key} name={key}>
|
|
@@ -106,7 +114,7 @@ export function normalizeToCollection(options: {
|
|
|
106
114
|
|
|
107
115
|
/** A collection asset */
|
|
108
116
|
CollectionComp?: React.ComponentType<any>;
|
|
109
|
-
}) {
|
|
117
|
+
}): React.ReactNode {
|
|
110
118
|
const { node, CollectionComp } = options;
|
|
111
119
|
|
|
112
120
|
if (
|
|
@@ -167,11 +175,17 @@ export function mergeRefs<T = any>(
|
|
|
167
175
|
};
|
|
168
176
|
}
|
|
169
177
|
|
|
178
|
+
type TypesToReferences<T> = {
|
|
179
|
+
[P in keyof T]: T[P] extends Schema.DataType<infer DT> ? DT : unknown;
|
|
180
|
+
};
|
|
181
|
+
|
|
170
182
|
/** Generates object reference properties from the provided object */
|
|
171
183
|
export function getObjectReferences<
|
|
172
|
-
OriginalPropertiesObject
|
|
173
|
-
ReferencesPropertyObject
|
|
174
|
-
>(
|
|
184
|
+
OriginalPropertiesObject,
|
|
185
|
+
ReferencesPropertyObject,
|
|
186
|
+
>(
|
|
187
|
+
propertiesObject: OriginalPropertiesObject,
|
|
188
|
+
): TypesToReferences<ReferencesPropertyObject> {
|
|
175
189
|
const result: any = {};
|
|
176
190
|
|
|
177
191
|
for (const itemProp in propertiesObject) {
|
|
@@ -183,3 +197,63 @@ export function getObjectReferences<
|
|
|
183
197
|
|
|
184
198
|
return result;
|
|
185
199
|
}
|
|
200
|
+
|
|
201
|
+
function parseArg(arg: unknown, deref = false): any {
|
|
202
|
+
if (isTemplateStringInstance(arg)) {
|
|
203
|
+
return `'${deref ? arg.toRefString() : arg.toValue()}'`;
|
|
204
|
+
} else if (Array.isArray(arg)) {
|
|
205
|
+
return `[${arg.map((a) => parseArg(a, true)).join(", ")}]`;
|
|
206
|
+
} else if (typeof arg === "string") {
|
|
207
|
+
return `'${arg}'`;
|
|
208
|
+
} else {
|
|
209
|
+
return arg;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function generateDSLFunction<R>(
|
|
214
|
+
name: string,
|
|
215
|
+
args: Array<unknown>,
|
|
216
|
+
): ExpressionTemplateInstance<R> {
|
|
217
|
+
const expressionArgs: Array<unknown> = [];
|
|
218
|
+
args.forEach((arg) => {
|
|
219
|
+
expressionArgs.push(parseArg(arg));
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
return expression`${name}(${expressionArgs.join(", ")})`;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Convert an single ExpressionHandler function to a DSL expression function
|
|
227
|
+
*/
|
|
228
|
+
export function wrapFunctionInType<T extends Array<unknown>, R>(
|
|
229
|
+
fn: ExpressionHandler<T, R>,
|
|
230
|
+
): (...args: WithTemplateTypes<T>) => ExpressionTemplateInstance<R> {
|
|
231
|
+
return (...args: WithTemplateTypes<T>): ExpressionTemplateInstance<R> => {
|
|
232
|
+
return generateDSLFunction(fn.name, args);
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Create a DSL Expression generation function by name and args specified by
|
|
238
|
+
* generic parameters
|
|
239
|
+
*/
|
|
240
|
+
export function makeFunctionByName<T extends Array<unknown>, R>(
|
|
241
|
+
name: string,
|
|
242
|
+
): (...args: WithTemplateTypes<T>) => ExpressionTemplateInstance<R> {
|
|
243
|
+
return (...args: WithTemplateTypes<T>): ExpressionTemplateInstance<R> => {
|
|
244
|
+
return generateDSLFunction(name, args);
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Takes map of functions and wraps them in a DSL syntax generator
|
|
250
|
+
*/
|
|
251
|
+
export function mapExpressionHandlersToFunctions<
|
|
252
|
+
T extends Record<string, ExpressionHandler<any, any>>,
|
|
253
|
+
>(functions: T): ExpressionArray<T> {
|
|
254
|
+
const result: any = {};
|
|
255
|
+
for (const fn of Object.values(functions)) {
|
|
256
|
+
result[fn.name] = wrapFunctionInType(fn);
|
|
257
|
+
}
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
@@ -12,7 +12,10 @@ export declare class SchemaGenerator {
|
|
|
12
12
|
private generatedDataTypes;
|
|
13
13
|
private logger;
|
|
14
14
|
hooks: {
|
|
15
|
-
createSchemaNode: SyncWaterfallHook<[
|
|
15
|
+
createSchemaNode: SyncWaterfallHook<[
|
|
16
|
+
node: Schema.DataType<unknown>,
|
|
17
|
+
originalProperty: Record<string | symbol, unknown>
|
|
18
|
+
], Record<string, any>>;
|
|
16
19
|
};
|
|
17
20
|
constructor(logger?: LoggingInterface);
|
|
18
21
|
/**
|
|
@@ -41,7 +44,7 @@ export type MakeArrayIntoIndexRef<T extends any[]> = {
|
|
|
41
44
|
} & BindingTemplateInstance;
|
|
42
45
|
export type MakeBindingRefable<T> = {
|
|
43
46
|
[P in keyof T]: T[P] extends object[] ? MakeArrayIntoIndexRef<T[P]> : T[P] extends unknown[] ? T[P] : MakeBindingRefable<T[P]>;
|
|
44
|
-
} & BindingTemplateInstance
|
|
47
|
+
} & BindingTemplateInstance<T>;
|
|
45
48
|
/**
|
|
46
49
|
* Adds bindings to an object so that the object can be directly used in JSX
|
|
47
50
|
*/
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BindingTemplateInstance, ExpressionTemplateInstance } from "..";
|
|
2
|
+
type Argument<T> = string | boolean | number | undefined | BindingTemplateInstance<T> | ExpressionTemplateInstance<T>;
|
|
3
|
+
/**
|
|
4
|
+
* Performs an assigment of a value to a binding by returning the expression
|
|
5
|
+
* {{<binding>}} = <value>
|
|
6
|
+
* @param binding
|
|
7
|
+
* @param value
|
|
8
|
+
*/
|
|
9
|
+
export declare const assign: <T>(binding: BindingTemplateInstance<any>, value: Argument<T>) => ExpressionTemplateInstance<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Returns an equality comparison between the two values
|
|
12
|
+
*/
|
|
13
|
+
export declare const equals: <A, B>(a: Argument<A>, b: Argument<B>) => ExpressionTemplateInstance<boolean>;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the negated version of the binding/expression
|
|
16
|
+
* by returning !(<value>)
|
|
17
|
+
* @param binding Binding/Expression to invert
|
|
18
|
+
* @returns Negated binding/expression
|
|
19
|
+
*/
|
|
20
|
+
export declare const not: (value: BindingTemplateInstance<boolean> | ExpressionTemplateInstance<boolean>) => ExpressionTemplateInstance<boolean>;
|
|
21
|
+
/**
|
|
22
|
+
* Creates an expression for the logical or'ing of the provided values
|
|
23
|
+
* e.g: <exp1> || <exp2> || ...
|
|
24
|
+
* @param values Array of bindings/expressions to logically or
|
|
25
|
+
* @returns boolean
|
|
26
|
+
*/
|
|
27
|
+
export declare const or: (...values: Array<BindingTemplateInstance<boolean> | ExpressionTemplateInstance<boolean>>) => ExpressionTemplateInstance<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an expression for the logical nor'ing of the provided values
|
|
30
|
+
* e.g: !(<exp1> || <exp2> || ...)
|
|
31
|
+
* @param values Array of bindings/expressions to logically nor
|
|
32
|
+
* @returns boolean
|
|
33
|
+
*/
|
|
34
|
+
export declare const nor: (...values: Array<BindingTemplateInstance<boolean> | ExpressionTemplateInstance<boolean>>) => ExpressionTemplateInstance<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* Creates an expression for the logical and'ing of the provided values
|
|
37
|
+
* e.g: <exp1> && <exp2> && ...
|
|
38
|
+
* @param values Array of bindings/expressions to logically and
|
|
39
|
+
* @returns boolean
|
|
40
|
+
*/
|
|
41
|
+
export declare const and: (...values: Array<BindingTemplateInstance<boolean> | ExpressionTemplateInstance<boolean>>) => ExpressionTemplateInstance<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Creates an expression for the logical nand'ing of the provided values
|
|
44
|
+
* e.g: !(<exp1> && <exp2> && ...)
|
|
45
|
+
* @param values Array of bindings/expressions to logically nand
|
|
46
|
+
* @returns boolean
|
|
47
|
+
*/
|
|
48
|
+
export declare const nand: (...values: Array<BindingTemplateInstance<boolean> | ExpressionTemplateInstance<boolean>>) => ExpressionTemplateInstance<boolean>;
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=native.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ExpressionHandler } from "@player-ui/player";
|
|
2
|
+
import { ExpressionTemplateInstance } from "../string-templates";
|
|
3
|
+
/**
|
|
4
|
+
* Test harness to make testing expressions easier.
|
|
5
|
+
* Given an expreesion and an initial data model the harness will execute the expression
|
|
6
|
+
* on and return the new state of the data model.
|
|
7
|
+
* @param exp expression to execute
|
|
8
|
+
* @param initialData data model to operate on
|
|
9
|
+
* @param expressions expression handlers for functions that are called
|
|
10
|
+
* @returns Final data model state
|
|
11
|
+
*/
|
|
12
|
+
export declare function testExpression(exp: ExpressionTemplateInstance, initialData: object, expressions?: Map<string, ExpressionHandler<any[], any>>): object;
|
|
13
|
+
//# sourceMappingURL=testing.d.ts.map
|
package/types/index.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export interface TemplateInstanceRefStringOptions {
|
|
|
15
15
|
toRefString: (options: TemplateRefStringOptions | undefined, value: string) => string;
|
|
16
16
|
}
|
|
17
17
|
declare const OpaqueIdentifier: unique symbol;
|
|
18
|
-
export type TemplateStringType = React.ReactElement & {
|
|
18
|
+
export type TemplateStringType<T extends string | number | boolean | unknown = any> = React.ReactElement & {
|
|
19
19
|
/** An identifier to show that this is a template type */
|
|
20
20
|
[OpaqueIdentifier]: true;
|
|
21
21
|
/** The value of the template string when in another string */
|
|
@@ -23,13 +23,15 @@ export type TemplateStringType = React.ReactElement & {
|
|
|
23
23
|
/** the raw value of the template string */
|
|
24
24
|
toValue: () => string;
|
|
25
25
|
/** the dereferenced value when used in another */
|
|
26
|
-
toRefString: (options?: TemplateRefStringOptions) =>
|
|
26
|
+
toRefString: (options?: TemplateRefStringOptions) => T;
|
|
27
|
+
/** Underlying type of this binding */
|
|
28
|
+
type: T;
|
|
27
29
|
};
|
|
28
|
-
export type BindingTemplateInstance = TemplateStringType & {
|
|
30
|
+
export type BindingTemplateInstance<DataType extends string | number | boolean | unknown = any> = TemplateStringType<DataType> & {
|
|
29
31
|
/** An identifier for a binding instance */
|
|
30
32
|
__type: "binding";
|
|
31
33
|
};
|
|
32
|
-
export type ExpressionTemplateInstance = TemplateStringType & {
|
|
34
|
+
export type ExpressionTemplateInstance<ReturnType extends string | number | boolean | unknown = any> = TemplateStringType<ReturnType> & {
|
|
33
35
|
/** The identifier for an expression instance */
|
|
34
36
|
__type: "expression";
|
|
35
37
|
};
|
|
@@ -39,11 +41,11 @@ export declare const TemplateStringComponent: (props: {
|
|
|
39
41
|
value: string;
|
|
40
42
|
}) => React.ReactElement<{
|
|
41
43
|
value: string;
|
|
42
|
-
}
|
|
44
|
+
}>;
|
|
43
45
|
/** A tagged-template constructor for a binding */
|
|
44
|
-
export declare const binding: (strings: TemplateStringsArray, ...nested: Array<TemplateStringType | string>) => BindingTemplateInstance
|
|
46
|
+
export declare const binding: <T>(strings: TemplateStringsArray, ...nested: Array<TemplateStringType | string>) => BindingTemplateInstance<T>;
|
|
45
47
|
/** A tagged-template constructor for an expression */
|
|
46
|
-
export declare const expression: (strings: TemplateStringsArray, ...nested: Array<ExpressionTemplateInstance | BindingTemplateInstance | string>) => ExpressionTemplateInstance
|
|
48
|
+
export declare const expression: <T>(strings: TemplateStringsArray, ...nested: Array<ExpressionTemplateInstance | BindingTemplateInstance | string>) => ExpressionTemplateInstance<T>;
|
|
47
49
|
/** Check if a value is a template string */
|
|
48
50
|
export declare const isTemplateStringInstance: (val: unknown) => val is ExpressionTemplateInstance | BindingTemplateInstance;
|
|
49
51
|
/** Check if a value is a binding */
|
package/types/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Asset, Expression, Navigation as PlayerNav, Schema, Validation } from "@player-ui/types";
|
|
2
2
|
import type { BindingTemplateInstance, ExpressionTemplateInstance } from "./string-templates";
|
|
3
|
+
import { ExpressionHandler } from "@player-ui/player";
|
|
3
4
|
export type WithChildren<T = Record<string, unknown>> = T & {
|
|
4
5
|
/** child nodes */
|
|
5
6
|
children?: React.ReactNode;
|
|
@@ -24,9 +25,13 @@ export type AssetPropsWithChildren<T extends Asset> = WithChildren<WithTemplateT
|
|
|
24
25
|
export type SwapKeysToType<T, K extends keyof T, NewType> = {
|
|
25
26
|
[P in keyof T]: P extends K ? NewType : T[P];
|
|
26
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Note: have to explicitly handle boolean cases, otherwise boolean gets distributed to
|
|
30
|
+
* the union as true | false breaking the mapping.
|
|
31
|
+
*/
|
|
27
32
|
export type WithTemplateTypes<T> = T extends Record<any, any> ? {
|
|
28
33
|
[P in keyof T]: WithTemplateTypes<T[P]>;
|
|
29
|
-
} : T | BindingTemplateInstance | ExpressionTemplateInstance
|
|
34
|
+
} : T extends boolean ? boolean | BindingTemplateInstance<boolean> | ExpressionTemplateInstance<boolean> : T | BindingTemplateInstance<T> | ExpressionTemplateInstance<T>;
|
|
30
35
|
type ValidKeys = "exp" | "onStart" | "onEnd";
|
|
31
36
|
type DeepReplace<T, Old, New> = {
|
|
32
37
|
[P in keyof T]: T[P] extends Old ? P extends ValidKeys ? New : DeepReplace<T[P], Old, New> : T[P] extends (infer R)[] ? DeepReplace<R, Old, New>[] : T[P] extends object ? DeepReplace<T[P], Old, New> : Extract<T[P], Old> extends Old ? DeepReplace<Extract<T[P], object>, Old, New> | Exclude<T[P], Old | object> | New : T[P];
|
|
@@ -47,6 +52,7 @@ export type DataTypeRefs<DataTypeObjects extends Record<string, Schema.DataType>
|
|
|
47
52
|
[Property in Extract<keyof DataTypeObjects, string> as `${Property}Ref`]: {
|
|
48
53
|
/** DataType name */
|
|
49
54
|
type: Property;
|
|
55
|
+
default?: DataTypeObjects[Property] extends Schema.DataType<infer BaseType> ? BaseType : unknown;
|
|
50
56
|
};
|
|
51
57
|
};
|
|
52
58
|
export type ValidatorFunctionRefs<ValidatorObjects extends {
|
|
@@ -70,5 +76,9 @@ export type DataTypeReference<DataTypeProp = {
|
|
|
70
76
|
export interface DSLSchema<DataTypeRef = DataTypeReference> {
|
|
71
77
|
[key: string]: [DataTypeRef] | DataTypeRef | [DSLSchema<DataTypeRef>] | DSLSchema<DataTypeRef>;
|
|
72
78
|
}
|
|
79
|
+
type ExpressionHandlerToFunction<T extends ExpressionHandler> = T extends ExpressionHandler<infer A, infer B> ? (...args: WithTemplateTypes<A>) => ExpressionTemplateInstance<B> : undefined;
|
|
80
|
+
export type ExpressionArray<T> = T extends Record<any, any> ? {
|
|
81
|
+
[P in keyof T]: ExpressionHandlerToFunction<T[P]>;
|
|
82
|
+
} : undefined;
|
|
73
83
|
export {};
|
|
74
84
|
//# sourceMappingURL=types.d.ts.map
|
package/types/utils.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import
|
|
2
|
+
import { ExpressionTemplateInstance } from "./string-templates";
|
|
3
|
+
import type { ExpressionArray, toJsonOptions, WithTemplateTypes } from "./types";
|
|
4
|
+
import { Schema } from "@player-ui/types";
|
|
5
|
+
import { ExpressionHandler } from "@player-ui/player";
|
|
3
6
|
/** Get an array version of the value */
|
|
4
7
|
export declare function toArray<T>(val: T | Array<T>): Array<T>;
|
|
5
8
|
/** Create a component version */
|
|
@@ -21,7 +24,7 @@ export declare function normalizeToCollection(options: {
|
|
|
21
24
|
TextComp?: React.ComponentType;
|
|
22
25
|
/** A collection asset */
|
|
23
26
|
CollectionComp?: React.ComponentType<any>;
|
|
24
|
-
}):
|
|
27
|
+
}): React.ReactNode;
|
|
25
28
|
type ReactChildArray = ReturnType<typeof React.Children.toArray>;
|
|
26
29
|
/**
|
|
27
30
|
*
|
|
@@ -35,7 +38,23 @@ export declare function flattenChildren(children: React.ReactNode): ReactChildAr
|
|
|
35
38
|
* used in an esm environment
|
|
36
39
|
*/
|
|
37
40
|
export declare function mergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>): React.RefCallback<T>;
|
|
41
|
+
type TypesToReferences<T> = {
|
|
42
|
+
[P in keyof T]: T[P] extends Schema.DataType<infer DT> ? DT : unknown;
|
|
43
|
+
};
|
|
38
44
|
/** Generates object reference properties from the provided object */
|
|
39
|
-
export declare function getObjectReferences<OriginalPropertiesObject
|
|
45
|
+
export declare function getObjectReferences<OriginalPropertiesObject, ReferencesPropertyObject>(propertiesObject: OriginalPropertiesObject): TypesToReferences<ReferencesPropertyObject>;
|
|
46
|
+
/**
|
|
47
|
+
* Convert an single ExpressionHandler function to a DSL expression function
|
|
48
|
+
*/
|
|
49
|
+
export declare function wrapFunctionInType<T extends Array<unknown>, R>(fn: ExpressionHandler<T, R>): (...args: WithTemplateTypes<T>) => ExpressionTemplateInstance<R>;
|
|
50
|
+
/**
|
|
51
|
+
* Create a DSL Expression generation function by name and args specified by
|
|
52
|
+
* generic parameters
|
|
53
|
+
*/
|
|
54
|
+
export declare function makeFunctionByName<T extends Array<unknown>, R>(name: string): (...args: WithTemplateTypes<T>) => ExpressionTemplateInstance<R>;
|
|
55
|
+
/**
|
|
56
|
+
* Takes map of functions and wraps them in a DSL syntax generator
|
|
57
|
+
*/
|
|
58
|
+
export declare function mapExpressionHandlersToFunctions<T extends Record<string, ExpressionHandler<any, any>>>(functions: T): ExpressionArray<T>;
|
|
40
59
|
export {};
|
|
41
60
|
//# sourceMappingURL=utils.d.ts.map
|