https://github.com/angular/angular
Raw File
Tip revision: 037b636eb29aee5756b4d2a7d81d964da0544b1c authored by Zach Arend on 26 May 2021, 16:34:11 UTC
release: cut the v12.0.2 release (#42353)
Tip revision: 037b636
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