Skip to content

Commit 2c0a829

Browse files
committed
fix
1 parent 9191710 commit 2c0a829

File tree

1 file changed

+62
-115
lines changed

1 file changed

+62
-115
lines changed

csharp/ToolGood.Algorithm.WebAssembly/Program.cs

Lines changed: 62 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ private static void Main(string[] args)
1515
[JSInvokable]
1616
public static string TryEvaluateString(string exp, string def = null, string data = null, string option = null)
1717
{
18-
AlgorithmEngine ae;
18+
AlgorithmEngineEx ae;
1919
if (option == null) {
20-
ae = new AlgorithmEngine();
20+
ae = new AlgorithmEngineEx();
2121
} else {
2222
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
23-
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
23+
ae = new AlgorithmEngineEx(bool.Parse(ops["IgnoreCase"].ToString()));
2424
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
2525
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
2626
ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString());
@@ -37,29 +37,27 @@ public static string TryEvaluateString(string exp, string def = null, string dat
3737
result["useDef"] = false;
3838
result["error"] = null;
3939
try {
40-
if (ae.Parse(exp)) {
41-
result["parse"] = true;
42-
var obj = ae.Evaluate();
43-
if (obj.IsNull) {
44-
result["result"] = null;
45-
return JsonSerializer.Serialize(result);
46-
}
47-
if (obj.IsError) {
48-
result["result"] = def;
49-
result["useDef"] = true;
50-
result["error"] = obj.ErrorMsg;
51-
return JsonSerializer.Serialize(result);
52-
}
53-
if (obj.Type == OperandType.DATE) {
54-
result["result"] = obj.DateValue.ToString();
55-
result["error"] = ae.LastError;
56-
return JsonSerializer.Serialize(result);
57-
}
58-
obj = obj.ToText("It can't be converted to string!");
59-
result["result"] = obj.TextValue;
40+
var fun = ae.Parse(exp);
41+
result["parse"] = true;
42+
var obj = fun.Calculate(ae);
43+
if (obj.IsNull) {
44+
result["result"] = null;
6045
return JsonSerializer.Serialize(result);
6146
}
62-
result["error"] = "Parameter exp invalid !";
47+
if (obj.IsError) {
48+
result["result"] = def;
49+
result["useDef"] = true;
50+
result["error"] = obj.ErrorMsg;
51+
return JsonSerializer.Serialize(result);
52+
}
53+
if (obj.Type == OperandType.DATE) {
54+
result["result"] = obj.DateValue.ToString();
55+
result["error"] = ae.LastError;
56+
return JsonSerializer.Serialize(result);
57+
}
58+
obj = obj.ToText("It can't be converted to string!");
59+
result["result"] = obj.TextValue;
60+
return JsonSerializer.Serialize(result);
6361
} catch (Exception ex) {
6462
result["error"] = ex.Message;
6563
}
@@ -71,12 +69,12 @@ public static string TryEvaluateString(string exp, string def = null, string dat
7169
[JSInvokable]
7270
public static string TryEvaluateNumber(string exp, decimal def, string data = null, string option = null)
7371
{
74-
AlgorithmEngine ae;
72+
AlgorithmEngineEx ae;
7573
if (option == null) {
76-
ae = new AlgorithmEngine();
74+
ae = new AlgorithmEngineEx();
7775
} else {
7876
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
79-
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
77+
ae = new AlgorithmEngineEx(bool.Parse(ops["IgnoreCase"].ToString()));
8078
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
8179
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
8280
ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString());
@@ -93,20 +91,18 @@ public static string TryEvaluateNumber(string exp, decimal def, string data = nu
9391
result["useDef"] = false;
9492
result["error"] = null;
9593
try {
96-
if (ae.Parse(exp)) {
97-
result["parse"] = true;
98-
var obj = ae.Evaluate();
99-
obj = obj.ToNumber("It can't be converted to number!");
100-
if (obj.IsError) {
101-
result["result"] = def;
102-
result["useDef"] = true;
103-
result["error"] = obj.ErrorMsg;
104-
return JsonSerializer.Serialize(result);
105-
}
106-
result["result"] = obj.NumberValue;
94+
var fun = ae.Parse(exp);
95+
result["parse"] = true;
96+
var obj = fun.Calculate(ae);
97+
obj = obj.ToNumber("It can't be converted to bool!");
98+
if (obj.IsError) {
99+
result["result"] = def;
100+
result["useDef"] = true;
101+
result["error"] = obj.ErrorMsg;
107102
return JsonSerializer.Serialize(result);
108103
}
109-
result["error"] = "Parameter exp invalid !";
104+
result["result"] = obj.NumberValue;
105+
return JsonSerializer.Serialize(result);
110106
} catch (Exception ex) {
111107
result["error"] = ex.Message;
112108
}
@@ -118,12 +114,12 @@ public static string TryEvaluateNumber(string exp, decimal def, string data = nu
118114
[JSInvokable]
119115
public static string TryEvaluateBool(string exp, bool def, string data = null, string option = null)
120116
{
121-
AlgorithmEngine ae;
117+
AlgorithmEngineEx ae;
122118
if (option == null) {
123-
ae = new AlgorithmEngine();
119+
ae = new AlgorithmEngineEx();
124120
} else {
125121
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
126-
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
122+
ae = new AlgorithmEngineEx(bool.Parse(ops["IgnoreCase"].ToString()));
127123
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
128124
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
129125
ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString());
@@ -140,20 +136,18 @@ public static string TryEvaluateBool(string exp, bool def, string data = null, s
140136
result["useDef"] = false;
141137
result["error"] = null;
142138
try {
143-
if (ae.Parse(exp)) {
144-
result["parse"] = true;
145-
var obj = ae.Evaluate();
146-
obj = obj.ToBoolean("It can't be converted to bool!");
147-
if (obj.IsError) {
148-
result["result"] = def;
149-
result["useDef"] = true;
150-
result["error"] = obj.ErrorMsg;
151-
return JsonSerializer.Serialize(result);
152-
}
153-
result["result"] = obj.BooleanValue;
139+
var fun = ae.Parse(exp);
140+
result["parse"] = true;
141+
var obj = fun.Calculate(ae);
142+
obj = obj.ToBoolean("It can't be converted to bool!");
143+
if (obj.IsError) {
144+
result["result"] = def;
145+
result["useDef"] = true;
146+
result["error"] = obj.ErrorMsg;
154147
return JsonSerializer.Serialize(result);
155148
}
156-
result["error"] = "Parameter exp invalid !";
149+
result["result"] = obj.BooleanValue;
150+
return JsonSerializer.Serialize(result);
157151
} catch (Exception ex) {
158152
result["error"] = ex.Message;
159153
}
@@ -165,12 +159,12 @@ public static string TryEvaluateBool(string exp, bool def, string data = null, s
165159
[JSInvokable]
166160
public static string TryEvaluateDateTime(string exp, DateTime def, string data = null, string option = null)
167161
{
168-
AlgorithmEngine ae;
162+
AlgorithmEngineEx ae;
169163
if (option == null) {
170-
ae = new AlgorithmEngine();
164+
ae = new AlgorithmEngineEx();
171165
} else {
172166
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
173-
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
167+
ae = new AlgorithmEngineEx(bool.Parse(ops["IgnoreCase"].ToString()));
174168
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
175169
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
176170
ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString());
@@ -187,20 +181,18 @@ public static string TryEvaluateDateTime(string exp, DateTime def, string data =
187181
result["useDef"] = false;
188182
result["error"] = null;
189183
try {
190-
if (ae.Parse(exp)) {
191-
result["parse"] = true;
192-
var obj = ae.Evaluate();
193-
obj = obj.ToMyDate("It can't be converted to datetime!");
194-
if (obj.IsError) {
195-
result["result"] = def;
196-
result["useDef"] = true;
197-
result["error"] = ae.LastError;
198-
return def.ToString("yyyy-MM-dd HH:mm:ss");
199-
}
200-
result["result"] = obj.DateValue.ToString();
201-
return JsonSerializer.Serialize(result);
184+
var fun = ae.Parse(exp);
185+
result["parse"] = true;
186+
var obj = fun.Calculate(ae);
187+
obj = obj.ToMyDate("It can't be converted to datetime!");
188+
if (obj.IsError) {
189+
result["result"] = def;
190+
result["useDef"] = true;
191+
result["error"] = ae.LastError;
192+
return def.ToString("yyyy-MM-dd HH:mm:ss");
202193
}
203-
result["error"] = "Parameter exp invalid !";
194+
result["result"] = obj.DateValue.ToString();
195+
return JsonSerializer.Serialize(result);
204196
} catch (Exception ex) {
205197
result["error"] = ex.Message;
206198
}
@@ -209,51 +201,6 @@ public static string TryEvaluateDateTime(string exp, DateTime def, string data =
209201
return JsonSerializer.Serialize(result);
210202
}
211203

212-
[JSInvokable]
213-
public static String GetSimplifiedFormula(String formula, string data = null, string option = null)
214-
{
215-
AlgorithmEngine ae;
216-
if (option == null) {
217-
ae = new AlgorithmEngine();
218-
} else {
219-
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
220-
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
221-
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
222-
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
223-
ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString());
224-
ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString()));
225-
ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString()));
226-
ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString()));
227-
ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString()));
228-
}
229-
if (data != null) {
230-
ae.AddParameterFromJson(data);
231-
}
232-
return ae.GetSimplifiedFormula(formula);
233-
}
234-
235-
[JSInvokable]
236-
public static string EvaluateFormula(string exp, string splitChars, string data = null, string option = null)
237-
{
238-
AlgorithmEngine ae;
239-
if (option == null) {
240-
ae = new AlgorithmEngine();
241-
} else {
242-
var ops = JsonSerializer.Deserialize<Dictionary<string, object>>(option);
243-
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
244-
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
245-
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
246-
ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString());
247-
ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString()));
248-
ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString()));
249-
ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString()));
250-
ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString()));
251-
}
252-
if (data != null) {
253-
ae.AddParameterFromJson(data);
254-
}
255-
return ae.EvaluateFormula(exp, splitChars.ToCharArray());
256-
}
257204

258205
[JSInvokable]
259206
public static bool IsKeywords(string parameter)

0 commit comments

Comments
 (0)