@cloudpss/expression 0.6.0-alpha.19 → 0.6.0-alpha.20
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/dist/definitions/utils.d.ts +11 -3
- package/dist/definitions/utils.d.ts.map +1 -1
- package/dist/definitions/utils.js +90 -75
- package/dist/definitions/utils.js.map +1 -1
- package/dist/expression.js +2 -2
- package/dist/expression.js.map +1 -1
- package/dist/main.d.ts +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +29 -39
- package/dist/main.js.map +1 -1
- package/dist/migrator/call.d.ts.map +1 -1
- package/dist/migrator/call.js +35 -1
- package/dist/migrator/call.js.map +1 -1
- package/dist/type.d.ts +1 -1
- package/dist/type.d.ts.map +1 -1
- package/dist/type.js +4 -2
- package/dist/type.js.map +1 -1
- package/package.json +3 -3
- package/src/definitions/utils.ts +95 -62
- package/src/expression.ts +2 -2
- package/src/main.ts +30 -38
- package/src/migrator/call.ts +33 -1
- package/src/type.ts +6 -2
- package/tests/definition.ts +39 -9
- package/tests/eval-complex.ts +12 -1
- package/tests/main.ts +49 -0
- package/tests/migrate.ts +21 -0
package/tests/definition.ts
CHANGED
|
@@ -84,7 +84,6 @@ describe('definitions', () => {
|
|
|
84
84
|
expect(def.toArgumentValue(undefined, { type: 'xxx', key: '', name: '', description: '', value: 1 })).toBe(
|
|
85
85
|
null,
|
|
86
86
|
);
|
|
87
|
-
// @ts-expect-error 测试 undefined 输入
|
|
88
87
|
expect(def.toArgumentValue(undefined, { type: 'real', key: '', name: '', description: '', value: 1 })).toBe(
|
|
89
88
|
null,
|
|
90
89
|
);
|
|
@@ -108,7 +107,7 @@ describe('definitions', () => {
|
|
|
108
107
|
value: 1,
|
|
109
108
|
choices: [{ key: '0', name: '', description: '' }],
|
|
110
109
|
}),
|
|
111
|
-
).toBe('
|
|
110
|
+
).toBe('');
|
|
112
111
|
expect(
|
|
113
112
|
def.toArgumentValue(null, {
|
|
114
113
|
type: 'choice',
|
|
@@ -121,8 +120,9 @@ describe('definitions', () => {
|
|
|
121
120
|
{ key: 1, name: '', description: '' },
|
|
122
121
|
],
|
|
123
122
|
}),
|
|
124
|
-
).toBe('
|
|
123
|
+
).toBe('');
|
|
125
124
|
expect(def.toArgumentValue(null, { type: 'logical', key: '', name: '', description: '', value: 1 })).toBe(null);
|
|
125
|
+
expect(def.toArgumentValue('0', { type: 'logical', key: '', name: '', description: '', value: 1 })).toBe(0);
|
|
126
126
|
expect(
|
|
127
127
|
def.toArgumentValue(null, {
|
|
128
128
|
type: 'logical',
|
|
@@ -135,7 +135,7 @@ describe('definitions', () => {
|
|
|
135
135
|
{ key: 1, name: '', description: '' },
|
|
136
136
|
],
|
|
137
137
|
}),
|
|
138
|
-
).toBe(
|
|
138
|
+
).toBe(null);
|
|
139
139
|
expect(
|
|
140
140
|
def.toArgumentValue(null, {
|
|
141
141
|
type: 'logical',
|
|
@@ -166,7 +166,7 @@ describe('definitions', () => {
|
|
|
166
166
|
{ key: '1', name: '', description: '' },
|
|
167
167
|
],
|
|
168
168
|
}),
|
|
169
|
-
).toBe('
|
|
169
|
+
).toBe('');
|
|
170
170
|
expect(
|
|
171
171
|
def.toArgumentValue(null, {
|
|
172
172
|
type: 'multiSelect',
|
|
@@ -196,7 +196,7 @@ describe('definitions', () => {
|
|
|
196
196
|
value: [1],
|
|
197
197
|
choices: [],
|
|
198
198
|
}),
|
|
199
|
-
).toStrictEqual(
|
|
199
|
+
).toStrictEqual(null);
|
|
200
200
|
expect(
|
|
201
201
|
def.toArgumentValue('', {
|
|
202
202
|
type: 'multiSelect',
|
|
@@ -206,7 +206,7 @@ describe('definitions', () => {
|
|
|
206
206
|
value: [1],
|
|
207
207
|
choices: Expression(''),
|
|
208
208
|
}),
|
|
209
|
-
).toStrictEqual(
|
|
209
|
+
).toStrictEqual(null);
|
|
210
210
|
expect(
|
|
211
211
|
def.toArgumentValue(['', '1', ' 2 '], {
|
|
212
212
|
type: 'multiSelect',
|
|
@@ -216,7 +216,37 @@ describe('definitions', () => {
|
|
|
216
216
|
value: [1],
|
|
217
217
|
choices: [],
|
|
218
218
|
}),
|
|
219
|
-
).toStrictEqual(
|
|
219
|
+
).toStrictEqual(null);
|
|
220
|
+
expect(
|
|
221
|
+
def.toArgumentValue(['0', '1', ' 2 '], {
|
|
222
|
+
type: 'multiSelect',
|
|
223
|
+
key: '',
|
|
224
|
+
name: '',
|
|
225
|
+
description: '',
|
|
226
|
+
value: [1],
|
|
227
|
+
choices: [],
|
|
228
|
+
}),
|
|
229
|
+
).toStrictEqual([0, 1, 2]);
|
|
230
|
+
expect(
|
|
231
|
+
def.toArgumentValue(['0', '1', ' 2 '], {
|
|
232
|
+
type: 'multiSelect',
|
|
233
|
+
key: '',
|
|
234
|
+
name: '',
|
|
235
|
+
description: '',
|
|
236
|
+
value: [],
|
|
237
|
+
choices: [],
|
|
238
|
+
}),
|
|
239
|
+
).toStrictEqual([0, 1, 2]);
|
|
240
|
+
expect(
|
|
241
|
+
def.toArgumentValue([1, 2], {
|
|
242
|
+
type: 'multiSelect',
|
|
243
|
+
key: '',
|
|
244
|
+
name: '',
|
|
245
|
+
description: '',
|
|
246
|
+
value: ['1'],
|
|
247
|
+
choices: [],
|
|
248
|
+
}),
|
|
249
|
+
).toStrictEqual(['1', '2']);
|
|
220
250
|
expect(
|
|
221
251
|
def.toArgumentValue(['', '1', ' 2 '], {
|
|
222
252
|
type: 'multiSelect',
|
|
@@ -226,6 +256,6 @@ describe('definitions', () => {
|
|
|
226
256
|
value: [1],
|
|
227
257
|
choices: Expression(''),
|
|
228
258
|
}),
|
|
229
|
-
).toStrictEqual(
|
|
259
|
+
).toStrictEqual(null);
|
|
230
260
|
});
|
|
231
261
|
});
|
package/tests/eval-complex.ts
CHANGED
|
@@ -102,7 +102,7 @@ describe('Evaluator should work correctly', () => {
|
|
|
102
102
|
const result = e.evaluate(Expression('a + a + a + a + a + a + a + a + a + a'), s);
|
|
103
103
|
expect(result).toBe(10);
|
|
104
104
|
});
|
|
105
|
-
it('should report loop', () => {
|
|
105
|
+
it('should report loop by scope', () => {
|
|
106
106
|
const s = new Scope(
|
|
107
107
|
{
|
|
108
108
|
a: Expression('b'),
|
|
@@ -111,6 +111,17 @@ describe('Evaluator should work correctly', () => {
|
|
|
111
111
|
},
|
|
112
112
|
true,
|
|
113
113
|
);
|
|
114
|
+
expect(() => e.evaluate(Expression('a'), s)).toThrow(/Execution recursion exceeds limit/);
|
|
115
|
+
});
|
|
116
|
+
it('should report loop by mirascript', () => {
|
|
117
|
+
const s = new Scope(
|
|
118
|
+
{
|
|
119
|
+
a: Expression('fn x{b}x()'),
|
|
120
|
+
b: Expression('fn x{c}x()'),
|
|
121
|
+
c: Expression('fn x{a}x()'),
|
|
122
|
+
},
|
|
123
|
+
true,
|
|
124
|
+
);
|
|
114
125
|
expect(() => e.evaluate(Expression('a'), s)).toThrow(/Maximum call depth exceeded/);
|
|
115
126
|
});
|
|
116
127
|
|
package/tests/main.ts
CHANGED
|
@@ -6,4 +6,53 @@ describe('Evaluator', () => {
|
|
|
6
6
|
expect(new Evaluator(null).logger).toBeTruthy();
|
|
7
7
|
expect(new Evaluator({}).logger).toBeTruthy();
|
|
8
8
|
});
|
|
9
|
+
|
|
10
|
+
describe('can evaluate choices', () => {
|
|
11
|
+
const e = new Evaluator();
|
|
12
|
+
|
|
13
|
+
it('on logical type', () => {
|
|
14
|
+
const c0 = e.evaluateChoices({ type: 'logical', value: null as never }, null);
|
|
15
|
+
expect(c0.length).toBe(2);
|
|
16
|
+
expect(c0[0].key).toBe(false);
|
|
17
|
+
expect(c0[1].key).toBe(true);
|
|
18
|
+
|
|
19
|
+
const c1 = e.evaluateChoices(
|
|
20
|
+
{
|
|
21
|
+
type: 'logical',
|
|
22
|
+
// @ts-expect-error Incomplete choice definition
|
|
23
|
+
choices: [{}, { key: true, name: 'Yes' }],
|
|
24
|
+
value: null as never,
|
|
25
|
+
},
|
|
26
|
+
null,
|
|
27
|
+
);
|
|
28
|
+
expect(c1.length).toBe(2);
|
|
29
|
+
expect(c1[0].key).toBe(false);
|
|
30
|
+
expect(c1[1].key).toBe(true);
|
|
31
|
+
expect(c1[0].name).toBe('');
|
|
32
|
+
expect(c1[1].name).toBe('Yes');
|
|
33
|
+
|
|
34
|
+
const c2 = e.evaluateChoices(
|
|
35
|
+
{
|
|
36
|
+
type: 'logical',
|
|
37
|
+
// @ts-expect-error Incomplete choice definition
|
|
38
|
+
choices: [{ key: 0 }, { key: '1' }, { key: 2 }],
|
|
39
|
+
value: null as never,
|
|
40
|
+
},
|
|
41
|
+
null,
|
|
42
|
+
);
|
|
43
|
+
expect(c2.length).toBe(2);
|
|
44
|
+
expect(c2[0].key).toBe(0);
|
|
45
|
+
expect(c2[1].key).toBe(1);
|
|
46
|
+
|
|
47
|
+
const c3 = e.evaluateChoices({ type: 'logical', choices: [], value: 0 }, null);
|
|
48
|
+
expect(c3.length).toBe(2);
|
|
49
|
+
expect(c3[0].key).toBe(0);
|
|
50
|
+
expect(c3[1].key).toBe(1);
|
|
51
|
+
|
|
52
|
+
const c4 = e.evaluateChoices({ type: 'logical', value: 'x' }, null);
|
|
53
|
+
expect(c4.length).toBe(2);
|
|
54
|
+
expect(c4[0].key).toBe('0');
|
|
55
|
+
expect(c4[1].key).toBe('1');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
9
58
|
});
|
package/tests/migrate.ts
CHANGED
|
@@ -273,6 +273,27 @@ describe('migrate', () => {
|
|
|
273
273
|
[`sum(m)`, `sum(m::flatten())`, 10],
|
|
274
274
|
[`prod([1,2,3])`, `product([1, 2, 3])`, 6],
|
|
275
275
|
[`prod(f)`, `product(f)`, 1],
|
|
276
|
+
[`abs(f) + round(f) + f`, `abs(f) + round(f) + f`, 3],
|
|
277
|
+
[
|
|
278
|
+
`abs([1,2,3;-4,-5,-6])`,
|
|
279
|
+
`matrix.entrywise([[1, 2, 3], [-4, -5, -6]], nil, abs)`,
|
|
280
|
+
[
|
|
281
|
+
[1, 2, 3],
|
|
282
|
+
[4, 5, 6],
|
|
283
|
+
],
|
|
284
|
+
],
|
|
285
|
+
[`max(1, 2, f) + f`, `max(1, 2, f) + f`, 3],
|
|
286
|
+
[`min(1, 2, f) + f`, `min(1, 2, f) + f`, 2],
|
|
287
|
+
[
|
|
288
|
+
`max([1,2;3,4], 1)`,
|
|
289
|
+
dedent`
|
|
290
|
+
max([[1, 2], [3, 4]], 1)
|
|
291
|
+
// # 转换日志
|
|
292
|
+
// - W: 函数行为可能不一致: max
|
|
293
|
+
// # 原始 math.js 表达式
|
|
294
|
+
// max([1,2;3,4], 1)`,
|
|
295
|
+
VmError,
|
|
296
|
+
],
|
|
276
297
|
])('should migrate expression: %s', (f, t, r) => {
|
|
277
298
|
const migrated = migrateMathJs(f as ExpressionSource, false, s);
|
|
278
299
|
expect(migrated).toBe(t);
|