Skip to content

Commit 2f95e32

Browse files
authored
refactor: update code files (#28)
* refactor: update code files * chore: update code style
1 parent d2591b7 commit 2f95e32

23 files changed

+2998
-2979
lines changed

bench/expr.bench.ts

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@ import { bench, describe } from "vitest";
33
import { compile, evaluate, register } from "../dist/index.esm.js";
44

55
const context = {
6-
user: {
7-
name: "John",
8-
age: 30,
9-
isAdmin: true,
10-
scores: [85, 90, 78, 92],
11-
address: {
12-
city: "New York",
13-
zip: "10001",
14-
},
15-
},
16-
products: [
17-
{ id: 1, name: "Laptop", price: 1200 },
18-
{ id: 2, name: "Phone", price: 800 },
19-
{ id: 3, name: "Tablet", price: 500 },
20-
],
21-
calculateTotal: (items: any[]) => {
22-
const total = items.reduce((sum, item) => sum + item.price, 0);
23-
return total;
24-
},
25-
applyDiscount: (total: number, percentage: number) => {
26-
const value = total * (1 - percentage / 100);
27-
return value;
28-
},
6+
user: {
7+
name: "John",
8+
age: 30,
9+
isAdmin: true,
10+
scores: [85, 90, 78, 92],
11+
address: {
12+
city: "New York",
13+
zip: "10001",
14+
},
15+
},
16+
products: [
17+
{ id: 1, name: "Laptop", price: 1200 },
18+
{ id: 2, name: "Phone", price: 800 },
19+
{ id: 3, name: "Tablet", price: 500 },
20+
],
21+
calculateTotal: (items: any[]) => {
22+
const total = items.reduce((sum, item) => sum + item.price, 0);
23+
return total;
24+
},
25+
applyDiscount: (total: number, percentage: number) => {
26+
const value = total * (1 - percentage / 100);
27+
return value;
28+
},
2929
};
3030

3131
const simpleExpression = "user.age + 5";
3232
const mediumExpression = 'user.scores[2] > 80 ? "Good" : "Needs improvement"';
3333
const complexExpression =
34-
'@applyDiscount(@calculateTotal(products), 10) > 2000 ? "High value" : "Standard"';
34+
'@applyDiscount(@calculateTotal(products), 10) > 2000 ? "High value" : "Standard"';
3535

3636
const complexExpression2 =
37-
'applyDiscount(calculateTotal(products), 10) > 2000 ? "High value" : "Standard"';
37+
'applyDiscount(calculateTotal(products), 10) > 2000 ? "High value" : "Standard"';
3838

3939
const simpleExpressionCompiler = compile(simpleExpression);
4040
const mediumExpressionCompiler = compile(mediumExpression);
@@ -48,80 +48,80 @@ parser.functions.calculateTotal = context.calculateTotal;
4848
parser.functions.applyDiscount = context.applyDiscount;
4949

5050
const newFunctionSimple = new Function(
51-
"context",
52-
`with(context) { return ${simpleExpression}; }`,
51+
"context",
52+
`with(context) { return ${simpleExpression}; }`,
5353
);
5454
const newFunctionMedium = new Function(
55-
"context",
56-
`with(context) { return ${mediumExpression}; }`,
55+
"context",
56+
`with(context) { return ${mediumExpression}; }`,
5757
);
5858
const newFunctionComplex = new Function(
59-
"context",
60-
`with(context) { return ${complexExpression2}; }`,
59+
"context",
60+
`with(context) { return ${complexExpression2}; }`,
6161
);
6262

6363
describe("Simple Expression Benchmarks", () => {
64-
bench("evaluate after compile (baseline) only interpreter", () => {
65-
simpleExpressionCompiler(context);
66-
});
67-
68-
bench("new Function (vs evaluate)", () => {
69-
newFunctionSimple(context);
70-
});
71-
72-
bench(
73-
"evaluate without compile (vs evaluate) tokenize + parse + interpreter",
74-
() => {
75-
evaluate(simpleExpression, context);
76-
},
77-
);
78-
79-
bench("expr-eval Parser (vs evaluate)", () => {
80-
// @ts-ignore
81-
Parser.evaluate(simpleExpression, context);
82-
});
64+
bench("evaluate after compile (baseline) only interpreter", () => {
65+
simpleExpressionCompiler(context);
66+
});
67+
68+
bench("new Function (vs evaluate)", () => {
69+
newFunctionSimple(context);
70+
});
71+
72+
bench(
73+
"evaluate without compile (vs evaluate) tokenize + parse + interpreter",
74+
() => {
75+
evaluate(simpleExpression, context);
76+
},
77+
);
78+
79+
bench("expr-eval Parser (vs evaluate)", () => {
80+
// @ts-ignore
81+
Parser.evaluate(simpleExpression, context);
82+
});
8383
});
8484

8585
describe("Medium Expression Benchmarks", () => {
86-
bench("evaluate after compile (baseline) only interpreter", () => {
87-
mediumExpressionCompiler(context);
88-
});
89-
90-
bench("new Function (vs evaluate)", () => {
91-
newFunctionMedium(context);
92-
});
93-
94-
bench(
95-
"evaluate without compile (vs evaluate) tokenize + parse + interpreter",
96-
() => {
97-
evaluate(mediumExpression, context);
98-
},
99-
);
100-
101-
bench("expr-eval Parser (vs evaluate)", () => {
102-
// @ts-ignore
103-
Parser.evaluate(mediumExpression, context);
104-
});
86+
bench("evaluate after compile (baseline) only interpreter", () => {
87+
mediumExpressionCompiler(context);
88+
});
89+
90+
bench("new Function (vs evaluate)", () => {
91+
newFunctionMedium(context);
92+
});
93+
94+
bench(
95+
"evaluate without compile (vs evaluate) tokenize + parse + interpreter",
96+
() => {
97+
evaluate(mediumExpression, context);
98+
},
99+
);
100+
101+
bench("expr-eval Parser (vs evaluate)", () => {
102+
// @ts-ignore
103+
Parser.evaluate(mediumExpression, context);
104+
});
105105
});
106106

107107
describe("Complex Expression Benchmarks", () => {
108-
bench("evaluate after compile (baseline) only interpreter", () => {
109-
complexExpressionCompiler(context);
110-
});
111-
112-
bench("new Function (vs evaluate)", () => {
113-
newFunctionComplex(context);
114-
});
115-
116-
bench(
117-
"evaluate without compile (vs evaluate) tokenize + parse + interpreter",
118-
() => {
119-
evaluate(complexExpression2, context);
120-
},
121-
);
122-
123-
bench("expr-eval Parser (vs evaluate)", () => {
124-
// @ts-ignore
125-
parser.evaluate(complexExpression2, context);
126-
});
108+
bench("evaluate after compile (baseline) only interpreter", () => {
109+
complexExpressionCompiler(context);
110+
});
111+
112+
bench("new Function (vs evaluate)", () => {
113+
newFunctionComplex(context);
114+
});
115+
116+
bench(
117+
"evaluate without compile (vs evaluate) tokenize + parse + interpreter",
118+
() => {
119+
evaluate(complexExpression2, context);
120+
},
121+
);
122+
123+
bench("expr-eval Parser (vs evaluate)", () => {
124+
// @ts-ignore
125+
parser.evaluate(complexExpression2, context);
126+
});
127127
});

biome.json

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
3-
"vcs": {
4-
"enabled": false,
5-
"clientKind": "git",
6-
"useIgnoreFile": false
7-
},
8-
"files": {
9-
"ignoreUnknown": false,
10-
"ignore": ["dist", "node_modules", "coverage"]
11-
},
12-
"formatter": {
13-
"enabled": true,
14-
"indentStyle": "tab"
15-
},
16-
"organizeImports": {
17-
"enabled": true
18-
},
19-
"linter": {
20-
"enabled": true,
21-
"rules": {
22-
"recommended": true
23-
},
24-
"ignore": ["node_modules", "dist", "tests", "bench", "coverage"]
25-
},
26-
"javascript": {
27-
"formatter": {
28-
"quoteStyle": "double"
29-
}
30-
}
2+
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
3+
"vcs": {
4+
"enabled": false,
5+
"clientKind": "git",
6+
"useIgnoreFile": false
7+
},
8+
"files": {
9+
"ignoreUnknown": false,
10+
"ignore": ["dist", "node_modules", "coverage"]
11+
},
12+
"formatter": {
13+
"enabled": true,
14+
"indentStyle": "space",
15+
"indentWidth": 2
16+
},
17+
"organizeImports": {
18+
"enabled": true
19+
},
20+
"linter": {
21+
"enabled": true,
22+
"rules": {
23+
"recommended": true
24+
},
25+
"ignore": ["node_modules", "dist", "tests", "bench", "coverage"]
26+
},
27+
"javascript": {
28+
"formatter": {
29+
"quoteStyle": "double"
30+
}
31+
}
3132
}

