-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (57 loc) · 1.4 KB
/
app.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
const fs = require("fs");
const chalk = require("chalk");
const getNotes = () => {
return "Get Notes";
};
const addNote = (header, body) => {
const notes = loadNote();
const duplicateNote = notes.filter(e => e.title === header);
if (duplicateNote.length === 0) {
notes.push({
title: header,
body: body
});
saveNote(notes);
console.log(chalk.green.inverse("New Note Added"));
} else {
console.log(chalk.red.inverse("Duplicate Note!!"));
}
};
const removeRemove = header => {
const readNote = loadNote();
const duplicateNote = readNote.filter(function(e) {
return e.title !== header;
});
if (duplicateNote.length !== 0) {
saveNote(duplicateNote);
} else {
console.log("Not duplicate");
}
};
const readNote = title => {
const readNote = loadNote();
const noteList = readNote.find(note => note.title === title);
if (!noteList) {
chalk.red.inverse(console.log("Not Data found"));
} else {
chalk.green.inverse(console.log(noteList));
}
};
const saveNote = function(notes) {
fs.writeFileSync("file.json", JSON.stringify(notes));
};
const loadNote = function() {
try {
const readfile = fs.readFileSync("file.json");
const json = JSON.parse(readfile.toString());
return json;
} catch (e) {
return [];
}
};
module.exports = {
getNotes: getNotes,
addNote: addNote,
removeRemove: removeRemove,
readNote: readNote
};