@@ -33,7 +33,10 @@ export class ODataVisitor extends ReducerVisitor {
33
33
let getParams = ( expression : IMethodExpression , ...typeofs : Array < string > ) => {
34
34
let params : Array < any > | undefined ,
35
35
getType = ( t : any ) => {
36
- if ( typeof t == 'object' ) {
36
+ if ( t == null )
37
+ return 'undefined'
38
+
39
+ if ( t != null && typeof t == 'object' ) {
37
40
if ( t . getTime && t . getTime ( ) >= 0 )
38
41
return 'date'
39
42
}
@@ -45,7 +48,7 @@ export class ODataVisitor extends ReducerVisitor {
45
48
if ( parameters . every ( expression => expression . type == ExpressionType . Literal ) == true ) {
46
49
params = parameters . map ( expr => ( < LiteralExpression > expr ) . value )
47
50
48
- if ( new RegExp ( '^' + typeofs . map ( t => t . endsWith ( '?' ) ? '(' + t . slice ( 0 , - 1 ) + ')?' : t ) . join ( ';' ) + ';?$' ) . test ( params . map ( p => getType ( p ) ) . join ( ';' ) + ';' ) == false )
51
+ if ( new RegExp ( '^' + typeofs . map ( t => t . endsWith ( '?' ) ? `( ${ t . slice ( 0 , - 1 ) } )?` : `( ${ t } )` ) . join ( ';' ) + ';?$' ) . test ( params . map ( p => getType ( p ) ) . join ( ';' ) + ';' ) == false )
49
52
throw new TypeError ( params . map ( p => getType ( p ) ) . join ( ', ' ) )
50
53
}
51
54
else if ( ( parameters . length == typeofs . length ) == false ) {
@@ -62,28 +65,44 @@ export class ODataVisitor extends ReducerVisitor {
62
65
switch ( expression . name ) {
63
66
// String Functions
64
67
case 'substringof' : // bool substringof(string po, string p1)
65
- if ( ( params = getParams ( expression , 'string' , 'string' ) ) != null )
68
+ if ( ( params = getParams ( expression , 'string|undefined' , 'string' ) ) != null ) {
69
+ if ( params [ 0 ] == null )
70
+ return new LiteralExpression ( false )
71
+
66
72
return new LiteralExpression ( String ( params [ 0 ] ) . indexOf ( String ( params [ 1 ] ) ) >= 0 )
73
+ }
67
74
68
75
break
69
76
70
77
case 'endswith' : // bool endswith(string p0, string p1)
71
- if ( ( params = getParams ( expression , 'string' , 'string' ) ) != null )
78
+ if ( ( params = getParams ( expression , 'string|undefined' , 'string' ) ) != null ) {
79
+ if ( params [ 0 ] == null )
80
+ return new LiteralExpression ( false )
81
+
72
82
return new LiteralExpression ( String ( params [ 0 ] ) . endsWith ( String ( params [ 1 ] ) ) )
73
-
83
+ }
74
84
break
75
85
76
86
case 'startswith' : // bool startswith(string p0, string p1)
77
- if ( ( params = getParams ( expression , 'string' , 'string' ) ) != null )
87
+ if ( ( params = getParams ( expression , 'string|undefined' , 'string' ) ) != null ) {
88
+ if ( params [ 0 ] == null )
89
+ return new LiteralExpression ( false )
90
+
78
91
return new LiteralExpression ( String ( params [ 0 ] ) . startsWith ( String ( params [ 1 ] ) ) )
92
+ }
79
93
80
94
break
81
95
82
96
case 'contains' : // bool contains(string p0, string p1)
83
- if ( ( params = getParams ( expression , 'string' , 'string' ) ) != null )
97
+ if ( ( params = getParams ( expression , 'string|undefined' , 'string' ) ) != null ) {
98
+ if ( params [ 0 ] == null )
99
+ return new LiteralExpression ( false )
100
+
84
101
return new LiteralExpression ( String ( params [ 0 ] ) . indexOf ( String ( params [ 1 ] ) ) >= 0 )
85
-
102
+ }
103
+
86
104
break
105
+
87
106
case 'length' : // int length(string p0)
88
107
if ( ( params = getParams ( expression , 'string' ) ) != null )
89
108
return new LiteralExpression ( String ( params [ 0 ] ) . length )
@@ -109,14 +128,22 @@ export class ODataVisitor extends ReducerVisitor {
109
128
break
110
129
111
130
case 'tolower' : // string tolower(string p0)
112
- if ( ( params = getParams ( expression , 'string' ) ) != null )
131
+ if ( ( params = getParams ( expression , 'string|undefined' ) ) != null ) {
132
+ if ( params [ 0 ] == null )
133
+ return new LiteralExpression ( params [ 0 ] )
134
+
113
135
return new LiteralExpression ( String ( params [ 0 ] ) . toLowerCase ( ) )
136
+ }
114
137
115
138
break
116
139
117
140
case 'toupper' : // string toupper(string p0)
118
- if ( ( params = getParams ( expression , 'string' ) ) != null )
141
+ if ( ( params = getParams ( expression , 'string|undefined' ) ) != null ) {
142
+ if ( params [ 0 ] == null )
143
+ return new LiteralExpression ( params [ 0 ] )
144
+
119
145
return new LiteralExpression ( String ( params [ 0 ] ) . toUpperCase ( ) )
146
+ }
120
147
121
148
break
122
149
0 commit comments