Add option to merge extracted strings with existing translations (Thanks to @ocombe)

This commit is contained in:
Kim Biesbjerg
2016-12-09 19:29:48 +01:00
parent 59ef277c64
commit 6a76e7b5cb
20 changed files with 301 additions and 242 deletions

View File

@@ -0,0 +1,69 @@
export interface TranslationType {
[key: string]: string
};
export class TranslationCollection {
public values: TranslationType = {};
public constructor(values: TranslationType = {}) {
this.values = values;
}
public add(key: string, val: string = ''): TranslationCollection {
return new TranslationCollection(Object.assign({}, this.values, { [key]: val }));
}
public addKeys(keys: string[]): TranslationCollection {
const values = keys.reduce((results, key) => {
results[key] = '';
return results;
}, {});
return new TranslationCollection(Object.assign({}, this.values, values));
}
public remove(key: string): TranslationCollection {
let newCollection = new TranslationCollection();
Object.keys(this.values).forEach(collectionKey => {
if (key !== collectionKey) {
newCollection = newCollection.add(key, this.values[key]);
}
});
return newCollection;
}
public union(collection: TranslationCollection): TranslationCollection {
return new TranslationCollection(Object.assign({}, this.values, collection.values));
}
public intersect(collection: TranslationCollection): TranslationCollection {
let newCollection = new TranslationCollection();
Object.keys(this.values).forEach(key => {
if (collection.has(key)) {
newCollection = newCollection.add(key, this.values[key]);
}
});
return newCollection;
}
public has(key: string): boolean {
return this.values.hasOwnProperty(key);
}
public get(key: string): string {
return this.values[key];
}
public keys(): string[] {
return Object.keys(this.values);
}
public count(): number {
return Object.keys(this.values).length;
}
public isEmpty(): boolean {
return Object.keys(this.values).length === 0;
}
}