forked from mate-academy/git-playground-task4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
55 lines (45 loc) · 1.11 KB
/
Copy pathstore.js
File metadata and controls
55 lines (45 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const fs = require("fs");
const path = require("path");
const FILE = path.join(__dirname, "..", "notes.json");
function load() {
try {
return JSON.parse(fs.readFileSync(FILE, "utf8"));
} catch {
return { nextId: 1, notes: [] };
}
}
function save(data) {
fs.writeFileSync(FILE, JSON.stringify(data, null, 2));
}
function all() {
return load().notes;
}
function add(text) {
const data = load();
const note = { id: data.nextId, text };
data.notes.push(note);
data.nextId += 1;
save(data);
return note;
}
function remove(id) {
const data = load();
const before = data.notes.length;
data.notes = data.notes.filter((n) => n.id !== id);
save(data);
return data.notes.length < before;
}
// Returns the notes whose text contains `term`.
function matches(notes, term) {
return notes.filter((note) => note.text.includes(term));
}
function search(term) {
return matches(load().notes, term);
}
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 };