Skip to content

Commit 33e0bbe

Browse files
committed
修改 bug
1 parent 7a95925 commit 33e0bbe

File tree

12 files changed

+76
-29
lines changed

12 files changed

+76
-29
lines changed

csharp/ToolGood.Algorithm.WebAssembly/Program.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace ToolGood.Algorithm.WebAssembly
22
{
33
using Microsoft.JSInterop;
4-
using System.Reflection.Metadata;
54
using System.Text.Json;
65
using ToolGood.Algorithm;
76
using ToolGood.Algorithm.Enums;
@@ -20,7 +19,7 @@ public static string TryEvaluateString(string exp, string def = null, string dat
2019
if (option == null) {
2120
ae = new AlgorithmEngine();
2221
} else {
23-
var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option);
22+
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
2423
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
2524
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
2625
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
@@ -33,7 +32,23 @@ public static string TryEvaluateString(string exp, string def = null, string dat
3332
if (data != null) {
3433
ae.AddParameterFromJson(data);
3534
}
36-
return ae.TryEvaluate(exp, def);
35+
try {
36+
if (ae.Parse(exp)) {
37+
var obj = ae.Evaluate();
38+
if (obj.IsNull) {
39+
return null;
40+
}
41+
if (obj.IsError) {
42+
return def;
43+
}
44+
if (obj.Type == OperandType.DATE) {
45+
return obj.DateValue.ToString();
46+
}
47+
obj = obj.ToText("It can't be converted to string!");
48+
return obj.TextValue;
49+
}
50+
} catch { }
51+
return def;
3752
}
3853

3954
[JSInvokable]
@@ -43,7 +58,7 @@ public static decimal TryEvaluateNumber(string exp, decimal def, string data = n
4358
if (option == null) {
4459
ae = new AlgorithmEngine();
4560
} else {
46-
var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option);
61+
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
4762
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
4863
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
4964
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
@@ -66,7 +81,7 @@ public static bool TryEvaluateBool(string exp, bool def, string data = null, str
6681
if (option == null) {
6782
ae = new AlgorithmEngine();
6883
} else {
69-
var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option);
84+
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
7085
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
7186
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
7287
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
@@ -89,7 +104,7 @@ public static string TryEvaluateDateTime(string exp, DateTime def, string data =
89104
if (option == null) {
90105
ae = new AlgorithmEngine();
91106
} else {
92-
var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option);
107+
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
93108
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
94109
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
95110
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
@@ -102,7 +117,22 @@ public static string TryEvaluateDateTime(string exp, DateTime def, string data =
102117
if (data != null) {
103118
ae.AddParameterFromJson(data);
104119
}
105-
return ae.TryEvaluate(exp, def).ToString("yyyy-MM-dd HH:mm:ss");
120+
try {
121+
if (ae.Parse(exp)) {
122+
var obj = ae.Evaluate();
123+
if (obj.IsNull) {
124+
return null;
125+
}
126+
if (obj.IsError) {
127+
return def.ToString("yyyy-MM-dd HH:mm:ss");
128+
}
129+
if (obj.Type == OperandType.DATE) {
130+
return obj.DateValue.ToString();
131+
}
132+
return obj.DateValue.ToString();
133+
}
134+
} catch { }
135+
return def.ToString("yyyy-MM-dd HH:mm:ss");
106136
}
107137
[JSInvokable]
108138

@@ -112,7 +142,7 @@ public static String GetSimplifiedFormula(String formula, string data = null, st
112142
if (option == null) {
113143
ae = new AlgorithmEngine();
114144
} else {
115-
var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option);
145+
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
116146
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
117147
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
118148
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
@@ -130,13 +160,13 @@ public static String GetSimplifiedFormula(String formula, string data = null, st
130160

131161

132162
[JSInvokable]
133-
public static string EvaluateFormula(string exp,string splitChars, string data = null, string option = null)
163+
public static string EvaluateFormula(string exp, string splitChars, string data = null, string option = null)
134164
{
135165
AlgorithmEngine ae;
136166
if (option == null) {
137167
ae = new AlgorithmEngine();
138168
} else {
139-
var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option);
169+
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
140170
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
141171
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
142172
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
@@ -168,7 +198,7 @@ public static string GetDiyNames(String exp)
168198
[JSInvokable]
169199
public static string UnitConversion(decimal src, string oldSrcUnit, string oldTarUnit, string name = null)
170200
{
171-
Dictionary<string,object> dic = new Dictionary<string,object>();
201+
Dictionary<string, object> dic = new Dictionary<string, object>();
172202
try {
173203
var r = AlgorithmEngineHelper.UnitConversion(src, oldSrcUnit, oldTarUnit, name);
174204
dic["code"] = 0;

csharp/ToolGood.Algorithm2.Test/AlgorithmEngine/AlgorithmEngineTest_vlookup.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,21 @@ public void lookup_test()
3939
{'蓝色':'XL','canBookCount':973,'saleCount':26,'specId':'27402e07efd89afa50733afa94cd6976'},
4040
{'蓝色':'XXL','canBookCount':985,'saleCount':14,'specId':'358b6c3b52bf711ac8ecfe7513a4f3ad'}
4141
]";
42+
var json2 = @"{'jsonArray':[
43+
{'灰色':'L','canBookCount':905,'saleCount':91,'specId':'43b0e72e98731aed69e1f0cc7d64bf4d'},
44+
{'灰色':'XL','canBookCount':929,'saleCount':70,'specId':'893746f5330dc3273d24aa1ac1a9a8b5'},
45+
{'灰色':'XXL','canBookCount':942,'saleCount':57,'specId':'42d994cba0210528142a743d4069700f'},
46+
{'白色':'L','canBookCount':862,'saleCount':136,'specId':'82114cbd2c10b5e97b01af1510807e2d'},
47+
{'白色':'XL','canBookCount':881,'saleCount':118,'specId':'c45d8408137e34adf8e695250c42a2e9'},
48+
{'白色':'XXL','canBookCount':917,'saleCount':82,'specId':'df78564262818d6eb0c428a37ab4a251'},
49+
{'蓝色':'L','canBookCount':962,'saleCount':35,'specId':'e959b6ab7c355e403a3312c75bd3d5b4','key':null},
50+
{'蓝色':'XL','canBookCount':973,'saleCount':26,'specId':'27402e07efd89afa50733afa94cd6976'},
51+
{'蓝色':'XXL','canBookCount':985,'saleCount':14,'specId':'358b6c3b52bf711ac8ecfe7513a4f3ad'}
52+
]}";
53+
4254
AlgorithmEngine engine = new AlgorithmEngine();
43-
engine.AddParameter("jsonArray", json);
55+
//engine.AddParameter("jsonArray", json);
56+
engine.AddParameterFromJson(json2);
4457
// 第二种方法
4558
//engine.AddParameter("jsonArray",Operand.CreateJson( json));
4659

csharp/ToolGood.Algorithm2.WebTest/wwwroot/Test/test-vlookup.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616
});
1717
QUnit.test('lookup_test', async function (assert) {
1818
await new Promise(resolve => setTimeout(resolve, 300));
19-
var json = [
20-
{ '灰色': 'L', 'canBookCount': 905, 'saleCount': 91, 'specId': '43b0e72e98731aed69e1f0cc7d64bf4d' },
21-
{ '灰色': 'XL', 'canBookCount': 929, 'saleCount': 70, 'specId': '893746f5330dc3273d24aa1ac1a9a8b5' },
22-
{ '灰色': 'XXL', 'canBookCount': 942, 'saleCount': 57, 'specId': '42d994cba0210528142a743d4069700f' },
23-
{ '白色': 'L', 'canBookCount': 862, 'saleCount': 136, 'specId': '82114cbd2c10b5e97b01af1510807e2d' },
24-
{ '白色': 'XL', 'canBookCount': 881, 'saleCount': 118, 'specId': 'c45d8408137e34adf8e695250c42a2e9' },
25-
{ '白色': 'XXL', 'canBookCount': 917, 'saleCount': 82, 'specId': 'df78564262818d6eb0c428a37ab4a251' },
26-
{ '蓝色': 'L', 'canBookCount': 962, 'saleCount': 35, 'specId': 'e959b6ab7c355e403a3312c75bd3d5b4', 'key': null },
27-
{ '蓝色': 'XL', 'canBookCount': 973, 'saleCount': 26, 'specId': '27402e07efd89afa50733afa94cd6976' },
28-
{ '蓝色': 'XXL', 'canBookCount': 985, 'saleCount': 14, 'specId': '358b6c3b52bf711ac8ecfe7513a4f3ad' }
29-
];
19+
var json = {
20+
'jsonArray': [
21+
{ '灰色': 'L', 'canBookCount': 905, 'saleCount': 91, 'specId': '43b0e72e98731aed69e1f0cc7d64bf4d' },
22+
{ '灰色': 'XL', 'canBookCount': 929, 'saleCount': 70, 'specId': '893746f5330dc3273d24aa1ac1a9a8b5' },
23+
{ '灰色': 'XXL', 'canBookCount': 942, 'saleCount': 57, 'specId': '42d994cba0210528142a743d4069700f' },
24+
{ '白色': 'L', 'canBookCount': 862, 'saleCount': 136, 'specId': '82114cbd2c10b5e97b01af1510807e2d' },
25+
{ '白色': 'XL', 'canBookCount': 881, 'saleCount': 118, 'specId': 'c45d8408137e34adf8e695250c42a2e9' },
26+
{ '白色': 'XXL', 'canBookCount': 917, 'saleCount': 82, 'specId': 'df78564262818d6eb0c428a37ab4a251' },
27+
{ '蓝色': 'L', 'canBookCount': 962, 'saleCount': 35, 'specId': 'e959b6ab7c355e403a3312c75bd3d5b4', 'key': null },
28+
{ '蓝色': 'XL', 'canBookCount': 973, 'saleCount': 26, 'specId': '27402e07efd89afa50733afa94cd6976' },
29+
{ '蓝色': 'XXL', 'canBookCount': 985, 'saleCount': 14, 'specId': '358b6c3b52bf711ac8ecfe7513a4f3ad' }
30+
]
31+
};
3032
var engine = new AlgorithmEngine();
3133

3234
var num = engine.TryEvaluate("lookup([jsonArray].Json(),'[saleCount]=91','specId')", null,json);
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

csharp/ToolGood.Algorithm2.WebTest/wwwroot/_framework/blazor.boot.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"mainAssemblyName": "ToolGood.Algorithm.WebAssembly",
33
"resources": {
4-
"hash": "sha256-/vBbt9jxdJDAwt17zobh1MwflWiTGQDWDc1Ywwnvu6A=",
4+
"hash": "sha256-5dLuKyzPJue7bw/+7ZiEM14R0BarpjYOnTWPm2JqFvw=",
55
"jsModuleNative": {
66
"dotnet.native.8.0.4.s1uvq6euq2.js": "sha256-ju0+XWE0ihz554eKEj0HQ8Opa61xpbUIaX5/MNo8we4="
77
},
@@ -209,12 +209,12 @@
209209
"mscorlib.wasm": "sha256-VCfYgt1mcjITabSTosCDVZ+68ea3Z7kakTFaLNLG+18=",
210210
"netstandard.wasm": "sha256-YOZxxR9PM3Dltcijfb0Q4FD39DkWZbZ1e2ScbZDvWJ4=",
211211
"System.Private.CoreLib.wasm": "sha256-NrT1QkAzVU//2wgK9vXdq77lCGvLJ4IOZ+Aegesyanc=",
212-
"ToolGood.Algorithm.wasm": "sha256-3K21HrqFV35lxzW+t4zi+axAw2oz35iFZMXr5hS5oZg=",
213-
"ToolGood.Algorithm.WebAssembly.wasm": "sha256-cZhoSRHddxJUhHNrErixlffrTxjiMfGE3k0c4LjI1XA="
212+
"ToolGood.Algorithm.wasm": "sha256-FEk7Xb1HmwcNIRmHrVonc+xytBJ6wUHxH03VaOxLjIs=",
213+
"ToolGood.Algorithm.WebAssembly.wasm": "sha256-kQRjKxHoxysV8/nXlzHyCJYUQnt2BuG+XREK/QbWi5g="
214214
},
215215
"pdb": {
216-
"ToolGood.Algorithm.pdb": "sha256-kgkYXmk4hsD0I9r34uWSCzhO1jYKScrclXFfNoPqmsU=",
217-
"ToolGood.Algorithm.WebAssembly.pdb": "sha256-C1p/1FifSeeEq/0flGK4jtrASxz5yh206EU49cRQ+bA="
216+
"ToolGood.Algorithm.pdb": "sha256-VzSOY6T8Ey5qwCP0BVqScrtw0EELVnmxv35QJcjtlGc=",
217+
"ToolGood.Algorithm.WebAssembly.pdb": "sha256-lvZMWC5xKR5M8DPkKUx7gmfUMMXyW8CFpW7j6WObId8="
218218
}
219219
},
220220
"cacheBootResources": true,

0 commit comments

Comments
 (0)