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/dist/lexer.d.ts
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* 直接是运算表达式?还是从模板开始 ${} 里面才算运算表达式
|
|
4
|
-
*/
|
|
5
|
-
evalMode?: boolean;
|
|
6
|
-
/**
|
|
7
|
-
* 只支持取变量。
|
|
8
|
-
*/
|
|
9
|
-
variableMode?: boolean;
|
|
10
|
-
/**
|
|
11
|
-
* 是否允许 filter 语法,比如:
|
|
12
|
-
*
|
|
13
|
-
* ${abc | html}
|
|
14
|
-
*/
|
|
15
|
-
allowFilter?: boolean;
|
|
16
|
-
}
|
|
1
|
+
import { LexerOptions, Token, TokenTypeName } from './types';
|
|
17
2
|
export declare const enum TokenEnum {
|
|
18
3
|
BooleanLiteral = 1,
|
|
19
4
|
RAW = 2,
|
|
@@ -33,22 +18,9 @@ export declare const enum TokenEnum {
|
|
|
33
18
|
OpenFilter = 16,
|
|
34
19
|
Char = 17
|
|
35
20
|
}
|
|
36
|
-
export declare type TokenTypeName = 'Boolean' | 'Raw' | 'Variable' | 'OpenScript' | 'CloseScript' | 'EOF' | 'Identifier' | 'Literal' | 'Numeric' | 'Punctuator' | 'String' | 'RegularExpression' | 'TemplateRaw' | 'TemplateLeftBrace' | 'TemplateRightBrace' | 'OpenFilter' | 'Char';
|
|
37
21
|
export declare const TokenName: {
|
|
38
22
|
[propName: string]: TokenTypeName;
|
|
39
23
|
};
|
|
40
|
-
export interface Position {
|
|
41
|
-
index: number;
|
|
42
|
-
line: number;
|
|
43
|
-
column: number;
|
|
44
|
-
}
|
|
45
|
-
export interface Token {
|
|
46
|
-
type: TokenTypeName;
|
|
47
|
-
value: any;
|
|
48
|
-
raw?: string;
|
|
49
|
-
start: Position;
|
|
50
|
-
end: Position;
|
|
51
|
-
}
|
|
52
24
|
export declare function lexer(input: string, options?: LexerOptions): {
|
|
53
25
|
next: () => Token;
|
|
54
26
|
};
|
package/dist/parser.d.ts
CHANGED
|
@@ -1,27 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare type NodeType = 'content' | 'raw' | 'conditional';
|
|
3
|
-
export interface ParserOptions {
|
|
4
|
-
/**
|
|
5
|
-
* 直接是运算表达式?还是从模板开始 ${} 里面才算运算表达式
|
|
6
|
-
*/
|
|
7
|
-
evalMode?: boolean;
|
|
8
|
-
/**
|
|
9
|
-
* 只支持取变量。
|
|
10
|
-
*/
|
|
11
|
-
variableMode?: boolean;
|
|
12
|
-
/**
|
|
13
|
-
* 是否允许 filter 语法,比如:
|
|
14
|
-
*
|
|
15
|
-
* ${abc | html}
|
|
16
|
-
*/
|
|
17
|
-
allowFilter?: boolean;
|
|
18
|
-
variableNamespaces?: Array<string>;
|
|
19
|
-
}
|
|
20
|
-
export interface ASTNode {
|
|
21
|
-
type: string;
|
|
22
|
-
start: Position;
|
|
23
|
-
end: Position;
|
|
24
|
-
[propname: string]: any;
|
|
25
|
-
}
|
|
26
|
-
export declare type ASTNodeOrNull = ASTNode | null;
|
|
1
|
+
import { ParserOptions, ASTNode } from './types';
|
|
27
2
|
export declare function parse(input: string, options?: ParserOptions): ASTNode;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Evaluator } from './evalutor';
|
|
2
|
+
export interface FilterMap {
|
|
3
|
+
[propName: string]: (this: FilterContext, input: any, ...args: any[]) => any;
|
|
4
|
+
}
|
|
5
|
+
export interface FunctionMap {
|
|
6
|
+
[propName: string]: (this: Evaluator, ast: Object, data: any) => any;
|
|
7
|
+
}
|
|
8
|
+
export interface FilterContext {
|
|
9
|
+
data: Object;
|
|
10
|
+
filter?: {
|
|
11
|
+
name: string;
|
|
12
|
+
args: Array<any>;
|
|
13
|
+
};
|
|
14
|
+
restFilters: Array<{
|
|
15
|
+
name: string;
|
|
16
|
+
args: Array<any>;
|
|
17
|
+
}>;
|
|
18
|
+
}
|
|
19
|
+
export interface EvaluatorOptions {
|
|
20
|
+
/**
|
|
21
|
+
* 可以外部传入 ast 节点处理器,定制或者扩充自定义函数
|
|
22
|
+
*/
|
|
23
|
+
functions?: FunctionMap;
|
|
24
|
+
/**
|
|
25
|
+
* 可以外部扩充 filter
|
|
26
|
+
*/
|
|
27
|
+
filters?: FilterMap;
|
|
28
|
+
defaultFilter?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface LexerOptions {
|
|
31
|
+
/**
|
|
32
|
+
* 直接是运算表达式?还是从模板开始 ${} 里面才算运算表达式
|
|
33
|
+
*/
|
|
34
|
+
evalMode?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* 只支持取变量。
|
|
37
|
+
*/
|
|
38
|
+
variableMode?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* 是否允许 filter 语法,比如:
|
|
41
|
+
*
|
|
42
|
+
* ${abc | html}
|
|
43
|
+
*/
|
|
44
|
+
allowFilter?: boolean;
|
|
45
|
+
}
|
|
46
|
+
export declare type TokenTypeName = 'Boolean' | 'Raw' | 'Variable' | 'OpenScript' | 'CloseScript' | 'EOF' | 'Identifier' | 'Literal' | 'Numeric' | 'Punctuator' | 'String' | 'RegularExpression' | 'TemplateRaw' | 'TemplateLeftBrace' | 'TemplateRightBrace' | 'OpenFilter' | 'Char';
|
|
47
|
+
export interface Position {
|
|
48
|
+
index: number;
|
|
49
|
+
line: number;
|
|
50
|
+
column: number;
|
|
51
|
+
}
|
|
52
|
+
export interface Token {
|
|
53
|
+
type: TokenTypeName;
|
|
54
|
+
value: any;
|
|
55
|
+
raw?: string;
|
|
56
|
+
start: Position;
|
|
57
|
+
end: Position;
|
|
58
|
+
}
|
|
59
|
+
export declare type NodeType = 'content' | 'raw' | 'conditional';
|
|
60
|
+
export interface ParserOptions {
|
|
61
|
+
/**
|
|
62
|
+
* 直接是运算表达式?还是从模板开始 ${} 里面才算运算表达式
|
|
63
|
+
*/
|
|
64
|
+
evalMode?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* 只支持取变量。
|
|
67
|
+
*/
|
|
68
|
+
variableMode?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* 是否允许 filter 语法,比如:
|
|
71
|
+
*
|
|
72
|
+
* ${abc | html}
|
|
73
|
+
*/
|
|
74
|
+
allowFilter?: boolean;
|
|
75
|
+
variableNamespaces?: Array<string>;
|
|
76
|
+
}
|
|
77
|
+
export interface ASTNode {
|
|
78
|
+
type: string;
|
|
79
|
+
start: Position;
|
|
80
|
+
end: Position;
|
|
81
|
+
[propname: string]: any;
|
|
82
|
+
}
|
|
83
|
+
export declare type ASTNodeOrNull = ASTNode | null;
|
package/package.json
CHANGED
package/rollup.config.js
CHANGED
|
@@ -30,6 +30,7 @@ export default {
|
|
|
30
30
|
plugins: [
|
|
31
31
|
isForLib && terser()
|
|
32
32
|
],
|
|
33
|
+
strict: !isForLib,
|
|
33
34
|
footer: isForLib ? `var evaluate = formula.evaluate;
|
|
34
35
|
var momentFormat = formula.momentFormat;
|
|
35
36
|
var parse = formula.parse;` : '',
|
|
@@ -67,7 +68,8 @@ export default {
|
|
|
67
68
|
json(),
|
|
68
69
|
resolve({
|
|
69
70
|
jsnext: true,
|
|
70
|
-
main: true
|
|
71
|
+
main: true,
|
|
72
|
+
browser: true
|
|
71
73
|
}),
|
|
72
74
|
typescript({
|
|
73
75
|
typescript: require('typescript')
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import path = require('path');
|
|
2
|
+
import fs = require('fs');
|
|
3
|
+
import doctrine = require('doctrine');
|
|
4
|
+
|
|
5
|
+
const workDir = path.resolve(path.dirname(__dirname));
|
|
6
|
+
const jsFile = path.join(workDir, 'src/evalutor.ts');
|
|
7
|
+
const outputFile = path.join(workDir, 'dist/doc.js');
|
|
8
|
+
const outputMD = path.join(workDir, 'dist/doc.md');
|
|
9
|
+
|
|
10
|
+
function getFormulaComments(contents: string) {
|
|
11
|
+
const comments: Array<{
|
|
12
|
+
fnName: string;
|
|
13
|
+
comments: string;
|
|
14
|
+
}> = [];
|
|
15
|
+
|
|
16
|
+
contents.replace(/\/\*[\s\S]+?\*\//g, (_, index, input) => {
|
|
17
|
+
const pos = index + _.length;
|
|
18
|
+
const following = input.substring(pos, pos + 200);
|
|
19
|
+
|
|
20
|
+
if (/^\s*fn(\w+)\(/.test(following)) {
|
|
21
|
+
comments.push({
|
|
22
|
+
fnName: RegExp.$1,
|
|
23
|
+
comments: _
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return _;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return comments;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function formatType(tag: any): string {
|
|
34
|
+
// console.log(tag);
|
|
35
|
+
if (tag.type === 'RestType') {
|
|
36
|
+
return `...${formatType(tag.expression)}`;
|
|
37
|
+
} else if (tag.type === 'TypeApplication') {
|
|
38
|
+
return `Array<${tag.applications
|
|
39
|
+
.map((item: any) => formatType(item))
|
|
40
|
+
.join(',')}>`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return tag.name;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function main(...params: Array<any>) {
|
|
47
|
+
const contents = fs.readFileSync(jsFile, 'utf8');
|
|
48
|
+
|
|
49
|
+
const comments = getFormulaComments(contents);
|
|
50
|
+
const result = comments.map(item => {
|
|
51
|
+
const ast = doctrine.parse(item.comments, {
|
|
52
|
+
unwrap: true
|
|
53
|
+
});
|
|
54
|
+
const result: any = {
|
|
55
|
+
name: item.fnName,
|
|
56
|
+
description: ast.description
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
let example = '';
|
|
60
|
+
let params: Array<any> = [];
|
|
61
|
+
let returns: any = undefined;
|
|
62
|
+
let namespace = '';
|
|
63
|
+
ast.tags.forEach(tag => {
|
|
64
|
+
if (tag.title === 'example') {
|
|
65
|
+
example = tag.description!;
|
|
66
|
+
} else if (tag.title === 'namespace') {
|
|
67
|
+
namespace = tag.name!;
|
|
68
|
+
} else if (tag.title === 'param') {
|
|
69
|
+
params.push({
|
|
70
|
+
type: formatType(tag.type),
|
|
71
|
+
name: tag.name,
|
|
72
|
+
description: tag.description
|
|
73
|
+
});
|
|
74
|
+
} else if (tag.title === 'returns') {
|
|
75
|
+
returns = {
|
|
76
|
+
type: formatType(tag.type),
|
|
77
|
+
description: tag.description
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
result.example = example;
|
|
83
|
+
result.params = params;
|
|
84
|
+
result.returns = returns;
|
|
85
|
+
result.namespace = namespace;
|
|
86
|
+
|
|
87
|
+
return result;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
fs.writeFileSync(
|
|
91
|
+
outputFile,
|
|
92
|
+
`/**\n * 公式文档\n */\nexports.doc = ${JSON.stringify(
|
|
93
|
+
result,
|
|
94
|
+
null,
|
|
95
|
+
2
|
|
96
|
+
)}`.replace(/\"(\w+)\"\:/g, (_, key) => `${key}:`),
|
|
97
|
+
'utf8'
|
|
98
|
+
);
|
|
99
|
+
console.log(`公式文档生成 > ${outputFile}`);
|
|
100
|
+
fs.writeFileSync(
|
|
101
|
+
outputFile.replace(/\.js$/, '.d.ts'),
|
|
102
|
+
// 可以通过以下命令生成
|
|
103
|
+
// tsc --declaration --emitDeclarationOnly --allowJs doc.js
|
|
104
|
+
[
|
|
105
|
+
`export var doc: {`,
|
|
106
|
+
` name: string;`,
|
|
107
|
+
` description: string;`,
|
|
108
|
+
` example: string;`,
|
|
109
|
+
` params: {`,
|
|
110
|
+
` type: string;`,
|
|
111
|
+
` name: string;`,
|
|
112
|
+
` description: string;`,
|
|
113
|
+
` }[];`,
|
|
114
|
+
` returns: {`,
|
|
115
|
+
` type: string;`,
|
|
116
|
+
` description: string;`,
|
|
117
|
+
` };`,
|
|
118
|
+
` namespace: string;`,
|
|
119
|
+
`}[];`
|
|
120
|
+
].join('\n'),
|
|
121
|
+
'utf8'
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const grouped: any = {};
|
|
125
|
+
result.forEach((item: any) => {
|
|
126
|
+
const scope = item.namespace || '其他';
|
|
127
|
+
const arr = grouped[scope] || (grouped[scope] = []);
|
|
128
|
+
|
|
129
|
+
arr.push(item);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
let md = '';
|
|
133
|
+
Object.keys(grouped).forEach(key => {
|
|
134
|
+
md += `## ${key}\n\n`;
|
|
135
|
+
|
|
136
|
+
grouped[key].forEach((item: any) => {
|
|
137
|
+
md += `### ${item.name}\n\n`;
|
|
138
|
+
|
|
139
|
+
md += `用法:\`${item.example}\`\n\n`;
|
|
140
|
+
|
|
141
|
+
if (item.params.length) {
|
|
142
|
+
// md += `参数:\n`;
|
|
143
|
+
|
|
144
|
+
item.params.forEach((param: any) => {
|
|
145
|
+
md += ` * \`${param.name}:${param.type}\` ${param.description}\n`;
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
if (item.returns) {
|
|
149
|
+
md += `\n返回:\`${item.returns.type}\` ${
|
|
150
|
+
item.returns.description || ''
|
|
151
|
+
}\n\n`;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
md += `${item.description}\n\n`;
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
fs.writeFileSync(outputMD, md, 'utf8');
|
|
159
|
+
console.log(`公式md生成 > ${outputMD}`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
main().catch(e => console.error(e));
|
package/scripts/lib.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {parse, evaluate} from '../src';
|
|
2
|
+
import moment from 'moment';
|
|
3
|
+
|
|
4
|
+
// 来自 https://vanillajstoolkit.com/polyfills/arrayfind/
|
|
5
|
+
if (!Array.prototype.find) {
|
|
6
|
+
Array.prototype.find = function (callback) {
|
|
7
|
+
// 1. Let O be ? ToObject(this value).
|
|
8
|
+
if (this == null) {
|
|
9
|
+
throw new TypeError('"this" is null or not defined');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
var o = Object(this);
|
|
13
|
+
|
|
14
|
+
// 2. Let len be ? ToLength(? Get(O, "length")).
|
|
15
|
+
var len = o.length >>> 0;
|
|
16
|
+
|
|
17
|
+
// 3. If IsCallable(callback) is false, throw a TypeError exception.
|
|
18
|
+
if (typeof callback !== 'function') {
|
|
19
|
+
throw new TypeError('callback must be a function');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
|
|
23
|
+
var thisArg = arguments[1];
|
|
24
|
+
|
|
25
|
+
// 5. Let k be 0.
|
|
26
|
+
var k = 0;
|
|
27
|
+
|
|
28
|
+
// 6. Repeat, while k < len
|
|
29
|
+
while (k < len) {
|
|
30
|
+
// a. Let Pk be ! ToString(k).
|
|
31
|
+
// b. Let kValue be ? Get(O, Pk).
|
|
32
|
+
// c. Let testResult be ToBoolean(? Call(callback, T, « kValue, k, O »)).
|
|
33
|
+
// d. If testResult is true, return kValue.
|
|
34
|
+
var kValue = o[k];
|
|
35
|
+
if (callback.call(thisArg, kValue, k, o)) {
|
|
36
|
+
return kValue;
|
|
37
|
+
}
|
|
38
|
+
// e. Increase k by 1.
|
|
39
|
+
k++;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 7. Return undefined.
|
|
43
|
+
return undefined;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function momentFormat(
|
|
48
|
+
input: any,
|
|
49
|
+
inputFormat: string,
|
|
50
|
+
outputFormat: string
|
|
51
|
+
) {
|
|
52
|
+
return moment(input, inputFormat).format(outputFormat);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export {parse, evaluate, moment};
|