https://github.com/angular/angular
Raw File
Tip revision: cc57d4c4998b4e38f940afdf358af37185028072 authored by Pawel Kozlowski on 17 April 2024, 15:37:56 UTC
release: cut the v17.3.5 release
Tip revision: cc57d4c
FirebaseRedirect.ts
import XRegExp from 'xregexp';

import {FirebaseRedirectConfig} from './FirebaseRedirector';
import {FirebaseRedirectSource} from './FirebaseRedirectSource';

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

  constructor(readonly rawConfig: FirebaseRedirectConfig) {
    this.source = (rawConfig.regex === undefined) ?
        FirebaseRedirectSource.fromGlobPattern(rawConfig.source) :
        FirebaseRedirectSource.fromRegexPattern(rawConfig.regex);
    this.destination = rawConfig.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