Optimize TranslationCollection

This commit is contained in:
Kim Biesbjerg 2016-12-10 19:20:44 +01:00
parent 43df1f0fb8
commit 2739545b9e

View File

@ -23,13 +23,22 @@ export class TranslationCollection {
} }
public remove(key: string): TranslationCollection { public remove(key: string): TranslationCollection {
let newCollection = new TranslationCollection(); return this.filter(k => key !== k);
Object.keys(this.values).forEach(collectionKey => { }
if (key !== collectionKey) {
newCollection = newCollection.add(key, this.values[key]); public forEach(callback: (key?: string, val?: string) => void): TranslationCollection {
Object.keys(this.values).forEach(key => callback.call(this, key, this.values[key]));
return this;
}
public filter(callback: (key?: string, val?: string) => boolean): TranslationCollection {
let values: TranslationType = {};
this.forEach((key: string, val: string) => {
if (callback.call(this, key, val)) {
values[key] = val;
} }
}); });
return newCollection; return new TranslationCollection(values);
} }
public union(collection: TranslationCollection): TranslationCollection { public union(collection: TranslationCollection): TranslationCollection {
@ -37,13 +46,13 @@ export class TranslationCollection {
} }
public intersect(collection: TranslationCollection): TranslationCollection { public intersect(collection: TranslationCollection): TranslationCollection {
let newCollection = new TranslationCollection(); let values: TranslationType = {};
Object.keys(this.values).forEach(key => { this.filter(key => collection.has(key))
if (collection.has(key)) { .forEach((key: string, val: string) => {
newCollection = newCollection.add(key, this.values[key]); values[key] = val;
}
}); });
return newCollection;
return new TranslationCollection(values);
} }
public has(key: string): boolean { public has(key: string): boolean {