-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02AddRemoveReadList.js
94 lines (71 loc) · 1.94 KB
/
02AddRemoveReadList.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
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
const fs=require('fs');
const chalk=require('chalk');
const loadNotes=()=>{
try {
const bufferData=fs.readFileSync('Note-json.json');
const data=bufferData.toString();
return JSON.parse(data);
} catch(e) {
return [];
}
};
const saveNotes=(getlist)=>{
const JsonData=JSON.stringify(getlist);
fs.writeFileSync('Note-json.json',JsonData);
};
//Add a note :::
const addNote=(title,body)=>{
const getList=loadNotes();
const duplicateNote=getList.find((note)=>title===note.title);
if(duplicateNote)
{
console.log(chalk.inverse.red('Title of Note is already taken!!'));
}
else{
getList.push(
{
title:title,
body:body,
},
)
console.log(chalk.inverse.greenBright('Add a new note!!'));
}
saveNotes(getList);
};
//Remove a Note:::::::
const removeNote=(title)=>{
const getList=loadNotes();
const getAnotherNote=getList.filter((note)=>note.title!==title);
if(getList.length > getAnotherNote.length)
{
console.log(chalk.greenBright.inverse('Note Removed!!'));
}
else{
console.log(chalk.red.inverse('Note Does not exist!'));
}
saveNotes(getAnotherNote);
};
//Read a note:::
const readNote=(title)=>{
const getNote=loadNotes();
const getData=getNote.find((note)=>title===note.title);
if(getData)
{
console.log(`Title is : ${chalk.inverse.grey(getData.title)} and Body is: ${chalk.inverse.red(getData.body)};`);
}
else{
console.log(chalk.inverse.greenBright('Note does not exist!!'));
}
};
// List a note:
const listNote=()=>{
const getNote=loadNotes();
for(const note of getNote)
console.log(`Title is : ${chalk.inverse.grey(note.title)} and Body is: ${chalk.inverse.red(note.body)};`);
};
module.exports={
addNote:addNote,
removeNote:removeNote,
readNote:readNote,
listNote:listNote,
}