react-native-divkit 0.1.0-alpha.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/LICENSE +176 -0
- package/README.md +340 -0
- package/dist/DivKit.d.ts +68 -0
- package/dist/DivKit.d.ts.map +1 -0
- package/dist/DivKit.js +400 -0
- package/dist/DivKit.js.map +1 -0
- package/dist/actions/array.d.ts +8 -0
- package/dist/actions/array.d.ts.map +1 -0
- package/dist/actions/array.js +139 -0
- package/dist/actions/array.js.map +1 -0
- package/dist/actions/copyToClipboard.d.ts +22 -0
- package/dist/actions/copyToClipboard.d.ts.map +1 -0
- package/dist/actions/copyToClipboard.js +63 -0
- package/dist/actions/copyToClipboard.js.map +1 -0
- package/dist/actions/dict.d.ts +6 -0
- package/dist/actions/dict.d.ts.map +1 -0
- package/dist/actions/dict.js +58 -0
- package/dist/actions/dict.js.map +1 -0
- package/dist/actions/index.d.ts +11 -0
- package/dist/actions/index.d.ts.map +1 -0
- package/dist/actions/index.js +11 -0
- package/dist/actions/index.js.map +1 -0
- package/dist/actions/updateStructure.d.ts +6 -0
- package/dist/actions/updateStructure.d.ts.map +1 -0
- package/dist/actions/updateStructure.js +116 -0
- package/dist/actions/updateStructure.js.map +1 -0
- package/dist/components/DivComponent.d.ts +29 -0
- package/dist/components/DivComponent.d.ts.map +1 -0
- package/dist/components/DivComponent.js +62 -0
- package/dist/components/DivComponent.js.map +1 -0
- package/dist/components/container/DivContainer.d.ts +26 -0
- package/dist/components/container/DivContainer.d.ts.map +1 -0
- package/dist/components/container/DivContainer.js +172 -0
- package/dist/components/container/DivContainer.js.map +1 -0
- package/dist/components/container/index.d.ts +3 -0
- package/dist/components/container/index.d.ts.map +1 -0
- package/dist/components/container/index.js +2 -0
- package/dist/components/container/index.js.map +1 -0
- package/dist/components/image/DivImage.d.ts +29 -0
- package/dist/components/image/DivImage.d.ts.map +1 -0
- package/dist/components/image/DivImage.js +122 -0
- package/dist/components/image/DivImage.js.map +1 -0
- package/dist/components/image/index.d.ts +3 -0
- package/dist/components/image/index.d.ts.map +1 -0
- package/dist/components/image/index.js +2 -0
- package/dist/components/image/index.js.map +1 -0
- package/dist/components/index.d.ts +14 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +11 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/state/DivState.d.ts +26 -0
- package/dist/components/state/DivState.d.ts.map +1 -0
- package/dist/components/state/DivState.js +121 -0
- package/dist/components/state/DivState.js.map +1 -0
- package/dist/components/state/index.d.ts +3 -0
- package/dist/components/state/index.d.ts.map +1 -0
- package/dist/components/state/index.js +2 -0
- package/dist/components/state/index.js.map +1 -0
- package/dist/components/text/DivText.d.ts +28 -0
- package/dist/components/text/DivText.d.ts.map +1 -0
- package/dist/components/text/DivText.js +143 -0
- package/dist/components/text/DivText.js.map +1 -0
- package/dist/components/text/index.d.ts +3 -0
- package/dist/components/text/index.d.ts.map +1 -0
- package/dist/components/text/index.js +2 -0
- package/dist/components/text/index.js.map +1 -0
- package/dist/components/utilities/Outer.d.ts +17 -0
- package/dist/components/utilities/Outer.d.ts.map +1 -0
- package/dist/components/utilities/Outer.js +210 -0
- package/dist/components/utilities/Outer.js.map +1 -0
- package/dist/components/utilities/Unknown.d.ts +11 -0
- package/dist/components/utilities/Unknown.d.ts.map +1 -0
- package/dist/components/utilities/Unknown.js +50 -0
- package/dist/components/utilities/Unknown.js.map +1 -0
- package/dist/components/utilities/index.d.ts +5 -0
- package/dist/components/utilities/index.d.ts.map +1 -0
- package/dist/components/utilities/index.js +3 -0
- package/dist/components/utilities/index.js.map +1 -0
- package/dist/context/ActionContext.d.ts +25 -0
- package/dist/context/ActionContext.d.ts.map +1 -0
- package/dist/context/ActionContext.js +20 -0
- package/dist/context/ActionContext.js.map +1 -0
- package/dist/context/DivKitContext.d.ts +33 -0
- package/dist/context/DivKitContext.d.ts.map +1 -0
- package/dist/context/DivKitContext.js +14 -0
- package/dist/context/DivKitContext.js.map +1 -0
- package/dist/context/EnabledContext.d.ts +31 -0
- package/dist/context/EnabledContext.d.ts.map +1 -0
- package/dist/context/EnabledContext.js +31 -0
- package/dist/context/EnabledContext.js.map +1 -0
- package/dist/context/StateContext.d.ts +57 -0
- package/dist/context/StateContext.d.ts.map +1 -0
- package/dist/context/StateContext.js +20 -0
- package/dist/context/StateContext.js.map +1 -0
- package/dist/context/index.d.ts +9 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +9 -0
- package/dist/context/index.js.map +1 -0
- package/dist/expressions/bigint.d.ts +8 -0
- package/dist/expressions/bigint.d.ts.map +1 -0
- package/dist/expressions/bigint.js +31 -0
- package/dist/expressions/bigint.js.map +1 -0
- package/dist/expressions/const.d.ts +15 -0
- package/dist/expressions/const.d.ts.map +1 -0
- package/dist/expressions/const.js +15 -0
- package/dist/expressions/const.js.map +1 -0
- package/dist/expressions/eval.d.ts +77 -0
- package/dist/expressions/eval.d.ts.map +1 -0
- package/dist/expressions/eval.js +459 -0
- package/dist/expressions/eval.js.map +1 -0
- package/dist/expressions/expressions.d.ts +7 -0
- package/dist/expressions/expressions.d.ts.map +1 -0
- package/dist/expressions/expressions.js +3191 -0
- package/dist/expressions/expressions.js.map +1 -0
- package/dist/expressions/funcs/array.d.ts +2 -0
- package/dist/expressions/funcs/array.d.ts.map +1 -0
- package/dist/expressions/funcs/array.js +381 -0
- package/dist/expressions/funcs/array.js.map +1 -0
- package/dist/expressions/funcs/colors.d.ts +2 -0
- package/dist/expressions/funcs/colors.d.ts.map +1 -0
- package/dist/expressions/funcs/colors.js +75 -0
- package/dist/expressions/funcs/colors.js.map +1 -0
- package/dist/expressions/funcs/customFuncs.d.ts +8 -0
- package/dist/expressions/funcs/customFuncs.d.ts.map +1 -0
- package/dist/expressions/funcs/customFuncs.js +114 -0
- package/dist/expressions/funcs/customFuncs.js.map +1 -0
- package/dist/expressions/funcs/datetime.d.ts +2 -0
- package/dist/expressions/funcs/datetime.d.ts.map +1 -0
- package/dist/expressions/funcs/datetime.js +182 -0
- package/dist/expressions/funcs/datetime.js.map +1 -0
- package/dist/expressions/funcs/dict.d.ts +2 -0
- package/dist/expressions/funcs/dict.d.ts.map +1 -0
- package/dist/expressions/funcs/dict.js +170 -0
- package/dist/expressions/funcs/dict.js.map +1 -0
- package/dist/expressions/funcs/funcs.d.ts +80 -0
- package/dist/expressions/funcs/funcs.d.ts.map +1 -0
- package/dist/expressions/funcs/funcs.js +146 -0
- package/dist/expressions/funcs/funcs.js.map +1 -0
- package/dist/expressions/funcs/index.d.ts +2 -0
- package/dist/expressions/funcs/index.d.ts.map +1 -0
- package/dist/expressions/funcs/index.js +23 -0
- package/dist/expressions/funcs/index.js.map +1 -0
- package/dist/expressions/funcs/interval.d.ts +2 -0
- package/dist/expressions/funcs/interval.d.ts.map +1 -0
- package/dist/expressions/funcs/interval.js +61 -0
- package/dist/expressions/funcs/interval.js.map +1 -0
- package/dist/expressions/funcs/math.d.ts +2 -0
- package/dist/expressions/funcs/math.d.ts.map +1 -0
- package/dist/expressions/funcs/math.js +324 -0
- package/dist/expressions/funcs/math.js.map +1 -0
- package/dist/expressions/funcs/std.d.ts +2 -0
- package/dist/expressions/funcs/std.d.ts.map +1 -0
- package/dist/expressions/funcs/std.js +293 -0
- package/dist/expressions/funcs/std.js.map +1 -0
- package/dist/expressions/funcs/stored.d.ts +4 -0
- package/dist/expressions/funcs/stored.d.ts.map +1 -0
- package/dist/expressions/funcs/stored.js +62 -0
- package/dist/expressions/funcs/stored.js.map +1 -0
- package/dist/expressions/funcs/strings.d.ts +2 -0
- package/dist/expressions/funcs/strings.d.ts.map +1 -0
- package/dist/expressions/funcs/strings.js +158 -0
- package/dist/expressions/funcs/strings.js.map +1 -0
- package/dist/expressions/funcs/trigonometry.d.ts +2 -0
- package/dist/expressions/funcs/trigonometry.d.ts.map +1 -0
- package/dist/expressions/funcs/trigonometry.js +92 -0
- package/dist/expressions/funcs/trigonometry.js.map +1 -0
- package/dist/expressions/json.d.ts +18 -0
- package/dist/expressions/json.d.ts.map +1 -0
- package/dist/expressions/json.js +271 -0
- package/dist/expressions/json.js.map +1 -0
- package/dist/expressions/parserCache.d.ts +4 -0
- package/dist/expressions/parserCache.d.ts.map +1 -0
- package/dist/expressions/parserCache.js +23 -0
- package/dist/expressions/parserCache.js.map +1 -0
- package/dist/expressions/simpleUnescapeString.d.ts +2 -0
- package/dist/expressions/simpleUnescapeString.d.ts.map +1 -0
- package/dist/expressions/simpleUnescapeString.js +61 -0
- package/dist/expressions/simpleUnescapeString.js.map +1 -0
- package/dist/expressions/utils.d.ts +29 -0
- package/dist/expressions/utils.d.ts.map +1 -0
- package/dist/expressions/utils.js +236 -0
- package/dist/expressions/utils.js.map +1 -0
- package/dist/expressions/variable.d.ts +82 -0
- package/dist/expressions/variable.d.ts.map +1 -0
- package/dist/expressions/variable.js +337 -0
- package/dist/expressions/variable.js.map +1 -0
- package/dist/expressions/walk.d.ts +7 -0
- package/dist/expressions/walk.d.ts.map +1 -0
- package/dist/expressions/walk.js +39 -0
- package/dist/expressions/walk.js.map +1 -0
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +11 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useAction.d.ts +102 -0
- package/dist/hooks/useAction.d.ts.map +1 -0
- package/dist/hooks/useAction.js +116 -0
- package/dist/hooks/useAction.js.map +1 -0
- package/dist/hooks/useDerivedFromVars.d.ts +72 -0
- package/dist/hooks/useDerivedFromVars.d.ts.map +1 -0
- package/dist/hooks/useDerivedFromVars.js +100 -0
- package/dist/hooks/useDerivedFromVars.js.map +1 -0
- package/dist/hooks/useVariable.d.ts +86 -0
- package/dist/hooks/useVariable.d.ts.map +1 -0
- package/dist/hooks/useVariable.js +130 -0
- package/dist/hooks/useVariable.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/stores/createObservable.d.ts +38 -0
- package/dist/stores/createObservable.d.ts.map +1 -0
- package/dist/stores/createObservable.js +49 -0
- package/dist/stores/createObservable.js.map +1 -0
- package/dist/utils/applyTemplate.d.ts +8 -0
- package/dist/utils/applyTemplate.d.ts.map +1 -0
- package/dist/utils/applyTemplate.js +94 -0
- package/dist/utils/applyTemplate.js.map +1 -0
- package/dist/utils/correctColor.d.ts +18 -0
- package/dist/utils/correctColor.d.ts.map +1 -0
- package/dist/utils/correctColor.js +79 -0
- package/dist/utils/correctColor.js.map +1 -0
- package/dist/utils/escapeRegExp.d.ts +2 -0
- package/dist/utils/escapeRegExp.d.ts.map +1 -0
- package/dist/utils/escapeRegExp.js +4 -0
- package/dist/utils/escapeRegExp.js.map +1 -0
- package/dist/utils/formatDate.d.ts +6 -0
- package/dist/utils/formatDate.d.ts.map +1 -0
- package/dist/utils/formatDate.js +325 -0
- package/dist/utils/formatDate.js.map +1 -0
- package/dist/utils/padLeft.d.ts +2 -0
- package/dist/utils/padLeft.d.ts.map +1 -0
- package/dist/utils/padLeft.js +7 -0
- package/dist/utils/padLeft.js.map +1 -0
- package/dist/utils/uniq.d.ts +2 -0
- package/dist/utils/uniq.d.ts.map +1 -0
- package/dist/utils/uniq.js +4 -0
- package/dist/utils/uniq.js.map +1 -0
- package/dist/utils/wrapError.d.ts +10 -0
- package/dist/utils/wrapError.d.ts.map +1 -0
- package/dist/utils/wrapError.js +9 -0
- package/dist/utils/wrapError.js.map +1 -0
- package/package.json +58 -0
- package/src/DivKit.tsx +542 -0
- package/src/actions/array.ts +170 -0
- package/src/actions/copyToClipboard.ts +82 -0
- package/src/actions/dict.ts +71 -0
- package/src/actions/index.ts +11 -0
- package/src/actions/updateStructure.ts +134 -0
- package/src/components/DivComponent.tsx +75 -0
- package/src/components/README.md +230 -0
- package/src/components/container/DivContainer.tsx +222 -0
- package/src/components/container/index.ts +2 -0
- package/src/components/image/DivImage.tsx +172 -0
- package/src/components/image/index.ts +2 -0
- package/src/components/index.ts +20 -0
- package/src/components/state/DivState.tsx +146 -0
- package/src/components/state/index.ts +2 -0
- package/src/components/text/DivText.tsx +186 -0
- package/src/components/text/index.ts +2 -0
- package/src/components/utilities/Outer.tsx +239 -0
- package/src/components/utilities/README.md +175 -0
- package/src/components/utilities/Unknown.tsx +60 -0
- package/src/components/utilities/index.ts +4 -0
- package/src/context/ActionContext.tsx +37 -0
- package/src/context/DivKitContext.tsx +54 -0
- package/src/context/EnabledContext.tsx +50 -0
- package/src/context/StateContext.tsx +75 -0
- package/src/context/index.ts +33 -0
- package/src/expressions/ast.d.ts +101 -0
- package/src/expressions/bigint.ts +38 -0
- package/src/expressions/const.ts +16 -0
- package/src/expressions/eval.ts +669 -0
- package/src/expressions/expressions.peggy +235 -0
- package/src/expressions/expressions.ts +2854 -0
- package/src/expressions/funcs/array.ts +412 -0
- package/src/expressions/funcs/colors.ts +100 -0
- package/src/expressions/funcs/customFuncs.ts +139 -0
- package/src/expressions/funcs/datetime.ts +232 -0
- package/src/expressions/funcs/dict.ts +207 -0
- package/src/expressions/funcs/funcs.ts +323 -0
- package/src/expressions/funcs/index.ts +23 -0
- package/src/expressions/funcs/interval.ts +76 -0
- package/src/expressions/funcs/math.ts +395 -0
- package/src/expressions/funcs/std.ts +392 -0
- package/src/expressions/funcs/stored.ts +62 -0
- package/src/expressions/funcs/strings.ts +200 -0
- package/src/expressions/funcs/trigonometry.ts +108 -0
- package/src/expressions/json.ts +367 -0
- package/src/expressions/parserCache.ts +32 -0
- package/src/expressions/simpleUnescapeString.ts +57 -0
- package/src/expressions/utils.ts +271 -0
- package/src/expressions/variable.ts +429 -0
- package/src/expressions/walk.ts +43 -0
- package/src/hooks/README.md +265 -0
- package/src/hooks/index.ts +28 -0
- package/src/hooks/useAction.ts +152 -0
- package/src/hooks/useDerivedFromVars.ts +187 -0
- package/src/hooks/useVariable.ts +157 -0
- package/src/index.ts +97 -0
- package/src/stores/createObservable.ts +64 -0
- package/src/types/alignment.d.ts +13 -0
- package/src/types/background.d.ts +71 -0
- package/src/types/base.d.ts +224 -0
- package/src/types/border.d.ts +46 -0
- package/src/types/componentContext.d.ts +98 -0
- package/src/types/container.d.ts +40 -0
- package/src/types/edgeInserts.d.ts +9 -0
- package/src/types/general.d.ts +3 -0
- package/src/types/image.d.ts +33 -0
- package/src/types/imageScale.d.ts +1 -0
- package/src/types/layoutParams.d.ts +27 -0
- package/src/types/sizes.d.ts +37 -0
- package/src/types/state.d.ts +19 -0
- package/src/types/text.d.ts +126 -0
- package/src/utils/applyTemplate.ts +145 -0
- package/src/utils/correctColor.ts +102 -0
- package/src/utils/escapeRegExp.ts +3 -0
- package/src/utils/formatDate.ts +385 -0
- package/src/utils/padLeft.ts +6 -0
- package/src/utils/uniq.ts +3 -0
- package/src/utils/wrapError.ts +21 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import type { EvalContext, EvalTypes, EvalTypesWithoutDatetime, EvalValue, EvalValueBase, IntegerValue, NumberValue } from './eval';
|
|
2
|
+
import type { Node, Variable } from './ast';
|
|
3
|
+
import type { VariablesMap } from './eval';
|
|
4
|
+
import { walk } from './walk';
|
|
5
|
+
import { parseColor, type ParsedColor } from '../utils/correctColor';
|
|
6
|
+
import { padLeft } from '../utils/padLeft';
|
|
7
|
+
import { MAX_INT, MIN_INT, toBigInt } from './bigint';
|
|
8
|
+
import { BOOLEAN, NUMBER } from './const';
|
|
9
|
+
import type { TypedValue } from '../../typings/common';
|
|
10
|
+
import type { MaybeMissing } from './json';
|
|
11
|
+
|
|
12
|
+
export class FuncError extends Error {
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function valToInternal(val: EvalValue): EvalValue {
|
|
16
|
+
if (val.type === 'url' || val.type === 'color') {
|
|
17
|
+
return {
|
|
18
|
+
type: 'string',
|
|
19
|
+
value: val.value
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return val;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function dateToString(date: Date): string {
|
|
27
|
+
return date
|
|
28
|
+
.toISOString()
|
|
29
|
+
.replace('T', ' ')
|
|
30
|
+
.replace(/\.\d{3}Z$/, '');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function valToString(val: EvalValue, stringifyComplex: boolean): string {
|
|
34
|
+
if (val.type === 'string') {
|
|
35
|
+
return val.value;
|
|
36
|
+
} else if (val.type === 'integer') {
|
|
37
|
+
return String(val.value);
|
|
38
|
+
} else if (val.type === 'number') {
|
|
39
|
+
let res = String(val.value);
|
|
40
|
+
|
|
41
|
+
if (!res.includes('.')) {
|
|
42
|
+
if (res.includes('e')) {
|
|
43
|
+
res = res.replace('e', '.0e');
|
|
44
|
+
} else {
|
|
45
|
+
res += '.0';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
res = res.replace(/e\+?/i, 'E');
|
|
50
|
+
|
|
51
|
+
return res;
|
|
52
|
+
} else if (val.type === 'boolean') {
|
|
53
|
+
return val.value ? 'true' : 'false';
|
|
54
|
+
} else if (val.type === 'datetime') {
|
|
55
|
+
return dateToString(val.value);
|
|
56
|
+
} else if (val.type === 'color') {
|
|
57
|
+
return stringifyColor(safeConvertColor(val.value));
|
|
58
|
+
} else if (val.type === 'url') {
|
|
59
|
+
return val.value;
|
|
60
|
+
} else if ((val.type === 'dict' || val.type === 'array') && stringifyComplex) {
|
|
61
|
+
return JSON.stringify(val.value);
|
|
62
|
+
} else if (val.type === 'dict') {
|
|
63
|
+
return '<dict>';
|
|
64
|
+
} else if (val.type === 'array') {
|
|
65
|
+
return '<array>';
|
|
66
|
+
} else if (val.type === 'function') {
|
|
67
|
+
return val.value[0].name || 'Function';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// For purpose when new eval value types will be added
|
|
71
|
+
throw new Error(`Unexpected type ${(val as EvalValueBase).type}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function valToPreview(val: EvalValue): string {
|
|
75
|
+
let res = valToString(val, false);
|
|
76
|
+
|
|
77
|
+
if (val.type === 'string') {
|
|
78
|
+
res = "'" +
|
|
79
|
+
res
|
|
80
|
+
.replace(/\\/g, '\\\\')
|
|
81
|
+
.replace(/'/g, '\\\'') +
|
|
82
|
+
"'";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return res;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function typeToString(type: string): string {
|
|
89
|
+
if (type === 'datetime') {
|
|
90
|
+
return 'DateTime';
|
|
91
|
+
}
|
|
92
|
+
return type.charAt(0).toUpperCase() + type.substring(1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function roundInteger(_ctx: EvalContext, val: bigint): bigint {
|
|
96
|
+
return toBigInt(val);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function checkIntegerOverflow(_ctx: EvalContext, val: number | bigint): void {
|
|
100
|
+
if (val < MIN_INT || val > MAX_INT) {
|
|
101
|
+
throw new Error('Integer overflow.');
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function checkUrl(val: unknown): void {
|
|
106
|
+
if (typeof val !== 'string') {
|
|
107
|
+
throw new Error('Incorrect url value.');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
// eslint-disable-next-line no-new
|
|
112
|
+
new URL(val);
|
|
113
|
+
} catch (_err) {
|
|
114
|
+
throw new Error('Incorrect url value.');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function safeCheckUrl(val: unknown): boolean {
|
|
119
|
+
try {
|
|
120
|
+
checkUrl(val);
|
|
121
|
+
return true;
|
|
122
|
+
} catch {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function gatherVarsFromAst(ast: Node): string[] {
|
|
128
|
+
const res = new Set<string>();
|
|
129
|
+
|
|
130
|
+
walk(ast, {
|
|
131
|
+
Variable(node: Variable): void {
|
|
132
|
+
res.add(node.id.name);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return [...res];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function evalError(msg: string, details: string): never {
|
|
140
|
+
throw new FuncError(`Failed to evaluate [${msg}]. ${details}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function evalOuterError(_msg: string, details: string): never {
|
|
144
|
+
throw new Error(details);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function containsUnsetVariables(ast: Node, variables: VariablesMap): boolean {
|
|
148
|
+
let result = false;
|
|
149
|
+
|
|
150
|
+
walk(ast, {
|
|
151
|
+
Variable(node: Variable): void {
|
|
152
|
+
if (!variables.has(node.id.name)) {
|
|
153
|
+
result = true;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function safeConvertColor(color: string): ParsedColor {
|
|
162
|
+
const res = parseColor(color);
|
|
163
|
+
|
|
164
|
+
if (res) {
|
|
165
|
+
return res;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
throw new Error('Unable to convert value to Color, expected format #AARRGGBB.');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function stringifyColor(color: ParsedColor): string {
|
|
172
|
+
return `#${[color.a, color.r, color.g, color.b].map(it => {
|
|
173
|
+
if (it < 0 || it > 255) {
|
|
174
|
+
throw new Error('Value out of range 0..1.');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return padLeft(Math.round(it).toString(16), 2);
|
|
178
|
+
}).join('').toUpperCase()}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function transformColorValue(color: string): string {
|
|
182
|
+
return stringifyColor(safeConvertColor(color));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function integerToNumber(integerValue: IntegerValue): NumberValue {
|
|
186
|
+
return {
|
|
187
|
+
type: NUMBER,
|
|
188
|
+
value: Number(integerValue.value)
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const EVAL_TYPE_TO_JS_TYPE = {
|
|
193
|
+
string: 'string',
|
|
194
|
+
number: 'number',
|
|
195
|
+
integer: 'number',
|
|
196
|
+
boolean: 'boolean',
|
|
197
|
+
color: 'string',
|
|
198
|
+
url: 'string',
|
|
199
|
+
array: 'array',
|
|
200
|
+
dict: 'object',
|
|
201
|
+
datetime: 'never'
|
|
202
|
+
};
|
|
203
|
+
export function convertJsValueToDivKit(
|
|
204
|
+
ctx: EvalContext | undefined,
|
|
205
|
+
val: unknown,
|
|
206
|
+
evalType: EvalTypes
|
|
207
|
+
): EvalValue {
|
|
208
|
+
if (evalType === 'function') {
|
|
209
|
+
throw new Error('Cannot convert function');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const jsType = EVAL_TYPE_TO_JS_TYPE[evalType];
|
|
213
|
+
|
|
214
|
+
let type: string = typeof val;
|
|
215
|
+
if (
|
|
216
|
+
jsType === 'array' && !Array.isArray(val) ||
|
|
217
|
+
jsType !== 'array' && type !== jsType ||
|
|
218
|
+
type === 'object' && val === null
|
|
219
|
+
) {
|
|
220
|
+
if (type === 'object') {
|
|
221
|
+
if (Array.isArray(val)) {
|
|
222
|
+
type = 'array';
|
|
223
|
+
} else if (val === null) {
|
|
224
|
+
type = 'null';
|
|
225
|
+
} else {
|
|
226
|
+
type = 'dict';
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
throw new Error(`Incorrect value type: expected ${typeToString(evalType)}, got ${typeToString(type)}.`);
|
|
230
|
+
}
|
|
231
|
+
if (jsType === 'number' && evalType === 'integer') {
|
|
232
|
+
if (ctx) {
|
|
233
|
+
checkIntegerOverflow(ctx, val as number);
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
val = toBigInt(val as number);
|
|
237
|
+
} catch (_err) {
|
|
238
|
+
throw new Error('Cannot convert value to integer.');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (jsType === 'string' && evalType === 'color') {
|
|
242
|
+
val = transformColorValue(val as string);
|
|
243
|
+
}
|
|
244
|
+
if (jsType === 'string' && evalType === 'url') {
|
|
245
|
+
checkUrl(val);
|
|
246
|
+
}
|
|
247
|
+
if (jsType === 'boolean' && evalType === BOOLEAN) {
|
|
248
|
+
val = val ? 1 : 0;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
type: evalType,
|
|
253
|
+
value: val
|
|
254
|
+
} as EvalValue;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function convertDivKitValueToJson(value: EvalValue) {
|
|
258
|
+
if (value.type === 'number' || value.type === 'integer') {
|
|
259
|
+
return Number(value.value);
|
|
260
|
+
} else if (value.type === 'boolean') {
|
|
261
|
+
return Boolean(value.value);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return value.value;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function convertTypedValue(value: MaybeMissing<TypedValue>) {
|
|
268
|
+
return convertDivKitValueToJson(
|
|
269
|
+
convertJsValueToDivKit(undefined, value.value, value.type as EvalTypesWithoutDatetime)
|
|
270
|
+
);
|
|
271
|
+
}
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
// Adapted from Web implementation - replaced Svelte stores with Observable
|
|
2
|
+
import type { Writable, Subscriber, Unsubscriber } from '../stores/createObservable';
|
|
3
|
+
import { writable } from '../stores/createObservable';
|
|
4
|
+
import type { EvalValue } from './eval';
|
|
5
|
+
import { parseColor } from '../utils/correctColor';
|
|
6
|
+
import { bigIntZero, toBigInt } from './bigint';
|
|
7
|
+
import { checkUrl, transformColorValue } from './utils';
|
|
8
|
+
import { BOOLEAN } from './const';
|
|
9
|
+
|
|
10
|
+
export type VariableType = 'string' | 'number' | 'integer' | 'boolean' | 'color' | 'url' | 'dict' | 'array';
|
|
11
|
+
export type VariableValue = string | number | bigint | boolean | null | undefined | object | unknown[];
|
|
12
|
+
|
|
13
|
+
export abstract class Variable<
|
|
14
|
+
ValueType extends ConvertedSetValue = any,
|
|
15
|
+
TypeName = VariableType,
|
|
16
|
+
ConvertedSetValue = ValueType
|
|
17
|
+
> {
|
|
18
|
+
protected name: string;
|
|
19
|
+
protected value: ValueType;
|
|
20
|
+
protected store?: Writable<ValueType>;
|
|
21
|
+
|
|
22
|
+
constructor(name: string, value: ConvertedSetValue) {
|
|
23
|
+
const val = this.convertValue(value);
|
|
24
|
+
|
|
25
|
+
this.name = name;
|
|
26
|
+
this.value = val;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getName(): string {
|
|
30
|
+
return this.name;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected abstract convertValue(value: unknown): ValueType;
|
|
34
|
+
|
|
35
|
+
subscribe(cb: Subscriber<ValueType>): Unsubscriber {
|
|
36
|
+
if (!this.store) {
|
|
37
|
+
this.store = writable(this.value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return this.store.subscribe(cb);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
set(val: string): void {
|
|
44
|
+
const value = this.fromString(val);
|
|
45
|
+
|
|
46
|
+
this.setValue(value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected abstract fromString(val: string): ValueType;
|
|
50
|
+
|
|
51
|
+
setValue(val: ConvertedSetValue): void {
|
|
52
|
+
const converted = this.convertValue(val);
|
|
53
|
+
|
|
54
|
+
this.value = converted;
|
|
55
|
+
|
|
56
|
+
if (this.store) {
|
|
57
|
+
this.store.set(converted);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getValue(): ValueType {
|
|
62
|
+
return this.value;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
abstract getType(): TypeName;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class StringVariable extends Variable<string, 'string'> {
|
|
69
|
+
protected convertValue(value: unknown) {
|
|
70
|
+
if (typeof value !== 'string') {
|
|
71
|
+
throw new Error('Incorrect variable value');
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
protected fromString(val: string) {
|
|
77
|
+
return val;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getType(): 'string' {
|
|
81
|
+
return 'string';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export class IntegerVariable extends Variable<bigint, 'integer', bigint | number> {
|
|
86
|
+
protected convertValue(value: unknown) {
|
|
87
|
+
if (typeof value !== 'bigint' && typeof value !== 'number') {
|
|
88
|
+
throw new Error('Incorrect variable value');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
return toBigInt(value);
|
|
93
|
+
} catch (_err) {
|
|
94
|
+
throw new Error('Incorrect variable value');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
protected fromString(val: string) {
|
|
99
|
+
try {
|
|
100
|
+
return toBigInt(val);
|
|
101
|
+
} catch (_err) {
|
|
102
|
+
throw new Error('Incorrect variable value');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
getType(): 'integer' {
|
|
107
|
+
return 'integer';
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export class NumberVariable extends Variable<number, 'number'> {
|
|
112
|
+
protected convertValue(value: unknown) {
|
|
113
|
+
if (
|
|
114
|
+
typeof value !== 'number' ||
|
|
115
|
+
Number.isNaN(value) ||
|
|
116
|
+
!isFinite(value)
|
|
117
|
+
) {
|
|
118
|
+
throw new Error('Incorrect variable value');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
protected fromString(val: string) {
|
|
125
|
+
const res = Number(val);
|
|
126
|
+
|
|
127
|
+
return this.convertValue(res);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
getType(): 'number' {
|
|
131
|
+
return 'number';
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export class BooleanVariable extends Variable<boolean, 'boolean'> {
|
|
136
|
+
protected convertValue(value: unknown) {
|
|
137
|
+
if (value !== 1 && value !== 0 && value !== true && value !== false) {
|
|
138
|
+
throw new Error('Incorrect variable value');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return Boolean(value);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
protected fromString(val: string) {
|
|
145
|
+
if (val === '1' || val === 'true') {
|
|
146
|
+
return true;
|
|
147
|
+
} else if (val === '0' || val === 'false') {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
throw new Error('Incorrect variable value');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
getType(): 'boolean' {
|
|
155
|
+
return 'boolean';
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export class ColorVariable extends Variable<string, 'color'> {
|
|
160
|
+
protected convertValue(value: unknown) {
|
|
161
|
+
if (typeof value !== 'string' || !parseColor(value)) {
|
|
162
|
+
throw new Error('Incorrect variable value');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return transformColorValue(value);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
protected fromString(val: string) {
|
|
169
|
+
return this.convertValue(val);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
getType(): 'color' {
|
|
173
|
+
return 'color';
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export class UrlVariable extends Variable<string, 'url'> {
|
|
178
|
+
protected convertValue(value: unknown) {
|
|
179
|
+
if (typeof value !== 'string') {
|
|
180
|
+
throw new Error('Incorrect variable value');
|
|
181
|
+
}
|
|
182
|
+
checkUrl(value);
|
|
183
|
+
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
protected fromString(val: string) {
|
|
188
|
+
checkUrl(val);
|
|
189
|
+
|
|
190
|
+
return val;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
getType(): 'url' {
|
|
194
|
+
return 'url';
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export class DictVariable extends Variable<object, 'dict'> {
|
|
199
|
+
protected convertValue(value: unknown) {
|
|
200
|
+
if (typeof value !== 'object' || !value) {
|
|
201
|
+
throw new Error('Incorrect variable value');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
protected fromString(val: string): object {
|
|
208
|
+
try {
|
|
209
|
+
return JSON.parse(val);
|
|
210
|
+
} catch (_err) {
|
|
211
|
+
throw new Error('Incorrect dict value');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
getType(): 'dict' {
|
|
216
|
+
return 'dict';
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export class ArrayVariable extends Variable<unknown[], 'array'> {
|
|
221
|
+
protected convertValue(value: unknown[]) {
|
|
222
|
+
if (!Array.isArray(value)) {
|
|
223
|
+
throw new Error('Incorrect variable value');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return value;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
protected fromString(val: string): unknown[] {
|
|
230
|
+
try {
|
|
231
|
+
return JSON.parse(val);
|
|
232
|
+
} catch (_err) {
|
|
233
|
+
throw new Error('Incorrect array value');
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
getType(): 'array' {
|
|
238
|
+
return 'array';
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export const TYPE_TO_CLASS: Record<VariableType, new (name: string, value: any) => Variable> = {
|
|
243
|
+
string: StringVariable,
|
|
244
|
+
number: NumberVariable,
|
|
245
|
+
integer: IntegerVariable,
|
|
246
|
+
boolean: BooleanVariable,
|
|
247
|
+
color: ColorVariable,
|
|
248
|
+
url: UrlVariable,
|
|
249
|
+
dict: DictVariable,
|
|
250
|
+
array: ArrayVariable
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
export function createVariable(
|
|
254
|
+
name: string,
|
|
255
|
+
type: VariableType,
|
|
256
|
+
value: unknown
|
|
257
|
+
): Variable {
|
|
258
|
+
if (!(type in TYPE_TO_CLASS)) {
|
|
259
|
+
throw new Error('Unsupported variable type');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return new (TYPE_TO_CLASS[type] as any)(name, value);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
266
|
+
function noop(): void {
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function constSubscribe<ValueType>(this: Variable<ValueType>, cb: Subscriber<ValueType>): Unsubscriber {
|
|
270
|
+
cb(this.value);
|
|
271
|
+
|
|
272
|
+
return noop;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function constSetter(): void {
|
|
276
|
+
throw new Error('Cannot change the value of this type of variable');
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
class ConstStringVariable extends StringVariable {}
|
|
280
|
+
class ConstNumberVariable extends NumberVariable {}
|
|
281
|
+
class ConstIntegerVariable extends IntegerVariable {}
|
|
282
|
+
class ConstBooleanVariable extends BooleanVariable {}
|
|
283
|
+
class ConstColorVariable extends ColorVariable {}
|
|
284
|
+
class ConstUrlVariable extends UrlVariable {}
|
|
285
|
+
class ConstDictVariable extends DictVariable {}
|
|
286
|
+
class ConstArrayVariable extends ArrayVariable {}
|
|
287
|
+
|
|
288
|
+
class ConstDatetimeVariable extends Variable<Date, 'datetime'> {
|
|
289
|
+
protected convertValue(value: unknown) {
|
|
290
|
+
if (!(value instanceof Date)) {
|
|
291
|
+
throw new Error('Incorrect variable value');
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return value;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
protected fromString(): never {
|
|
298
|
+
throw new Error('Datetime variable does not support setter from string');
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
getType(): 'datetime' {
|
|
302
|
+
return 'datetime';
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export const CONST_TYPE_TO_CLASS: {
|
|
307
|
+
string: typeof StringVariable;
|
|
308
|
+
number: typeof NumberVariable;
|
|
309
|
+
integer: typeof IntegerVariable;
|
|
310
|
+
boolean: typeof BooleanVariable;
|
|
311
|
+
color: typeof ColorVariable;
|
|
312
|
+
url: typeof UrlVariable;
|
|
313
|
+
dict: typeof DictVariable;
|
|
314
|
+
array: typeof ArrayVariable;
|
|
315
|
+
datetime: typeof ConstDatetimeVariable;
|
|
316
|
+
} = {
|
|
317
|
+
string: ConstStringVariable,
|
|
318
|
+
number: ConstNumberVariable,
|
|
319
|
+
integer: ConstIntegerVariable,
|
|
320
|
+
boolean: ConstBooleanVariable,
|
|
321
|
+
color: ConstColorVariable,
|
|
322
|
+
url: ConstUrlVariable,
|
|
323
|
+
dict: ConstDictVariable,
|
|
324
|
+
array: ConstArrayVariable,
|
|
325
|
+
datetime: ConstDatetimeVariable
|
|
326
|
+
} as any;
|
|
327
|
+
|
|
328
|
+
for (const type in CONST_TYPE_TO_CLASS) {
|
|
329
|
+
const Class = CONST_TYPE_TO_CLASS[type as keyof typeof CONST_TYPE_TO_CLASS];
|
|
330
|
+
|
|
331
|
+
Class.prototype.subscribe = constSubscribe;
|
|
332
|
+
Class.prototype.set = constSetter;
|
|
333
|
+
Class.prototype.setValue = constSetter;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function createConstVariable(
|
|
337
|
+
name: string,
|
|
338
|
+
type: VariableType | 'datetime',
|
|
339
|
+
value: unknown
|
|
340
|
+
): Variable {
|
|
341
|
+
if (!(type in CONST_TYPE_TO_CLASS)) {
|
|
342
|
+
throw new Error('Unsupported variable type');
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return new (CONST_TYPE_TO_CLASS[type] as any)(name, value);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function defaultValueByType(type: keyof typeof TYPE_TO_CLASS): VariableValue {
|
|
349
|
+
if (type === 'integer') {
|
|
350
|
+
return bigIntZero;
|
|
351
|
+
}
|
|
352
|
+
if (type === 'boolean' || type === 'number') {
|
|
353
|
+
return 0;
|
|
354
|
+
}
|
|
355
|
+
if (type === 'dict') {
|
|
356
|
+
return {};
|
|
357
|
+
}
|
|
358
|
+
if (type === 'array') {
|
|
359
|
+
return [];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return '';
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function variableToValue(variable: Variable): EvalValue {
|
|
366
|
+
const type = variable.getType();
|
|
367
|
+
let value = variable.getValue();
|
|
368
|
+
|
|
369
|
+
if (type === BOOLEAN) {
|
|
370
|
+
value = value ? 1 : 0;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return {
|
|
374
|
+
type,
|
|
375
|
+
value
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function variableValueFromString(val: string, type: VariableType): VariableValue {
|
|
380
|
+
if (type === 'string') {
|
|
381
|
+
return val;
|
|
382
|
+
} else if (type === 'integer') {
|
|
383
|
+
try {
|
|
384
|
+
return toBigInt(val);
|
|
385
|
+
} catch (_err) {
|
|
386
|
+
throw new Error('Incorrect variable value');
|
|
387
|
+
}
|
|
388
|
+
} else if (type === 'number') {
|
|
389
|
+
const converted = Number(val);
|
|
390
|
+
if (
|
|
391
|
+
Number.isNaN(converted) ||
|
|
392
|
+
!isFinite(converted)
|
|
393
|
+
) {
|
|
394
|
+
throw new Error('Incorrect variable value');
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return converted;
|
|
398
|
+
} else if (type === 'boolean') {
|
|
399
|
+
if (val === '1' || val === 'true') {
|
|
400
|
+
return true;
|
|
401
|
+
} else if (val === '0' || val === 'false') {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
throw new Error('Incorrect variable value');
|
|
406
|
+
} else if (type === 'color') {
|
|
407
|
+
if (typeof val !== 'string' || !parseColor(val)) {
|
|
408
|
+
throw new Error('Incorrect variable value');
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return transformColorValue(val);
|
|
412
|
+
} else if (type === 'url') {
|
|
413
|
+
if (typeof val !== 'string') {
|
|
414
|
+
throw new Error('Incorrect variable value');
|
|
415
|
+
}
|
|
416
|
+
checkUrl(val);
|
|
417
|
+
|
|
418
|
+
return val;
|
|
419
|
+
} else if (type === 'dict' || type === 'array') {
|
|
420
|
+
try {
|
|
421
|
+
return JSON.parse(val);
|
|
422
|
+
} catch (_err) {
|
|
423
|
+
throw new Error('Incorrect dict value');
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// For purpose when new eval value types will be added
|
|
428
|
+
throw new Error(`Unexpected type ${type}`);
|
|
429
|
+
}
|