react-native-divkit 0.1.0-alpha.1 → 0.1.0-alpha.3

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.
Files changed (150) hide show
  1. package/README.md +200 -201
  2. package/dist/DivKit.d.ts.map +1 -1
  3. package/dist/DivKit.js +24 -7
  4. package/dist/DivKit.js.map +1 -1
  5. package/dist/actions/array.d.ts.map +1 -1
  6. package/dist/actions/array.js +1 -1
  7. package/dist/actions/array.js.map +1 -1
  8. package/dist/actions/copyToClipboard.d.ts.map +1 -1
  9. package/dist/actions/copyToClipboard.js +2 -1
  10. package/dist/actions/copyToClipboard.js.map +1 -1
  11. package/dist/actions/dict.d.ts.map +1 -1
  12. package/dist/actions/dict.js.map +1 -1
  13. package/dist/actions/updateStructure.d.ts.map +1 -1
  14. package/dist/actions/updateStructure.js.map +1 -1
  15. package/dist/components/container/DivContainer.d.ts.map +1 -1
  16. package/dist/components/container/DivContainer.js +3 -5
  17. package/dist/components/container/DivContainer.js.map +1 -1
  18. package/dist/components/image/DivImage.d.ts.map +1 -1
  19. package/dist/components/image/DivImage.js +1 -6
  20. package/dist/components/image/DivImage.js.map +1 -1
  21. package/dist/components/state/DivState.d.ts.map +1 -1
  22. package/dist/components/state/DivState.js +3 -5
  23. package/dist/components/state/DivState.js.map +1 -1
  24. package/dist/components/text/DivText.d.ts.map +1 -1
  25. package/dist/components/text/DivText.js +4 -4
  26. package/dist/components/text/DivText.js.map +1 -1
  27. package/dist/components/utilities/Background.d.ts +11 -0
  28. package/dist/components/utilities/Background.d.ts.map +1 -0
  29. package/dist/components/utilities/Background.js +73 -0
  30. package/dist/components/utilities/Background.js.map +1 -0
  31. package/dist/components/utilities/Outer.d.ts.map +1 -1
  32. package/dist/components/utilities/Outer.js +24 -10
  33. package/dist/components/utilities/Outer.js.map +1 -1
  34. package/dist/context/index.d.ts.map +1 -1
  35. package/dist/context/index.js.map +1 -1
  36. package/dist/expressions/eval.d.ts.map +1 -1
  37. package/dist/expressions/eval.js +19 -11
  38. package/dist/expressions/eval.js.map +1 -1
  39. package/dist/expressions/funcs/array.d.ts.map +1 -1
  40. package/dist/expressions/funcs/array.js +72 -168
  41. package/dist/expressions/funcs/array.js.map +1 -1
  42. package/dist/expressions/funcs/colors.d.ts.map +1 -1
  43. package/dist/expressions/funcs/colors.js.map +1 -1
  44. package/dist/expressions/funcs/customFuncs.d.ts.map +1 -1
  45. package/dist/expressions/funcs/customFuncs.js +6 -4
  46. package/dist/expressions/funcs/customFuncs.js.map +1 -1
  47. package/dist/expressions/funcs/datetime.d.ts.map +1 -1
  48. package/dist/expressions/funcs/datetime.js +1 -1
  49. package/dist/expressions/funcs/datetime.js.map +1 -1
  50. package/dist/expressions/funcs/dict.d.ts.map +1 -1
  51. package/dist/expressions/funcs/dict.js.map +1 -1
  52. package/dist/expressions/funcs/funcs.d.ts.map +1 -1
  53. package/dist/expressions/funcs/funcs.js +21 -13
  54. package/dist/expressions/funcs/funcs.js.map +1 -1
  55. package/dist/expressions/funcs/math.d.ts.map +1 -1
  56. package/dist/expressions/funcs/math.js +40 -20
  57. package/dist/expressions/funcs/math.js.map +1 -1
  58. package/dist/expressions/funcs/std.d.ts.map +1 -1
  59. package/dist/expressions/funcs/std.js +4 -4
  60. package/dist/expressions/funcs/std.js.map +1 -1
  61. package/dist/expressions/funcs/strings.d.ts.map +1 -1
  62. package/dist/expressions/funcs/strings.js +1 -2
  63. package/dist/expressions/funcs/strings.js.map +1 -1
  64. package/dist/expressions/funcs/trigonometry.js +2 -2
  65. package/dist/expressions/funcs/trigonometry.js.map +1 -1
  66. package/dist/expressions/json.d.ts +2 -2
  67. package/dist/expressions/json.d.ts.map +1 -1
  68. package/dist/expressions/json.js +6 -4
  69. package/dist/expressions/json.js.map +1 -1
  70. package/dist/expressions/utils.d.ts.map +1 -1
  71. package/dist/expressions/utils.js +9 -10
  72. package/dist/expressions/utils.js.map +1 -1
  73. package/dist/expressions/variable.d.ts.map +1 -1
  74. package/dist/expressions/variable.js +3 -7
  75. package/dist/expressions/variable.js.map +1 -1
  76. package/dist/expressions/walk.d.ts.map +1 -1
  77. package/dist/expressions/walk.js.map +1 -1
  78. package/dist/hooks/index.d.ts.map +1 -1
  79. package/dist/hooks/index.js.map +1 -1
  80. package/dist/hooks/useAction.d.ts.map +1 -1
  81. package/dist/hooks/useAction.js.map +1 -1
  82. package/dist/hooks/useDerivedFromVars.d.ts.map +1 -1
  83. package/dist/hooks/useDerivedFromVars.js.map +1 -1
  84. package/dist/hooks/useVariable.d.ts.map +1 -1
  85. package/dist/hooks/useVariable.js +4 -4
  86. package/dist/hooks/useVariable.js.map +1 -1
  87. package/dist/index.d.ts.map +1 -1
  88. package/dist/index.js.map +1 -1
  89. package/dist/stores/createObservable.d.ts.map +1 -1
  90. package/dist/stores/createObservable.js.map +1 -1
  91. package/dist/utils/applyTemplate.d.ts +2 -2
  92. package/dist/utils/applyTemplate.d.ts.map +1 -1
  93. package/dist/utils/applyTemplate.js +13 -13
  94. package/dist/utils/applyTemplate.js.map +1 -1
  95. package/dist/utils/correctColor.d.ts.map +1 -1
  96. package/dist/utils/correctColor.js +8 -6
  97. package/dist/utils/correctColor.js.map +1 -1
  98. package/dist/utils/formatDate.d.ts.map +1 -1
  99. package/dist/utils/formatDate.js +7 -10
  100. package/dist/utils/formatDate.js.map +1 -1
  101. package/dist/utils/wrapError.d.ts.map +1 -1
  102. package/dist/utils/wrapError.js.map +1 -1
  103. package/package.json +8 -7
  104. package/src/DivKit.tsx +258 -220
  105. package/src/actions/array.ts +91 -64
  106. package/src/actions/copyToClipboard.ts +28 -19
  107. package/src/actions/dict.ts +36 -26
  108. package/src/actions/updateStructure.ts +86 -61
  109. package/src/components/README.md +38 -18
  110. package/src/components/container/DivContainer.tsx +4 -14
  111. package/src/components/image/DivImage.tsx +1 -11
  112. package/src/components/state/DivState.tsx +3 -9
  113. package/src/components/text/DivText.tsx +8 -20
  114. package/src/components/utilities/Background.tsx +120 -0
  115. package/src/components/utilities/Outer.tsx +24 -23
  116. package/src/components/utilities/README.md +37 -32
  117. package/src/context/index.ts +2 -11
  118. package/src/expressions/ast.d.ts +16 -9
  119. package/src/expressions/eval.ts +82 -37
  120. package/src/expressions/funcs/array.ts +129 -209
  121. package/src/expressions/funcs/colors.ts +1 -3
  122. package/src/expressions/funcs/customFuncs.ts +6 -4
  123. package/src/expressions/funcs/datetime.ts +10 -3
  124. package/src/expressions/funcs/dict.ts +16 -2
  125. package/src/expressions/funcs/funcs.ts +75 -89
  126. package/src/expressions/funcs/math.ts +103 -43
  127. package/src/expressions/funcs/std.ts +4 -7
  128. package/src/expressions/funcs/strings.ts +9 -25
  129. package/src/expressions/funcs/trigonometry.ts +2 -2
  130. package/src/expressions/json.ts +60 -53
  131. package/src/expressions/utils.ts +24 -22
  132. package/src/expressions/variable.ts +5 -21
  133. package/src/expressions/walk.ts +6 -3
  134. package/src/hooks/README.md +61 -53
  135. package/src/hooks/index.ts +3 -18
  136. package/src/hooks/useAction.ts +1 -3
  137. package/src/hooks/useDerivedFromVars.ts +3 -13
  138. package/src/hooks/useVariable.ts +7 -17
  139. package/src/index.ts +10 -48
  140. package/src/stores/createObservable.ts +35 -35
  141. package/src/types/alignment.d.ts +15 -6
  142. package/src/types/background.d.ts +6 -2
  143. package/src/types/base.d.ts +41 -9
  144. package/src/types/componentContext.d.ts +27 -22
  145. package/src/types/container.d.ts +1 -4
  146. package/src/types/text.d.ts +1 -1
  147. package/src/utils/applyTemplate.ts +103 -109
  148. package/src/utils/correctColor.ts +9 -8
  149. package/src/utils/formatDate.ts +175 -86
  150. package/src/utils/wrapError.ts +7 -4
