@@ -18,33 +18,26 @@ export function isConfig(arg: Rule | Config): arg is Config {
1818 return ( arg as Config ) . config !== undefined ;
1919}
2020
21- export function replaceReferenceWithMasterId (
22- targetRef : string ,
23- masterId : string
24- ) : string {
25- return targetRef . replace ( '{masterId}' , masterId ) ;
26- }
27-
2821export function replaceReferenceWithFields (
2922 fields : FirebaseFirestore . DocumentData ,
3023 targetCollection : string
3124) : { hasFields : boolean ; targetCollection : string } {
32- const pRegex = / \{ ( [ ^ ) ] + ) \} / g;
33- const matches = pRegex . exec ( targetCollection ) ;
25+ const pRegex = / ( [ \$ ] [ ^ \/ ] * | $ ) / g;
26+ const matches = targetCollection . match ( pRegex ) ; // Using global flag always returns an empty string at the end
27+ matches . pop ( ) ;
28+
3429 let hasFields = false ;
35- if ( matches && fields ) {
30+ if ( matches . length > 0 && fields ) {
3631 hasFields = true ;
37- matches . forEach ( ( ) => {
38- const field = fields [ matches [ 1 ] ] ;
32+ matches . forEach ( match => {
33+ const field = fields [ match . replace ( '$' , '' ) ] ;
3934 if ( field ) {
4035 console . log (
41- `integrify: Detected dynamic reference, replacing [${ matches [ 0 ] } ] with [${ field } ]`
36+ `integrify: Detected dynamic reference, replacing [${ match } ] with [${ field } ]`
4237 ) ;
43- targetCollection = targetCollection . replace ( matches [ 0 ] , field ) ;
38+ targetCollection = targetCollection . replace ( match , field ) ;
4439 } else {
45- throw new Error (
46- `integrify: Missing dynamic reference: [${ matches [ 0 ] } ]`
47- ) ;
40+ throw new Error ( `integrify: Missing dynamic reference: [${ match } ]` ) ;
4841 }
4942 } ) ;
5043 }
0 commit comments