(chore) run prettier

This commit is contained in:
Kim Biesbjerg 2019-09-18 14:16:47 +02:00
parent 7d0d52429f
commit 306622b9a0
28 changed files with 61 additions and 113 deletions

View File

@ -35,7 +35,6 @@ export const cli = yargs
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) { if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
throw new Error(`The path you supplied was not found: '${dir}'`); throw new Error(`The path you supplied was not found: '${dir}'`);
} }
}); });
return true; return true;
}) })
@ -100,12 +99,7 @@ const extractTask = new ExtractTask(cli.input, cli.output, {
}); });
// Parsers // Parsers
const parsers: ParserInterface[] = [ const parsers: ParserInterface[] = [new PipeParser(), new DirectiveParser(), new ServiceParser(), new MarkerParser()];
new PipeParser(),
new DirectiveParser(),
new ServiceParser(),
new MarkerParser()
];
extractTask.setParsers(parsers); extractTask.setParsers(parsers);
// Post processors // Post processors

View File

@ -118,11 +118,7 @@ export class ExtractTask implements TaskInterface {
/** /**
* Run strings through configured post processors * Run strings through configured post processors
*/ */
protected process( protected process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
draft: TranslationCollection,
extracted: TranslationCollection,
existing: TranslationCollection
): TranslationCollection {
this.postProcessors.forEach(postProcessor => { this.postProcessors.forEach(postProcessor => {
draft = postProcessor.process(draft, extracted, existing); draft = postProcessor.process(draft, extracted, existing);
}); });

View File

@ -4,14 +4,16 @@ import { NamespacedJsonCompiler } from '../compilers/namespaced-json.compiler';
import { PoCompiler } from '../compilers/po.compiler'; import { PoCompiler } from '../compilers/po.compiler';
export class CompilerFactory { export class CompilerFactory {
public static create(format: string, options?: {}): CompilerInterface { public static create(format: string, options?: {}): CompilerInterface {
switch (format) { switch (format) {
case 'pot': return new PoCompiler(options); case 'pot':
case 'json': return new JsonCompiler(options); return new PoCompiler(options);
case 'namespaced-json': return new NamespacedJsonCompiler(options); case 'json':
default: throw new Error(`Unknown format: ${format}`); return new JsonCompiler(options);
case 'namespaced-json':
return new NamespacedJsonCompiler(options);
default:
throw new Error(`Unknown format: ${format}`);
} }
} }
} }

View File

@ -1,11 +1,9 @@
import { TranslationCollection } from '../utils/translation.collection'; import { TranslationCollection } from '../utils/translation.collection';
export interface CompilerInterface { export interface CompilerInterface {
extension: string; extension: string;
compile(collection: TranslationCollection): string; compile(collection: TranslationCollection): string;
parse(contents: string): TranslationCollection; parse(contents: string): TranslationCollection;
} }

View File

@ -5,7 +5,6 @@ import { stripBOM } from '../utils/utils';
import { flatten } from 'flat'; import { flatten } from 'flat';
export class JsonCompiler implements CompilerInterface { export class JsonCompiler implements CompilerInterface {
public indentation: string = '\t'; public indentation: string = '\t';
public extension: string = 'json'; public extension: string = 'json';
@ -31,5 +30,4 @@ export class JsonCompiler implements CompilerInterface {
protected isNamespacedJsonFormat(values: any): boolean { protected isNamespacedJsonFormat(values: any): boolean {
return Object.keys(values).some(key => typeof values[key] === 'object'); return Object.keys(values).some(key => typeof values[key] === 'object');
} }
} }

View File

@ -5,7 +5,6 @@ import { stripBOM } from '../utils/utils';
import { flatten, unflatten } from 'flat'; import { flatten, unflatten } from 'flat';
export class NamespacedJsonCompiler implements CompilerInterface { export class NamespacedJsonCompiler implements CompilerInterface {
public indentation: string = '\t'; public indentation: string = '\t';
public extension = 'json'; public extension = 'json';
@ -27,5 +26,4 @@ export class NamespacedJsonCompiler implements CompilerInterface {
const values: {} = flatten(JSON.parse(stripBOM(contents))); const values: {} = flatten(JSON.parse(stripBOM(contents)));
return new TranslationCollection(values); return new TranslationCollection(values);
} }
} }

View File

@ -5,7 +5,6 @@ import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils
import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstTextAttribute } from '@angular/compiler'; import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstTextAttribute } from '@angular/compiler';
export class DirectiveParser implements ParserInterface { export class DirectiveParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null { public extract(source: string, filePath: string): TranslationCollection | null {
if (filePath && isPathAngularComponent(filePath)) { if (filePath && isPathAngularComponent(filePath)) {
source = extractComponentInlineTemplate(source); source = extractComponentInlineTemplate(source);
@ -42,13 +41,16 @@ export class DirectiveParser implements ParserInterface {
return [node]; return [node];
} }
return node.children.reduce((result: TmplAstElement[], childNode: TmplAstNode) => { return node.children.reduce(
(result: TmplAstElement[], childNode: TmplAstNode) => {
if (this.isElement(childNode)) { if (this.isElement(childNode)) {
const children = this.findChildrenElements(childNode); const children = this.findChildrenElements(childNode);
return result.concat(children); return result.concat(children);
} }
return result; return result;
}, [node]); },
[node]
);
} }
protected parseTemplate(template: string, path: string): TmplAstNode[] { protected parseTemplate(template: string, path: string): TmplAstNode[] {
@ -56,9 +58,7 @@ export class DirectiveParser implements ParserInterface {
} }
protected isElement(node: any): node is TmplAstElement { protected isElement(node: any): node is TmplAstElement {
return node return node && node.attributes !== undefined && node.children !== undefined;
&& node.attributes !== undefined
&& node.children !== undefined;
} }
protected isTranslatable(node: TmplAstNode): boolean { protected isTranslatable(node: TmplAstNode): boolean {
@ -70,7 +70,7 @@ export class DirectiveParser implements ParserInterface {
protected getElementTranslateAttrValue(element: TmplAstElement): string { protected getElementTranslateAttrValue(element: TmplAstElement): string {
const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate'); const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate');
return attr && attr.value || ''; return (attr && attr.value) || '';
} }
protected getElementContents(element: TmplAstElement): string { protected getElementContents(element: TmplAstElement): string {
@ -79,5 +79,4 @@ export class DirectiveParser implements ParserInterface {
const end = element.endSourceSpan.start.offset; const end = element.endSourceSpan.start.offset;
return contents.substring(start, end).trim(); return contents.substring(start, end).trim();
} }
} }

View File

@ -8,7 +8,6 @@ const MARKER_MODULE_NAME = '@biesbjerg/ngx-translate-extract-marker';
const MARKER_IMPORT_NAME = 'marker'; const MARKER_IMPORT_NAME = 'marker';
export class MarkerParser implements ParserInterface { export class MarkerParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null { public extract(source: string, filePath: string): TranslationCollection | null {
const sourceFile = tsquery.ast(source, filePath); const sourceFile = tsquery.ast(source, filePath);
@ -30,5 +29,4 @@ export class MarkerParser implements ParserInterface {
}); });
return collection; return collection;
} }
} }

