Skip to content

Commit fbd432f

Browse files
author
linzhijun
committed
增加方法
1 parent aeeb791 commit fbd432f

File tree

10 files changed

+92
-7
lines changed

10 files changed

+92
-7
lines changed

csharp/ToolGood.Algorithm.WebAssembly/Program.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace ToolGood.Algorithm.WebAssembly
22
{
33
using Microsoft.JSInterop;
4+
using System.Reflection.Metadata;
5+
using System.Text.Json;
46
using ToolGood.Algorithm;
57
using ToolGood.Algorithm.Enums;
68

@@ -101,8 +103,82 @@ public static string TryEvaluateDateTime(string exp, DateTime def, string data =
101103
}
102104
return ae.TryEvaluate(exp, def).ToString("yyyy-MM-dd HH:mm:ss");
103105
}
106+
[JSInvokable]
107+
108+
public static String GetSimplifiedFormula(String formula, string data = null, string option = null)
109+
{
110+
AlgorithmEngine ae;
111+
if (option == null) {
112+
ae = new AlgorithmEngine();
113+
} else {
114+
var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option);
115+
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
116+
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
117+
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
118+
ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString());
119+
ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString()));
120+
ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString()));
121+
ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString()));
122+
ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString()));
123+
}
124+
if (data != null) {
125+
ae.AddParameterFromJson(data);
126+
}
127+
return ae.GetSimplifiedFormula(formula);
128+
}
129+
130+
131+
[JSInvokable]
132+
public static string EvaluateFormula(string exp,string splitChars, string data = null, string option = null)
133+
{
134+
AlgorithmEngine ae;
135+
if (option == null) {
136+
ae = new AlgorithmEngine();
137+
} else {
138+
var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option);
139+
ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString()));
140+
ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString());
141+
ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString());
142+
ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString());
143+
ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString()));
144+
ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString()));
145+
ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString()));
146+
ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString()));
147+
}
148+
if (data != null) {
149+
ae.AddParameterFromJson(data);
150+
}
151+
return ae.EvaluateFormula(exp, splitChars.ToCharArray());
152+
}
153+
154+
[JSInvokable]
155+
public static bool IsKeywords(string parameter)
156+
{
157+
return AlgorithmEngineHelper.IsKeywords(parameter);
158+
}
159+
160+
[JSInvokable]
161+
public static string GetDiyNames(String exp)
162+
{
163+
return JsonSerializer.Serialize(AlgorithmEngineHelper.GetDiyNames(exp));
164+
}
104165

105166

167+
[JSInvokable]
168+
public static string UnitConversion(decimal src, string oldSrcUnit, string oldTarUnit, string name = null)
169+
{
170+
Dictionary<string,object> dic = new Dictionary<string,object>();
171+
try {
172+
var r = AlgorithmEngineHelper.UnitConversion(src, oldSrcUnit, oldTarUnit, name);
173+
dic["code"] = 0;
174+
dic["result"] = r;
175+
176+
} catch (Exception ex) {
177+
dic["code"] = 1;
178+
dic["error"] = ex.Message;
179+
}
180+
return JsonSerializer.Serialize(dic);
181+
}
106182

107183
}
108184
}

csharp/ToolGood.Algorithm2.WebTest/wwwroot/ToolGood.Algorithm.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ AlgorithmEngine = class {
2424
if (typeof def == "number") { return this.TryEvaluateNumber(exp, def, data); }
2525
if (typeof def == "boolean") { return this.TryEvaluateBool(exp, def, data); }
2626
if (typeof def == "object" && def instanceof Date) { return this.TryEvaluateDateTime(exp, def, data); }
27-
2827
return this.TryEvaluateString(exp, def, data);
2928
}
3029

31-
};
30+
GetSimplifiedFormula = function (exp, data) { return DotNet.invokeMethod('ToolGood.Algorithm.WebAssembly', 'GetSimplifiedFormula', exp, data, JSON.stringify(this)); }
31+
EvaluateFormula = function (exp, splitChars, data) { return DotNet.invokeMethod('ToolGood.Algorithm.WebAssembly', 'EvaluateFormula', exp, splitChars, data, JSON.stringify(this)); }
32+
};
33+
AlgorithmEngineHelper = new class {
34+
IsKeywords = function (parameter) { return DotNet.invokeMethod('ToolGood.Algorithm.WebAssembly', 'IsKeywords', parameter); }
35+
GetDiyNames = function (exp) { return JSON.parse(DotNet.invokeMethod('ToolGood.Algorithm.WebAssembly', 'GetDiyNames', exp)); }
36+
UnitConversion = function (src, oldSrcUnit, oldTarUnit, name) { return JSON.parse(DotNet.invokeMethod('ToolGood.Algorithm.WebAssembly', 'UnitConversion', src, oldSrcUnit, oldTarUnit, name)); }
37+
38+
}();
39+
40+
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-+6jR/+4mhj6IYlEaRHA5fixqeML72rqIBxlvRpfZsaY=",
4+
"hash": "sha256-g87qVupByI7CTUkn65Ezj+hOSRTQO9xWsmux9TimU+A=",
55
"jsModuleNative": {
66
"dotnet.native.8.0.4.rkw194kvb3.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-CfDWYV2K9/n7chBK3fwN31VUi/kT+gZKG7Ty+dq2pEE=",
213-
"ToolGood.Algorithm.WebAssembly.wasm": "sha256-EWod2UabayYFT5UPii9TsFu2sh90lVC5jGtM4YeD2cY="
212+
"ToolGood.Algorithm.wasm": "sha256-3xkLwHcQuLsJ/6SsJlfTeeACZecAHO3d8L2sgij1Hn8=",
213+
"ToolGood.Algorithm.WebAssembly.wasm": "sha256-cbPrVyJ6kMQH7BTEL0t/HVc42pKEmNEvA5B7i3Xhy+Y="
214214
},
215215
"pdb": {
216-
"ToolGood.Algorithm.pdb": "sha256-/RAmiQrZkOcWJewe8uXuncwBeEMEMeTBXOkpw6WwB8M=",
217-
"ToolGood.Algorithm.WebAssembly.pdb": "sha256-0DIQ1pqepWsS/etk568iKUnz03OVB4IW8Av9gG+wLQk="
216+
"ToolGood.Algorithm.pdb": "sha256-C+rmcQ+rfFAFFJI1p9v5Obd4yp0YPYN6i3SJZNVHVgY=",
217+
"ToolGood.Algorithm.WebAssembly.pdb": "sha256-6mIKqNTrfOMcFQ68Hs74SZ8MSwXolbBckIX1H8B2+NE="
218218
}
219219
},
220220
"cacheBootResources": true,
Binary file not shown.

0 commit comments

Comments
 (0)