|
1 | 1 | namespace ToolGood.Algorithm.WebAssembly |
2 | 2 | { |
3 | 3 | using Microsoft.JSInterop; |
| 4 | + using System.Reflection.Metadata; |
| 5 | + using System.Text.Json; |
4 | 6 | using ToolGood.Algorithm; |
5 | 7 | using ToolGood.Algorithm.Enums; |
6 | 8 |
|
@@ -101,8 +103,82 @@ public static string TryEvaluateDateTime(string exp, DateTime def, string data = |
101 | 103 | } |
102 | 104 | return ae.TryEvaluate(exp, def).ToString("yyyy-MM-dd HH:mm:ss"); |
103 | 105 | } |
| 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 | + } |
104 | 165 |
|
105 | 166 |
|
| 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 | + } |
106 | 182 |
|
107 | 183 | } |
108 | 184 | } |
0 commit comments