View File

@ -1,7 +1,5 @@
import { TranslationCollection } from '../utils/translation.collection'; import { TranslationCollection } from '../utils/translation.collection';
export interface ParserInterface { export interface ParserInterface {
extract(source: string, filePath: string): TranslationCollection | null; extract(source: string, filePath: string): TranslationCollection | null;
} }

View File

@ -3,7 +3,6 @@ import { TranslationCollection } from '../utils/translation.collection';
import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils'; import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
export class PipeParser implements ParserInterface { export class PipeParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null { public extract(source: string, filePath: string): TranslationCollection | null {
if (filePath && isPathAngularComponent(filePath)) { if (filePath && isPathAngularComponent(filePath)) {
source = extractComponentInlineTemplate(source); source = extractComponentInlineTemplate(source);
@ -17,11 +16,10 @@ export class PipeParser implements ParserInterface {
const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g; const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g;
let matches: RegExpExecArray; let matches: RegExpExecArray;
while (matches = regExp.exec(template)) { while ((matches = regExp.exec(template))) {
collection = collection.add(matches[2].split('\\\'').join('\'')); collection = collection.add(matches[2].split("\\'").join("'"));
} }
return collection; return collection;
} }
} }

View File

@ -8,7 +8,6 @@ const TRANSLATE_SERVICE_TYPE_REFERENCE = 'TranslateService';
const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream']; const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream'];
export class ServiceParser implements ParserInterface { export class ServiceParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null { public extract(source: string, filePath: string): TranslationCollection | null {
const sourceFile = tsquery.ast(source, filePath); const sourceFile = tsquery.ast(source, filePath);
@ -37,5 +36,4 @@ export class ServiceParser implements ParserInterface {
}); });
return collection; return collection;
} }
} }

