/
FirebaseRedirect.ts
27 lines (22 loc) 路 1.01 KB
/
FirebaseRedirect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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]);
}
}