-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.js
More file actions
103 lines (85 loc) · 2.15 KB
/
Copy pathnote.js
File metadata and controls
103 lines (85 loc) · 2.15 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
const fs = require("fs");
const chalk = require("chalk");
// add note function
const addNote = function (title, body) {
const notes = loadNotes();
// const duplicateNotes = notes.filter(function (note) {
// return note.title === title;
// });
// 2nd one is efficient way we can use arrow function also to reduce the code
const duplicateNotes = notes.find(function (note) {
return note.title === title;
// console.log(duplicateNotes);
});
// if (duplicateNotes.length === 0)
if (!duplicateNotes) {
notes.push({
title: title,
body: body,
});
// console.log(notes);
saveNotes(notes);
console.log("Notes added");
} else {
console.log("title taken");
}
};
const saveNotes = function (notes) {
const dataJson = JSON.stringify(notes);
fs.writeFileSync("note.json", dataJson);
};
const loadNotes = function () {
try {
const data = fs.readFileSync("note.json");
const dataJSON = data.toString();
return JSON.parse(dataJSON);
} catch (e) {
return [];
}
};
// remove note functionality
const removeNote = function (title) {
// console.log(title);
const notes = loadNotes();
const notesTokeep = notes.filter(function (note) {
return note.title !== title;
});
if (notes.length > notesTokeep.length) {
console.log(chalk.green.inverse("Note removed"));
} else {
console.log(chalk.red.inverse("NO note found"));
}
saveNotes(notesTokeep);
};
//list notes
const listNotes = () => {
const notes = loadNotes();
const allNote = notes.forEach((note) => {
console.log(note.title);
});
if (!allNote) {
console.log(chalk.red.inverse("Note note to show"));
} else {
console.log(allNote);
console.log(chalk.inverse("Your Notes are"));
}
};
// read note
const readNote = (title) => {
const notes = loadNotes();
const note = notes.find((note) => {
return note.title === title;
});
if (note) {
console.log(chalk.inverse(note.title));
console.log(note.body);
} else {
console.log(chalk.red.inverse("Notes not found"));
}
};
module.exports = {
addNote: addNote,
removeNote: removeNote,
listNotes: listNotes,
readNote: readNote,
};