All files / src/http/parser url-parameters.ts

30% Statements 6/20
0% Branches 0/4
20% Functions 1/5
30% Lines 6/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 518x               51x 51x   51x 51x                                                                         8x  
import PathParser from 'route/utility/path-parser';
import UrlParameters from 'http/type/url-parameters';
 
class UrlParametersParser {
	private parameter_keys: string[];
	private regex: RegExp;
 
	public constructor(path: string) {
		const parser = new PathParser(path);
		const parsed_path = parser.parse();
 
		this.regex = parsed_path.regex;
		this.parameter_keys = parsed_path.parameter_keys;
	}
 
	public parse(url: string | undefined): UrlParameters {
		const result: UrlParameters = {};
 
		if (url === undefined) {
			return result;
		}
 
		const regex = this.getRegex();
		const match = url.match(regex);
 
		if (match === null) {
			return result;
		}
 
		const parameter_keys = this.getParameterKeys();
 
		parameter_keys.forEach((parameter_key, index) => {
			const parameter_value = match[index + 1];
 
			result[parameter_key] = parameter_value;
		});
 
		return result;
	}
 
	private getRegex(): RegExp {
		return this.regex;
	}
 
	private getParameterKeys(): string[] {
		return this.parameter_keys;
	}
}
 
export default UrlParametersParser;