-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.js
104 lines (84 loc) · 2.72 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
94
95
96
97
98
99
100
101
102
103
104
const fs = require("fs");
const chalk = require("chalk");
/* ****************************************************
========= FUNCTION FOR ADDING NOTE ==================
*****************************************************/
const addNote = (title, body) => {
const notes = loadNotes();
const duplicateNote = notes.find(note =>
note.title === title ? true : false
);
if (!duplicateNote) {
notes.push({
title: title,
body: body
});
saveNote(notes);
console.log(chalk.inverse.green("New Note Added!"));
} else {
console.log(chalk.inverse.red("Note title taken!"));
}
};
/* ****************************************************
========= FUNCTION FOR GETTING NOTE =================
*****************************************************/
const readNotes = title => {
const notes = loadNotes();
const findTitle = notes.find(note => note.title === title);
if (findTitle) {
console.log(chalk.inverse.green(`${findTitle.title}`));
console.log(findTitle.body);
} else {
console.log(chalk.inverse.red('No Note Found!!!'));
}
};
/* ****************************************************
========= FUNCTION FOR LISTING NOTE =================
*****************************************************/
const listNotes = () => {
const notes = loadNotes();
console.log(chalk.inverse.yellow("Your Notes"));
notes.forEach(titleName => console.log(titleName.title));
};
/* ****************************************************
========= FUNCTION FOR REMOVING NOTE ================
*****************************************************/
const removeNote = title => {
const notes = loadNotes();
const newNotes = notes.filter(note => note.title !== title);
if (newNotes.length === notes.length) {
console.log(chalk.inverse.red(`No Note Found With Title: ${title}`));
} else {
saveNote(newNotes);
console.log(chalk.inverse.green(`Note Removed With Title: ${title}`));
}
};
/* **************************************** ************
========= FUNCTION FOR LOADING NOTES ================
*****************************************************/
const loadNotes = () => {
try {
const dataBUffer = fs.readFileSync("notes.json");
const dataJSON = dataBUffer.toString();
return JSON.parse(dataJSON);
} catch (err) {
return [];
}
};
/* ****************************************************
========= FUNCTION FOR SAVING NOTE ==================
*****************************************************/
const saveNote = notes => {
try {
const stringifyJson = JSON.stringify(notes);
fs.writeFileSync("notes.json", stringifyJson);
} catch (error) {
console.error(error);
}
};
module.exports = {
addNote,
removeNote,
readNotes,
listNotes
};