https://github.com/angular/angular
Raw File
Tip revision: a491295d9e099c05ec26bee8b694cda688e44b33 authored by Andrew Scott on 04 August 2021, 16:59:03 UTC
release: cut the v12.1.5 release (#43044)
Tip revision: a491295
FirebaseRedirect.ts
import * as XRegExp from 'xregexp';
import { FirebaseRedirectConfig } from './FirebaseRedirector';
import { FirebaseRedirectSource } from './FirebaseRedirectSource';

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

  constructor(readonly rawConfig: FirebaseRedirectConfig) {
    const {source, regex, destination} = rawConfig;
    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