11namespace 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 ;
0 commit comments