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,232 @@
|
|
|
1
|
+
/* eslint-disable no-nested-ternary */
|
|
2
|
+
import type { DatetimeValue, EvalContext, EvalValue, IntegerValue, StringValue } from '../eval';
|
|
3
|
+
import { registerFunc } from './funcs';
|
|
4
|
+
import { DATETIME, INTEGER, STRING } from '../const';
|
|
5
|
+
import { valToString } from '../utils';
|
|
6
|
+
import { toBigInt } from '../bigint';
|
|
7
|
+
import { formatDate } from '../../utils/formatDate';
|
|
8
|
+
|
|
9
|
+
function getMaxDate(date: Date): number {
|
|
10
|
+
const copy = new Date(date);
|
|
11
|
+
|
|
12
|
+
copy.setUTCMonth(copy.getUTCMonth() + 1);
|
|
13
|
+
copy.setUTCDate(0);
|
|
14
|
+
|
|
15
|
+
return copy.getUTCDate();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parseUnixTime(_ctx: EvalContext, arg: IntegerValue): EvalValue {
|
|
19
|
+
return {
|
|
20
|
+
type: DATETIME,
|
|
21
|
+
value: new Date(Number(arg.value) * 1000)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function parseUnixTimeAsLocal(_ctx: EvalContext, arg: IntegerValue): EvalValue {
|
|
26
|
+
const date = new Date(Number(arg.value) * 1000);
|
|
27
|
+
const offset = date.getTimezoneOffset();
|
|
28
|
+
date.setMinutes(date.getMinutes() - offset);
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
type: DATETIME,
|
|
32
|
+
value: date
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function nowLocal(): EvalValue {
|
|
37
|
+
return {
|
|
38
|
+
type: DATETIME,
|
|
39
|
+
value: new Date()
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function addMillis(_ctx: EvalContext, datetime: DatetimeValue, milliseconds: IntegerValue): EvalValue {
|
|
44
|
+
return {
|
|
45
|
+
type: DATETIME,
|
|
46
|
+
value: new Date(datetime.value.getTime() + Number(milliseconds.value))
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function setYear(_ctx: EvalContext, datetime: DatetimeValue, year: IntegerValue): EvalValue {
|
|
51
|
+
const copy = new Date(datetime.value);
|
|
52
|
+
|
|
53
|
+
copy.setUTCFullYear(Number(year.value));
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
type: DATETIME,
|
|
57
|
+
value: copy
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function setMonth(_ctx: EvalContext, datetime: DatetimeValue, month: IntegerValue): EvalValue {
|
|
62
|
+
const intVal = Number(month.value);
|
|
63
|
+
|
|
64
|
+
if (intVal < 1 || intVal > 12) {
|
|
65
|
+
throw new Error(`Expecting month in [1..12], instead got ${intVal}.`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const copy = new Date(datetime.value);
|
|
69
|
+
copy.setUTCMonth(intVal - 1);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
type: DATETIME,
|
|
73
|
+
value: copy
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function setDay(_ctx: EvalContext, datetime: DatetimeValue, day: IntegerValue): EvalValue {
|
|
78
|
+
const copy = new Date(datetime.value);
|
|
79
|
+
const intVal = Number(day.value);
|
|
80
|
+
|
|
81
|
+
if (intVal <= 0 && intVal !== -1 || intVal > getMaxDate(copy)) {
|
|
82
|
+
throw new Error(`Unable to set day ${intVal} for date ${valToString(datetime, false)}.`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
copy.setUTCDate(intVal === -1 ? 0 : intVal);
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
type: DATETIME,
|
|
89
|
+
value: copy
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function setHours(_ctx: EvalContext, datetime: DatetimeValue, hours: IntegerValue): EvalValue {
|
|
94
|
+
const intVal = Number(hours.value);
|
|
95
|
+
|
|
96
|
+
if (intVal < 0 || intVal > 23) {
|
|
97
|
+
throw new Error(`Expecting hours in [0..23], instead got ${intVal}.`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const copy = new Date(datetime.value);
|
|
101
|
+
copy.setUTCHours(intVal);
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
type: DATETIME,
|
|
105
|
+
value: copy
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function setMinutes(_ctx: EvalContext, datetime: DatetimeValue, minutes: IntegerValue): EvalValue {
|
|
110
|
+
const intVal = Number(minutes.value);
|
|
111
|
+
|
|
112
|
+
if (intVal < 0 || intVal > 59) {
|
|
113
|
+
throw new Error(`Expecting minutes in [0..59], instead got ${intVal}.`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const copy = new Date(datetime.value);
|
|
117
|
+
|
|
118
|
+
copy.setUTCMinutes(intVal);
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
type: DATETIME,
|
|
122
|
+
value: copy
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function setSeconds(_ctx: EvalContext, datetime: DatetimeValue, seconds: IntegerValue): EvalValue {
|
|
127
|
+
const intVal = Number(seconds.value);
|
|
128
|
+
|
|
129
|
+
if (intVal < 0 || intVal > 59) {
|
|
130
|
+
throw new Error(`Expecting seconds in [0..59], instead got ${intVal}.`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const copy = new Date(datetime.value);
|
|
134
|
+
copy.setUTCSeconds(intVal);
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
type: DATETIME,
|
|
138
|
+
value: copy
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function setMillis(_ctx: EvalContext, datetime: DatetimeValue, millis: IntegerValue): EvalValue {
|
|
143
|
+
const intVal = Number(millis.value);
|
|
144
|
+
|
|
145
|
+
if (intVal < 0 || intVal > 999) {
|
|
146
|
+
throw new Error(`Expecting millis in [0..999], instead got ${intVal}.`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const copy = new Date(datetime.value);
|
|
150
|
+
copy.setUTCMilliseconds(intVal);
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
type: DATETIME,
|
|
154
|
+
value: copy
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const getter = (
|
|
159
|
+
method: 'getUTCFullYear' | 'getUTCMonth' | 'getUTCDate' | 'getUTCDay' | 'getUTCHours' | 'getUTCMinutes' |
|
|
160
|
+
'getUTCSeconds' | 'getUTCMilliseconds'
|
|
161
|
+
) => {
|
|
162
|
+
return (_ctx: EvalContext, datetime: DatetimeValue): EvalValue => {
|
|
163
|
+
const copy = new Date(datetime.value.getTime());
|
|
164
|
+
|
|
165
|
+
let value: number = copy[method]();
|
|
166
|
+
|
|
167
|
+
if (method === 'getUTCMonth') {
|
|
168
|
+
++value;
|
|
169
|
+
} else if (method === 'getUTCDay' && value === 0) {
|
|
170
|
+
value = 7;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
type: INTEGER,
|
|
175
|
+
value: toBigInt(value)
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
function makeFormat(isUTC: boolean) {
|
|
181
|
+
return (ctx: EvalContext, datetime: DatetimeValue, format: StringValue, locale?: StringValue): EvalValue => {
|
|
182
|
+
return {
|
|
183
|
+
type: STRING,
|
|
184
|
+
value: formatDate(datetime.value, format.value, {
|
|
185
|
+
locale: locale?.value,
|
|
186
|
+
isUTC,
|
|
187
|
+
weekStartDay: ctx.weekStartDay
|
|
188
|
+
})
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const getYear = getter('getUTCFullYear');
|
|
194
|
+
const getMonth = getter('getUTCMonth');
|
|
195
|
+
const getDay = getter('getUTCDate');
|
|
196
|
+
const getDayOfWeek = getter('getUTCDay');
|
|
197
|
+
const getHours = getter('getUTCHours');
|
|
198
|
+
const getMinutes = getter('getUTCMinutes');
|
|
199
|
+
const getSeconds = getter('getUTCSeconds');
|
|
200
|
+
const getMillis = getter('getUTCMilliseconds');
|
|
201
|
+
|
|
202
|
+
const formatAsLocal = makeFormat(false);
|
|
203
|
+
const formatAsUTC = makeFormat(true);
|
|
204
|
+
|
|
205
|
+
export function registerDatetime(): void {
|
|
206
|
+
registerFunc('parseUnixTime', [INTEGER], parseUnixTime);
|
|
207
|
+
registerFunc('parseUnixTimeAsLocal', [INTEGER], parseUnixTimeAsLocal);
|
|
208
|
+
registerFunc('nowLocal', [], nowLocal);
|
|
209
|
+
registerFunc('addMillis', [DATETIME, INTEGER], addMillis);
|
|
210
|
+
|
|
211
|
+
registerFunc('setYear', [DATETIME, INTEGER], setYear);
|
|
212
|
+
registerFunc('setMonth', [DATETIME, INTEGER], setMonth);
|
|
213
|
+
registerFunc('setDay', [DATETIME, INTEGER], setDay);
|
|
214
|
+
registerFunc('setHours', [DATETIME, INTEGER], setHours);
|
|
215
|
+
registerFunc('setMinutes', [DATETIME, INTEGER], setMinutes);
|
|
216
|
+
registerFunc('setSeconds', [DATETIME, INTEGER], setSeconds);
|
|
217
|
+
registerFunc('setMillis', [DATETIME, INTEGER], setMillis);
|
|
218
|
+
|
|
219
|
+
registerFunc('getYear', [DATETIME], getYear);
|
|
220
|
+
registerFunc('getMonth', [DATETIME], getMonth);
|
|
221
|
+
registerFunc('getDay', [DATETIME], getDay);
|
|
222
|
+
registerFunc('getDayOfWeek', [DATETIME], getDayOfWeek);
|
|
223
|
+
registerFunc('getHours', [DATETIME], getHours);
|
|
224
|
+
registerFunc('getMinutes', [DATETIME], getMinutes);
|
|
225
|
+
registerFunc('getSeconds', [DATETIME], getSeconds);
|
|
226
|
+
registerFunc('getMillis', [DATETIME], getMillis);
|
|
227
|
+
|
|
228
|
+
registerFunc('formatDateAsLocal', [DATETIME, STRING], formatAsLocal);
|
|
229
|
+
registerFunc('formatDateAsUTC', [DATETIME, STRING], formatAsUTC);
|
|
230
|
+
registerFunc('formatDateAsLocalWithLocale', [DATETIME, STRING, STRING], formatAsLocal);
|
|
231
|
+
registerFunc('formatDateAsUTCWithLocale', [DATETIME, STRING, STRING], formatAsUTC);
|
|
232
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { toBigInt } from '../bigint';
|
|
2
|
+
import { ARRAY, BOOLEAN, COLOR, DICT, INTEGER, NUMBER, STRING, URL } from '../const';
|
|
3
|
+
import type { ArrayValue, BooleanValue, DictValue, EvalContext, EvalTypes, EvalTypesWithoutDatetime, EvalValue, IntegerValue, NumberValue, StringValue } from '../eval';
|
|
4
|
+
import { checkUrl, convertJsValueToDivKit, transformColorValue } from '../utils';
|
|
5
|
+
import { registerFunc, registerMethod } from './funcs';
|
|
6
|
+
|
|
7
|
+
function getProp(obj: object, path: string[]): unknown {
|
|
8
|
+
let current: object = obj;
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < path.length; ++i) {
|
|
11
|
+
if (!current) {
|
|
12
|
+
throw new Error(`Missing property "${path[i]}" in the dict.`);
|
|
13
|
+
}
|
|
14
|
+
const val = current[path[i] as keyof typeof current];
|
|
15
|
+
if (val === undefined) {
|
|
16
|
+
throw new Error(`Missing property "${path[i]}" in the dict.`);
|
|
17
|
+
}
|
|
18
|
+
current = val;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return current;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function dictGetter(evalType: EvalTypesWithoutDatetime) {
|
|
25
|
+
return (ctx: EvalContext, dict: DictValue, ...path: StringValue[]): EvalValue => {
|
|
26
|
+
if (path.length === 0) {
|
|
27
|
+
throw new Error('Non empty argument list is required.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const val = getProp(dict.value, path.map(it => it.value));
|
|
31
|
+
|
|
32
|
+
return convertJsValueToDivKit(ctx, val, evalType);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function optWrapper<ValueType extends EvalValue>(
|
|
37
|
+
func: (ctx: EvalContext, dict: DictValue, ...path: StringValue[]) => EvalValue,
|
|
38
|
+
fallbackType: EvalTypes
|
|
39
|
+
) {
|
|
40
|
+
return (ctx: EvalContext, fallback: ValueType, dict: DictValue, ...path: StringValue[]) => {
|
|
41
|
+
try {
|
|
42
|
+
return func(ctx, dict, ...path);
|
|
43
|
+
} catch (_err) {
|
|
44
|
+
// ignore error
|
|
45
|
+
|
|
46
|
+
let value = fallback.value;
|
|
47
|
+
if (fallbackType === 'color') {
|
|
48
|
+
value = transformColorValue(value as string);
|
|
49
|
+
} else if (fallbackType === 'url') {
|
|
50
|
+
checkUrl(value);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
type: fallbackType,
|
|
54
|
+
value
|
|
55
|
+
} as unknown as EvalValue;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const getDictString = dictGetter(STRING);
|
|
61
|
+
const getDictNumber = dictGetter(NUMBER);
|
|
62
|
+
const getDictInteger = dictGetter(INTEGER);
|
|
63
|
+
const getDictBoolean = dictGetter(BOOLEAN);
|
|
64
|
+
const getDictColor = dictGetter(COLOR);
|
|
65
|
+
const getDictUrl = dictGetter(URL);
|
|
66
|
+
const getDictArray = dictGetter(ARRAY);
|
|
67
|
+
const getDictDict = dictGetter(DICT);
|
|
68
|
+
|
|
69
|
+
const getDictOptString = optWrapper<StringValue>(getDictString, STRING);
|
|
70
|
+
const getDictOptNumber = optWrapper<NumberValue>(getDictNumber, NUMBER);
|
|
71
|
+
const getDictOptInteger = optWrapper<IntegerValue>(getDictInteger, INTEGER);
|
|
72
|
+
const getDictOptBoolean = optWrapper<BooleanValue>(getDictBoolean, BOOLEAN);
|
|
73
|
+
const getDictOptColor = optWrapper<BooleanValue>(getDictColor, COLOR);
|
|
74
|
+
const getDictOptUrl = optWrapper<BooleanValue>(getDictUrl, URL);
|
|
75
|
+
|
|
76
|
+
function getDictOptArray(ctx: EvalContext, dict: DictValue, ...path: StringValue[]): EvalValue {
|
|
77
|
+
try {
|
|
78
|
+
return getDictArray(ctx, dict, ...path);
|
|
79
|
+
} catch (_err) {
|
|
80
|
+
// ignore error
|
|
81
|
+
return {
|
|
82
|
+
type: ARRAY,
|
|
83
|
+
value: []
|
|
84
|
+
} as unknown as EvalValue;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function getDictOptDict(ctx: EvalContext, dict: DictValue, ...path: StringValue[]): EvalValue {
|
|
89
|
+
try {
|
|
90
|
+
return getDictDict(ctx, dict, ...path);
|
|
91
|
+
} catch (_err) {
|
|
92
|
+
// ignore error
|
|
93
|
+
return {
|
|
94
|
+
type: DICT,
|
|
95
|
+
value: {}
|
|
96
|
+
} as unknown as EvalValue;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function containsKey(_ext: EvalContext, dict: DictValue, key: StringValue): BooleanValue {
|
|
101
|
+
return {
|
|
102
|
+
type: BOOLEAN,
|
|
103
|
+
value: key.value in dict.value ? 1 : 0
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function isEmpty(_ext: EvalContext, dict: DictValue): BooleanValue {
|
|
108
|
+
return {
|
|
109
|
+
type: BOOLEAN,
|
|
110
|
+
value: Object.keys(dict.value).length ? 0 : 1
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function len(_ext: EvalContext, dict: DictValue): IntegerValue {
|
|
115
|
+
return {
|
|
116
|
+
type: INTEGER,
|
|
117
|
+
value: toBigInt(Object.keys(dict.value).length)
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function getDictKeys(_ext: EvalContext, dict: DictValue): ArrayValue {
|
|
122
|
+
return {
|
|
123
|
+
type: ARRAY,
|
|
124
|
+
value: Object.keys(dict.value)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function getDictValues(_ext: EvalContext, dict: DictValue): ArrayValue {
|
|
129
|
+
return {
|
|
130
|
+
type: ARRAY,
|
|
131
|
+
value: Object.values(dict.value)
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function registerDict(): void {
|
|
136
|
+
const STRING_VARARG = {
|
|
137
|
+
type: STRING,
|
|
138
|
+
isVararg: true
|
|
139
|
+
} as const;
|
|
140
|
+
|
|
141
|
+
registerFunc('getDictString', [DICT, STRING_VARARG], getDictString);
|
|
142
|
+
registerFunc('getStringFromDict', [DICT, STRING_VARARG], getDictString);
|
|
143
|
+
|
|
144
|
+
registerFunc('getDictNumber', [DICT, STRING_VARARG], getDictNumber);
|
|
145
|
+
registerFunc('getNumberFromDict', [DICT, STRING_VARARG], getDictNumber);
|
|
146
|
+
|
|
147
|
+
registerFunc('getDictInteger', [DICT, STRING_VARARG], getDictInteger);
|
|
148
|
+
registerFunc('getIntegerFromDict', [DICT, STRING_VARARG], getDictInteger);
|
|
149
|
+
|
|
150
|
+
registerFunc('getDictBoolean', [DICT, STRING_VARARG], getDictBoolean);
|
|
151
|
+
registerFunc('getBooleanFromDict', [DICT, STRING_VARARG], getDictBoolean);
|
|
152
|
+
|
|
153
|
+
registerFunc('getDictColor', [DICT, STRING_VARARG], getDictColor);
|
|
154
|
+
registerFunc('getColorFromDict', [DICT, STRING_VARARG], getDictColor);
|
|
155
|
+
|
|
156
|
+
registerFunc('getDictUrl', [DICT, STRING_VARARG], getDictUrl);
|
|
157
|
+
registerFunc('getUrlFromDict', [DICT, STRING_VARARG], getDictUrl);
|
|
158
|
+
|
|
159
|
+
registerFunc('getDictOptString', [STRING, DICT, STRING_VARARG], getDictOptString);
|
|
160
|
+
registerFunc('getOptStringFromDict', [STRING, DICT, STRING_VARARG], getDictOptString);
|
|
161
|
+
|
|
162
|
+
registerFunc('getDictOptNumber', [NUMBER, DICT, STRING_VARARG], getDictOptNumber);
|
|
163
|
+
registerFunc('getOptNumberFromDict', [NUMBER, DICT, STRING_VARARG], getDictOptNumber);
|
|
164
|
+
|
|
165
|
+
registerFunc('getDictOptInteger', [INTEGER, DICT, STRING_VARARG], getDictOptInteger);
|
|
166
|
+
registerFunc('getOptIntegerFromDict', [INTEGER, DICT, STRING_VARARG], getDictOptInteger);
|
|
167
|
+
|
|
168
|
+
registerFunc('getDictOptBoolean', [BOOLEAN, DICT, STRING_VARARG], getDictOptBoolean);
|
|
169
|
+
registerFunc('getOptBooleanFromDict', [BOOLEAN, DICT, STRING_VARARG], getDictOptBoolean);
|
|
170
|
+
|
|
171
|
+
registerFunc('getDictOptColor', [COLOR, DICT, STRING_VARARG], getDictOptColor);
|
|
172
|
+
registerFunc('getOptColorFromDict', [COLOR, DICT, STRING_VARARG], getDictOptColor);
|
|
173
|
+
|
|
174
|
+
registerFunc('getDictOptColor', [STRING, DICT, STRING_VARARG], getDictOptColor);
|
|
175
|
+
registerFunc('getOptColorFromDict', [STRING, DICT, STRING_VARARG], getDictOptColor);
|
|
176
|
+
|
|
177
|
+
registerFunc('getDictOptUrl', [STRING, DICT, STRING_VARARG], getDictOptUrl);
|
|
178
|
+
registerFunc('getOptUrlFromDict', [STRING, DICT, STRING_VARARG], getDictOptUrl);
|
|
179
|
+
|
|
180
|
+
registerFunc('getDictOptUrl', [URL, DICT, STRING_VARARG], getDictOptUrl);
|
|
181
|
+
registerFunc('getOptUrlFromDict', [URL, DICT, STRING_VARARG], getDictOptUrl);
|
|
182
|
+
|
|
183
|
+
registerFunc('getDictFromDict', [DICT, STRING_VARARG], getDictDict);
|
|
184
|
+
|
|
185
|
+
registerFunc('getArrayFromDict', [DICT, STRING_VARARG], getDictArray);
|
|
186
|
+
|
|
187
|
+
registerFunc('getOptArrayFromDict', [DICT, STRING_VARARG], getDictOptArray);
|
|
188
|
+
|
|
189
|
+
registerFunc('getOptDictFromDict', [DICT, STRING_VARARG], getDictOptDict);
|
|
190
|
+
|
|
191
|
+
registerFunc('len', [DICT], len);
|
|
192
|
+
registerFunc('getDictKeys', [DICT], getDictKeys);
|
|
193
|
+
registerFunc('getDictValues', [DICT], getDictValues);
|
|
194
|
+
|
|
195
|
+
registerMethod('getString', [DICT, STRING_VARARG], getDictString);
|
|
196
|
+
registerMethod('getBoolean', [DICT, STRING_VARARG], getDictBoolean);
|
|
197
|
+
registerMethod('getInteger', [DICT, STRING_VARARG], getDictInteger);
|
|
198
|
+
registerMethod('getNumber', [DICT, STRING_VARARG], getDictNumber);
|
|
199
|
+
registerMethod('getUrl', [DICT, STRING_VARARG], getDictUrl);
|
|
200
|
+
registerMethod('getColor', [DICT, STRING_VARARG], getDictColor);
|
|
201
|
+
registerMethod('getArray', [DICT, STRING_VARARG], getDictArray);
|
|
202
|
+
registerMethod('getDict', [DICT, STRING_VARARG], getDictDict);
|
|
203
|
+
registerMethod('containsKey', [DICT, STRING], containsKey);
|
|
204
|
+
registerMethod('isEmpty', [DICT], isEmpty);
|
|
205
|
+
registerMethod('getKeys', [DICT], getDictKeys);
|
|
206
|
+
registerMethod('getValues', [DICT], getDictValues);
|
|
207
|
+
}
|