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.
- package/README.md +1 -1
- package/__mocks__/styleMock.js +6 -0
- package/__mocks__/svgMock.js +5 -0
- package/__tests__/__snapshots__/lexer.test.ts.snap +31 -0
- package/__tests__/__snapshots__/parser.test.ts.snap +2306 -0
- package/__tests__/evalute.test.ts +437 -0
- package/__tests__/fomula.test.ts +194 -0
- package/__tests__/jest.setup.js +22 -0
- package/__tests__/lexer.test.ts +55 -0
- package/__tests__/parser.test.ts +201 -0
- package/dist/evalutor.d.ts +5 -0
- package/dist/filter.d.ts +6 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +3032 -3841
- package/dist/lexer.d.ts +1 -29
- package/dist/parser.d.ts +1 -26
- package/dist/types.d.ts +83 -0
- package/package.json +1 -1
- package/rollup.config.js +3 -1
- package/scripts/genDoc.ts +162 -0
- package/scripts/lib.ts +55 -0
- package/src/evalutor.ts +1938 -0
- package/src/filter.ts +48 -0
- package/src/index.ts +35 -0
- package/src/lexer.ts +775 -0
- package/src/parser.ts +875 -0
- package/src/types.ts +119 -0
- package/tsconfig.json +36 -0
- package/.prettierrc +0 -20
- package/dist/util.d.ts +0 -36
package/src/types.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import {Evaluator} from './evalutor';
|
|
2
|
+
|
|
3
|
+
export interface FilterMap {
|
|
4
|
+
[propName: string]: (this: FilterContext, input: any, ...args: any[]) => any;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface FunctionMap {
|
|
8
|
+
[propName: string]: (this: Evaluator, ast: Object, data: any) => any;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface FilterContext {
|
|
12
|
+
data: Object;
|
|
13
|
+
filter?: {
|
|
14
|
+
name: string;
|
|
15
|
+
args: Array<any>;
|
|
16
|
+
};
|
|
17
|
+
restFilters: Array<{
|
|
18
|
+
name: string;
|
|
19
|
+
args: Array<any>;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface EvaluatorOptions {
|
|
24
|
+
/**
|
|
25
|
+
* 可以外部传入 ast 节点处理器,定制或者扩充自定义函数
|
|
26
|
+
*/
|
|
27
|
+
functions?: FunctionMap;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 可以外部扩充 filter
|
|
31
|
+
*/
|
|
32
|
+
filters?: FilterMap;
|
|
33
|
+
|
|
34
|
+
defaultFilter?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface LexerOptions {
|
|
38
|
+
/**
|
|
39
|
+
* 直接是运算表达式?还是从模板开始 ${} 里面才算运算表达式
|
|
40
|
+
*/
|
|
41
|
+
evalMode?: boolean;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 只支持取变量。
|
|
45
|
+
*/
|
|
46
|
+
variableMode?: boolean;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 是否允许 filter 语法,比如:
|
|
50
|
+
*
|
|
51
|
+
* ${abc | html}
|
|
52
|
+
*/
|
|
53
|
+
allowFilter?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type TokenTypeName =
|
|
57
|
+
| 'Boolean'
|
|
58
|
+
| 'Raw'
|
|
59
|
+
| 'Variable'
|
|
60
|
+
| 'OpenScript'
|
|
61
|
+
| 'CloseScript'
|
|
62
|
+
| 'EOF'
|
|
63
|
+
| 'Identifier'
|
|
64
|
+
| 'Literal'
|
|
65
|
+
| 'Numeric'
|
|
66
|
+
| 'Punctuator'
|
|
67
|
+
| 'String'
|
|
68
|
+
| 'RegularExpression'
|
|
69
|
+
| 'TemplateRaw'
|
|
70
|
+
| 'TemplateLeftBrace'
|
|
71
|
+
| 'TemplateRightBrace'
|
|
72
|
+
| 'OpenFilter'
|
|
73
|
+
| 'Char';
|
|
74
|
+
|
|
75
|
+
export interface Position {
|
|
76
|
+
index: number;
|
|
77
|
+
line: number;
|
|
78
|
+
column: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface Token {
|
|
82
|
+
type: TokenTypeName;
|
|
83
|
+
value: any;
|
|
84
|
+
raw?: string;
|
|
85
|
+
start: Position;
|
|
86
|
+
end: Position;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type NodeType = 'content' | 'raw' | 'conditional';
|
|
90
|
+
|
|
91
|
+
export interface ParserOptions {
|
|
92
|
+
/**
|
|
93
|
+
* 直接是运算表达式?还是从模板开始 ${} 里面才算运算表达式
|
|
94
|
+
*/
|
|
95
|
+
evalMode?: boolean;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 只支持取变量。
|
|
99
|
+
*/
|
|
100
|
+
variableMode?: boolean;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 是否允许 filter 语法,比如:
|
|
104
|
+
*
|
|
105
|
+
* ${abc | html}
|
|
106
|
+
*/
|
|
107
|
+
allowFilter?: boolean;
|
|
108
|
+
|
|
109
|
+
variableNamespaces?: Array<string>;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface ASTNode {
|
|
113
|
+
type: string;
|
|
114
|
+
start: Position;
|
|
115
|
+
end: Position;
|
|
116
|
+
[propname: string]: any;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export type ASTNodeOrNull = ASTNode | null;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "dist/",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"target": "es5",
|
|
6
|
+
"lib": ["es6", "dom", "ES2015"],
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"jsx": "react",
|
|
9
|
+
"moduleResolution": "node",
|
|
10
|
+
"rootDir": "",
|
|
11
|
+
"importHelpers": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"allowSyntheticDefaultImports": true,
|
|
14
|
+
"sourceRoot": "src",
|
|
15
|
+
"noImplicitReturns": true,
|
|
16
|
+
"noImplicitThis": true,
|
|
17
|
+
"noImplicitAny": true,
|
|
18
|
+
"strictNullChecks": true,
|
|
19
|
+
"experimentalDecorators": true,
|
|
20
|
+
"emitDecoratorMetadata": true,
|
|
21
|
+
"skipLibCheck": true,
|
|
22
|
+
"skipDefaultLibCheck": true,
|
|
23
|
+
"allowJs": true
|
|
24
|
+
},
|
|
25
|
+
"include": ["src/**/*"],
|
|
26
|
+
"exclude": [
|
|
27
|
+
"node_modules",
|
|
28
|
+
"build",
|
|
29
|
+
"scripts",
|
|
30
|
+
"acceptance-tests",
|
|
31
|
+
"webpack",
|
|
32
|
+
"jest",
|
|
33
|
+
"src/setupTests.ts"
|
|
34
|
+
],
|
|
35
|
+
"types": ["typePatches"]
|
|
36
|
+
}
|
package/.prettierrc
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"printWidth": 80,
|
|
3
|
-
"tabWidth": 2,
|
|
4
|
-
"useTabs": false,
|
|
5
|
-
"singleQuote": true,
|
|
6
|
-
"semi": true,
|
|
7
|
-
"trailingComma": "none",
|
|
8
|
-
"bracketSpacing": false,
|
|
9
|
-
"quoteProps": "consistent",
|
|
10
|
-
"arrowParens": "avoid",
|
|
11
|
-
"jsxBracketSameLine": false,
|
|
12
|
-
"overrides": [
|
|
13
|
-
{
|
|
14
|
-
"files": "src/locale/*.ts",
|
|
15
|
-
"options": {
|
|
16
|
-
"printWidth": 800
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
]
|
|
20
|
-
}
|
package/dist/util.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import moment from 'moment';
|
|
2
|
-
export declare function createObject(superProps?: {
|
|
3
|
-
[propName: string]: any;
|
|
4
|
-
}, props?: {
|
|
5
|
-
[propName: string]: any;
|
|
6
|
-
}, properties?: any): object;
|
|
7
|
-
export declare function cloneObject(target: any, persistOwnProps?: boolean): any;
|
|
8
|
-
export declare function isObject(obj: any): any;
|
|
9
|
-
export declare function string2regExp(value: string, caseSensitive?: boolean): RegExp;
|
|
10
|
-
export declare function getVariable(data: {
|
|
11
|
-
[propName: string]: any;
|
|
12
|
-
}, key: string | undefined, canAccessSuper?: boolean): any;
|
|
13
|
-
export declare function setVariable(data: {
|
|
14
|
-
[propName: string]: any;
|
|
15
|
-
}, key: string, value: any, convertKeyToPath?: boolean): void;
|
|
16
|
-
export declare function deleteVariable(data: {
|
|
17
|
-
[propName: string]: any;
|
|
18
|
-
}, key: string): void;
|
|
19
|
-
/**
|
|
20
|
-
* 将例如像 a.b.c 或 a[1].b 的字符串转换为路径数组
|
|
21
|
-
*
|
|
22
|
-
* @param string 要转换的字符串
|
|
23
|
-
*/
|
|
24
|
-
export declare const keyToPath: (string: string) => string[];
|
|
25
|
-
export declare const tokenize: (str: string, data: object, defaultFilter?: string) => string;
|
|
26
|
-
export declare const prettyBytes: (num: number) => string;
|
|
27
|
-
export declare const escapeHtml: (str: string) => string;
|
|
28
|
-
export declare function formatDuration(value: number): string;
|
|
29
|
-
export declare const relativeValueRe: RegExp;
|
|
30
|
-
export declare const filterDate: (value: string, data?: object, format?: string, utc?: boolean) => moment.Moment;
|
|
31
|
-
export declare function parseDuration(str: string): moment.Duration | undefined;
|
|
32
|
-
export declare function stripNumber(number: number): number;
|
|
33
|
-
export declare function pickValues(names: string, data: object): any;
|
|
34
|
-
export declare function resolveVariable(path?: string, data?: any): any;
|
|
35
|
-
export declare function isPureVariable(path?: any): path is string;
|
|
36
|
-
export declare const resolveVariableAndFilter: (path?: string | undefined, data?: object, defaultFilter?: string, fallbackValue?: (value: any) => any) => any;
|