@@ -3,10 +3,12 @@ import type { EvalTypes, EvalValue } from '../eval';
3
3
  import type { EvalContext } from '../eval';
4
4
  import { integerToNumber } from '../utils';
5
5
 
6
- export type FuncArg = EvalTypes | {
7
- type: EvalTypes;
8
- isVararg?: boolean;
9
- }
6
+ export type FuncArg =
7
+ | EvalTypes
8
+ | {
9
+ type: EvalTypes;
10
+ isVararg?: boolean;
11
+ };
10
12
 
11
13
  export interface Func {
12
14
  name?: string;
@@ -19,27 +21,31 @@ export const funcByArgs: Map<string, Func> = new Map();
19
21
  export const methods: Map<string, Func[]> = new Map();
20
22
  export const methodByArgs: Map<string, Func> = new Map();
21
23
 
22
- export type FuncMatchError = {
23
- type: 'mismatch';
24
- expected: EvalTypes;
25
- found: EvalTypes;
26
- def: Func;
27
- hasOverloads: boolean;
28
- } | {
29
- type: 'few';
30
- expected: number;
31
- found: number;
32
- def: Func;
33
- hasOverloads: boolean;
34
- } | {
35
- type: 'many';
36
- expected: number;
37
- found: number;
38
- def: Func;
39
- hasOverloads: boolean;
40
- } | {
41
- type: 'missing';
42
- };
24
+ export type FuncMatchError =
25
+ | {
26
+ type: 'mismatch';
27
+ expected: EvalTypes;
28
+ found: EvalTypes;
29
+ def: Func;
30
+ hasOverloads: boolean;
31
+ }
32
+ | {
33
+ type: 'few';
34
+ expected: number;
35
+ found: number;
36
+ def: Func;
37
+ hasOverloads: boolean;
38
+ }
39
+ | {
40
+ type: 'many';
41
+ expected: number;
42
+ found: number;
43
+ def: Func;
44
+ hasOverloads: boolean;
45
+ }
46
+ | {
47
+ type: 'missing';
48
+ };
43
49
 
44
50
  export interface FuncMatchFound {
45
51
  func: Func;
@@ -51,35 +57,19 @@ export type FuncMatch = FuncMatchFound | FuncMatchError;
51
57
  // no args
52
58
  export function registerFunc(name: string, args: [], cb: (ctx?: EvalContext) => EvalValue): void;
53
59
  // one specific arg
54
- export function registerFunc<
55
- A0 extends EvalTypes
56
- >(
60
+ export function registerFunc<A0 extends EvalTypes>(
57
61
  name: string,
58
62
  args: [A0],
59
- cb: (
60
- ctx: EvalContext,
61
- arg0: Extract<EvalValue, { type: A0 }>
62
- ) => EvalValue
63
+ cb: (ctx: EvalContext, arg0: Extract<EvalValue, { type: A0 }>) => EvalValue
63
64
  ): void;
64
65
  // two specific args
65
- export function registerFunc<
66
- A0 extends EvalTypes,
67
- A1 extends EvalTypes
68
- >(
66
+ export function registerFunc<A0 extends EvalTypes, A1 extends EvalTypes>(
69
67
  name: string,
70
68
  args: [A0, A1],
71
- cb: (
72
- ctx: EvalContext,
73
- arg0: Extract<EvalValue, { type: A0 }>,
74
- arg1: Extract<EvalValue, { type: A1 }>
75
- ) => EvalValue
69
+ cb: (ctx: EvalContext, arg0: Extract<EvalValue, { type: A0 }>, arg1: Extract<EvalValue, { type: A1 }>) => EvalValue
76
70
  ): void;
77
71
  // three specific args
78
- export function registerFunc<
79
- A0 extends EvalTypes,
80
- A1 extends EvalTypes,
81
- A2 extends EvalTypes
82
- >(
72
+ export function registerFunc<A0 extends EvalTypes, A1 extends EvalTypes, A2 extends EvalTypes>(
83
73
  name: string,
84
74
  args: [A0, A1, A2],
85
75
  cb: (
@@ -90,11 +80,7 @@ export function registerFunc<
90
80
  ) => EvalValue
91
81
  ): void;
92
82
  // any args
93
- export function registerFunc(
94
- name: string,
95
- args: FuncArg[],
96
- cb: (ctx: EvalContext, ...args: any[]) => EvalValue
97
- ): void;
83
+ export function registerFunc(name: string, args: FuncArg[], cb: (ctx: EvalContext, ...args: any[]) => EvalValue): void;
98
84
 
99
85
  export function registerFunc(
100
86
  name: string,
@@ -113,12 +99,17 @@ export function registerFunc(
113
99
  }
114
100
  arr.push(desc);
115
101
 
116
- const funcKey = name + ':' + args.map(it => {
117
- if (typeof it === 'object') {
118
- return it.type;
119
- }
120
- return it;
121
- }).join('#');
102
+ const funcKey =
103
+ name +
104
+ ':' +
105
+ args
106
+ .map(it => {
107
+ if (typeof it === 'object') {
108
+ return it.type;
109
+ }
110
+ return it;
111
+ })
112
+ .join('#');
122
113
 
123
114
  funcByArgs.set(funcKey, desc);
124
115
  }
@@ -126,35 +117,19 @@ export function registerFunc(
126
117
  // no args
127
118
  export function registerMethod(name: string, args: [], cb: (ctx?: EvalContext) => EvalValue): void;
128
119
  // one specific arg
129
- export function registerMethod<
130
- A0 extends EvalTypes
131
- >(
120
+ export function registerMethod<A0 extends EvalTypes>(
132
121
  name: string,
133
122
  args: [A0],
134
- cb: (
135
- ctx: EvalContext,
136
- arg0: Extract<EvalValue, { type: A0 }>
137
- ) => EvalValue
123
+ cb: (ctx: EvalContext, arg0: Extract<EvalValue, { type: A0 }>) => EvalValue
138
124
  ): void;
139
125
  // two specific args
140
- export function registerMethod<
141
- A0 extends EvalTypes,
142
- A1 extends EvalTypes
143
- >(
126
+ export function registerMethod<A0 extends EvalTypes, A1 extends EvalTypes>(
144
127
  name: string,
145
128
  args: [A0, A1],
146
- cb: (
147
- ctx: EvalContext,
148
- arg0: Extract<EvalValue, { type: A0 }>,
149
- arg1: Extract<EvalValue, { type: A1 }>
150
- ) => EvalValue
129
+ cb: (ctx: EvalContext, arg0: Extract<EvalValue, { type: A0 }>, arg1: Extract<EvalValue, { type: A1 }>) => EvalValue
151
130
  ): void;
152
131
  // three specific args
153
- export function registerMethod<
154
- A0 extends EvalTypes,
155
- A1 extends EvalTypes,
156
- A2 extends EvalTypes
157
- >(
132
+ export function registerMethod<A0 extends EvalTypes, A1 extends EvalTypes, A2 extends EvalTypes>(
158
133
  name: string,
159
134
  args: [A0, A1, A2],
160
135
  cb: (
@@ -188,20 +163,31 @@ export function registerMethod(
188
163
  }
189
164
  arr.push(desc);
190
165
 
191
- const funcKey = name + ':' + args.map(it => {
192
- if (typeof it === 'object') {
193
- return it.type;
194
- }
195
- return it;
196
- }).join('#');
166
+ const funcKey =
167
+ name +
168
+ ':' +
169
+ args
170
+ .map(it => {
171
+ if (typeof it === 'object') {
172
+ return it.type;
173
+ }
174
+ return it;
175
+ })
176
+ .join('#');
197
177
 
198
178
  methodByArgs.set(funcKey, desc);
199
179
  }
200
180
 
201
- function matchFuncArgs(func: Func, args: EvalValue[], hasOverloads: boolean): {
202
- type: 'match';
203
- conversions: number;
204
- } | FuncMatchError {
181
+ function matchFuncArgs(
182
+ func: Func,
183
+ args: EvalValue[],
184
+ hasOverloads: boolean
185
+ ):
186
+ | {
187
+ type: 'match';
188
+ conversions: number;
189
+ }
190
+ | FuncMatchError {
205
191
  const minArgs = func.args.length;
206
192
  let maxArgs = func.args.length;
207
193
  let conversions = 0;
@@ -319,5 +305,5 @@ export function convertArgs(func: Func, args: EvalValue[]): EvalValue[] {
319
305
  }
320
306
 
321
307
  export function funcToKey(funcName: string, func: Func): string {
322
- return funcName + ':' + func.args.map(arg => typeof arg === 'string' ? arg : arg.type).join('#');
308
+ return funcName + ':' + func.args.map(arg => (typeof arg === 'string' ? arg : arg.type)).join('#');
323
309
  }
@@ -329,53 +329,113 @@ export function registerMath(): void {
329
329
  registerFunc('mod', [INTEGER, INTEGER], modInteger);
330
330
  registerFunc('mod', [NUMBER, NUMBER], modNumber);
331
331
 
332
- registerFunc('mul', [{
333
- type: INTEGER,
334
- isVararg: true
335
- }], mulInteger);
336
- registerFunc('mul', [{
337
- type: NUMBER,
338
- isVararg: true
339
- }], mulNumber);
340
-
341
- registerFunc('sub', [{
342
- type: INTEGER,
343
- isVararg: true
344
- }], subInteger);
345
- registerFunc('sub', [{
346
- type: NUMBER,
347
- isVararg: true
348
- }], subNumber);
349
-
350
- registerFunc('sum', [{
351
- type: INTEGER,
352
- isVararg: true
353
- }], sumInteger);
354
- registerFunc('sum', [{
355
- type: NUMBER,
356
- isVararg: true
357
- }], sumNumber);
332
+ registerFunc(
333
+ 'mul',
334
+ [
335
+ {
336
+ type: INTEGER,
337
+ isVararg: true
338
+ }
339
+ ],
340
+ mulInteger
341
+ );
342
+ registerFunc(
343
+ 'mul',
344
+ [
345
+ {
346
+ type: NUMBER,
347
+ isVararg: true
348
+ }
349
+ ],
350
+ mulNumber
351
+ );
352
+
353
+ registerFunc(
354
+ 'sub',
355
+ [
356
+ {
357
+ type: INTEGER,
358
+ isVararg: true
359
+ }
360
+ ],
361
+ subInteger
362
+ );
363
+ registerFunc(
364
+ 'sub',
365
+ [
366
+ {
367
+ type: NUMBER,
368
+ isVararg: true
369
+ }
370
+ ],
371
+ subNumber
372
+ );
373
+
374
+ registerFunc(
375
+ 'sum',
376
+ [
377
+ {
378
+ type: INTEGER,
379
+ isVararg: true
380
+ }
381
+ ],
382
+ sumInteger
383
+ );
384
+ registerFunc(
385
+ 'sum',
386
+ [
387
+ {
388
+ type: NUMBER,
389
+ isVararg: true
390
+ }
391
+ ],
392
+ sumNumber
393
+ );
358
394
 
359
395
  registerFunc('abs', [INTEGER], absInteger);
360
396
  registerFunc('abs', [NUMBER], absNumber);
361
397
 
362
- registerFunc('max', [{
363
- type: INTEGER,
364
- isVararg: true
365
- }], maxInt);
366
- registerFunc('max', [{
367
- type: NUMBER,
368
- isVararg: true
369
- }], maxNum);
370
-
371
- registerFunc('min', [{
372
- type: INTEGER,
373
- isVararg: true
374
- }], minInt);
375
- registerFunc('min', [{
376
- type: NUMBER,
377
- isVararg: true
378
- }], minNum);
398
+ registerFunc(
399
+ 'max',
400
+ [
401
+ {
402
+ type: INTEGER,
403
+ isVararg: true
404
+ }
405
+ ],
406
+ maxInt
407
+ );
408
+ registerFunc(
409
+ 'max',
410
+ [
411
+ {
412
+ type: NUMBER,
413
+ isVararg: true
414
+ }
415
+ ],
416
+ maxNum
417
+ );
418
+
419
+ registerFunc(
420
+ 'min',
421
+ [
422
+ {
423
+ type: INTEGER,
424
+ isVararg: true
425
+ }
426
+ ],
427
+ minInt
428
+ );
429
+ registerFunc(
430
+ 'min',
431
+ [
432
+ {
433
+ type: NUMBER,
434
+ isVararg: true
435
+ }
436
+ ],
437
+ minNum
438
+ );
379
439
 
380
440
  registerFunc('maxNumber', [], maxNumber);
381
441
  registerFunc('minNumber', [], minNumber);
@@ -26,7 +26,6 @@ function toString(
26
26
  };
27
27
  }
28
28
 
29
-
30
29
  function toNumber(_ctx: EvalContext, arg: IntegerValue | StringValue): EvalValue {
31
30
  const num = Number(arg.value);
32
31
 
@@ -43,7 +42,6 @@ function toNumber(_ctx: EvalContext, arg: IntegerValue | StringValue): EvalValue
43
42
  };
44
43
  }
45
44
 
46
-
47
45
  function toIntegerNumber(_ctx: EvalContext, arg: NumberValue): EvalValue {
48
46
  if (arg.value > MAX_INT || arg.value < MIN_INT) {
49
47
  throw new Error('Unable to convert value to Integer.');
@@ -79,7 +77,6 @@ function toIntegerBoolean(_ctx: EvalContext, arg: BooleanValue): EvalValue {
79
77
  };
80
78
  }
81
79
 
82
-
83
80
  function toBooleanInteger(_ctx: EvalContext, arg: IntegerValue): EvalValue {
84
81
  const intVal = Number(arg.value);
85
82
  if (intVal !== 1 && intVal !== 0) {
@@ -250,10 +247,10 @@ function decimalFormat(
250
247
  const pattern = format.value;
251
248
  const patternWithoutGroupping = pattern.replace(/,/g, '');
252
249
  if (
253
- !/^((#+)|(#*0+))(\.0*#*)?$/.test(patternWithoutGroupping) &&
254
- !/^#*0*\.((0*#*)|(#+))$/.test(patternWithoutGroupping) ||
250
+ (!/^((#+)|(#*0+))(\.0*#*)?$/.test(patternWithoutGroupping) &&
251
+ !/^#*0*\.((0*#*)|(#+))$/.test(patternWithoutGroupping)) ||
255
252
  /,.*,/.test(pattern) ||
256
- pattern.indexOf(',') > pattern.indexOf('.') && pattern.indexOf('.') > -1
253
+ (pattern.indexOf(',') > pattern.indexOf('.') && pattern.indexOf('.') > -1)
257
254
  ) {
258
255
  throw new Error('Incorrect format pattern.');
259
256
  }
@@ -269,7 +266,7 @@ function decimalFormat(
269
266
  const groupIndex = rawInteger.indexOf(',');
270
267
  const digitsInGroup = groupIndex > -1 ? rawInteger.length - groupIndex - 1 : -1;
271
268
 
272
- if (groupIndex > -1 && digitsInGroup < 1 || rawFraction.indexOf(',') > -1) {
269
+ if ((groupIndex > -1 && digitsInGroup < 1) || rawFraction.indexOf(',') > -1) {
273
270
  throw new Error('Incorrect format pattern.');
274
271
  }
275
272
 
@@ -25,10 +25,7 @@ function substring(_ctx: EvalContext, str: StringValue, start: IntegerValue, end
25
25
  throw new Error('Indexes should be in ascending order.');
26
26
  }
27
27
 
28
- if (
29
- start.value < 0 || start.value > str.value.length ||
30
- end.value < 0 || end.value > str.value.length
31
- ) {
28
+ if (start.value < 0 || start.value > str.value.length || end.value < 0 || end.value > str.value.length) {
32
29
  throw new Error('Indexes are out of bounds.');
33
30
  }
34
31
 
@@ -103,16 +100,13 @@ function toLowerCase(_ctx: EvalContext, str: StringValue): EvalValue {
103
100
  };
104
101
  }
105
102
 
106
- function calcPad(
107
- ctx: EvalContext,
108
- val: StringValue | IntegerValue,
109
- len: IntegerValue,
110
- pad: StringValue
111
- ): string {
103
+ function calcPad(ctx: EvalContext, val: StringValue | IntegerValue, len: IntegerValue, pad: StringValue): string {
112
104
  if (!pad.value.length) {
113
- ctx.warnings.push(wrapError(new Error('String for padding is empty.'), {
114
- level: 'warn'
115
- }));
105
+ ctx.warnings.push(
106
+ wrapError(new Error('String for padding is empty.'), {
107
+ level: 'warn'
108
+ })
109
+ );
116
110
  return '';
117
111
  }
118
112
 
@@ -129,12 +123,7 @@ function calcPad(
129
123
  return part;
130
124
  }
131
125
 
132
- function padStart(
133
- ctx: EvalContext,
134
- val: StringValue | IntegerValue,
135
- len: IntegerValue,
136
- pad: StringValue
137
- ): EvalValue {
126
+ function padStart(ctx: EvalContext, val: StringValue | IntegerValue, len: IntegerValue, pad: StringValue): EvalValue {
138
127
  const prefix = calcPad(ctx, val, len, pad);
139
128
 
140
129
  return {
@@ -143,12 +132,7 @@ function padStart(
143
132
  };
144
133
  }
145
134
 
146
- function padEnd(
147
- ctx: EvalContext,
148
- val: StringValue | IntegerValue,
149
- len: IntegerValue,
150
- pad: StringValue
151
- ): EvalValue {
135
+ function padEnd(ctx: EvalContext, val: StringValue | IntegerValue, len: IntegerValue, pad: StringValue): EvalValue {
152
136
  const suffix = calcPad(ctx, val, len, pad);
153
137
 
154
138
  return {
@@ -12,14 +12,14 @@ function pi(): EvalValue {
12
12
  function toRadians(_ctx: EvalContext, degrees: NumberValue): EvalValue {
13
13
  return {
14
14
  type: NUMBER,
15
- value: degrees.value / 180 * Math.PI
15
+ value: (degrees.value / 180) * Math.PI
16
16
  };
17
17
  }
18
18
 
19
19
  function toDegrees(_ctx: EvalContext, radians: NumberValue): EvalValue {
20
20
  return {
21
21
  type: NUMBER,
22
- value: radians.value / Math.PI * 180
22
+ value: (radians.value / Math.PI) * 180
23
23
  };
24
24
  }
25
25
 
@@ -27,23 +27,21 @@ class ExpressionBinding {
27
27
  * @param variables
28
28
  * @param logError
29
29
  */
30
- apply<T>(
31
- {
32
- variables,
33
- customFunctions,
34
- logError,
35
- store,
36
- weekStartDay,
37
- keepComplex
38
- }: {
39
- variables: VariablesMap;
40
- customFunctions: CustomFunctions | undefined;
41
- logError: LogError;
42
- store: Store | undefined;
43
- weekStartDay: number;
44
- keepComplex?: boolean;
45
- }
46
- ): {
30
+ apply<T>({
31
+ variables,
32
+ customFunctions,
33
+ logError,
34
+ store,
35
+ weekStartDay,
36
+ keepComplex
37
+ }: {
38
+ variables: VariablesMap;
39
+ customFunctions: CustomFunctions | undefined;
40
+ logError: LogError;
41
+ store: Store | undefined;
42
+ weekStartDay: number;
43
+ keepComplex?: boolean;
44
+ }): {
47
45
  result: T;
48
46
  usedVars?: Set<Variable>;
49
47
  } {
@@ -57,12 +55,14 @@ class ExpressionBinding {
57
55
  const result = res.result;
58
56
 
59
57
  if (result.type === 'error') {
60
- logError(wrapError(new Error('Expression execution error'), {
61
- additional: {
62
- message: result.value,
63
- expression: this.expr
64
- }
65
- }));
58
+ logError(
59
+ wrapError(new Error('Expression execution error'), {
60
+ additional: {
61
+ message: result.value,
62
+ expression: this.expr
63
+ }
64
+ })
65
+ );
66
66
  return {
67
67
  result: undefined as T,
68
68
  usedVars: res.usedVars
@@ -130,11 +130,13 @@ class ExpressionBinding {
130
130
  usedVars: res.usedVars
131
131
  };
132
132
  } catch (err) {
133
- logError(wrapError(new Error('Expression execution error'), {
134
- additional: {
135
- expression: this.expr
136
- }
137
- }));
133
+ logError(
134
+ wrapError(new Error('Expression execution error'), {
135
+ additional: {
136
+ expression: this.expr
137
+ }
138
+ })
139
+ );
138
140
  return {
139
141
  result: undefined as T,
140
142
  usedVars: res?.usedVars
@@ -165,17 +167,15 @@ class VariableBinding {
165
167
  }
166
168
  }
167
169
 
168
- export type MaybeMissing<T> = T | (
169
- T extends (infer U)[] ?
170
- MaybeMissing<U>[] :
171
- (
172
- T extends object ?
173
- {
174
- [P in keyof T]?: MaybeMissing<T[P]>;
175
- } :
176
- T | undefined
177
- )
178
- );
170
+ export type MaybeMissing<T> =
171
+ | T
172
+ | (T extends (infer U)[]
173
+ ? MaybeMissing<U>[]
174
+ : T extends object
175
+ ? {
176
+ [P in keyof T]?: MaybeMissing<T[P]>;
177
+ }
178
+ : T | undefined);
179
179
 
180
180
  function hasExpressions(str: string): boolean {
181
181
  return str.indexOf('@{') > -1 || str.indexOf('\\') > -1;
@@ -197,20 +197,24 @@ function prepareVarsObj<T>(
197
197
 
198
198
  if (process.env.ENABLE_EXPRESSIONS || process.env.ENABLE_EXPRESSIONS === undefined) {
199
199
  try {
200
- const ast = cacheGet(jsonProp) || parse(jsonProp, {
201
- startRule: 'JsonStringContents'
202
- });
200
+ const ast =
201
+ cacheGet(jsonProp) ||
202
+ parse(jsonProp, {
203
+ startRule: 'JsonStringContents'
204
+ });
203
205
  cacheSet(jsonProp, ast);
204
206
  const propVars = gatherVarsFromAst(ast);
205
207
  store.vars.push(...propVars);
206
208
 
207
209
  return new ExpressionBinding(ast, jsonProp);
208
210
  } catch (err) {
209
- logError(wrapError(new Error('Unable to parse expression'), {
210
- additional: {
211
- expression: jsonProp
212
- }
213
- }));
211
+ logError(
212
+ wrapError(new Error('Unable to parse expression'), {
213
+ additional: {
214
+ expression: jsonProp
215
+ }
216
+ })
217
+ );
214
218
  return undefined;
215
219
  }
216
220
  } else {
@@ -222,11 +226,13 @@ function prepareVarsObj<T>(
222
226
  try {
223
227
  return simpleUnescapeString(jsonProp);
224
228
  } catch (err: any) {
225
- logError(wrapError(err as Error, {
226
- additional: {
227
- expression: jsonProp
228
- }
229
- }));
229
+ logError(
230
+ wrapError(err as Error, {
231
+ additional: {
232
+ expression: jsonProp
233
+ }
234
+ })
235
+ );
230
236
  return undefined;
231
237
  }
232
238
  }
@@ -265,7 +271,8 @@ function applyVars<T>(
265
271
  ) {
266
272
  return jsonProp.apply<T>(opts);
267
273
  } else if (
268
- (!process.env.ENABLE_EXPRESSIONS && process.env.ENABLE_EXPRESSIONS !== undefined) &&
274
+ !process.env.ENABLE_EXPRESSIONS &&
275
+ process.env.ENABLE_EXPRESSIONS !== undefined &&
269
276
  jsonProp instanceof VariableBinding
270
277
  ) {
271
278
  return {