@fincity/kirun-js 2.9.0 → 2.11.0

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.
@@ -2,41 +2,45 @@ import { ExecutionException } from '../../exception/ExecutionException';
2
2
  import { LinkedList } from '../../util/LinkedList';
3
3
  import { StringBuilder } from '../../util/string/StringBuilder';
4
4
  import { StringFormatter } from '../../util/string/StringFormatter';
5
- import { FunctionExecutionParameters } from '../FunctionExecutionParameters';
6
5
  import { ExpressionEvaluationException } from './exception/ExpressionEvaluationException';
7
6
  import { Expression } from './Expression';
8
7
  import { ExpressionToken } from './ExpressionToken';
9
8
  import { ExpressionTokenValue } from './ExpressionTokenValue';
10
9
  import { Operation } from './Operation';
11
- import { LogicalNullishCoalescingOperator } from './operators/binary/LogicalNullishCoalescingOperator';
12
- import { ArithmeticAdditionOperator } from './operators/binary/ArithmeticAdditionOperator';
13
- import { ArithmeticDivisionOperator } from './operators/binary/ArithmeticDivisionOperator';
14
- import { ArithmeticIntegerDivisionOperator } from './operators/binary/ArithmeticInetgerDivisionOperator';
15
- import { ArithmeticModulusOperator } from './operators/binary/ArithmeticModulusOperator';
16
- import { ArithmeticMultiplicationOperator } from './operators/binary/ArithmeticMultiplicationOperator';
17
- import { ArithmeticSubtractionOperator } from './operators/binary/ArithmeticSubtractionOperator';
18
- import { ArrayOperator } from './operators/binary/ArrayOperator';
19
- import { BinaryOperator } from './operators/binary/BinaryOperator';
20
- import { BitwiseAndOperator } from './operators/binary/BitwiseAndOperator';
21
- import { BitwiseLeftShiftOperator } from './operators/binary/BitwiseLeftShiftOperator';
22
- import { BitwiseOrOperator } from './operators/binary/BitwiseOrOperator';
23
- import { BitwiseRightShiftOperator } from './operators/binary/BitwiseRightShiftOperator';
24
- import { BitwiseUnsignedRightShiftOperator } from './operators/binary/BitwiseUnsignedRightShiftOperator';
25
- import { BitwiseXorOperator } from './operators/binary/BitwiseXorOperator';
26
- import { LogicalAndOperator } from './operators/binary/LogicalAndOperator';
27
- import { LogicalEqualOperator } from './operators/binary/LogicalEqualOperator';
28
- import { LogicalGreaterThanEqualOperator } from './operators/binary/LogicalGreaterThanEqualOperator';
29
- import { LogicalGreaterThanOperator } from './operators/binary/LogicalGreaterThanOperator';
30
- import { LogicalLessThanEqualOperator } from './operators/binary/LogicalLessThanEqualOperator';
31
- import { LogicalLessThanOperator } from './operators/binary/LogicalLessThanOperator';
32
- import { LogicalNotEqualOperator } from './operators/binary/LogicalNotEqualOperator';
33
- import { LogicalOrOperator } from './operators/binary/LogicalOrOperator';
34
- import { ObjectOperator } from './operators/binary/ObjectOperator';
35
- import { ArithmeticUnaryMinusOperator } from './operators/unary/ArithmeticUnaryMinusOperator';
36
- import { ArithmeticUnaryPlusOperator } from './operators/unary/ArithmeticUnaryPlusOperator';
37
- import { BitwiseComplementOperator } from './operators/unary/BitwiseComplementOperator';
38
- import { LogicalNotOperator } from './operators/unary/LogicalNotOperator';
39
- import { UnaryOperator } from './operators/unary/UnaryOperator';
10
+ import { LogicalNullishCoalescingOperator } from './operators/binary';
11
+ import {
12
+ ArithmeticAdditionOperator,
13
+ ArithmeticDivisionOperator,
14
+ ArithmeticIntegerDivisionOperator,
15
+ ArithmeticModulusOperator,
16
+ ArithmeticMultiplicationOperator,
17
+ ArithmeticSubtractionOperator,
18
+ ArrayOperator,
19
+ ArrayRangeOperator,
20
+ BinaryOperator,
21
+ BitwiseAndOperator,
22
+ BitwiseLeftShiftOperator,
23
+ BitwiseOrOperator,
24
+ BitwiseRightShiftOperator,
25
+ BitwiseUnsignedRightShiftOperator,
26
+ BitwiseXorOperator,
27
+ LogicalAndOperator,
28
+ LogicalEqualOperator,
29
+ LogicalGreaterThanEqualOperator,
30
+ LogicalGreaterThanOperator,
31
+ LogicalLessThanEqualOperator,
32
+ LogicalLessThanOperator,
33
+ LogicalNotEqualOperator,
34
+ LogicalOrOperator,
35
+ ObjectOperator,
36
+ } from './operators/binary/';
37
+ import {
38
+ ArithmeticUnaryMinusOperator,
39
+ ArithmeticUnaryPlusOperator,
40
+ BitwiseComplementOperator,
41
+ LogicalNotOperator,
42
+ UnaryOperator,
43
+ } from './operators/unary';
40
44
  import { LiteralTokenValueExtractor } from './tokenextractor/LiteralTokenValueExtractor';
