3.11 Removing a Note

Since we have re-factored our code, the further programming is going to follow that modularization. Everything would be a lot simpler than without that refactor.

var removeNote = (title) => {
notes = fetchNotes();
var filteredNotes = notes.filter((note) => note.title !== title);
saveNotes(filteredNotes);
}

Remark

Similarly, to increase responsiveness, we would need to add a return value for removeNote to indicate the success or failure about removing that note.

return notes.length !== filteredNotes.length;

In app.js, the remove branch should be modified to

} else if (command === 'remove') {
var noteRemoved = notes.removeNote(argv.title);
var message = noteRemoved ? 'note was removed' : 'note not found';
console.log(message);

Here is a demo:

jude@jude-ThinkPad-S5:/media/jude/Jude 1TB/nodejs/notes-app$ node app.js remove --title="cs245 a7"
note was removed
jude@jude-ThinkPad-S5:/media/jude/Jude 1TB/nodejs/notes-app$ node app.js remove --title="cs245 a7"
note not found