https://github.com/angular/angular
Raw File
Tip revision: 7bd5ea4a7ee24b96b3ecfd201111ff9deaf1ba4c authored by Andrew Kushnir on 02 June 2021, 18:02:43 UTC
release: cut the v12.1.0-next.4 release
Tip revision: 7bd5ea4
FirebaseRedirect.ts
import * as XRegExp from 'xregexp';
import { FirebaseRedirectConfig } from './FirebaseRedirector';
import { FirebaseRedirectSource } from './FirebaseRedirectSource';

export class FirebaseRedirect {
  source: FirebaseRedirectSource;
  destination: string;

  constructor({source, regex, destination}: FirebaseRedirectConfig) {
    this.source = (typeof source === 'string') ?
      FirebaseRedirectSource.fromGlobPattern(source) :
      FirebaseRedirectSource.fromRegexPattern(regex!);
    this.destination = destination;
  }

  replace(url: string): string | undefined {
    const match = this.source.match(url);

    if (!match) {
      return undefined;
    }

    const namedReplacers = this.source.namedGroups.map<[RegExp, string]>(name => [ XRegExp(`:${name}`, 'g'), match[name] ]);
    const restReplacers = this.source.restNamedGroups.map<[RegExp, string]>(name => [ XRegExp(`:${name}\\*`, 'g'), match[name] ]);
    return XRegExp.replaceEach(this.destination, [...namedReplacers, ...restReplacers]);
  }
}
back to top