Refactor and clean code

This commit is contained in:
Kim Biesbjerg
2016-12-09 14:40:21 +01:00
parent 73801a9cc5
commit 59ef277c64
6 changed files with 37 additions and 42 deletions

View File

@@ -5,14 +5,14 @@ export abstract class AbstractTemplateParser {
* makes the assumption that it is an Angular Component
*/
protected _isAngularComponent(path: string): boolean {
return new RegExp(/\.(ts|js)$/, 'i').test(path);
return new RegExp(/\.ts|js$/, 'i').test(path);
}
/**
* Extracts inline template from components
*/
protected _extractInlineTemplate(contents: string): string {
const match = new RegExp(/template\s?:\s?("|\'|`)((.|[\r\n])+?[^\\])\1/).exec(contents);
const match = new RegExp(/template\s*:\s*(["'`])((.|[\r\n])+?[^\\])\1/).exec(contents);
if (match !== null) {
return match[2];
}

View File

@@ -19,7 +19,7 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
template = this._normalizeTemplateAttributes(template);
$(template)
.find('[translate],[ng2-translate]')
.find('[translate], [ng2-translate]')
.addBack()
.each((i: number, element: CheerioElement) => {
const $element = $(element);
@@ -31,8 +31,8 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
$element
.contents()
.toArray()
.filter(textNode => textNode.type === 'text')
.map(textNode => textNode.nodeValue.trim())
.filter(node => node.type === 'text')
.map(node => node.nodeValue.trim())
.filter(text => text.length > 0)
.forEach(text => collection.add(text));
}

View File

@@ -15,7 +15,7 @@ export class PipeParser extends AbstractTemplateParser implements ParserInterfac
protected _parseTemplate(template: string): StringCollection {
const collection = new StringCollection();
const regExp = new RegExp(/([\'"`])([^\1\r\n]*)\1\s+\|\s*translate(:.*?)?/, 'g');
const regExp = new RegExp(/(['"`])([^\1\r\n]*)\1\s*\|\s*translate(:.*?)?/, 'g');
let matches;
while (matches = regExp.exec(template)) {

View File

@@ -11,13 +11,14 @@ export class ServiceParser implements ParserInterface {
return collection;
}
const methodRegExp: RegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?([\'"`])([^\1\r\n]+)\2\]?)/);
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)) {
if (this._stringContainsArray(matches[1])) {
collection.add(this._stringToArray(matches[1]));
const matchCollection = StringCollection.fromArray(this._stringToArray(matches[1]));
collection.merge(matchCollection);
} else {
collection.add(matches[3]);
}
@@ -26,6 +27,18 @@ export class ServiceParser implements ParserInterface {
return collection;
}
/**
* 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);
if (matches === null) {
return '';
}
return matches[1];
}
/**
* Checks if string contains an array
*/
@@ -44,16 +57,4 @@ export class ServiceParser implements ParserInterface {
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);
if (matches === null) {
return '';
}
return matches[1];
}
}