View File

@ -2,11 +2,9 @@ import { TranslationCollection } from '../utils/translation.collection';
import { PostProcessorInterface } from './post-processor.interface'; import { PostProcessorInterface } from './post-processor.interface';
export class KeyAsDefaultValuePostProcessor implements PostProcessorInterface { export class KeyAsDefaultValuePostProcessor implements PostProcessorInterface {
public name: string = 'KeyAsDefaultValue'; public name: string = 'KeyAsDefaultValue';
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.map((key, val) => val === '' ? key : val); return draft.map((key, val) => (val === '' ? key : val));
} }
} }

View File

@ -2,11 +2,9 @@ import { TranslationCollection } from '../utils/translation.collection';
import { PostProcessorInterface } from './post-processor.interface'; import { PostProcessorInterface } from './post-processor.interface';
export class NullAsDefaultValuePostProcessor implements PostProcessorInterface { export class NullAsDefaultValuePostProcessor implements PostProcessorInterface {
public name: string = 'NullAsDefaultValue'; public name: string = 'NullAsDefaultValue';
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.map((key, val) => existing.get(key) === undefined ? null : val); return draft.map((key, val) => (existing.get(key) === undefined ? null : val));
} }
} }

View File

@ -1,9 +1,7 @@
import { TranslationCollection } from '../utils/translation.collection'; import { TranslationCollection } from '../utils/translation.collection';
export interface PostProcessorInterface { export interface PostProcessorInterface {
name: string; name: string;
process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection; process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection;
} }

View File

@ -2,11 +2,9 @@ import { TranslationCollection } from '../utils/translation.collection';
import { PostProcessorInterface } from './post-processor.interface'; import { PostProcessorInterface } from './post-processor.interface';
export class PurgeObsoleteKeysPostProcessor implements PostProcessorInterface { export class PurgeObsoleteKeysPostProcessor implements PostProcessorInterface {
public name: string = 'PurgeObsoleteKeys'; public name: string = 'PurgeObsoleteKeys';
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.intersect(extracted); return draft.intersect(extracted);
} }
} }

View File

@ -2,11 +2,9 @@ import { TranslationCollection } from '../utils/translation.collection';
import { PostProcessorInterface } from './post-processor.interface'; import { PostProcessorInterface } from './post-processor.interface';
export class SortByKeyPostProcessor implements PostProcessorInterface { export class SortByKeyPostProcessor implements PostProcessorInterface {
public name: string = 'SortByKey'; public name: string = 'SortByKey';
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.sort(); return draft.sort();
} }
} }

View File

