Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ A tiny command-line notes tool, used as the practice repo for Unit 4, Lesson 4 (
- `node notes.js add <text>` — add a note
- `node notes.js list` — list all notes
- `node notes.js search <term>` — list notes containing a term
- `node notes.js edit <id> <text>` — change a note's text
- `node notes.js delete <id>` — delete a note

Layout: `notes.js` is the entry point, `lib/store.js` loads, saves, and searches notes, `lib/config.js` holds settings, and `tests/` holds the test suite (`npm test`).
Expand Down
9 changes: 8 additions & 1 deletion lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ function search(term) {
return matches(load().notes, term);
}

module.exports = { all, add, remove, search, matches };
function edit(id, text) {
const data = load();
const note = data.notes.find((n) => n.id === id);
note.text = text;
save(data);
}

module.exports = { all, add, remove, search, matches, edit };
9 changes: 8 additions & 1 deletion notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ function main() {
}
break;
}
case "edit": {
const id = Number(rest[0]);
const text = rest.slice(1).join(" ").trim();
store.edit(id, text);
console.log(`Updated note #${id}`);
break;
}
case "delete": {
const id = Number(rest[0]);
const ok = store.remove(id);
console.log(ok ? `Deleted note #${id}` : `No note #${id} found`);
break;
}
default:
console.log("Commands: add <text> | list | search <term> | delete <id>");
console.log("Commands: add <text> | list | search <term> | edit <id> <text> | delete <id>");
console.log(`(Session locks after ${config.SESSION_TIMEOUT_MINUTES} minutes of inactivity.)`);
}
}
Expand Down
Loading