Created
December 12, 2018 22:14
-
-
Save TrideepLD/fcd14877a054fa667fc021fc17c2b367 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
//We are creating functions so that we can call them in one line for seperate objects and functions to use | |
var fetchNotes = () => { | |
try { | |
var notesString = fs.readFileSync('notes-data.json'); | |
return JSON.parse(notesString); | |
} catch (e) { | |
return [];// This happens when try is an error so there is now smth to catch. | |
} | |
}; | |
var saveNotes = (notes) => { | |
fs.writeFileSync('notes-data.json', JSON.stringify(notes));//(file name, string) | |
}; | |
var addNote = (title, body) => { | |
var notes = fetchNotes(); | |
var note = { | |
title, | |
body | |
}; | |
var duplicateNotes = notes.filter((note) => note.title===title); | |
if (duplicateNotes.length === 0) { | |
notes.push(note); | |
saveNotes(notes); | |
return note; | |
} | |
}; | |
var getAll = () => { | |
return fetchNotes(); | |
}; | |
var getNote = (title) => { | |
var notes = fetchNotes(); | |
var filteredNotes = notes.filter((note) => note.title === title); | |
return filteredNotes[0]; | |
}; | |
var removeNote = (title) => { | |
var notes = fetchNotes(); | |
var filteredNotes = notes.filter((note) => note.title !== title); | |
saveNotes(filteredNotes); | |
return notes.length !== filteredNotes.length; | |
}; | |
var logNote = (note) => { | |
console.log('---'); | |
console.log(`Title: ${note.title}`); | |
console.log(`Body: ${note.body}`); | |
}; | |
module.exports = { | |
addNote: addNote, | |
getAll: getAll, | |
getNote: getNote, | |
removeNote: removeNote, | |
logNote: logNote, | |
}; | |
// module.exports.addNote = () => { | |
// console.log('addNote'); | |
// return 'New note'; | |
// }; | |
// //Method one of writing this down | |
// module.exports.add = function add(a, b) { | |
// return a+b; | |
// }; | |
// //Method two of writing the above | |
// /* | |
// module.exports.add = (a, b) => {return a+b;}; | |
// */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment