amis-formula 1.3.13 → 2.0.0-beta.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.
@@ -0,0 +1,201 @@
1
+ import {parse} from '../src/index';
2
+ test('parser:simple', () => {
3
+ expect(
4
+ parse('expression result is ${a + b}', {
5
+ evalMode: false
6
+ })
7
+ ).toMatchSnapshot();
8
+ });
9
+
10
+ test('parser:complex', () => {
11
+ expect(
12
+ parse('raw content ${`es tempalte ${`deeper${a + 3}`}`}', {
13
+ evalMode: false
14
+ })
15
+ ).toMatchSnapshot();
16
+ });
17
+
18
+ test('parser:evalMode', () => {
19
+ expect(
20
+ parse('a + b', {
21
+ evalMode: true
22
+ })
23
+ ).toMatchSnapshot();
24
+ });
25
+
26
+ test('parser:template', () => {
27
+ expect(
28
+ parse('`abc${a + b}`', {
29
+ evalMode: true
30
+ })
31
+ ).toMatchSnapshot();
32
+ });
33
+
34
+ test('parser:string', () => {
35
+ expect(
36
+ parse('"string literall, escape \\""', {
37
+ evalMode: true
38
+ })
39
+ ).toMatchSnapshot();
40
+ });
41
+
42
+ test('parser:number', () => {
43
+ expect(
44
+ parse('-1 + 2.5 + 3', {
45
+ evalMode: true
46
+ })
47
+ ).toMatchSnapshot();
48
+ });
49
+
50
+ test('parser:single-string', () => {
51
+ expect(
52
+ parse("'string'", {
53
+ evalMode: true
54
+ })
55
+ ).toMatchSnapshot();
56
+ });
57
+
58
+ test('parser:object-literall', () => {
59
+ expect(
60
+ parse("{a: 1, 'b': 2, [`c`]: 3, d: {}}", {
61
+ evalMode: true
62
+ })
63
+ ).toMatchSnapshot();
64
+ });
65
+
66
+ test('parser:array-literall', () => {
67
+ expect(
68
+ parse('[a, b, 1, 2, {a: 1}]', {
69
+ evalMode: true
70
+ })
71
+ ).toMatchSnapshot();
72
+ });
73
+
74
+ test('parser:variable-geter', () => {
75
+ expect(
76
+ parse('doAction(a.b, a[b], a["c"], a[`d`])', {
77
+ evalMode: true
78
+ })
79
+ ).toMatchSnapshot();
80
+ });
81
+
82
+ test('parser:variable-geter2', () => {
83
+ expect(
84
+ parse('a[b]["c"][d][`x`]', {
85
+ evalMode: true
86
+ })
87
+ ).toMatchSnapshot();
88
+
89
+ expect(
90
+ parse('a[b]["c"].d[`x`]', {
91
+ evalMode: true
92
+ })
93
+ ).toMatchSnapshot();
94
+ });
95
+ test('parser:multi-expression', () => {
96
+ expect(
97
+ parse('(a.b, a[b], a["c"], a[`d`])', {
98
+ evalMode: true
99
+ })
100
+ ).toMatchSnapshot();
101
+ });
102
+
103
+ test('parser:functionCall', () => {
104
+ expect(
105
+ parse('doAction(a, doAction(b))', {
106
+ evalMode: true
107
+ })
108
+ ).toMatchSnapshot();
109
+ });
110
+
111
+ test('parser:filter', () => {
112
+ expect(
113
+ parse('\\$abc is ${abc | html}', {
114
+ evalMode: false
115
+ })
116
+ ).toMatchSnapshot();
117
+ });
118
+
119
+ test('parser:filter-escape', () => {
120
+ expect(
121
+ parse('\\$abc is ${abc | date: YYYY-MM-DD HH\\:mm\\:ss}', {
122
+ evalMode: false
123
+ })
124
+ ).toMatchSnapshot();
125
+ });
126
+
127
+ test('parser:conditional', () => {
128
+ expect(
129
+ parse('a ? b : c', {
130
+ evalMode: true
131
+ })
132
+ ).toMatchSnapshot();
133
+ });
134
+
135
+ test('parser:binary-expression', () => {
136
+ expect(
137
+ parse('a && b && c', {
138
+ evalMode: true
139
+ })
140
+ ).toMatchSnapshot();
141
+
142
+ expect(
143
+ parse('a && b || c', {
144
+ evalMode: true
145
+ })
146
+ ).toMatchSnapshot();
147
+
148
+ expect(
149
+ parse('a || b && c', {
150
+ evalMode: true
151
+ })
152
+ ).toMatchSnapshot();
153
+
154
+ expect(
155
+ parse('a !== b === c', {
156
+ evalMode: true
157
+ })
158
+ ).toMatchSnapshot();
159
+ });
160
+
161
+ test('parser:group-expression', () => {
162
+ expect(
163
+ parse('a && (b && c)', {
164
+ evalMode: true
165
+ })
166
+ ).toMatchSnapshot();
167
+ });
168
+
169
+ test('parser:unary-expression', () => {
170
+ expect(
171
+ parse('!!a', {
172
+ evalMode: true
173
+ })
174
+ ).toMatchSnapshot();
175
+ });
176
+
177
+ test('parser:anonymous-function', () => {
178
+ expect(
179
+ parse('() => 1', {
180
+ evalMode: true
181
+ })
182
+ ).toMatchSnapshot();
183
+
184
+ expect(
185
+ parse('() => "string"', {
186
+ evalMode: true
187
+ })
188
+ ).toMatchSnapshot();
189
+
190
+ expect(
191
+ parse('(a) => `${a.a}---${a.b}`', {
192
+ evalMode: true
193
+ })
194
+ ).toMatchSnapshot();
195
+ });
196
+
197
+ // test('parser:test', () => {
198
+ // console.log(JSON.stringify(parse('ARRAYMAP(arr, (item) => item.abc)', {
199
+ // evalMode: true
200
+ // }), null, 2));
201
+ // });
@@ -1063,3 +1063,8 @@ export declare class Evaluator {
1063
1063
  */
1064
1064
  fnJOIN(arr: any[], separator?: string): string;
1065
1065
  }
1066
+ export declare function createObject(superProps?: {
1067
+ [propName: string]: any;
1068
+ }, props?: {
1069
+ [propName: string]: any;
1070
+ }, properties?: any): object;
package/dist/filter.d.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { FilterMap } from './evalutor';
1
+ import { FilterMap } from './types';
2
+ /**
3
+ * filter 是历史包袱,不建议使用。因为这是之前的语法,所以在公式解析里面做了兼容。
4
+ * 建议用 ${ LEFT(xxx) } 这种函数调用语法。
5
+ */
2
6
  export declare const filters: FilterMap;
3
7
  export declare function registerFilter(name: string, fn: (input: any, ...args: any[]) => any): void;
8
+ export declare function extendsFilters(value: FilterMap): void;
4
9
  export declare function getFilters(): FilterMap;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Evaluator, EvaluatorOptions } from './evalutor';
2
- import { ASTNode, parse, ParserOptions } from './parser';
1
+ import { Evaluator } from './evalutor';
2
+ import { parse } from './parser';
3
3
  import { lexer } from './lexer';
4
- import { registerFilter, filters, getFilters } from './filter';
5
- export { parse, lexer, Evaluator, filters, getFilters, registerFilter };
6
- export * from './util';
4
+ import { registerFilter, filters, getFilters, extendsFilters } from './filter';
5
+ import type { FilterContext, ASTNode, ParserOptions, EvaluatorOptions } from './types';
6
+ export { parse, lexer, Evaluator, FilterContext, filters, getFilters, registerFilter, extendsFilters };
7
7
  export declare function evaluate(astOrString: string | ASTNode, data: any, options?: ParserOptions & EvaluatorOptions): any;