-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.js
110 lines (74 loc) · 2.02 KB
/
notes.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
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
const fs = require('fs');
var fetchNotes = () => {
try{
// this part will allow us to keep adding notes without
// removing whats already there
var notesString = fs.readFileSync('notes-data.json');
//return fs.readFileSync('notes-data.json');
return JSON.parse(notesString);
}catch(e){
notes = [];
}
};
var saveNotes = (notes) => {
fs.writeFileSync('notes-data.json' , JSON.stringify(notes));
};
var addNote = (title ,body) => {
// empty array
var notes = fetchNotes();
// represents a new note
var note = {
title,
body
};
// filtering for duplicate notes
var duplicateNote = notes.filter((note) => {
return note.title === title;
});
if(duplicateNote.length === 0){
// pushing the new note to the notes
notes.push(note);
saveNotes(notes);
return note;
}
};
var getAll = () => {
return fetchNotes();
};
var getNote = (title) => {
console.log("Reading : " , title)
// fetch notes
var notes = fetchNotes();
//notes.filter to only return notes that match the argument passed in
var filterNotes = notes.filter((note) => {
return note.title === title;
});
// return the first item in the array
return filterNotes[0];
};
var removeNotes = (title) => {
// fetch the notes
var notes = fetchNotes();
// filter out notes, removing the one with title of argument
var filterNotes = notes.filter((note) => {
return note.title !== title;
});
// save new notes array
saveNotes(filterNotes)
// if true then we assume a note was removed
// if false then we assume that a note was not removed
return notes.length !== filterNotes.length;
}
var logNote = (note) => {
console.log('--');
console.log(`Title: ${note.title}`);
console.log(`Body: ${note.body}`);
};
module.exports = {
// this is ES6 syntax
addNote,
getAll,
getNote,
removeNotes,
logNote
};