Add support for extracting array of strings in ServiceParser + tests
This commit is contained in:
@@ -10,19 +10,41 @@ export class ServiceParser implements ParserInterface {
|
||||
return results;
|
||||
}
|
||||
|
||||
const methodPattern: string = '(?:get|instant)\\s*\\\(\\s*([\'"`])([^\\1\\r\\n]+)\\1';
|
||||
const regExp: RegExp = new RegExp(`${translateServiceVar}\.${methodPattern}`, 'g');
|
||||
const methodRegExp: RegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?([\'"`])([^\1\r\n]+)\2\]?)/);
|
||||
const regExp: RegExp = new RegExp(`${translateServiceVar}\.${methodRegExp.source}`, 'g');
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
results.push(matches[2]);
|
||||
if (this._stringContainsArray(matches[1])) {
|
||||
results.push(...this._stringToArray(matches[1]));
|
||||
} else {
|
||||
results.push(matches[3]);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract name of TranslateService variable for use in patterns
|
||||
* Checks if string contains an array
|
||||
*/
|
||||
protected _stringContainsArray(input: string): boolean {
|
||||
return input.startsWith('[') && input.endsWith(']');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts string to array
|
||||
*/
|
||||
protected _stringToArray(input: string): string[] {
|
||||
if (this._stringContainsArray(input)) {
|
||||
return eval(input);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts name of TranslateService variable for use in patterns
|
||||
*/
|
||||
protected _extractTranslateServiceVar(contents: string): string {
|
||||
const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);
|
||||
|
Reference in New Issue
Block a user