-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
80 lines (75 loc) · 2.4 KB
/
Copy pathnotes.js
File metadata and controls
80 lines (75 loc) · 2.4 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
const fs = require('fs')
const chalk = require('chalk')
const addNotes = function(title,body){
const notes = getNotes()
const duplicateNotes = notes.filter( (note) => title===note.title )
if(duplicateNotes.length===0){
notes.push({
title : title,
body : body
})
fs.writeFileSync('notes.json',JSON.stringify(notes))
console.log(chalk.inverse.green("Note was Added"))
}
else{
console.log(chalk.inverse.red('Title already taken'))
}
}
const getNotes = function(){
try{
const notesBuffer = fs.readFileSync('notes.json')
const notesString = notesBuffer.toString()
return JSON.parse(notesString)
}
catch(error){
return []
}
}
const removeNotes = function(title){
const notes = getNotes()
if(notes.length === 0){
console.log(chalk.inverse.red("No notes present"))
}else{
const duplicateNotes = notes.filter((note) => title!==note.title)
if(duplicateNotes.length===notes.length){
console.log(chalk.inverse.red("Note with given title not present"))
}
else{
fs.writeFileSync('notes.json',JSON.stringify(duplicateNotes))
console.log(chalk.inverse.green("Note with title: "+ title+" was deletedgit "))
}
}
}
const listNotes = function(){
const notes = getNotes()
if(notes.length === 0){
console.log(chalk.inverse.red("No notes present"))
}else{
notes.forEach(note => {
console.log(chalk.inverse.blue("Note title: "+ note.title))
console.log(chalk.inverse.blue("Note : "+ note.body))
});
}
}
const readNotes = function(title){
const notes = getNotes()
if(notes.length === 0){
console.log(chalk.inverse.red("No notes present"))
}else{
const duplicateNotes = notes.filter((note) => title===note.title)
if(duplicateNotes.length===1){
console.log(chalk.inverse.blue(duplicateNotes[0].title))
console.log(chalk.inverse.blue(duplicateNotes[0].body))
}
else{
console.log(chalk.inverse.red("No note was present with given title"))
}
}
}
module.exports = {
getNotes : getNotes,
addNotes : addNotes,
removeNotes : removeNotes,
listNotes : listNotes,
readNotes : readNotes
}