@ -2,7 +2,7 @@
* Assumes file is an Angular component if type is javascript/typescript * Assumes file is an Angular component if type is javascript/typescript
*/ */
export function isPathAngularComponent(path: string): boolean { export function isPathAngularComponent(path: string): boolean {
return (/\.ts|js$/i).test(path); return /\.ts|js$/i.test(path);
} }
/** /**

View File

@ -4,7 +4,6 @@ import { TranslationCollection } from '../../src/utils/translation.collection';
import { NamespacedJsonCompiler } from '../../src/compilers/namespaced-json.compiler'; import { NamespacedJsonCompiler } from '../../src/compilers/namespaced-json.compiler';
describe('NamespacedJsonCompiler', () => { describe('NamespacedJsonCompiler', () => {
let compiler: NamespacedJsonCompiler; let compiler: NamespacedJsonCompiler;
beforeEach(() => { beforeEach(() => {
@ -23,7 +22,10 @@ describe('NamespacedJsonCompiler', () => {
} }
`; `;
const collection: TranslationCollection = compiler.parse(contents); const collection: TranslationCollection = compiler.parse(contents);
expect(collection.values).to.deep.equal({'NAMESPACE.KEY.FIRST_KEY': '', 'NAMESPACE.KEY.SECOND_KEY': 'VALUE' }); expect(collection.values).to.deep.equal({
'NAMESPACE.KEY.FIRST_KEY': '',
'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
});
}); });
it('should unflatten keys on compile', () => { it('should unflatten keys on compile', () => {
@ -59,11 +61,10 @@ describe('NamespacedJsonCompiler', () => {
it('should not reorder keys when compiled', () => { it('should not reorder keys when compiled', () => {
const collection = new TranslationCollection({ const collection = new TranslationCollection({
'BROWSE': '', BROWSE: '',
'LOGIN': '' LOGIN: ''
}); });
const result: string = compiler.compile(collection); const result: string = compiler.compile(collection);
expect(result).to.equal('{\n\t"BROWSE": "",\n\t"LOGIN": ""\n}'); expect(result).to.equal('{\n\t"BROWSE": "",\n\t"LOGIN": ""\n}');
}); });
}); });

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { DirectiveParser } from '../../src/parsers/directive.parser'; import { DirectiveParser } from '../../src/parsers/directive.parser';
describe('DirectiveParser', () => { describe('DirectiveParser', () => {
const templateFilename: string = 'test.template.html'; const templateFilename: string = 'test.template.html';
const componentFilename: string = 'test.component.ts'; const componentFilename: string = 'test.component.ts';
@ -86,5 +85,4 @@ describe('DirectiveParser', () => {
const keys = parser.extract(contents, componentFilename).keys(); const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal([]); expect(keys).to.deep.equal([]);
}); });
}); });

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { MarkerParser } from '../../src/parsers/marker.parser'; import { MarkerParser } from '../../src/parsers/marker.parser';
describe('MarkerParser', () => { describe('MarkerParser', () => {
const componentFilename: string = 'test.component.ts'; const componentFilename: string = 'test.component.ts';
let parser: MarkerParser; let parser: MarkerParser;
@ -12,7 +11,6 @@ describe('MarkerParser', () => {
parser = new MarkerParser(); parser = new MarkerParser();
}); });
it('should extract strings using marker function', () => { it('should extract strings using marker function', () => {
const contents = ` const contents = `
import { marker } from '@biesbjerg/ngx-translate-extract-marker'; import { marker } from '@biesbjerg/ngx-translate-extract-marker';
@ -35,11 +33,6 @@ describe('MarkerParser', () => {
_('Mix ' + \`of \` + 'different ' + \`types\`); _('Mix ' + \`of \` + 'different ' + \`types\`);
`; `;
const keys = parser.extract(contents, componentFilename).keys(); const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal([ expect(keys).to.deep.equal(['Hello world', 'This is a very very very very long line.', 'Mix of different types']);
'Hello world',
'This is a very very very very long line.',
'Mix of different types'
]);
}); });
}); });

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { PipeParser } from '../../src/parsers/pipe.parser'; import { PipeParser } from '../../src/parsers/pipe.parser';
describe('PipeParser', () => { describe('PipeParser', () => {
const templateFilename: string = 'test.template.html'; const templateFilename: string = 'test.template.html';
let parser: PipeParser; let parser: PipeParser;
@ -118,5 +117,4 @@ describe('PipeParser', () => {
const keys = parser.extract(contents, templateFilename).keys(); const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['message']); expect(keys).to.deep.equal(['message']);
}); });
}); });

View File

@ -2,12 +2,9 @@ import { expect } from 'chai';
import { ServiceParser } from '../../src/parsers/service.parser'; import { ServiceParser } from '../../src/parsers/service.parser';
class TestServiceParser extends ServiceParser { class TestServiceParser extends ServiceParser {}
}
describe('ServiceParser', () => { describe('ServiceParser', () => {
const componentFilename: string = 'test.component.ts'; const componentFilename: string = 'test.component.ts';
let parser: TestServiceParser; let parser: TestServiceParser;
@ -44,7 +41,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Fallback message']); expect(keys).to.deep.equal(['Fallback message']);
}); });
it('should extract strings in TranslateService\'s get() method', () => { it("should extract strings in TranslateService's get() method", () => {
const contents = ` const contents = `
@Component({ }) @Component({ })
export class AppComponent { export class AppComponent {
@ -57,7 +54,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello World']); expect(keys).to.deep.equal(['Hello World']);
}); });
it('should extract strings in TranslateService\'s instant() method', () => { it("should extract strings in TranslateService's instant() method", () => {
const contents = ` const contents = `
@Component({ }) @Component({ })
export class AppComponent { export class AppComponent {
@ -70,7 +67,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello World']); expect(keys).to.deep.equal(['Hello World']);
}); });
it('should extract strings in TranslateService\'s stream() method', () => { it("should extract strings in TranslateService's stream() method", () => {
const contents = ` const contents = `
@Component({ }) @Component({ })
export class AppComponent { export class AppComponent {
@ -83,7 +80,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello World']); expect(keys).to.deep.equal(['Hello World']);
}); });
it('should extract array of strings in TranslateService\'s get() method', () => { it("should extract array of strings in TranslateService's get() method", () => {
const contents = ` const contents = `
@Component({ }) @Component({ })
export class AppComponent { export class AppComponent {
@ -96,7 +93,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello', 'World']); expect(keys).to.deep.equal(['Hello', 'World']);
}); });
it('should extract array of strings in TranslateService\'s instant() method', () => { it("should extract array of strings in TranslateService's instant() method", () => {
const contents = ` const contents = `
@Component({ }) @Component({ })
export class AppComponent { export class AppComponent {
@ -109,7 +106,7 @@ describe('ServiceParser', () => {
expect(key).to.deep.equal(['Hello', 'World']); expect(key).to.deep.equal(['Hello', 'World']);
}); });
it('should extract array of strings in TranslateService\'s stream() method', () => { it("should extract array of strings in TranslateService's stream() method", () => {
const contents = ` const contents = `
@Component({ }) @Component({ })
export class AppComponent { export class AppComponent {
@ -298,5 +295,4 @@ describe('ServiceParser', () => {
const keys = parser.extract(contents, componentFilename).keys(); const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal(['Back']); expect(keys).to.deep.equal(['Back']);
}); });
}); });

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils'; import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils';
describe('Utils', () => { describe('Utils', () => {
it('should recognize js extension as angular component', () => { it('should recognize js extension as angular component', () => {
const result = isPathAngularComponent('test.js'); const result = isPathAngularComponent('test.js');
expect(result).to.equal(true); expect(result).to.equal(true);
@ -63,5 +62,4 @@ describe('Utils', () => {
const template = extractComponentInlineTemplate(contents); const template = extractComponentInlineTemplate(contents);
expect(template).to.equal('\n\t\t\t\t\t<p>\n\t\t\t\t\t\tHello World\n\t\t\t\t\t</p>\n\t\t\t\t'); expect(template).to.equal('\n\t\t\t\t\t<p>\n\t\t\t\t\t\tHello World\n\t\t\t\t\t</p>\n\t\t\t\t');
}); });
}); });

View File

@ -5,7 +5,6 @@ import { KeyAsDefaultValuePostProcessor } from '../../src/post-processors/key-as
import { TranslationCollection } from '../../src/utils/translation.collection'; import { TranslationCollection } from '../../src/utils/translation.collection';
describe('KeyAsDefaultValuePostProcessor', () => { describe('KeyAsDefaultValuePostProcessor', () => {
let processor: PostProcessorInterface; let processor: PostProcessorInterface;
beforeEach(() => { beforeEach(() => {
@ -27,5 +26,4 @@ describe('KeyAsDefaultValuePostProcessor', () => {
'Use this key as value as well': 'Use this key as value as well' 'Use this key as value as well': 'Use this key as value as well'
}); });
}); });
}); });

View File

@ -5,7 +5,6 @@ import { NullAsDefaultValuePostProcessor } from '../../src/post-processors/null-
import { TranslationCollection } from '../../src/utils/translation.collection'; import { TranslationCollection } from '../../src/utils/translation.collection';
describe('NullAsDefaultValuePostProcessor', () => { describe('NullAsDefaultValuePostProcessor', () => {
let processor: PostProcessorInterface; let processor: PostProcessorInterface;
beforeEach(() => { beforeEach(() => {
@ -38,5 +37,4 @@ describe('NullAsDefaultValuePostProcessor', () => {
'String A': 'Streng A' 'String A': 'Streng A'
}); });
}); });
}); });

View File

@ -5,15 +5,14 @@ import { PurgeObsoleteKeysPostProcessor } from '../../src/post-processors/purge-
import { TranslationCollection } from '../../src/utils/translation.collection'; import { TranslationCollection } from '../../src/utils/translation.collection';
describe('PurgeObsoleteKeysPostProcessor', () => { describe('PurgeObsoleteKeysPostProcessor', () => {
let postProcessor: PostProcessorInterface;
let processor: PostProcessorInterface;
beforeEach(() => { beforeEach(() => {
processor = new PurgeObsoleteKeysPostProcessor(); postProcessor = new PurgeObsoleteKeysPostProcessor();
}); });
it('should purge obsolete keys', () => { it('should purge obsolete keys', () => {
const collection = new TranslationCollection({ const draft = new TranslationCollection({
'I am completely new': '', 'I am completely new': '',
'I already exist': '', 'I already exist': '',
'I already exist but was not present in extract': '' 'I already exist but was not present in extract': ''
@ -27,10 +26,9 @@ describe('PurgeObsoleteKeysPostProcessor', () => {
'I already exist but was not present in extract': '' 'I already exist but was not present in extract': ''
}); });
expect(processor.process(collection, extracted, existing).values).to.deep.equal({ expect(postProcessor.process(draft, extracted, existing).values).to.deep.equal({
'I am completely new': '', 'I am completely new': '',
'I already exist': '' 'I already exist': ''
}); });
}); });
}); });

View File

@ -5,7 +5,6 @@ import { SortByKeyPostProcessor } from '../../src/post-processors/sort-by-key.po
import { TranslationCollection } from '../../src/utils/translation.collection'; import { TranslationCollection } from '../../src/utils/translation.collection';
describe('SortByKeyPostProcessor', () => { describe('SortByKeyPostProcessor', () => {
let processor: PostProcessorInterface; let processor: PostProcessorInterface;
beforeEach(() => { beforeEach(() => {
@ -14,20 +13,19 @@ describe('SortByKeyPostProcessor', () => {
it('should sort keys alphanumerically', () => { it('should sort keys alphanumerically', () => {
const collection = new TranslationCollection({ const collection = new TranslationCollection({
'z': 'last value', z: 'last value',
'a': 'a value', a: 'a value',
'9': 'a numeric key', '9': 'a numeric key',
'b': 'another value' b: 'another value'
}); });
const extracted = new TranslationCollection(); const extracted = new TranslationCollection();
const existing = new TranslationCollection(); const existing = new TranslationCollection();
expect(processor.process(collection, extracted, existing).values).to.deep.equal({ expect(processor.process(collection, extracted, existing).values).to.deep.equal({
'9': 'a numeric key', '9': 'a numeric key',
'a': 'a value', a: 'a value',
'b': 'another value', b: 'another value',
'z': 'last value' z: 'last value'
}); });
}); });
}); });

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { TranslationCollection } from '../../src/utils/translation.collection'; import { TranslationCollection } from '../../src/utils/translation.collection';
describe('StringCollection', () => { describe('StringCollection', () => {
let collection: TranslationCollection; let collection: TranslationCollection;
beforeEach(() => { beforeEach(() => {
@ -64,12 +63,15 @@ describe('StringCollection', () => {
it('should merge with other collection', () => { it('should merge with other collection', () => {
collection = collection.add('oldKey', 'oldVal'); collection = collection.add('oldKey', 'oldVal');
const newCollection = new TranslationCollection({ newKey: 'newVal' }); const newCollection = new TranslationCollection({ newKey: 'newVal' });
expect(collection.union(newCollection).values).to.deep.equal({ oldKey: 'oldVal', newKey: 'newVal' }); expect(collection.union(newCollection).values).to.deep.equal({
oldKey: 'oldVal',
newKey: 'newVal'
});
}); });
it('should intersect with passed collection', () => { it('should intersect with passed collection', () => {
collection = collection.addKeys(['red', 'green', 'blue']); collection = collection.addKeys(['red', 'green', 'blue']);
const newCollection = new TranslationCollection( { red: '', blue: '' }); const newCollection = new TranslationCollection({ red: '', blue: '' });
expect(collection.intersect(newCollection).values).to.deep.equal({ red: '', blue: '' }); expect(collection.intersect(newCollection).values).to.deep.equal({ red: '', blue: '' });
}); });
@ -88,7 +90,10 @@ describe('StringCollection', () => {
it('should map values', () => { it('should map values', () => {
collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' }); collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' });
collection = collection.map((key, val) => 'mapped value'); collection = collection.map((key, val) => 'mapped value');
expect(collection.values).to.deep.equal({ red: 'mapped value', green: 'mapped value', blue: 'mapped value' }); expect(collection.values).to.deep.equal({
red: 'mapped value',
green: 'mapped value',
blue: 'mapped value'
});
}); });
}); });