41
45
  import { TokenValueExtractor } from './tokenextractor/TokenValueExtractor';
42
46
  import { Tuple2 } from '../../util/Tuples';
@@ -82,6 +86,7 @@ export class ExpressionEvaluator {
82
86
  [Operation.NULLISH_COALESCING_OPERATOR, new LogicalNullishCoalescingOperator()],
83
87
 
84
88
  [Operation.ARRAY_OPERATOR, new ArrayOperator()],
89
+ [Operation.ARRAY_RANGE_INDEX_OPERATOR, new ArrayRangeOperator()],
85
90
  [Operation.OBJECT_OPERATOR, new ObjectOperator()],
86
91
  ]);
87
92
 
@@ -315,7 +320,7 @@ export class ExpressionEvaluator {
315
320
  if (key.length > 2 && valuesMap.has(key))
316
321
  tokens.push(new ExpressionTokenValue(str, this.getValue(str, valuesMap)));
317
322
  else {
318
- let v: any = undefined;
323
+ let v: any;
319
324
  try {
320
325
  v = LiteralTokenValueExtractor.INSTANCE.getValue(str);
321
326
  } catch (err) {
@@ -30,6 +30,7 @@ export class Operation {
30
30
  public static readonly UNARY_BITWISE_COMPLEMENT: Operation = new Operation('UN: ~', '~');
31
31
 
32
32
  public static readonly ARRAY_OPERATOR: Operation = new Operation('[');
33
+ public static readonly ARRAY_RANGE_INDEX_OPERATOR: Operation = new Operation('..');
33
34
  public static readonly OBJECT_OPERATOR: Operation = new Operation('.');
34
35
 
35
36
  public static readonly NULLISH_COALESCING_OPERATOR: Operation = new Operation('??');
@@ -39,7 +40,7 @@ export class Operation {
39
40
  private static readonly VALUE_OF: Map<string, Operation> = new Map([
40
41
  ['MULTIPLICATION', Operation.MULTIPLICATION],
41
42
  ['DIVISION', Operation.DIVISION],
42
- ['INTEGER_DIVISON', Operation.INTEGER_DIVISION],
43
+ ['INTEGER_DIVISION', Operation.INTEGER_DIVISION],
43
44
  ['MOD', Operation.MOD],
44
45
  ['ADDITION', Operation.ADDITION],
45
46
  ['SUBTRACTION', Operation.SUBTRACTION],
@@ -64,6 +65,7 @@ export class Operation {
64
65
  ['UNARY_LOGICAL_NOT', Operation.UNARY_LOGICAL_NOT],
65
66
  ['UNARY_BITWISE_COMPLEMENT', Operation.UNARY_BITWISE_COMPLEMENT],
66
67
  ['ARRAY_OPERATOR', Operation.ARRAY_OPERATOR],
68
+ ['ARRAY_RANGE_INDEX_OPERATOR', Operation.ARRAY_RANGE_INDEX_OPERATOR],
67
69
  ['OBJECT_OPERATOR', Operation.OBJECT_OPERATOR],
68
70
  ['NULLISH_COALESCING_OPERATOR', Operation.NULLISH_COALESCING_OPERATOR],
69
71
  ['CONDITIONAL_TERNARY_OPERATOR', Operation.CONDITIONAL_TERNARY_OPERATOR],
@@ -123,6 +125,7 @@ export class Operation {
123
125
  [Operation.UNARY_BITWISE_COMPLEMENT, 1],
124
126
  [Operation.ARRAY_OPERATOR, 1],
125
127
  [Operation.OBJECT_OPERATOR, 1],
128
+ [Operation.ARRAY_RANGE_INDEX_OPERATOR, 2],
126
129
  [Operation.MULTIPLICATION, 2],
127
130
  [Operation.DIVISION, 2],
128
131
  [Operation.INTEGER_DIVISION, 2],
@@ -153,6 +156,7 @@ export class Operation {
153
156
  ...Array.from(Operation.LOGICAL_OPERATORS),
154
157
  ...Array.from(Operation.BITWISE_OPERATORS),
155
158
  Operation.ARRAY_OPERATOR,
159
+ Operation.ARRAY_RANGE_INDEX_OPERATOR,
156
160
  Operation.OBJECT_OPERATOR,
157
161
  ...Array.from(Operation.CONDITIONAL_OPERATORS),
158
162
  ].map((e) => e.getOperator()),
@@ -164,6 +168,7 @@ export class Operation {
164
168
  ...Array.from(Operation.LOGICAL_OPERATORS),
165
169
  ...Array.from(Operation.BITWISE_OPERATORS),
166
170
  Operation.ARRAY_OPERATOR,
171
+ Operation.ARRAY_RANGE_INDEX_OPERATOR,
167
172
  Operation.OBJECT_OPERATOR,
168
173
  ...Array.from(Operation.CONDITIONAL_OPERATORS),
169
174
  ]
@@ -192,9 +197,10 @@ export class Operation {
192
197
  .map((e) => e.length)
193
198
  .reduce((a, c) => (a > c ? a : c), 0);
194
199
 
195
- private operator: string;
196
- private operatorName: string;
197
- private _shouldBeWrappedInSpace: boolean;
200
+ private readonly operator: string;
201
+ private readonly operatorName: string;
202
+ private readonly _shouldBeWrappedInSpace: boolean;
203
+
198
204
  public constructor(
199
205
  operator: string,
200
206
  operatorName?: string,
@@ -0,0 +1,7 @@
1
+ import { BinaryOperator } from './BinaryOperator';
2
+
3
+ export class ArrayRangeOperator extends BinaryOperator {
4
+ public apply(t: any, u: any): any {
5
+ return `${t ?? ''}..${u ?? ''}`;
6
+ }
7
+ }
@@ -22,3 +22,4 @@ export * from './LogicalLessThanOperator';
22
22
  export * from './LogicalOrOperator';
23
23
  export * from './ObjectOperator';
24
24
  export * from './LogicalNullishCoalescingOperator';
25
+ export * from './ArrayRangeOperator';
@@ -6,7 +6,7 @@ import { ExpressionEvaluationException } from '../exception/ExpressionEvaluation
6
6
 
7
7
  export abstract class TokenValueExtractor {
8
8
  public static readonly REGEX_SQUARE_BRACKETS: RegExp = /[\[\]]/;
9
- public static readonly REGEX_DOT: RegExp = /\./;
9
+ public static readonly REGEX_DOT: RegExp = /(?<!\.)\.(?!\.)/;
10
10
 
11
11
  public getValue(token: string): any {
12
12
  let prefix: string = this.getPrefix();
@@ -71,7 +71,8 @@ export abstract class TokenValueExtractor {
71
71
 
72
72
  if (cPart === 'length') return this.getLength(token, cElement);
73
73
 
74
- if (Array.isArray(cElement)) return this.handleArrayAccess(token, cPart, cElement);
74
+ if (typeof cElement == 'string' || Array.isArray(cElement))
75
+ return this.handleArrayAccess(token, cPart, cElement);
75
76
 
76
77
  return this.handleObjectAccess(token, parts, partNumber, cPart, cElement);
77
78
  }
@@ -91,8 +92,29 @@ export abstract class TokenValueExtractor {
91
92
  );
92
93
  }
93
94
 
94
- private handleArrayAccess(token: string, cPart: string, cArray: any[]): any {
95
- const index: number = parseInt(cPart);
95
+ private handleArrayAccess(token: string, cPart: string, cArray: any[] | string): any {
96
+ const dotDotIndex = cPart.indexOf('..');
97
+ if (dotDotIndex >= 0) {
98
+ const startIndex = cPart.substring(0, dotDotIndex);
99
+ const endIndex = cPart.substring(dotDotIndex + 2);
100
+
101
+ let intStart = startIndex.length == 0 ? 0 : parseInt(startIndex);
102
+ let intEnd = endIndex.length == 0 ? cArray.length : parseInt(endIndex);
103
+
104
+ if (isNaN(intStart) || isNaN(intEnd)) return undefined;
105
+
106
+ while (intStart < 0) intStart += cArray.length;
107
+ while (intEnd < 0) intEnd += cArray.length;
108
+
109
+ const cArrayType = typeof cArray;
110
+ if (intStart >= intEnd) return cArrayType == 'string' ? '' : [];
111
+
112
+ return cArrayType == 'string'
113
+ ? (cArray as string).substring(intStart, intEnd)
114
+ : cArray.slice(intStart, intEnd);
115
+ }
116
+
117
+ let index: number = parseInt(cPart);
96
118
 
97
119
  if (isNaN(index)) {
98
120
  throw new ExpressionEvaluationException(
@@ -101,7 +123,8 @@ export abstract class TokenValueExtractor {
101
123
  );
102
124
  }
103
125
 
104
- if (index < 0 || index >= cArray.length) {
126
+ while (index < 0) index = cArray.length + index;
127
+ if (index >= cArray.length) {
105
128
  return undefined;
106
129
  }
107
130
 
@@ -137,7 +160,11 @@ export abstract class TokenValueExtractor {
137
160
  partNumber: number,
138
161
  jsonElement: any,
139
162
  ): void {
140
- if (typeof jsonElement != 'object' || Array.isArray(jsonElement))
163
+ const jsonElementType = typeof jsonElement;
164
+ if (
165
+ (jsonElementType != 'object' && jsonElementType != 'string') ||
166
+ Array.isArray(jsonElement)
167
+ )
141
168
  throw new ExpressionEvaluationException(
142
169
  token,
143
170
  StringFormatter.format(