diff --git a/src/parsers/abstract-template.parser.ts b/src/parsers/abstract-template.parser.ts deleted file mode 100644 index 098ef20..0000000 --- a/src/parsers/abstract-template.parser.ts +++ /dev/null @@ -1,24 +0,0 @@ -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(path: string): boolean { - return (/\.ts|js$/i).test(path); - } - - /** - * Extracts inline template from components - */ - protected extractInlineTemplate(contents: string): string { - const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/; - const match = regExp.exec(contents); - if (match !== null) { - return match[2]; - } - - return ''; - } - -} diff --git a/src/parsers/directive.parser.ts b/src/parsers/directive.parser.ts index 71ea8cb..372b1d0 100644 --- a/src/parsers/directive.parser.ts +++ b/src/parsers/directive.parser.ts @@ -1,16 +1,16 @@ import { ParserInterface } from './parser.interface'; -import { AbstractTemplateParser } from './abstract-template.parser'; import { TranslationCollection } from '../utils/translation.collection'; +import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils'; import * as cheerio from 'cheerio'; const $ = cheerio.load('', {xmlMode: true}); -export class DirectiveParser extends AbstractTemplateParser implements ParserInterface { +export class DirectiveParser implements ParserInterface { public extract(contents: string, path?: string): TranslationCollection { - if (path && this.isAngularComponent(path)) { - contents = this.extractInlineTemplate(contents); + if (path && isPathAngularComponent(path)) { + contents = extractComponentInlineTemplate(contents); } return this.parseTemplate(contents); diff --git a/src/parsers/pipe.parser.ts b/src/parsers/pipe.parser.ts index 0f7b048..98eb983 100644 --- a/src/parsers/pipe.parser.ts +++ b/src/parsers/pipe.parser.ts @@ -1,12 +1,12 @@ import { ParserInterface } from './parser.interface'; -import { AbstractTemplateParser } from './abstract-template.parser'; import { TranslationCollection } from '../utils/translation.collection'; +import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils'; -export class PipeParser extends AbstractTemplateParser implements ParserInterface { +export class PipeParser implements ParserInterface { public extract(contents: string, path?: string): TranslationCollection { - if (path && this.isAngularComponent(path)) { - contents = this.extractInlineTemplate(contents); + if (path && isPathAngularComponent(path)) { + contents = extractComponentInlineTemplate(contents); } return this.parseTemplate(contents); diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 62eece0..f14a8a8 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,3 +1,22 @@ export function _(key: string | string[]): string | string[] { return key; } + +/** + * Assumes file is an Angular component if type is javascript/typescript + */ +export function isPathAngularComponent(path: string): boolean { + return (/\.ts|js$/i).test(path); +} + +/** + * Extract inline template from a component + */ +export function extractComponentInlineTemplate(contents: string): string { + const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/; + const match = regExp.exec(contents); + if (match !== null) { + return match[2]; + } + return ''; +} diff --git a/tests/compilers/namespaced-json.compiler.spec.ts b/tests/compilers/namespaced-json.compiler.spec.ts index 8d74dd1..8ca093a 100644 --- a/tests/compilers/namespaced-json.compiler.spec.ts +++ b/tests/compilers/namespaced-json.compiler.spec.ts @@ -37,9 +37,9 @@ describe('NamespacedJsonCompiler', () => { it('should preserve numeric values on compile', () => { const collection = new TranslationCollection({ - "option.0": '', - "option.1": '', - "option.2": '' + 'option.0': '', + 'option.1': '', + 'option.2': '' }); const result: string = compiler.compile(collection); expect(result).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}'); diff --git a/tests/parsers/abstract-template.parser.spec.ts b/tests/parsers/utils.spec.ts similarity index 56% rename from tests/parsers/abstract-template.parser.spec.ts rename to tests/parsers/utils.spec.ts index 2b98a9e..7ea151c 100644 --- a/tests/parsers/abstract-template.parser.spec.ts +++ b/tests/parsers/utils.spec.ts @@ -1,39 +1,21 @@ import { expect } from 'chai'; -import { AbstractTemplateParser } from '../../src/parsers/abstract-template.parser'; +import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils'; -class TestTemplateParser extends AbstractTemplateParser { - - public isAngularComponent(filePath: string): boolean { - return super.isAngularComponent(filePath); - } - - public extractInlineTemplate(contents: string): string { - return super.extractInlineTemplate(contents); - } - -} - -describe('AbstractTemplateParser', () => { - - let parser: TestTemplateParser; - - beforeEach(() => { - parser = new TestTemplateParser(); - }); +describe('Utils', () => { it('should recognize js extension as angular component', () => { - const result = parser.isAngularComponent('test.js'); + const result = isPathAngularComponent('test.js'); expect(result).to.equal(true); }); it('should recognize ts extension as angular component', () => { - const result = parser.isAngularComponent('test.ts'); + const result = isPathAngularComponent('test.ts'); expect(result).to.equal(true); }); it('should not recognize html extension as angular component', () => { - const result = parser.isAngularComponent('test.html'); + const result = isPathAngularComponent('test.html'); expect(result).to.equal(false); }); @@ -45,7 +27,7 @@ describe('AbstractTemplateParser', () => { }) export class TestComponent { } `; - const template = parser.extractInlineTemplate(contents); + const template = extractComponentInlineTemplate(contents); expect(template).to.equal('

Hello World

'); }); @@ -66,7 +48,7 @@ describe('AbstractTemplateParser', () => { }) export class TestComponent { } `; - const template = parser.extractInlineTemplate(contents); + const template = extractComponentInlineTemplate(contents); expect(template).to.equal('\n\t\t\t\t\t

\n\t\t\t\t\t\tHello World\n\t\t\t\t\t

\n\t\t\t\t'); }); diff --git a/tests/post-processors/purge-obsolete-keys.post-processor.spec.ts b/tests/post-processors/purge-obsolete-keys.post-processor.spec.ts index e4b849f..cb17d5c 100644 --- a/tests/post-processors/purge-obsolete-keys.post-processor.spec.ts +++ b/tests/post-processors/purge-obsolete-keys.post-processor.spec.ts @@ -4,7 +4,7 @@ import { PostProcessorInterface } from '../../src/post-processors/post-processor import { PurgeObsoleteKeysPostProcessor } from '../../src/post-processors/purge-obsolete-keys.post-processor'; import { TranslationCollection } from '../../src/utils/translation.collection'; -describe('KeyAsDefaultValuePostProcessor', () => { +describe('PurgeObsoleteKeysPostProcessor', () => { let processor: PostProcessorInterface;