Skip to content
Open
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 3 (
- `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 count` — print how many notes you have
- `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
6 changes: 5 additions & 1 deletion lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function remove(id) {
return data.notes.length < before;
}

function count() {
return load().notes.length;
}

// Returns the notes whose text contains `term`.
function matches(notes, term) {
return notes.filter((note) => note.text.includes(term));
Expand All @@ -45,4 +49,4 @@ function search(term) {
return matches(load().notes, term);
}

module.exports = { all, add, remove, search, matches };
module.exports = { all, add, remove, count, search, matches };
7 changes: 6 additions & 1 deletion notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ function main() {
}
break;
}
case "count": {
const total = store.count();
console.log(`You have ${total} ${total === 1 ? "note" : "notes"}.`);
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> | count | delete <id>");
console.log(`(Session locks after ${config.SESSION_TIMEOUT_MINUTES} minutes of inactivity.)`);
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/notes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ test("search returns nothing when no note contains the term", () => {
const result = matches(notes, "xyz");
assert.strictEqual(result.length, 0);
});

test("count matches the number of stored notes", () => {
const { all, count } = require("../lib/store");
assert.strictEqual(count(), all().length);
});
Loading