-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
74 lines (59 loc) · 1.89 KB
/
notes.js
File metadata and controls
74 lines (59 loc) · 1.89 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
import chalk from "chalk";
import FileOperations from "./fileOperations.js";
class Notes {
constructor() {
this.logSuccess = chalk.green;
this.logError = chalk.red;
this.logWarning = chalk.hex("#FFA500");
this.logTitle = chalk.bold.blue;
}
capitalizeFirstLetter(str) {
return str.replace(/\b\w/g, function (char) {
return char.toUpperCase();
});
}
addNote(noteTitle, body) {
const title = this.capitalizeFirstLetter(noteTitle);
const notes = FileOperations.loadNotes();
const duplicateNote = notes.find((note) => note.title === title);
if (duplicateNote) {
console.log(this.logError("Note Title already taken!"));
return;
}
notes.push({ title, body });
FileOperations.saveNotes(notes);
console.log(this.logSuccess("New Note added successfully!"));
}
removeNote(noteTitle) {
const title = this.capitalizeFirstLetter(noteTitle);
let notes = FileOperations.loadNotes();
const filteredNotes = notes.filter((note) => note.title !== title);
if (notes.length === filteredNotes.length) {
console.log(this.logError("No Note Found!"));
return;
}
FileOperations.saveNotes(filteredNotes);
console.log(this.logSuccess("Note successfully deleted!"));
}
listNotes() {
const notes = FileOperations.loadNotes();
if (notes.length === 0) {
console.log(this.logWarning("No Note Found to List!"));
return;
}
console.log(this.logTitle("Your Notes:"));
notes.forEach((note) => console.log(note.title));
}
readNote(noteTitle) {
const title = this.capitalizeFirstLetter(noteTitle);
const notes = FileOperations.loadNotes();
const note = notes.find((note) => note.title === title);
if (!note) {
console.log(this.logError("No Note Found!"));
return;
}
console.log(this.logTitle(note.title));
console.log(note.body);
}
}
export default Notes;