Ir para o conteúdo

CódigoNão tentada

Classe NotesStore

Dificuldade: fácil

Construa um NotesStore com addNote(state, name) e getNotes(state).

  • estados válidos: completed, active, others
  • addNote lança Error('Name cannot be empty') para um nome vazio
  • ambos os métodos lançam um Error com a mensagem Invalid state <state> (por exemplo Invalid state done) para estados desconhecidos
  • getNotes retorna os nomes na ordem de inserção; os stores não compartilham notas
Ver solução de referência
type State = 'completed' | 'active' | 'others';
export class NotesStore {
private notes: { state: State; name: string }[] = [];
private validStates = ['completed', 'active', 'others'];
addNote(state: State, name: string): void {
if (name === '') {
throw new Error('Name cannot be empty');
}
if (!this.validStates.includes(state)) {
throw new Error(`Invalid state ${state}`);
}
this.notes.push({ state, name });
}
getNotes(state: State): string[] {
if (!this.validStates.includes(state)) {
throw new Error(`Invalid state ${state}`);
}
const result: string[] = [];
for (const note of this.notes) {
if (note.state === state) {
result.push(note.name);
}
}
return result;
}
}
Carregando editor…