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,392 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ArrayValue,
|
|
3
|
+
BooleanValue,
|
|
4
|
+
ColorValue,
|
|
5
|
+
DictValue,
|
|
6
|
+
EvalContext,
|
|
7
|
+
EvalValue,
|
|
8
|
+
IntegerValue,
|
|
9
|
+
NumberValue,
|
|
10
|
+
StringValue,
|
|
11
|
+
UrlValue
|
|
12
|
+
} from '../eval';
|
|
13
|
+
import type { VariableType, VariableValue } from '../variable';
|
|
14
|
+
import { registerFunc, registerMethod } from './funcs';
|
|
15
|
+
import { ARRAY, BOOLEAN, COLOR, DICT, INTEGER, NUMBER, STRING, URL } from '../const';
|
|
16
|
+
import { checkUrl, transformColorValue, valToString } from '../utils';
|
|
17
|
+
import { MAX_INT, MIN_INT, toBigInt } from '../bigint';
|
|
18
|
+
|
|
19
|
+
function toString(
|
|
20
|
+
_ctx: EvalContext,
|
|
21
|
+
arg: IntegerValue | NumberValue | BooleanValue | ColorValue | UrlValue | StringValue | ArrayValue | DictValue
|
|
22
|
+
): EvalValue {
|
|
23
|
+
return {
|
|
24
|
+
type: STRING,
|
|
25
|
+
value: valToString(arg, true)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
function toNumber(_ctx: EvalContext, arg: IntegerValue | StringValue): EvalValue {
|
|
31
|
+
const num = Number(arg.value);
|
|
32
|
+
|
|
33
|
+
if (Number.isNaN(num) || !Number.isFinite(num)) {
|
|
34
|
+
throw new Error('Unable to convert value to Number.');
|
|
35
|
+
}
|
|
36
|
+
if (arg.value === '') {
|
|
37
|
+
throw new Error('Unable to convert value to Number.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
type: NUMBER,
|
|
42
|
+
value: num
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
function toIntegerNumber(_ctx: EvalContext, arg: NumberValue): EvalValue {
|
|
48
|
+
if (arg.value > MAX_INT || arg.value < MIN_INT) {
|
|
49
|
+
throw new Error('Unable to convert value to Integer.');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const num = arg.value - (arg.value % 1);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
type: INTEGER,
|
|
56
|
+
value: toBigInt(num)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function toIntegerString(_ctx: EvalContext, arg: StringValue): EvalValue {
|
|
61
|
+
let num: bigint;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
num = toBigInt(arg.value);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
throw new Error('Unable to convert value to Integer.');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
type: INTEGER,
|
|
71
|
+
value: num
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toIntegerBoolean(_ctx: EvalContext, arg: BooleanValue): EvalValue {
|
|
76
|
+
return {
|
|
77
|
+
type: INTEGER,
|
|
78
|
+
value: toBigInt(arg.value ? 1 : 0)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
function toBooleanInteger(_ctx: EvalContext, arg: IntegerValue): EvalValue {
|
|
84
|
+
const intVal = Number(arg.value);
|
|
85
|
+
if (intVal !== 1 && intVal !== 0) {
|
|
86
|
+
throw new Error('Unable to convert value to Boolean.');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
type: BOOLEAN,
|
|
91
|
+
value: intVal
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function toBooleanString(_ctx: EvalContext, arg: StringValue): EvalValue {
|
|
96
|
+
if (arg.value !== 'true' && arg.value !== 'false') {
|
|
97
|
+
throw new Error('Unable to convert value to Boolean.');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
type: BOOLEAN,
|
|
102
|
+
value: arg.value === 'true' ? 1 : 0
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function toColor(_ctx: EvalContext, arg: StringValue): EvalValue {
|
|
107
|
+
return {
|
|
108
|
+
type: COLOR,
|
|
109
|
+
value: transformColorValue(arg.value)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function toUrl(_ctx: EvalContext, arg: StringValue): EvalValue {
|
|
114
|
+
checkUrl(arg.value);
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
type: URL,
|
|
118
|
+
value: arg.value
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function encodeUri(_ctx: EvalContext, str: StringValue): EvalValue {
|
|
123
|
+
try {
|
|
124
|
+
return {
|
|
125
|
+
type: STRING,
|
|
126
|
+
value: encodeURIComponent(str.value)
|
|
127
|
+
};
|
|
128
|
+
} catch (_err) {
|
|
129
|
+
throw new Error('Unable to encodeUri string.');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function decodeUri(_ctx: EvalContext, str: StringValue): EvalValue {
|
|
134
|
+
try {
|
|
135
|
+
return {
|
|
136
|
+
type: STRING,
|
|
137
|
+
value: decodeURIComponent(str.value)
|
|
138
|
+
};
|
|
139
|
+
} catch (_err) {
|
|
140
|
+
throw new Error('Unable to decodeUri string.');
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function getValueForced(
|
|
145
|
+
ctx: EvalContext,
|
|
146
|
+
varName: StringValue,
|
|
147
|
+
fallback: IntegerValue | NumberValue | StringValue | BooleanValue | UrlValue | ColorValue,
|
|
148
|
+
type: VariableType
|
|
149
|
+
): EvalValue {
|
|
150
|
+
const variable = ctx.variables.get(varName.value);
|
|
151
|
+
let value: VariableValue;
|
|
152
|
+
|
|
153
|
+
if (variable && variable.getType() === type) {
|
|
154
|
+
value = variable.getValue();
|
|
155
|
+
|
|
156
|
+
if (!ctx.storeUsedVars) {
|
|
157
|
+
ctx.storeUsedVars = new Set();
|
|
158
|
+
}
|
|
159
|
+
ctx.storeUsedVars.add(variable);
|
|
160
|
+
} else {
|
|
161
|
+
value = fallback.value;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (type === 'color') {
|
|
165
|
+
value = transformColorValue(value as string);
|
|
166
|
+
} else if (type === 'url') {
|
|
167
|
+
checkUrl(value);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
type,
|
|
172
|
+
// value is synced with type by params
|
|
173
|
+
value: value as any
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function getValue(
|
|
178
|
+
ctx: EvalContext,
|
|
179
|
+
varName: StringValue,
|
|
180
|
+
fallback: IntegerValue | NumberValue | StringValue | BooleanValue | UrlValue | ColorValue
|
|
181
|
+
): EvalValue {
|
|
182
|
+
return getValueForced(ctx, varName, fallback, fallback.type);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function getColorValue(
|
|
186
|
+
ctx: EvalContext,
|
|
187
|
+
varName: StringValue,
|
|
188
|
+
fallback: IntegerValue | NumberValue | StringValue | BooleanValue | UrlValue | ColorValue
|
|
189
|
+
) {
|
|
190
|
+
return getValueForced(ctx, varName, fallback, 'color');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function getUrlValue(
|
|
194
|
+
ctx: EvalContext,
|
|
195
|
+
varName: StringValue,
|
|
196
|
+
fallback: IntegerValue | NumberValue | StringValue | BooleanValue | UrlValue | ColorValue
|
|
197
|
+
) {
|
|
198
|
+
return getValueForced(ctx, varName, fallback, 'url');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function firstDiffChar(str0: string, str1: string): string {
|
|
202
|
+
for (let i = 0; i < str1.length; ++i) {
|
|
203
|
+
const char0 = str0.charAt(i);
|
|
204
|
+
const char1 = str1.charAt(i);
|
|
205
|
+
|
|
206
|
+
if (char0 !== char1 && char1) {
|
|
207
|
+
return char1;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return '';
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const TEST_NUMBER = 1234567890;
|
|
215
|
+
|
|
216
|
+
function numberFractionDivider(locale?: string): string {
|
|
217
|
+
const formatter0 = new Intl.NumberFormat(locale, {
|
|
218
|
+
maximumFractionDigits: 0
|
|
219
|
+
});
|
|
220
|
+
const formatter1 = new Intl.NumberFormat(locale, {
|
|
221
|
+
minimumFractionDigits: 1
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const str0 = formatter0.format(TEST_NUMBER);
|
|
225
|
+
const str1 = formatter1.format(TEST_NUMBER);
|
|
226
|
+
|
|
227
|
+
return firstDiffChar(str0, str1);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function numberGroupingDivider(locale?: string): string {
|
|
231
|
+
const formatter0 = new Intl.NumberFormat(locale, {
|
|
232
|
+
useGrouping: false
|
|
233
|
+
});
|
|
234
|
+
const formatter1 = new Intl.NumberFormat(locale, {
|
|
235
|
+
useGrouping: true
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const str0 = formatter0.format(TEST_NUMBER);
|
|
239
|
+
const str1 = formatter1.format(TEST_NUMBER);
|
|
240
|
+
|
|
241
|
+
return firstDiffChar(str0, str1);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function decimalFormat(
|
|
245
|
+
_ctx: EvalContext,
|
|
246
|
+
arg: IntegerValue | NumberValue,
|
|
247
|
+
format: StringValue,
|
|
248
|
+
locale?: StringValue
|
|
249
|
+
): EvalValue {
|
|
250
|
+
const pattern = format.value;
|
|
251
|
+
const patternWithoutGroupping = pattern.replace(/,/g, '');
|
|
252
|
+
if (
|
|
253
|
+
!/^((#+)|(#*0+))(\.0*#*)?$/.test(patternWithoutGroupping) &&
|
|
254
|
+
!/^#*0*\.((0*#*)|(#+))$/.test(patternWithoutGroupping) ||
|
|
255
|
+
/,.*,/.test(pattern) ||
|
|
256
|
+
pattern.indexOf(',') > pattern.indexOf('.') && pattern.indexOf('.') > -1
|
|
257
|
+
) {
|
|
258
|
+
throw new Error('Incorrect format pattern.');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const rawParts = pattern.split('.');
|
|
262
|
+
const rawInteger = rawParts[0];
|
|
263
|
+
const rawFraction = rawParts[1] || '';
|
|
264
|
+
|
|
265
|
+
const parts = pattern.replace(/[^#0.]/g, '').split('.');
|
|
266
|
+
const integer = parts[0];
|
|
267
|
+
const fraction = parts[1] || '';
|
|
268
|
+
|
|
269
|
+
const groupIndex = rawInteger.indexOf(',');
|
|
270
|
+
const digitsInGroup = groupIndex > -1 ? rawInteger.length - groupIndex - 1 : -1;
|
|
271
|
+
|
|
272
|
+
if (groupIndex > -1 && digitsInGroup < 1 || rawFraction.indexOf(',') > -1) {
|
|
273
|
+
throw new Error('Incorrect format pattern.');
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
let minimumIntegerDigits = 0;
|
|
278
|
+
while (integer[integer.length - 1 - minimumIntegerDigits] === '0') {
|
|
279
|
+
++minimumIntegerDigits;
|
|
280
|
+
}
|
|
281
|
+
let minimumFractionDigits = 0;
|
|
282
|
+
while (fraction[minimumFractionDigits] === '0') {
|
|
283
|
+
++minimumFractionDigits;
|
|
284
|
+
}
|
|
285
|
+
let maximumFractionDigits = minimumFractionDigits;
|
|
286
|
+
while (fraction[maximumFractionDigits] === '#') {
|
|
287
|
+
++maximumFractionDigits;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const formatter = new Intl.NumberFormat(locale?.value || undefined, {
|
|
291
|
+
useGrouping: false,
|
|
292
|
+
minimumIntegerDigits: Math.min(Math.max(minimumIntegerDigits, 1), 21),
|
|
293
|
+
minimumFractionDigits: Math.min(Math.max(minimumFractionDigits, 0), 100),
|
|
294
|
+
maximumFractionDigits: Math.min(Math.max(maximumFractionDigits, minimumFractionDigits, 0), 100),
|
|
295
|
+
roundingMode: 'halfEven' as any
|
|
296
|
+
} as any);
|
|
297
|
+
|
|
298
|
+
let result = formatter.format(arg.value);
|
|
299
|
+
|
|
300
|
+
if (groupIndex > -1 && digitsInGroup > 0) {
|
|
301
|
+
const groupChar = numberGroupingDivider(locale?.value);
|
|
302
|
+
const fractionChar = numberFractionDivider(locale?.value);
|
|
303
|
+
|
|
304
|
+
if (groupChar && fractionChar) {
|
|
305
|
+
const resultParts = result.split(fractionChar);
|
|
306
|
+
const resultInteger = resultParts[0];
|
|
307
|
+
let res = '';
|
|
308
|
+
for (let i = resultInteger.length - 1; i >= 0; --i) {
|
|
309
|
+
res = resultInteger[i] + res;
|
|
310
|
+
|
|
311
|
+
// eslint-disable-next-line max-depth
|
|
312
|
+
if (i > 0 && (resultInteger.length - i) % digitsInGroup === 0) {
|
|
313
|
+
res = groupChar + res;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
result = res + (resultParts.length > 1 ? fractionChar + resultParts[1] : '');
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (minimumFractionDigits === 0 && maximumFractionDigits === 0 && pattern.endsWith('.')) {
|
|
322
|
+
// force fraction delimeter on end
|
|
323
|
+
const divider = numberFractionDivider(locale?.value);
|
|
324
|
+
if (divider) {
|
|
325
|
+
result += divider;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
type: STRING,
|
|
331
|
+
value: result
|
|
332
|
+
};
|
|
333
|
+
} catch (_err) {
|
|
334
|
+
throw new Error('Incorrect or unsupported number format.' + _err + ' ' + locale?.value || undefined);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export function registerStd(): void {
|
|
339
|
+
registerFunc('toString', [INTEGER], toString);
|
|
340
|
+
registerFunc('toString', [NUMBER], toString);
|
|
341
|
+
registerFunc('toString', [BOOLEAN], toString);
|
|
342
|
+
registerFunc('toString', [COLOR], toString);
|
|
343
|
+
registerFunc('toString', [URL], toString);
|
|
344
|
+
registerFunc('toString', [STRING], toString);
|
|
345
|
+
registerFunc('toString', [ARRAY], toString);
|
|
346
|
+
registerFunc('toString', [DICT], toString);
|
|
347
|
+
|
|
348
|
+
registerFunc('toNumber', [INTEGER], toNumber);
|
|
349
|
+
registerFunc('toNumber', [STRING], toNumber);
|
|
350
|
+
|
|
351
|
+
registerFunc('toInteger', [NUMBER], toIntegerNumber);
|
|
352
|
+
registerFunc('toInteger', [STRING], toIntegerString);
|
|
353
|
+
registerFunc('toInteger', [BOOLEAN], toIntegerBoolean);
|
|
354
|
+
|
|
355
|
+
registerFunc('toBoolean', [INTEGER], toBooleanInteger);
|
|
356
|
+
registerFunc('toBoolean', [STRING], toBooleanString);
|
|
357
|
+
|
|
358
|
+
registerFunc('toColor', [STRING], toColor);
|
|
359
|
+
|
|
360
|
+
registerFunc('toUrl', [STRING], toUrl);
|
|
361
|
+
|
|
362
|
+
registerFunc('encodeUri', [STRING], encodeUri);
|
|
363
|
+
registerFunc('decodeUri', [STRING], decodeUri);
|
|
364
|
+
|
|
365
|
+
registerFunc('getIntegerValue', [STRING, INTEGER], getValue);
|
|
366
|
+
registerFunc('getNumberValue', [STRING, NUMBER], getValue);
|
|
367
|
+
registerFunc('getBooleanValue', [STRING, BOOLEAN], getValue);
|
|
368
|
+
registerFunc('getStringValue', [STRING, STRING], getValue);
|
|
369
|
+
registerFunc('getColorValue', [STRING, COLOR], getColorValue);
|
|
370
|
+
registerFunc('getColorValue', [STRING, STRING], getColorValue);
|
|
371
|
+
registerFunc('getUrlValue', [STRING, URL], getUrlValue);
|
|
372
|
+
registerFunc('getUrlValue', [STRING, STRING], getUrlValue);
|
|
373
|
+
|
|
374
|
+
registerMethod('toString', [INTEGER], toString);
|
|
375
|
+
registerMethod('toString', [NUMBER], toString);
|
|
376
|
+
registerMethod('toString', [BOOLEAN], toString);
|
|
377
|
+
registerMethod('toString', [COLOR], toString);
|
|
378
|
+
registerMethod('toString', [URL], toString);
|
|
379
|
+
registerMethod('toString', [STRING], toString);
|
|
380
|
+
registerMethod('toString', [ARRAY], toString);
|
|
381
|
+
registerMethod('toString', [DICT], toString);
|
|
382
|
+
|
|
383
|
+
registerFunc('decimalFormat', [INTEGER, STRING], decimalFormat);
|
|
384
|
+
registerFunc('decimalFormat', [NUMBER, STRING], decimalFormat);
|
|
385
|
+
registerFunc('decimalFormat', [INTEGER, STRING, STRING], decimalFormat);
|
|
386
|
+
registerFunc('decimalFormat', [NUMBER, STRING, STRING], decimalFormat);
|
|
387
|
+
|
|
388
|
+
registerMethod('decimalFormat', [INTEGER, STRING], decimalFormat);
|
|
389
|
+
registerMethod('decimalFormat', [NUMBER, STRING], decimalFormat);
|
|
390
|
+
registerMethod('decimalFormat', [INTEGER, STRING, STRING], decimalFormat);
|
|
391
|
+
registerMethod('decimalFormat', [NUMBER, STRING, STRING], decimalFormat);
|
|
392
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { EvalContext, EvalValue, StringValue } from '../eval';
|
|
2
|
+
import { registerFunc } from './funcs';
|
|
3
|
+
import { ARRAY, BOOLEAN, COLOR, DICT, INTEGER, NUMBER, STRING, URL } from '../const';
|
|
4
|
+
import { checkUrl, convertJsValueToDivKit } from '../utils';
|
|
5
|
+
|
|
6
|
+
export function getStored(evalType: 'string' | 'number' | 'integer' | 'boolean' | 'color' | 'url' | 'array' | 'dict') {
|
|
7
|
+
return (ctx: EvalContext, name: StringValue, fallback?: EvalValue): EvalValue => {
|
|
8
|
+
if (!ctx.store) {
|
|
9
|
+
if (!fallback) {
|
|
10
|
+
throw new Error('Missing value.');
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
type: evalType,
|
|
14
|
+
value: fallback.value
|
|
15
|
+
} as EvalValue;
|
|
16
|
+
}
|
|
17
|
+
let expectedType: 'boolean' | 'number' | 'string';
|
|
18
|
+
if (evalType === 'boolean') {
|
|
19
|
+
expectedType = 'boolean';
|
|
20
|
+
} else if (evalType === 'number' || evalType === 'integer') {
|
|
21
|
+
expectedType = 'number';
|
|
22
|
+
} else {
|
|
23
|
+
expectedType = 'string';
|
|
24
|
+
}
|
|
25
|
+
let val;
|
|
26
|
+
if (ctx.store.get) {
|
|
27
|
+
val = ctx.store.get(name.value, evalType);
|
|
28
|
+
} else if (ctx.store.getValue) {
|
|
29
|
+
val = ctx.store.getValue(name.value, expectedType);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (val === undefined) {
|
|
33
|
+
if (!fallback) {
|
|
34
|
+
throw new Error('Missing value.');
|
|
35
|
+
}
|
|
36
|
+
if (evalType === 'url') {
|
|
37
|
+
checkUrl(fallback.value);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
type: evalType,
|
|
41
|
+
value: fallback.value
|
|
42
|
+
} as EvalValue;
|
|
43
|
+
} else if (evalType === 'url') {
|
|
44
|
+
checkUrl(val);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return convertJsValueToDivKit(ctx, val, evalType);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function registerStored(): void {
|
|
52
|
+
registerFunc('getStoredIntegerValue', [STRING, INTEGER], getStored(INTEGER));
|
|
53
|
+
registerFunc('getStoredNumberValue', [STRING, NUMBER], getStored(NUMBER));
|
|
54
|
+
registerFunc('getStoredStringValue', [STRING, STRING], getStored(STRING));
|
|
55
|
+
registerFunc('getStoredUrlValue', [STRING, URL], getStored(URL));
|
|
56
|
+
registerFunc('getStoredUrlValue', [STRING, STRING], getStored(URL));
|
|
57
|
+
registerFunc('getStoredColorValue', [STRING, COLOR], getStored(COLOR));
|
|
58
|
+
registerFunc('getStoredColorValue', [STRING, STRING], getStored(COLOR));
|
|
59
|
+
registerFunc('getStoredBooleanValue', [STRING, BOOLEAN], getStored(BOOLEAN));
|
|
60
|
+
registerFunc('getStoredArrayValue', [STRING], getStored(ARRAY));
|
|
61
|
+
registerFunc('getStoredDictValue', [STRING], getStored(DICT));
|
|
62
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { EvalContext, EvalValue, IntegerValue, StringValue } from '../eval';
|
|
2
|
+
import { registerFunc } from './funcs';
|
|
3
|
+
import { BOOLEAN, INTEGER, STRING } from '../const';
|
|
4
|
+
import { escapeRegExp } from '../../utils/escapeRegExp';
|
|
5
|
+
import { valToString } from '../utils';
|
|
6
|
+
import { wrapError } from '../../utils/wrapError';
|
|
7
|
+
import { toBigInt } from '../bigint';
|
|
8
|
+
|
|
9
|
+
function len(_ctx: EvalContext, arg: StringValue): EvalValue {
|
|
10
|
+
return {
|
|
11
|
+
type: INTEGER,
|
|
12
|
+
value: toBigInt(arg.value.length)
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function contains(_ctx: EvalContext, wholeStr: StringValue, partStr: StringValue): EvalValue {
|
|
17
|
+
return {
|
|
18
|
+
type: BOOLEAN,
|
|
19
|
+
value: wholeStr.value.includes(partStr.value) ? 1 : 0
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function substring(_ctx: EvalContext, str: StringValue, start: IntegerValue, end: IntegerValue): EvalValue {
|
|
24
|
+
if (end.value < start.value) {
|
|
25
|
+
throw new Error('Indexes should be in ascending order.');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (
|
|
29
|
+
start.value < 0 || start.value > str.value.length ||
|
|
30
|
+
end.value < 0 || end.value > str.value.length
|
|
31
|
+
) {
|
|
32
|
+
throw new Error('Indexes are out of bounds.');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
type: STRING,
|
|
37
|
+
value: str.value.substring(Number(start.value), Number(end.value))
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function replaceAll(_ctx: EvalContext, str: StringValue, what: StringValue, replacer: StringValue): EvalValue {
|
|
42
|
+
let res: string;
|
|
43
|
+
|
|
44
|
+
if (what.value) {
|
|
45
|
+
res = str.value.replace(new RegExp(escapeRegExp(what.value), 'g'), replacer.value);
|
|
46
|
+
} else {
|
|
47
|
+
// empty str
|
|
48
|
+
res = str.value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
type: STRING,
|
|
53
|
+
value: res
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function index(_ctx: EvalContext, str: StringValue, what: StringValue): EvalValue {
|
|
58
|
+
return {
|
|
59
|
+
type: INTEGER,
|
|
60
|
+
value: toBigInt(str.value.indexOf(what.value))
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function lastIndex(_ctx: EvalContext, str: StringValue, what: StringValue): EvalValue {
|
|
65
|
+
return {
|
|
66
|
+
type: INTEGER,
|
|
67
|
+
value: toBigInt(str.value.lastIndexOf(what.value))
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function trim(_ctx: EvalContext, str: StringValue): EvalValue {
|
|
72
|
+
return {
|
|
73
|
+
type: STRING,
|
|
74
|
+
value: str.value.trim()
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function trimLeft(_ctx: EvalContext, str: StringValue): EvalValue {
|
|
79
|
+
return {
|
|
80
|
+
type: STRING,
|
|
81
|
+
value: str.value.replace(/^\s+/, '')
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function trimRight(_ctx: EvalContext, str: StringValue): EvalValue {
|
|
86
|
+
return {
|
|
87
|
+
type: STRING,
|
|
88
|
+
value: str.value.replace(/\s+$/, '')
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function toUpperCase(_ctx: EvalContext, str: StringValue): EvalValue {
|
|
93
|
+
return {
|
|
94
|
+
type: STRING,
|
|
95
|
+
value: str.value.toUpperCase()
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function toLowerCase(_ctx: EvalContext, str: StringValue): EvalValue {
|
|
100
|
+
return {
|
|
101
|
+
type: STRING,
|
|
102
|
+
value: str.value.toLowerCase()
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function calcPad(
|
|
107
|
+
ctx: EvalContext,
|
|
108
|
+
val: StringValue | IntegerValue,
|
|
109
|
+
len: IntegerValue,
|
|
110
|
+
pad: StringValue
|
|
111
|
+
): string {
|
|
112
|
+
if (!pad.value.length) {
|
|
113
|
+
ctx.warnings.push(wrapError(new Error('String for padding is empty.'), {
|
|
114
|
+
level: 'warn'
|
|
115
|
+
}));
|
|
116
|
+
return '';
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let part = '';
|
|
120
|
+
const str = val.type === STRING ? val.value : valToString(val, false);
|
|
121
|
+
|
|
122
|
+
while (part.length + str.length < len.value) {
|
|
123
|
+
part += pad.value;
|
|
124
|
+
}
|
|
125
|
+
if (part.length > 0 && part.length + str.length > len.value) {
|
|
126
|
+
part = part.substring(0, Number(len.value) - Number(str.length));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return part;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function padStart(
|
|
133
|
+
ctx: EvalContext,
|
|
134
|
+
val: StringValue | IntegerValue,
|
|
135
|
+
len: IntegerValue,
|
|
136
|
+
pad: StringValue
|
|
137
|
+
): EvalValue {
|
|
138
|
+
const prefix = calcPad(ctx, val, len, pad);
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
type: STRING,
|
|
142
|
+
value: prefix + valToString(val, false)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function padEnd(
|
|
147
|
+
ctx: EvalContext,
|
|
148
|
+
val: StringValue | IntegerValue,
|
|
149
|
+
len: IntegerValue,
|
|
150
|
+
pad: StringValue
|
|
151
|
+
): EvalValue {
|
|
152
|
+
const suffix = calcPad(ctx, val, len, pad);
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
type: STRING,
|
|
156
|
+
value: valToString(val, false) + suffix
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function testRegex(_ctx: EvalContext, str: StringValue, regex: StringValue): EvalValue {
|
|
161
|
+
let re: RegExp;
|
|
162
|
+
try {
|
|
163
|
+
re = new RegExp(regex.value);
|
|
164
|
+
} catch (err) {
|
|
165
|
+
throw new Error('Invalid regular expression.');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
type: BOOLEAN,
|
|
170
|
+
value: re.test(str.value) ? 1 : 0
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function encodeRegex(_ctx: EvalContext, str: StringValue): EvalValue {
|
|
175
|
+
return {
|
|
176
|
+
type: STRING,
|
|
177
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping
|
|
178
|
+
value: str.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function registerStrings(): void {
|
|
183
|
+
registerFunc('len', [STRING], len);
|
|
184
|
+
registerFunc('contains', [STRING, STRING], contains);
|
|
185
|
+
registerFunc('substring', [STRING, INTEGER, INTEGER], substring);
|
|
186
|
+
registerFunc('replaceAll', [STRING, STRING, STRING], replaceAll);
|
|
187
|
+
registerFunc('index', [STRING, STRING], index);
|
|
188
|
+
registerFunc('lastIndex', [STRING, STRING], lastIndex);
|
|
189
|
+
registerFunc('trim', [STRING], trim);
|
|
190
|
+
registerFunc('trimLeft', [STRING], trimLeft);
|
|
191
|
+
registerFunc('trimRight', [STRING], trimRight);
|
|
192
|
+
registerFunc('toUpperCase', [STRING], toUpperCase);
|
|
193
|
+
registerFunc('toLowerCase', [STRING], toLowerCase);
|
|
194
|
+
registerFunc('padStart', [STRING, INTEGER, STRING], padStart);
|
|
195
|
+
registerFunc('padStart', [INTEGER, INTEGER, STRING], padStart);
|
|
196
|
+
registerFunc('padEnd', [STRING, INTEGER, STRING], padEnd);
|
|
197
|
+
registerFunc('padEnd', [INTEGER, INTEGER, STRING], padEnd);
|
|
198
|
+
registerFunc('testRegex', [STRING, STRING], testRegex);
|
|
199
|
+
registerFunc('encodeRegex', [STRING], encodeRegex);
|
|
200
|
+
}
|