-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.js
51 lines (43 loc) · 1002 Bytes
/
notes.js
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
const fs = require('fs');
const fetchNotes = () => {
try {
const noteString = fs.readFileSync('notes-data.json');
return JSON.parse(noteString);
} catch (e) {
return [];
}
};
const saveNotes = (notes) => {
fs.writeFileSync('notes-data.json', JSON.stringify(notes));
};
const addNote = (title, body) => {
const notes = fetchNotes();
const note = {
title,
body,
};
const unique = notes.filter(item => item.title === title);
if (unique.length === 0) {
notes.push(note);
saveNotes(notes);
return note;
}
};
const remove = (title) => {
const notes = fetchNotes();
const filteredNotes = notes.filter(item => item.title !== title);
saveNotes(notes);
return filteredNotes.length === notes.length;
};
const getNote = (title) => {
const notes = fetchNotes();
const filter = notes.filter(item => item.title === title);
return filter[0];
};
const getAll = () => fetchNotes();
module.exports = {
remove,
addNote,
getNote,
getAll,
};