1+ import { ExtractBindingSyntax } from "../types" ;
12import type {
23 ParseResultErrors ,
34 StructureValue ,
@@ -92,7 +93,7 @@ export const isMetadataPath = (
9293} ;
9394
9495/**
95- * An input is considered property binding syntax when
96+ * An input is considered as an allowed binding when
9697 *
9798 * a. is empty curly bracket e.g `{}` or `{ }`
9899 *
@@ -104,7 +105,7 @@ export const isMetadataPath = (
104105 *
105106 * e. is not OData path e.g {/path/to/...} or {path/to/...}
106107 */
107- export const isPropertyBindingInfo = (
108+ export const isBindingAllowed = (
108109 input : string ,
109110 binding ?: StructureValue ,
110111 errors ?: ParseResultErrors
@@ -146,3 +147,67 @@ export const isPropertyBindingInfo = (
146147 }
147148 return false ;
148149} ;
150+
151+ /**
152+ * Regular expression to extract binding syntax.
153+ *
154+ * Also handles escaping of '{' and '}'.
155+ */
156+ // eslint-disable-next-line no-useless-escape
157+ const start = / ( \\ [ \\ \{ \} ] ) | ( \{ ) / g;
158+ // eslint-disable-next-line no-useless-escape
159+ const end = / ( \\ [ \\ \{ \} ] ) | ( \} ) / g;
160+
161+ export const extractBindingSyntax = ( input : string ) : ExtractBindingSyntax [ ] => {
162+ const result : ExtractBindingSyntax [ ] = [ ] ;
163+ let startRegResult : RegExpExecArray | null ;
164+ let endRegResult : RegExpExecArray | null ;
165+ // resetting
166+ start . lastIndex = 0 ;
167+ let startIndex = 0 ;
168+ let lastIndex = 0 ;
169+ let endIndex = 0 ;
170+ const text = input ;
171+ if ( text . trim ( ) === "" ) {
172+ return [ { startIndex, endIndex, expression : input } ] ;
173+ }
174+ while ( ( startRegResult = start . exec ( input ) ) !== null ) {
175+ // scape special chars
176+ if ( startRegResult [ 1 ] ) {
177+ continue ;
178+ }
179+ const startInput = input . slice ( startRegResult . index ) ;
180+ // collect all closing bracket(s)
181+ end . lastIndex = 0 ;
182+ while ( ( endRegResult = end . exec ( startInput ) ) !== null ) {
183+ // scape special chars
184+ if ( endRegResult [ 1 ] ) {
185+ break ;
186+ }
187+ lastIndex = endRegResult . index ;
188+ }
189+ if ( lastIndex === startRegResult . index ) {
190+ // missing closing bracket
191+ const expression = startInput . slice ( 0 , input . length ) ;
192+ result . push ( {
193+ startIndex : startRegResult . index ,
194+ endIndex : input . length ,
195+ expression,
196+ } ) ;
197+ input = startInput . slice ( input . length ) ;
198+ } else {
199+ const expression = startInput . slice ( 0 , lastIndex + 1 ) ;
200+ startIndex = endIndex + startRegResult . index ;
201+ endIndex = startIndex + lastIndex + 1 ;
202+ result . push ( {
203+ startIndex,
204+ endIndex,
205+ expression,
206+ } ) ;
207+ input = startInput . slice ( lastIndex + 1 ) ;
208+ // resetting
209+ start . lastIndex = 0 ;
210+ }
211+ }
212+ return result ;
213+ } ;
0 commit comments