-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
76 lines (63 loc) · 1.8 KB
/
notes.js
File metadata and controls
76 lines (63 loc) · 1.8 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
const fs = require('fs');
const chalk = require('chalk');
const readNotes = () => {
const fileData = fs.readFileSync('./notes.json').toString();
const jsonData = JSON.parse(fileData);
return jsonData;
};
const saveNotes = (notes) => {
const dataString = JSON.stringify(notes);
fs.writeFileSync('./notes.json', dataString);
};
const addNote = (title, body) => {
const notes = readNotes();
const titleIndex = notes.find(note => note.title === title);
if (!titleIndex) {
notes.push({
title,
body
});
saveNotes(notes);
console.log(chalk.green('Note added!'));
} else {
console.log(chalk.red('Title already exist, Try with different title.'));
}
};
const removeNote = (title) => {
const notes = readNotes();
const noteIndex = notes.findIndex(note => note.title === title);
if (noteIndex > -1) {
notes.splice(noteIndex, 1);
saveNotes(notes);
console.log(chalk.green(`Note with title: ${title} removed.`));
} else {
console.log(chalk.red(`Unable to find note with title: ${title}.`));
}
};
const listNotes = () => {
const notes = readNotes();
if (notes.length > 0) {
console.log(chalk.blue.inverse('Your Notes'));
notes.forEach(note => {
console.log(note.title);
});
} else {
console.log(chalk.red('No notes found!'));
}
};
const readNote = (title) => {
const notes = readNotes();
const note = notes.find(note => note.title === title);
if (note) {
console.log(chalk.bold.blue(note.title));
console.log(note.body);
} else {
console.log(chalk.red(`Unable to find note with title: ${title}.`));
}
};
module.exports = {
addNote,
removeNote,
listNotes,
readNote
};