11namespace ToolGood . Algorithm . WebAssembly
22{
33 using Microsoft . JSInterop ;
4+ using System . Text ;
45 using System . Text . Json ;
56 using ToolGood . Algorithm ;
67 using ToolGood . Algorithm . Enums ;
@@ -11,29 +12,6 @@ private static void Main(string[] args)
1112 {
1213 System . Text . Encoding . RegisterProvider ( System . Text . CodePagesEncodingProvider . Instance ) ;
1314 }
14- [ JSInvokable ]
15- public static string GetErrorMessage ( string exp , string def = null , string data = null , string option = null )
16- {
17- AlgorithmEngine ae ;
18- if ( option == null ) {
19- ae = new AlgorithmEngine ( ) ;
20- } else {
21- var ops = JsonSerializer . Deserialize < Dictionary < string , object > > ( option ) ;
22- ae = new AlgorithmEngine ( bool . Parse ( ops [ "IgnoreCase" ] . ToString ( ) ) ) ;
23- ae . UseExcelIndex = bool . Parse ( ops [ "UseExcelIndex" ] . ToString ( ) ) ;
24- ae . UseTempDict = bool . Parse ( ops [ "UseTempDict" ] . ToString ( ) ) ;
25- ae . UseLocalTime = bool . Parse ( ops [ "UseLocalTime" ] . ToString ( ) ) ;
26- ae . DistanceUnit = ( DistanceUnitType ) ( int . Parse ( ops [ "DistanceUnit" ] . ToString ( ) ) ) ;
27- ae . AreaUnit = ( AreaUnitType ) ( int . Parse ( ops [ "AreaUnit" ] . ToString ( ) ) ) ;
28- ae . VolumeUnit = ( VolumeUnitType ) ( int . Parse ( ops [ "VolumeUnit" ] . ToString ( ) ) ) ;
29- ae . MassUnit = ( MassUnitType ) ( int . Parse ( ops [ "MassUnit" ] . ToString ( ) ) ) ;
30- }
31- if ( data != null ) {
32- ae . AddParameterFromJson ( data ) ;
33- }
34- ae . TryEvaluate ( exp , def ) ;
35- return ae . LastError ;
36- }
3715
3816 [ JSInvokable ]
3917 public static string TryEvaluateString ( string exp , string def = null , string data = null , string option = null )
@@ -55,27 +33,45 @@ public static string TryEvaluateString(string exp, string def = null, string dat
5533 if ( data != null ) {
5634 ae . AddParameterFromJson ( data ) ;
5735 }
36+ Dictionary < string , object > result = new Dictionary < string , object > ( ) ;
37+ result [ "parse" ] = false ;
38+ result [ "useDef" ] = false ;
39+ result [ "error" ] = null ;
5840 try {
5941 if ( ae . Parse ( exp ) ) {
42+ result [ "parse" ] = true ;
6043 var obj = ae . Evaluate ( ) ;
6144 if ( obj . IsNull ) {
62- return null ;
45+ result [ "result" ] = null ;
46+ return JsonSerializer . Serialize ( result ) ;
6347 }
6448 if ( obj . IsError ) {
65- return def ;
49+ result [ "result" ] = def ;
50+ result [ "useDef" ] = true ;
51+ result [ "error" ] = obj . ErrorMsg ;
52+ return JsonSerializer . Serialize ( result ) ;
6653 }
6754 if ( obj . Type == OperandType . DATE ) {
68- return obj . DateValue . ToString ( ) ;
55+ result [ "result" ] = obj . DateValue . ToString ( ) ;
56+ result [ "error" ] = ae . LastError ;
57+ return JsonSerializer . Serialize ( result ) ;
6958 }
7059 obj = obj . ToText ( "It can't be converted to string!" ) ;
71- return obj . TextValue ;
60+ result [ "result" ] = obj . TextValue ;
61+ return JsonSerializer . Serialize ( result ) ;
7262 }
73- } catch { }
74- return def ;
63+ result [ "error" ] = "Parameter exp invalid !" ;
64+ } catch ( Exception ex ) {
65+ result [ "error" ] = ex . Message ;
66+ }
67+ result [ "useDef" ] = true ;
68+ result [ "result" ] = def ;
69+ return JsonSerializer . Serialize ( result ) ;
7570 }
7671
72+
7773 [ JSInvokable ]
78- public static decimal TryEvaluateNumber ( string exp , decimal def , string data = null , string option = null )
74+ public static string TryEvaluateNumber ( string exp , decimal def , string data = null , string option = null )
7975 {
8076 AlgorithmEngine ae ;
8177 if ( option == null ) {
@@ -94,11 +90,35 @@ public static decimal TryEvaluateNumber(string exp, decimal def, string data = n
9490 if ( data != null ) {
9591 ae . AddParameterFromJson ( data ) ;
9692 }
97- return ae . TryEvaluate ( exp , def ) ;
93+ Dictionary < string , object > result = new Dictionary < string , object > ( ) ;
94+ result [ "parse" ] = false ;
95+ result [ "useDef" ] = false ;
96+ result [ "error" ] = null ;
97+ try {
98+ if ( ae . Parse ( exp ) ) {
99+ result [ "parse" ] = true ;
100+ var obj = ae . Evaluate ( ) ;
101+ obj = obj . ToNumber ( "It can't be converted to number!" ) ;
102+ if ( obj . IsError ) {
103+ result [ "result" ] = def ;
104+ result [ "useDef" ] = true ;
105+ result [ "error" ] = obj . ErrorMsg ;
106+ return JsonSerializer . Serialize ( result ) ;
107+ }
108+ result [ "result" ] = obj . NumberValue ;
109+ return JsonSerializer . Serialize ( result ) ;
110+ }
111+ result [ "error" ] = "Parameter exp invalid !" ;
112+ } catch ( Exception ex ) {
113+ result [ "error" ] = ex . Message ;
114+ }
115+ result [ "useDef" ] = true ;
116+ result [ "result" ] = def ;
117+ return JsonSerializer . Serialize ( result ) ;
98118 }
99119
100120 [ JSInvokable ]
101- public static bool TryEvaluateBool ( string exp , bool def , string data = null , string option = null )
121+ public static string TryEvaluateBool ( string exp , bool def , string data = null , string option = null )
102122 {
103123 AlgorithmEngine ae ;
104124 if ( option == null ) {
@@ -117,7 +137,31 @@ public static bool TryEvaluateBool(string exp, bool def, string data = null, str
117137 if ( data != null ) {
118138 ae . AddParameterFromJson ( data ) ;
119139 }
120- return ae . TryEvaluate ( exp , def ) ;
140+ Dictionary < string , object > result = new Dictionary < string , object > ( ) ;
141+ result [ "parse" ] = false ;
142+ result [ "useDef" ] = false ;
143+ result [ "error" ] = null ;
144+ try {
145+ if ( ae . Parse ( exp ) ) {
146+ result [ "parse" ] = true ;
147+ var obj = ae . Evaluate ( ) ;
148+ obj = obj . ToBoolean ( "It can't be converted to bool!" ) ;
149+ if ( obj . IsError ) {
150+ result [ "result" ] = def ;
151+ result [ "useDef" ] = true ;
152+ result [ "error" ] = obj . ErrorMsg ;
153+ return JsonSerializer . Serialize ( result ) ;
154+ }
155+ result [ "result" ] = obj . BooleanValue ;
156+ return JsonSerializer . Serialize ( result ) ;
157+ }
158+ result [ "error" ] = "Parameter exp invalid !" ;
159+ } catch ( Exception ex ) {
160+ result [ "error" ] = ex . Message ;
161+ }
162+ result [ "useDef" ] = true ;
163+ result [ "result" ] = def ;
164+ return JsonSerializer . Serialize ( result ) ;
121165 }
122166
123167 [ JSInvokable ]
@@ -140,22 +184,31 @@ public static string TryEvaluateDateTime(string exp, DateTime def, string data =
140184 if ( data != null ) {
141185 ae . AddParameterFromJson ( data ) ;
142186 }
187+ Dictionary < string , object > result = new Dictionary < string , object > ( ) ;
188+ result [ "parse" ] = false ;
189+ result [ "useDef" ] = false ;
190+ result [ "error" ] = null ;
143191 try {
144192 if ( ae . Parse ( exp ) ) {
193+ result [ "parse" ] = true ;
145194 var obj = ae . Evaluate ( ) ;
146- if ( obj . IsNull ) {
147- return null ;
148- }
195+ obj = obj . ToMyDate ( "It can't be converted to datetime!" ) ;
149196 if ( obj . IsError ) {
197+ result [ "result" ] = def ;
198+ result [ "useDef" ] = true ;
199+ result [ "error" ] = ae . LastError ;
150200 return def . ToString ( "yyyy-MM-dd HH:mm:ss" ) ;
151201 }
152- if ( obj . Type == OperandType . DATE ) {
153- return obj . DateValue . ToString ( ) ;
154- }
155- return obj . DateValue . ToString ( ) ;
202+ result [ "result" ] = obj . DateValue . ToString ( ) ;
203+ return JsonSerializer . Serialize ( result ) ;
156204 }
157- } catch { }
158- return def . ToString ( "yyyy-MM-dd HH:mm:ss" ) ;
205+ result [ "error" ] = "Parameter exp invalid !" ;
206+ } catch ( Exception ex ) {
207+ result [ "error" ] = ex . Message ;
208+ }
209+ result [ "useDef" ] = true ;
210+ result [ "result" ] = def . ToString ( "yyyy-MM-dd HH:mm:ss" ) ;
211+ return JsonSerializer . Serialize ( result ) ;
159212 }
160213 [ JSInvokable ]
161214
0 commit comments