@fincity/kirun-js 1.7.0 → 1.8.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.
- package/__tests__/engine/function/system/array/ArrayToObjectTest.ts +180 -0
- package/__tests__/engine/function/system/array/RemoveDuplicatesTest.ts +74 -0
- package/__tests__/engine/function/system/loop/CountLoopTest.ts +1 -1
- package/__tests__/engine/runtime/expression/tokenextractor/ObjectValueSetterExtractorTest.ts +51 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/module.js +1 -1
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +9 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/engine/function/system/array/AbstractArrayFunction.ts +5 -0
- package/src/engine/function/system/array/ArrayFunctionRepository.ts +4 -0
- package/src/engine/function/system/array/ArrayToObject.ts +77 -0
- package/src/engine/function/system/array/RemoveDuplicates.ts +64 -0
- package/src/engine/function/system/object/ObjectFunctionRepository.ts +2 -0
- package/src/engine/function/system/object/ObjectPutValue.ts +61 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticAdditionOperator.ts +3 -2
- package/src/engine/runtime/expression/tokenextractor/ObjectValueSetterExtractor.ts +172 -0
- package/src/engine/runtime/expression/tokenextractor/TokenValueExtractor.ts +1 -1
- package/src/index.ts +1 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { KIRuntimeException } from '../../../exception/KIRuntimeException';
|
|
2
|
+
import { isNullValue } from '../../../util/NullCheck';
|
|
3
|
+
import { duplicate } from '../../../util/duplicate';
|
|
4
|
+
import { StringFormatter } from '../../../util/string/StringFormatter';
|
|
5
|
+
import { StringUtil } from '../../../util/string/StringUtil';
|
|
6
|
+
import { Expression } from '../Expression';
|
|
7
|
+
import { ExpressionTokenValue } from '../ExpressionTokenValue';
|
|
8
|
+
import { Operation } from '../Operation';
|
|
9
|
+
import { ExpressionEvaluationException } from '../exception/ExpressionEvaluationException';
|
|
10
|
+
import { TokenValueExtractor } from './TokenValueExtractor';
|
|
11
|
+
|
|
12
|
+
export class ObjectValueSetterExtractor extends TokenValueExtractor {
|
|
13
|
+
private store: any;
|
|
14
|
+
private prefix: string;
|
|
15
|
+
constructor(store: any, prefix: string) {
|
|
16
|
+
super();
|
|
17
|
+
this.store = store;
|
|
18
|
+
this.prefix = prefix;
|
|
19
|
+
}
|
|
20
|
+
protected getValueInternal(token: string) {
|
|
21
|
+
let parts: string[] = token.split(TokenValueExtractor.REGEX_DOT);
|
|
22
|
+
return this.retrieveElementFrom(token, parts, 1, this.store);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public getStore(): any {
|
|
26
|
+
return this.store;
|
|
27
|
+
}
|
|
28
|
+
public setStore(store: any): ObjectValueSetterExtractor {
|
|
29
|
+
this.store = store;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public setValue(
|
|
34
|
+
token: string,
|
|
35
|
+
value: any,
|
|
36
|
+
overwrite: boolean = true,
|
|
37
|
+
deleteOnNull: boolean = false,
|
|
38
|
+
) {
|
|
39
|
+
this.store = duplicate(this.store);
|
|
40
|
+
this.modifyStore(token, value, overwrite, deleteOnNull);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private modifyStore(
|
|
44
|
+
stringToken: string,
|
|
45
|
+
value: any,
|
|
46
|
+
overwrite: boolean,
|
|
47
|
+
deleteOnNull: boolean,
|
|
48
|
+
) {
|
|
49
|
+
const exp = new Expression(stringToken);
|
|
50
|
+
const tokens = exp.getTokens();
|
|
51
|
+
tokens.removeLast();
|
|
52
|
+
const ops = exp.getOperations();
|
|
53
|
+
|
|
54
|
+
let op = ops.removeLast();
|
|
55
|
+
let token = tokens.removeLast();
|
|
56
|
+
let mem =
|
|
57
|
+
token instanceof ExpressionTokenValue
|
|
58
|
+
? (token as ExpressionTokenValue).getElement()
|
|
59
|
+
: token.getExpression();
|
|
60
|
+
|
|
61
|
+
let el = this.store;
|
|
62
|
+
|
|
63
|
+
while (!ops.isEmpty()) {
|
|
64
|
+
if (op == Operation.OBJECT_OPERATOR) {
|
|
65
|
+
el = this.getDataFromObject(el, mem, ops.peekLast());
|
|
66
|
+
} else {
|
|
67
|
+
el = this.getDataFromArray(el, mem, ops.peekLast());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
op = ops.removeLast();
|
|
71
|
+
token = tokens.removeLast();
|
|
72
|
+
mem =
|
|
73
|
+
token instanceof ExpressionTokenValue
|
|
74
|
+
? (token as ExpressionTokenValue).getElement()
|
|
75
|
+
: token.getExpression();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (op == Operation.OBJECT_OPERATOR)
|
|
79
|
+
this.putDataInObject(el, mem, value, overwrite, deleteOnNull);
|
|
80
|
+
else this.putDataInArray(el, mem, value, overwrite, deleteOnNull);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private getDataFromArray(el: any, mem: string, nextOp: Operation): any {
|
|
84
|
+
if (!Array.isArray(el))
|
|
85
|
+
throw new KIRuntimeException(
|
|
86
|
+
StringFormatter.format('Expected an array but found $', el),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const index = parseInt(mem);
|
|
90
|
+
if (isNaN(index))
|
|
91
|
+
throw new KIRuntimeException(
|
|
92
|
+
StringFormatter.format('Expected an array index but found $', mem),
|
|
93
|
+
);
|
|
94
|
+
if (index < 0)
|
|
95
|
+
throw new KIRuntimeException(
|
|
96
|
+
StringFormatter.format('Array index is out of bound - $', mem),
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
let je = el[index];
|
|
100
|
+
|
|
101
|
+
if (isNullValue(je)) {
|
|
102
|
+
je = nextOp == Operation.OBJECT_OPERATOR ? {} : [];
|
|
103
|
+
el[index] = je;
|
|
104
|
+
}
|
|
105
|
+
return je;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private getDataFromObject(el: any, mem: string, nextOp: Operation): any {
|
|
109
|
+
if (Array.isArray(el) || typeof el !== 'object')
|
|
110
|
+
throw new KIRuntimeException(
|
|
111
|
+
StringFormatter.format('Expected an object but found $', el),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
let je = el[mem];
|
|
115
|
+
|
|
116
|
+
if (isNullValue(je)) {
|
|
117
|
+
je = nextOp == Operation.OBJECT_OPERATOR ? {} : [];
|
|
118
|
+
el[mem] = je;
|
|
119
|
+
}
|
|
120
|
+
return je;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private putDataInArray(
|
|
124
|
+
el: any,
|
|
125
|
+
mem: string,
|
|
126
|
+
value: any,
|
|
127
|
+
overwrite: boolean,
|
|
128
|
+
deleteOnNull: boolean,
|
|
129
|
+
): void {
|
|
130
|
+
if (!Array.isArray(el))
|
|
131
|
+
throw new KIRuntimeException(
|
|
132
|
+
StringFormatter.format('Expected an array but found $', el),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const index = parseInt(mem);
|
|
136
|
+
if (isNaN(index))
|
|
137
|
+
throw new KIRuntimeException(
|
|
138
|
+
StringFormatter.format('Expected an array index but found $', mem),
|
|
139
|
+
);
|
|
140
|
+
if (index < 0)
|
|
141
|
+
throw new KIRuntimeException(
|
|
142
|
+
StringFormatter.format('Array index is out of bound - $', mem),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
if (overwrite || isNullValue(el[index])) {
|
|
146
|
+
if (deleteOnNull && isNullValue(value)) el.splice(index, 1);
|
|
147
|
+
else el[index] = value;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private putDataInObject(
|
|
152
|
+
el: any,
|
|
153
|
+
mem: string,
|
|
154
|
+
value: any,
|
|
155
|
+
overwrite: boolean,
|
|
156
|
+
deleteOnNull: boolean,
|
|
157
|
+
): void {
|
|
158
|
+
if (Array.isArray(el) || typeof el !== 'object')
|
|
159
|
+
throw new KIRuntimeException(
|
|
160
|
+
StringFormatter.format('Expected an object but found $', el),
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
if (overwrite || isNullValue(el[mem])) {
|
|
164
|
+
if (deleteOnNull && isNullValue(value)) delete el[mem];
|
|
165
|
+
else el[mem] = value;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
getPrefix(): string {
|
|
170
|
+
return this.prefix;
|
|
171
|
+
}
|
|
172
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -27,6 +27,7 @@ export * from './engine/runtime/FunctionExecutionParameters';
|
|
|
27
27
|
export * from './engine/runtime/expression/Expression';
|
|
28
28
|
export * from './engine/runtime/expression/tokenextractor/TokenValueExtractor';
|
|
29
29
|
export * from './engine/runtime/expression/tokenextractor/LiteralTokenValueExtractor';
|
|
30
|
+
export * from './engine/runtime/expression/tokenextractor/ObjectValueSetterExtractor';
|
|
30
31
|
export * from './engine/runtime/expression/ExpressionEvaluator';
|
|
31
32
|
export * from './engine/runtime/expression/Operation';
|
|
32
33
|
export * from './engine/runtime/expression/ExpressionToken';
|