mysticsymbolic.github.io/lib/vocabulary.ts

20 wiersze
423 B
TypeScript
Czysty Zwykły widok Historia

2021-03-27 12:29:42 +00:00
export type VocabularyType = {
name: string;
};
export class Vocabulary<T extends VocabularyType> {
readonly itemMap: Map<string, T>;
constructor(readonly items: T[]) {
this.itemMap = new Map(items.map((item) => [item.name, item]));
}
get(name: string): T {
const item = this.itemMap.get(name);
if (!item) {
throw new Error(`Unable to find the item "${name}"!`);
}
return item;
}
}