Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 4x 4x 4x 16x 4x 6x 4x 13x 5x 8x 5x 3x 4x 10x 10x 13x 10x 10x 2x 8x 4x 3x 3x 4x 2x 1x 1x | import type { ScriptEventFieldSchema } from "shared/lib/entities/entitiesTypes";
import type { VariableType } from "shared/lib/resources/types";
import type {
ScriptValue,
ScriptVariableElement,
ScriptValueVariable,
} from "shared/lib/scriptValue/types";
import { isScriptVariableElement } from "shared/lib/scriptValue/types";
type VariableFieldType = NonNullable<ScriptEventFieldSchema["variableType"]>;
export interface VariableFieldCandidate {
id: string;
type: VariableType;
length?: number;
}
const arrayVariableTypes: VariableType[] = ["array"];
export const allowedVariableTypesForFieldType = (
type: VariableFieldType,
): VariableType[] | undefined =>
type === "any" ? undefined : arrayVariableTypes;
export const variableTypeAllowsIndex = (type: VariableFieldType): boolean =>
type !== "arrayReference";
export const variableValueForType = (
type: VariableFieldType,
variableId: string,
index: ScriptValue,
isArray: boolean,
): string | ScriptValueVariable => {
if (type === "arrayReference") {
return {
type: "variable",
value: variableId,
};
}
if (type === "arrayElement" || isArray) {
return {
type: "variable",
value: variableId,
index,
};
}
return variableId;
};
export const defaultVariableValueForType = (
type: VariableFieldType,
candidates: VariableFieldCandidate[],
preferredVariableId?: string,
): string | ScriptValueVariable | undefined => {
const allowedTypes = allowedVariableTypesForFieldType(type);
const compatibleCandidates = candidates.filter(
(candidate) => !allowedTypes || allowedTypes.includes(candidate.type),
);
const candidate =
compatibleCandidates.find(({ id }) => id === preferredVariableId) ??
compatibleCandidates[0];
if (!candidate) {
return undefined;
}
return variableValueForType(
type,
candidate.id,
{ type: "number", value: 0 },
candidate.type === "array",
);
};
export const defaultValueForUnionType = (
field: ScriptEventFieldSchema,
type: string,
defaultVariableId: string,
): unknown => {
const defaultValue =
typeof field.defaultValue === "object" && field.defaultValue !== null
? (field.defaultValue as Record<string, unknown>)[type]
: undefined;
return defaultValue === "LAST_VARIABLE" ? defaultVariableId : defaultValue;
};
export const defaultVariableElementValue = (
defaultValue: unknown,
defaultVariableId: string,
): ScriptVariableElement | undefined => {
if (!isScriptVariableElement(defaultValue)) {
return undefined;
}
return defaultValue.value === "LAST_VARIABLE"
? { ...defaultValue, value: defaultVariableId }
: defaultValue;
};
|