Add StringCollection to make it easier to work with strings

This commit is contained in:
Kim Biesbjerg
2016-12-09 05:18:04 +01:00
parent befd841457
commit 73801a9cc5
17 changed files with 204 additions and 111 deletions

View File

@@ -4,8 +4,8 @@ export abstract class AbstractTemplateParser {
* Checks if file is of type javascript or typescript and
* makes the assumption that it is an Angular Component
*/
protected _isAngularComponent(filePath: string): boolean {
return new RegExp(/\.(ts|js)$/, 'i').test(filePath);
protected _isAngularComponent(path: string): boolean {
return new RegExp(/\.(ts|js)$/, 'i').test(path);
}
/**

View File

@@ -1,20 +1,21 @@
import { ParserInterface } from './parser.interface';
import { AbstractTemplateParser } from './abstract-template.parser';
import { StringCollection } from '../utils/string.collection';
import * as $ from 'cheerio';
export class DirectiveParser extends AbstractTemplateParser implements ParserInterface {
public process(filePath: string, contents: string): string[] {
if (this._isAngularComponent(filePath)) {
public extract(contents: string, path?: string): StringCollection {
if (path && this._isAngularComponent(path)) {
contents = this._extractInlineTemplate(contents);
}
return this._parseTemplate(contents);
}
protected _parseTemplate(template: string): string[] {
let results: string[] = [];
protected _parseTemplate(template: string): StringCollection {
const collection = new StringCollection();
template = this._normalizeTemplateAttributes(template);
$(template)
@@ -25,7 +26,7 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
const attr = $element.attr('translate') || $element.attr('ng2-translate');
if (attr) {
results.push(attr);
collection.add(attr);
} else {
$element
.contents()
@@ -33,11 +34,11 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
.filter(textNode => textNode.type === 'text')
.map(textNode => textNode.nodeValue.trim())
.filter(text => text.length > 0)
.forEach(text => results.push(text));
.forEach(text => collection.add(text));
}
});
return results;
return collection;
}
}

View File

@@ -1,5 +1,7 @@
import { StringCollection } from '../utils/string.collection';
export interface ParserInterface {
process(filePath: string, contents: string): string[];
extract(contents: string, path?: string): StringCollection;
}

View File

@@ -1,27 +1,28 @@
import { ParserInterface } from './parser.interface';
import { AbstractTemplateParser } from './abstract-template.parser';
import { StringCollection } from '../utils/string.collection';
export class PipeParser extends AbstractTemplateParser implements ParserInterface {
public process(filePath: string, contents: string): string[] {
if (this._isAngularComponent(filePath)) {
public extract(contents: string, path?: string): StringCollection {
if (path && this._isAngularComponent(path)) {
contents = this._extractInlineTemplate(contents);
}
return this._parseTemplate(contents);
}
protected _parseTemplate(template: string): string[] {
let results: string[] = [];
protected _parseTemplate(template: string): StringCollection {
const collection = new StringCollection();
const regExp = new RegExp(/([\'"`])([^\1\r\n]*)\1\s+\|\s*translate(:.*?)?/, 'g');
let matches;
while (matches = regExp.exec(template)) {
results.push(matches[2]);
collection.add(matches[2]);
}
return results;
return collection;
}
}

View File

@@ -1,13 +1,14 @@
import { ParserInterface } from './parser.interface';
import { StringCollection } from '../utils/string.collection';
export class ServiceParser implements ParserInterface {
public process(filePath: string, contents: string): string[] {
let results: string[] = [];
public extract(contents: string, path?: string): StringCollection {
const collection = new StringCollection();
const translateServiceVar = this._extractTranslateServiceVar(contents);
if (!translateServiceVar) {
return results;
return collection;
}
const methodRegExp: RegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?([\'"`])([^\1\r\n]+)\2\]?)/);
@@ -16,13 +17,13 @@ export class ServiceParser implements ParserInterface {
let matches;
while (matches = regExp.exec(contents)) {
if (this._stringContainsArray(matches[1])) {
results.push(...this._stringToArray(matches[1]));
collection.add(this._stringToArray(matches[1]));
} else {
results.push(matches[3]);
collection.add(matches[3]);
}
}
return results;
return collection;
}
/**