-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
76 lines (68 loc) · 1.83 KB
/
Copy pathnotes.js
File metadata and controls
76 lines (68 loc) · 1.83 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
const fs=require("fs");
const chalk=require("chalk");
const getNotes=() => "Your notes are...";
const addNotes=function(title,body){
const notes =loadnotes();
const duplicateNote=notes.find((note) => note.title===title);
if(!duplicateNote){
notes.push({
title:title,
body:body
});
savenote(notes);
console.log("New Note Added");
}
else{
console.log("Given title pre exsist....give some other title");
}
}
const savenote =(notes) => {
const JSONnotes=JSON.stringify(notes);
fs.writeFileSync("notes.json",JSONnotes);
}
const loadnotes=() => {
try{
const dataBuffer=fs.readFileSync("notes.json");
const JSONdata=dataBuffer.toString();
const data=JSON.parse(JSONdata);
return data;
}catch(e){
return [];
}
}
const removeNotes=(title) => {
const notes=loadnotes();
const newNotes=notes.filter((note) => note.title !== title);
if(newNotes.length!==notes.length){
console.log(chalk.bgGreen("Note removed!!"));
savenote(newNotes);
}else{
console.log(chalk.bgRed("No note found!!"));
}
}
const listNotes=() =>{
console.log(chalk.greenBright("Your Notes...."));
const notes=loadnotes();
notes.forEach((note,index) => {
console.log(chalk.yellowBright(index+1+". "+note.title));
});
}
const readNotes=(title) =>{
console.log(chalk.blueBright("Reading Notes....."));
const notes=loadnotes();
const Newnote=notes.find((note) => note.title===title);
if(Newnote){
console.log(chalk.yellowBright(Newnote.title));
console.log(Newnote.body);
}
else{
console.log(chalk.bgRed("Note not found!!"));
}
}
module.exports={
getNotes:getNotes,
addNotes:addNotes,
removeNotes:removeNotes,
listNotes:listNotes,
readNotes:readNotes
};