-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
65 lines (55 loc) · 1.48 KB
/
notes.js
File metadata and controls
65 lines (55 loc) · 1.48 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
const fs = require('fs');
const chalk = require('chalk');
const getNotes = () => {
return "All your notes";
}
const addNote = (title, body) => {
const notes = loadNotes();
const duplicateNotes = notes.filter((note) => note.title === title)
if(duplicateNotes.length === 0) {
notes.push({
title: title,
body: body
});
saveNotes(notes);
console.log(chalk.green.inverse('New Note added!'));
} else {
console.log(chalk.red.inverse('Title already taken!'));
}
}
const removeNote = (title) => {
const notes = loadNotes();
const unremovedNotes = notes.filter((note) => note.title !== title);
if(notes.length === unremovedNotes.length) {
console.log(chalk.red.inverse('No Note Found!'));
}else {
console.log(chalk.green.inverse('Note Removed!'));
}
saveNotes(unremovedNotes);
}
const saveNotes = (notes) => {
const notesJSON = JSON.stringify(notes);
fs.writeFileSync('notes.json', notesJSON);
}
const loadNotes = () => {
try {
const dataBuffer = fs.readFileSync('notes.json');
const notesJSON = dataBuffer.toString();
return JSON.parse(notesJSON);
} catch(e) {
return [];
}
}
const listNotes = () => {
console.log(chalk.blue("Your Notes :"));
const notes = loadNotes();
notes.forEach((note) => {
console.log(note.title);
});
}
module.exports = {
getNotes,
addNote,
removeNote,
listNotes
}