-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
129 lines (108 loc) · 3.58 KB
/
notes.js
File metadata and controls
129 lines (108 loc) · 3.58 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
console.log('-> Starting notes.js');
const fs = require('fs');
const dateFormat = require('dateformat');
//We define a variable to catch the current date
var day = dateFormat(new Date(), "yyyy-mm-dd h:MM:ss");
var jsonFile = 'notes.JSON';
var saveNotes = (note) => {
fs.writeFileSync(jsonFile,JSON.stringify(note));
}
var readJson = (jsonFile) => {
try {
var originalJson = fs.readFileSync(jsonFile);
//I parse the TXT in a JSON Obj
var notes = JSON.parse(originalJson);
return notes;
} catch (e) {
console.log('JSON files does´t exist. JASON File created from scratch');
}
};
//ADD Note Function
var addNote = (title,body) => {
//I create an empty array to fill with the Json:
var notes = [];
//I format a variable to show what I have inserted
var newNote = {
title: title,
body: body,
day: day
};
//We try to open the JSON files
var notes = readJson(jsonFile);
//Check the duplicate based on the title
var duplicateNotes = notes.filter((item) => item.title===title);
//If i do not find note with the given title
if(duplicateNotes.length === 0){
//If not duplicates found, I push the note in the array
notes.push(newNote);
//Save the note
saveNotes(notes);
//I print out the added note
console.log('Added the note: Title-> '+JSON.stringify(newNote));
}else{
//If duplicates found, I return the alert
console.log('Found already -> '+duplicateNotes.length+' with the tile -> '+title);
console.log('Note will be not inserted because duplicated!');
}
};
//Read all notes function
var fetchNotes = function(err){
if(err) throw err;
var results = [];
var n=0;
console.log('List of all:');
try {
var results = JSON.parse(fs.readFileSync(jsonFile));
results.forEach(function(item) {
n++;
console.log('My Note in JSON file-> '+n+') Title:'+item.title+' - Body:'+item.body+' - Created at: '+item.day);
});
} catch (e) {
console.log('JSON files does´t exist or it is empty!');
}
};
//Remove a note
var removeNote = (title) => {
console.log('Try to remove note with Title = '+title);
var notes = readJson(jsonFile);
var foundNotes = notes.filter((item) => item.title===title);
if(foundNotes.length === 0){
console.log('No notes found to be removed');
}else{
finalNotes = notes.filter((item) => item.title!==title);
console.log(finalNotes);
saveNotes(finalNotes);
console.log('Note Removed!');
}
};
//Read one note/notes based on title
var fetchNote = function(title,err){
if(err) throw err;
//We try to open the JSON files
try {
var originalJson = fs.readFileSync('notes.JSON');
//I parse the TXT in a JSON Obj
var notes = JSON.parse(originalJson);
} catch (e) {
console.log('JSON files does´t exist. JASON File created from scratch');
}
//we filter the found note:
var foundNotes = notes.filter((item) => item.title===title);
//we print out the notes found:
if(foundNotes.length > 0){
console.log('Found number of duplicates -> '+foundNotes.length);
if(foundNotes.length===1){
console.log('Found note -> '+JSON.stringify(foundNotes));
}else{
console.log('Found notes -> '+JSON.stringify(foundNotes));
}
} else {
console.log('No notes found');
}
};
module.exports = {
addNote,
fetchNotes,
removeNote,
fetchNote
};