From 59ef277c64a8fe6a3c8ad1ce957838412235eca7 Mon Sep 17 00:00:00 2001 From: Kim Biesbjerg Date: Fri, 9 Dec 2016 14:40:21 +0100 Subject: [PATCH] Refactor and clean code --- src/example.ts | 4 +-- src/extractor.ts | 34 ++++++++++--------------- src/parsers/abstract-template.parser.ts | 4 +-- src/parsers/directive.parser.ts | 6 ++--- src/parsers/pipe.parser.ts | 2 +- src/parsers/service.parser.ts | 29 +++++++++++---------- 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/example.ts b/src/example.ts index 7b01d31..f5c1152 100644 --- a/src/example.ts +++ b/src/example.ts @@ -10,8 +10,8 @@ const dest = '/your/project/template.json'; try { const collection: StringCollection = extractor.process(src); - const output: string = extractor.save(dest); - console.log({ strings: collection.keys(), output: output }); + const serialized: string = extractor.save(dest); + console.log({ strings: collection.keys(), serialized }); } catch (e) { console.log(`Something went wrong: ${e.toString()}`); } diff --git a/src/extractor.ts b/src/extractor.ts index 7174d64..fbfd5b6 100644 --- a/src/extractor.ts +++ b/src/extractor.ts @@ -10,18 +10,18 @@ import * as fs from 'fs'; export class Extractor { + public patterns: string[] = [ + '/**/*.html', + '/**/*.ts', + '/**/*.js' + ]; + public parsers: ParserInterface[] = [ new PipeParser(), new DirectiveParser(), new ServiceParser() ]; - public find: string[] = [ - '/**/*.html', - '/**/*.ts', - '/**/*.js' - ]; - public collection: StringCollection = new StringCollection(); public constructor(public serializer: SerializerInterface) { } @@ -30,7 +30,7 @@ export class Extractor { * Process dir */ public process(dir: string): StringCollection { - this._getFiles(dir).forEach(path => { + this._readDir(dir, this.patterns).forEach(path => { const contents: string = fs.readFileSync(path, 'utf-8'); this.parsers.forEach((parser: ParserInterface) => { this.collection.merge(parser.extract(contents, path)); @@ -57,20 +57,14 @@ export class Extractor { } /** - * Get all files in dir that matches glob patterns + * Get all files in dir matching find patterns */ - protected _getFiles(dir: string): string[] { - let results: string[] = []; - - this.find.forEach(pattern => { - const files = glob - .sync(dir + pattern) - .filter(path => fs.statSync(path).isFile()); - - results = [...results, ...files]; - }); - - return results; + protected _readDir(dir: string, patterns: string[]): string[] { + return patterns.reduce((results, pattern) => { + return glob.sync(dir + pattern) + .filter(path => fs.statSync(path).isFile()) + .concat(results); + }, []); } } diff --git a/src/parsers/abstract-template.parser.ts b/src/parsers/abstract-template.parser.ts index 3260836..817c3c1 100644 --- a/src/parsers/abstract-template.parser.ts +++ b/src/parsers/abstract-template.parser.ts @@ -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]; } diff --git a/src/parsers/directive.parser.ts b/src/parsers/directive.parser.ts index 831d395..6df9f00 100644 --- a/src/parsers/directive.parser.ts +++ b/src/parsers/directive.parser.ts @@ -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)); } diff --git a/src/parsers/pipe.parser.ts b/src/parsers/pipe.parser.ts index 04355ee..77a37b7 100644 --- a/src/parsers/pipe.parser.ts +++ b/src/parsers/pipe.parser.ts @@ -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)) { diff --git a/src/parsers/service.parser.ts b/src/parsers/service.parser.ts index ea0d95a..b2c4a15 100644 --- a/src/parsers/service.parser.ts +++ b/src/parsers/service.parser.ts @@ -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]; - } - }