Files
dario-piotrowicz 66c6dbdc79 refactor(docs-infra): introduce max-len 120 eslint rule (#43439)
add the max-len rule to the aio eslintrc and fix what code
breaks such rule

PR Close #43439
2021-09-15 10:47:35 -07:00

34 lines
1.0 KiB
TypeScript

import * as 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]);
}
}