-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
113 lines (102 loc) · 3 KB
/
notes.js
File metadata and controls
113 lines (102 loc) · 3 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
const fs = require('fs')
const chalk = require('chalk')
const n1="These are your notes..."
const filename = 'notes.json'
const getnotes = ()=>{
return n1;
}
const addList = (title, body)=>{
const listadd = loadList()
//console.log(list)
//const duplicateList = listadd.filter((duplicate)=> duplicate.title === title)
const duplicateLists = listadd.find((duplicate)=> duplicate.title === title)
try{
if (!duplicateLists){
listadd.push({
title: title,
body: body
})
//console.log('the length of duplicate list is: '+duplicateList.length)
//console.log('title coming is:'+listadd.title)
//console.log(duplicateList)
console.log('data being pushed to File: '+ filename)
saveList(listadd)
}
else{
console.log("Title already exist!")
}
}
catch (e){
console.log("There was an error while pushing data to array.")
}
}
const remListTitle = (title,body)=>{
const remlist = loadList()
//console.log(remlist)
const removabletitle = remlist.filter((remtitle)=>remtitle.title !== title
//console.log(removabletitle)
)
if (remlist.length > removabletitle.length ){
console.log(chalk.bgGreen('Note Removed!'))
saveList(removabletitle)
}
else{
console.log(chalk.bgRed('Note not Found!'))
}
}
const listnote = () =>{
const notelist = loadList()
//console.log(notelist)
console.log(chalk.blue.inverse('Your Notes'))
notelist.forEach((note)=>{
console.log(note.title)
})
}
const readList = (title)=>{
const rl= loadList()
//const rlt=JSON.parse(rl)
//console.log(rl.title)
debugger
const read = rl.find((rt)=> rt.title === title)
if (read){
console.log(read.title)
console.log(read.body)
}
else{
console.log('Note not found')
}
// if (read){
// }
// else{
// }
}
const saveList = (list)=>{
const listJSON = JSON.stringify(list)
fs.writeFileSync(filename,listJSON)
//console.log("New Title added")
//console.log("New Title added with following details! \n Title:" + listJSON.toString(title) + "\n Body:"+listJSON.body)
}
const loadList = ()=>{
try{
const dataBuffer = fs.readFileSync(filename)
//console.log(dataBuffer)
const dataJSON = dataBuffer.toString()
//console.log(dataJSON.title)
const data = JSON.parse(dataJSON)
//console.log(data)
return data
}
catch (e) {
console.log("No File exist with the name: "+ filename + '\nTrying to create file with filename: '+ filename)
fs.writeFileSync(filename)
console.log('File Created successfuly!')
return []
}
}
module.exports = {
getNotes: getnotes,
addList: addList,
removeList: remListTitle,
listNotes: listnote,
readLists: readList
}