| 
									
										
										
										
											2018-01-25 10:13:30 +00:00
										 |  |  | import * as XRegExp from 'xregexp'; | 
					
						
							|  |  |  | import { FirebaseGlob } from './FirebaseGlob'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class FirebaseRedirect { | 
					
						
							|  |  |  |   glob = new FirebaseGlob(this.source); | 
					
						
							|  |  |  |   constructor(public source: string, public destination: string) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-03 12:46:38 +03:00
										 |  |  |   replace(url: string): string | undefined { | 
					
						
							| 
									
										
										
										
											2018-01-25 10:13:30 +00:00
										 |  |  |     const match = this.glob.match(url); | 
					
						
							| 
									
										
										
										
											2019-10-03 12:46:38 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!match) { | 
					
						
							|  |  |  |       return undefined; | 
					
						
							| 
									
										
										
										
											2018-01-25 10:13:30 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-03 12:46:38 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const paramReplacers = Object.keys(this.glob.namedParams).map(name => [ XRegExp(`:${name}`, 'g'), match[name] ]); | 
					
						
							|  |  |  |     const restReplacers = Object.keys(this.glob.restParams).map(name => [ XRegExp(`:${name}\\*`, 'g'), match[name] ]); | 
					
						
							|  |  |  |     return XRegExp.replaceEach(this.destination, [...paramReplacers, ...restReplacers]); | 
					
						
							| 
									
										
										
										
											2018-01-25 10:13:30 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } |