|
| 1 | +namespace ToolGood.Algorithm.WebAssembly |
| 2 | +{ |
| 3 | + using Microsoft.JSInterop; |
| 4 | + using System.Reflection.Metadata; |
| 5 | + using System.Text.Json; |
| 6 | + using ToolGood.Algorithm; |
| 7 | + using ToolGood.Algorithm.Enums; |
| 8 | + |
| 9 | + public class Program |
| 10 | + { |
| 11 | + private static void Main(string[] args) |
| 12 | + { |
| 13 | + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); |
| 14 | + } |
| 15 | + |
| 16 | + [JSInvokable] |
| 17 | + public static string TryEvaluateString(string exp, string def = null, string data = null, string option = null) |
| 18 | + { |
| 19 | + AlgorithmEngine ae; |
| 20 | + if (option == null) { |
| 21 | + ae = new AlgorithmEngine(); |
| 22 | + } else { |
| 23 | + var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option); |
| 24 | + ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString())); |
| 25 | + ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString()); |
| 26 | + ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString()); |
| 27 | + ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString()); |
| 28 | + ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString())); |
| 29 | + ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString())); |
| 30 | + ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString())); |
| 31 | + ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString())); |
| 32 | + } |
| 33 | + if (data != null) { |
| 34 | + ae.AddParameterFromJson(data); |
| 35 | + } |
| 36 | + return ae.TryEvaluate(exp, def); |
| 37 | + } |
| 38 | + |
| 39 | + [JSInvokable] |
| 40 | + public static decimal TryEvaluateNumber(string exp, decimal def, string data = null, string option = null) |
| 41 | + { |
| 42 | + AlgorithmEngine ae; |
| 43 | + if (option == null) { |
| 44 | + ae = new AlgorithmEngine(); |
| 45 | + } else { |
| 46 | + var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option); |
| 47 | + ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString())); |
| 48 | + ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString()); |
| 49 | + ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString()); |
| 50 | + ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString()); |
| 51 | + ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString())); |
| 52 | + ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString())); |
| 53 | + ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString())); |
| 54 | + ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString())); |
| 55 | + } |
| 56 | + if (data != null) { |
| 57 | + ae.AddParameterFromJson(data); |
| 58 | + } |
| 59 | + return ae.TryEvaluate(exp, def); |
| 60 | + } |
| 61 | + |
| 62 | + [JSInvokable] |
| 63 | + public static bool TryEvaluateBool(string exp, bool def, string data = null, string option = null) |
| 64 | + { |
| 65 | + AlgorithmEngine ae; |
| 66 | + if (option == null) { |
| 67 | + ae = new AlgorithmEngine(); |
| 68 | + } else { |
| 69 | + var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option); |
| 70 | + ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString())); |
| 71 | + ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString()); |
| 72 | + ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString()); |
| 73 | + ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString()); |
| 74 | + ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString())); |
| 75 | + ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString())); |
| 76 | + ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString())); |
| 77 | + ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString())); |
| 78 | + } |
| 79 | + if (data != null) { |
| 80 | + ae.AddParameterFromJson(data); |
| 81 | + } |
| 82 | + return ae.TryEvaluate(exp, def); |
| 83 | + } |
| 84 | + |
| 85 | + [JSInvokable] |
| 86 | + public static string TryEvaluateDateTime(string exp, DateTime def, string data = null, string option = null) |
| 87 | + { |
| 88 | + AlgorithmEngine ae; |
| 89 | + if (option == null) { |
| 90 | + ae = new AlgorithmEngine(); |
| 91 | + } else { |
| 92 | + var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option); |
| 93 | + ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString())); |
| 94 | + ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString()); |
| 95 | + ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString()); |
| 96 | + ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString()); |
| 97 | + ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString())); |
| 98 | + ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString())); |
| 99 | + ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString())); |
| 100 | + ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString())); |
| 101 | + } |
| 102 | + if (data != null) { |
| 103 | + ae.AddParameterFromJson(data); |
| 104 | + } |
| 105 | + return ae.TryEvaluate(exp, def).ToString("yyyy-MM-dd HH:mm:ss"); |
| 106 | + } |
| 107 | + [JSInvokable] |
| 108 | + |
| 109 | + public static String GetSimplifiedFormula(String formula, string data = null, string option = null) |
| 110 | + { |
| 111 | + AlgorithmEngine ae; |
| 112 | + if (option == null) { |
| 113 | + ae = new AlgorithmEngine(); |
| 114 | + } else { |
| 115 | + var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option); |
| 116 | + ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString())); |
| 117 | + ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString()); |
| 118 | + ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString()); |
| 119 | + ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString()); |
| 120 | + ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString())); |
| 121 | + ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString())); |
| 122 | + ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString())); |
| 123 | + ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString())); |
| 124 | + } |
| 125 | + if (data != null) { |
| 126 | + ae.AddParameterFromJson(data); |
| 127 | + } |
| 128 | + return ae.GetSimplifiedFormula(formula); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + [JSInvokable] |
| 133 | + public static string EvaluateFormula(string exp,string splitChars, string data = null, string option = null) |
| 134 | + { |
| 135 | + AlgorithmEngine ae; |
| 136 | + if (option == null) { |
| 137 | + ae = new AlgorithmEngine(); |
| 138 | + } else { |
| 139 | + var ops = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(option); |
| 140 | + ae = new AlgorithmEngine(bool.Parse(ops["IgnoreCase"].ToString())); |
| 141 | + ae.UseExcelIndex = bool.Parse(ops["UseExcelIndex"].ToString()); |
| 142 | + ae.UseTempDict = bool.Parse(ops["UseTempDict"].ToString()); |
| 143 | + ae.UseLocalTime = bool.Parse(ops["UseLocalTime"].ToString()); |
| 144 | + ae.DistanceUnit = (DistanceUnitType)(int.Parse(ops["DistanceUnit"].ToString())); |
| 145 | + ae.AreaUnit = (AreaUnitType)(int.Parse(ops["AreaUnit"].ToString())); |
| 146 | + ae.VolumeUnit = (VolumeUnitType)(int.Parse(ops["VolumeUnit"].ToString())); |
| 147 | + ae.MassUnit = (MassUnitType)(int.Parse(ops["MassUnit"].ToString())); |
| 148 | + } |
| 149 | + if (data != null) { |
| 150 | + ae.AddParameterFromJson(data); |
| 151 | + } |
| 152 | + return ae.EvaluateFormula(exp, splitChars.ToCharArray()); |
| 153 | + } |
| 154 | + |
| 155 | + [JSInvokable] |
| 156 | + public static bool IsKeywords(string parameter) |
| 157 | + { |
| 158 | + return AlgorithmEngineHelper.IsKeywords(parameter); |
| 159 | + } |
| 160 | + |
| 161 | + [JSInvokable] |
| 162 | + public static string GetDiyNames(String exp) |
| 163 | + { |
| 164 | + return JsonSerializer.Serialize(AlgorithmEngineHelper.GetDiyNames(exp)); |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + [JSInvokable] |
| 169 | + public static string UnitConversion(decimal src, string oldSrcUnit, string oldTarUnit, string name = null) |
| 170 | + { |
| 171 | + Dictionary<string,object> dic = new Dictionary<string,object>(); |
| 172 | + try { |
| 173 | + var r = AlgorithmEngineHelper.UnitConversion(src, oldSrcUnit, oldTarUnit, name); |
| 174 | + dic["code"] = 0; |
| 175 | + dic["result"] = r; |
| 176 | + |
| 177 | + } catch (Exception ex) { |
| 178 | + dic["code"] = 1; |
| 179 | + dic["error"] = ex.Message; |
| 180 | + } |
| 181 | + return JsonSerializer.Serialize(dic); |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | +} |
0 commit comments