-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
132 lines (122 loc) · 3.58 KB
/
notes.js
File metadata and controls
132 lines (122 loc) · 3.58 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//file system module to add,read,delete notes in notes.json
const fs=require('fs');
//chalk module to give colourful output msgs
const chalk = require('chalk')
//function to add a note
const addNote=(title,body)=>{
//loading the required notes
const notes=loadNotes();
//checking if any note with same title is there
const duplicateNote = notes.find((note) => note.title === title)
if(!duplicateNote){
//adding the note
notes.push({title:title,
body:body
})
//saving the note
saveNote(notes)
console.log(chalk.green.inverse('New note added!'))
}else{
console.log(chalk.red.inverse('Note title taken!'))
}
}
//function to remove a note
const removeNote=(title)=>{
//loading the notes
const notes=loadNotes();
//creating a seperate array and keeping all the notes except the one with the title
const updatedNotes=notes.filter((note)=>{
return note.title!==title
})
//if successfully removed then saving the note
if(notes.length>updatedNotes.length){
saveNote(updatedNotes)
console.log(chalk.green.inverse('Note removed!'))
}else{
console.log(chalk.red.inverse('No note found!'))
}
}
//function list all the notes title
const listNotes=()=>{
//loading the notes
const notes=loadNotes();
console.log(chalk.green.inverse('Your Notes'))
let i=1;
//if no notes are there then displaying the msg
if(notes.length==0){
console.log(chalk.red.inverse('No note found!'))
}else{
//listing all the notes
for(let note of notes){
console.log(`${i}. ${note.title}`)
i++;
}
}
}
//function to read a note
const readNote=(title)=>{
//loading the notes
const notes=loadNotes();
//checking if note with that title is present or not
const note = notes.find((note) => note.title === title)
if(note){
//displaying the note
console.log(chalk.inverse(note.title))
console.log(note.body)
}else{
console.log(chalk.red.inverse(`No note found with title "${title}"`))
}
}
//function to read all the notes
const readAllNotes=()=>{
//loading the notes
const notes=loadNotes();
if(notes.length!=0){
//traversing through the array of notes and reading it
let i=1
console.log(chalk.green.inverse('Your Notes'))
for(let note of notes){
console.log(chalk.inverse(`${i}. ${note.title}`))
console.log(note.body)
i++;
}
}else{
console.log(chalk.red.inverse(`No notes found!`))
}
}
//function to remove all the notes
const removeAll=()=>{
let arr='[]'
//writing an empty array in the notes.json file
fs.writeFileSync('./notes.json',arr)
console.log(chalk.green.inverse('All notes removed successfully'))
}
//function to load the notes
const loadNotes=()=>{
try{
//getting the buffer
let data=fs.readFileSync('./notes.json')
//stringifying the data buffer
data =data.toString();
//converting the string to object and returning it
return JSON.parse(data);
}catch(e){
return []
}
}
//function to save a note
const saveNote=(notes)=>{
//stringfying the object
const dataJSON=JSON.stringify(notes);
//writing the object in the notes.json file
fs.writeFileSync('./notes.json',dataJSON)
}
//exporting the modules
module.exports={
addNote:addNote,
removeNote:removeNote,
listNotes:listNotes,
readNote:readNote,
readAllNotes:readAllNotes,
removeAll:removeAll
}