-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
109 lines (81 loc) · 2.05 KB
/
Copy pathnotes.js
File metadata and controls
109 lines (81 loc) · 2.05 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const fs = require('fs');
const getNotes = () =>{
return "Getting Notes";
}
const AddNotes = function(title,body) {
notes = loadNotes();
// const dupNotes = notes.filter((note)=>{
// if (note.title == title)
// return true;
// })
//notes.filter searches whole array regardless of dup found or not
// if (dupNotes.length === 0){
// notes.push({"title":title,"body":body})
// saveNotes(notes);
// }
// else {
// console.log("Note exist");
// }
// better approach
const dupNote = notes.find((note)=> note.title === title)
if (!dupNote){
notes.push({"title":title,"body":body})
saveNotes(notes);
}
else {
console.log("Note exist");
}
}
saveNotes = (notes) =>{
notes_string = JSON.stringify(notes);
fs.writeFileSync('notes.json',notes_string);
console.log("Notes updated")
}
const ReadNotes = function(title){
notes = loadNotes();
res_note = notes.find((note)=> note.title === title)
if (res_note){
console.log("Title "+res_note.title );
console.log("Body : "+res_note.body);
}
else
console.log("Note not found");
}
loadNotes = () =>{
try{
let databuffer = fs.readFileSync("notes.json","utf8");
let result = JSON.parse(databuffer);
return result;
}
catch(e){
return [];
}
}
const RemoveNotes = function (title) {
let notes = loadNotes();
updatedNotes = notes.filter((note) =>{
if (note.title === title)
return false;
else
return true;
})
if (updatedNotes.length < notes.length){
saveNotes(updatedNotes);
console.log("Note Removed")
}
else{
console.log("Notes doesn't exists");
}
}
ListNotes = function (){
notes = loadNotes();
notes.forEach(element => {
console.log(element.title);
});
}
module.exports = {
getNotes : getNotes,
AddNotes : AddNotes,
RemoveNotes:RemoveNotes,
ListNotes : ListNotes,
ReadNote: ReadNotes}