package.json

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
11
{
2-
"name": "@antv/expr",
3-
"version": "1.0.1",
4-
"description": "A secure, high-performance expression evaluator for dynamic chart rendering",
5-
"main": "dist/index.cjs.js",
6-
"module": "dist/index.esm.js",
7-
"types": "dist/index.d.ts",
8-
"files": ["dist", "LICENSE", "README.md", "package.json"],
9-
"scripts": {
10-
"build": "rollup -c",
11-
"test": "vitest run --coverage",
12-
"benchmark": "vitest bench",
13-
"prepublishOnly": "pnpm run test && pnpm run build"
14-
},
15-
"keywords": [
16-
"expression",
17-
"evaluator",
18-
"parser",
19-
"secure",
20-
"antv",
21-
"chart",
22-
"expr"
23-
],
24-
"repository": {
25-
"type": "git",
26-
"url": "https://github.com/antvis/expr"
27-
},
28-
"license": "MIT",
29-
"devDependencies": {
30-
"@biomejs/biome": "1.9.4",
31-
"@rollup/plugin-node-resolve": "^16.0.0",
32-
"@rollup/plugin-terser": "^0.4.4",
33-
"@rollup/plugin-typescript": "^12.1.2",
34-
"@vitest/coverage-v8": "^3.0.8",
35-
"expr-eval": "^2.0.2",
36-
"rollup": "^4.34.6",
37-
"tslib": "^2.8.1",
38-
"vitest": "^3.0.8"
39-
}
2+
"name": "@antv/expr",
3+
"version": "1.0.1",
4+
"description": "A secure, high-performance expression evaluator for dynamic chart rendering",
5+
"main": "dist/index.cjs.js",
6+
"module": "dist/index.esm.js",
7+
"types": "dist/index.d.ts",
8+
"files": ["dist", "LICENSE", "README.md", "package.json"],
9+
"scripts": {
10+
"build": "rollup -c && npm run size",
11+
"test": "vitest run --coverage",
12+
"size": "limit-size",
13+
"benchmark": "vitest bench",
14+
"prepublishOnly": "pnpm run test && pnpm run build"
15+
},
16+
"keywords": [
17+
"expression",
18+
"evaluator",
19+
"parser",
20+
"secure",
21+
"antv",
22+
"chart",
23+
"expr"
24+
],
25+
"devDependencies": {
26+
"@biomejs/biome": "1.9.4",
27+
"@rollup/plugin-node-resolve": "^16.0.0",
28+
"@rollup/plugin-terser": "^0.4.4",
29+
"@rollup/plugin-typescript": "^12.1.2",
30+
"@vitest/coverage-v8": "^3.0.8",
31+
"expr-eval": "^2.0.2",
32+
"limit-size": "^0.1.4",
33+
"rollup": "^4.34.6",
34+
"tslib": "^2.8.1",
35+
"vitest": "^3.0.8"
36+
},
37+
"limit-size": [
38+
{
39+
"path": "dist/index.cjs.js",
40+
"limit": "8 Kb"
41+
},
42+
{
43+
"path": "dist/index.cjs.js",
44+
"limit": "3 Kb",
45+
"gzip": true
46+
}
47+
],
48+
"repository": {
49+
"type": "git",
50+
"url": "https://github.com/antvis/expr"
51+
},
52+
"license": "MIT"
4053
}

0 commit comments

Comments
 (0)