-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.js
93 lines (76 loc) · 1.91 KB
/
notes.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
const fs=require('fs')
const chalk=require('chalk')
const getNotes =()=>"Your notes is loaded"
//adds notes to notes.json
const addNotes=(title,body)=>{
const notes = loadNotes()
const duplicateItem=notes.find((note)=>note.title===title)
debugger
if(duplicateItem===undefined){
const newNotes ={
title:title,
body: body
}
notes.push(newNotes)
saveNotes(notes)
console.log(chalk.green.bold("New Notes added !"))
}
else{
console.log(chalk.red.bold("Notes title already exist !"))
}
}
//removes notes from notes.json
const removeNotes =(title)=>{
const notes=loadNotes()
const newNotes = notes.filter((note)=>note.title !=title)
if(newNotes.length < notes.length){
saveNotes(newNotes)
console.log(chalk.green.bold.inverse("Notes is removed!"))
}
else{
console.log(chalk.red.bold.inverse("Notes doesn't exist !"))
}
}
//list all notes in notes.json
const listNotes=()=>{
console.log(chalk.blue("your notes"))
const listnotes=loadNotes()
const notes = loadNotes()
notes.forEach(note => {
console.log(note.title)
});
}
//read notes from notes.json with title as argument
const readNotes =title=>{
const notes =loadNotes()
const findNotes =notes.find((note)=>note.title === title)
if(findNotes){
console.log("Title: "+chalk.green(findNotes.title))
console.log("Body: "+findNotes.body)
}
else{
console.log("Notes not exist !")
}
}
//save notes to notes.json
const saveNotes =(notess)=>{
const JSONdata = JSON.stringify(notess)
fs.writeFileSync('notes.json',JSONdata)
}
//load notes.json and return object
const loadNotes = ()=>{
try {
const dataBuffer = fs.readFileSync('notes.json')
const dataJSON=dataBuffer.toString()
return JSON.parse(dataJSON)
} catch (e) {
return []
}
}
module.exports={
getNotes:getNotes,
addNotes:addNotes,
removeNotes:removeNotes,
listNotes:listNotes,
readNotes:readNotes
}