Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2.  
  3. var fetchNotes = () => {
  4.     try {
  5.         var noteString = fs.readFileSync('notes-data.json');
  6.         return JSON.parse(noteString);
  7.     } catch (e) {
  8.         return [];
  9.     }
  10. };
  11.  
  12. var saveNotes = (notes) => {
  13.     fs.writeFileSync('notes-data.json', JSON.stringify(notes));
  14. };
  15.  
  16. var addNote = (title, body) => {
  17.     var notes = fetchNotes();
  18.     var note = {
  19.         title,
  20.         body
  21.     };
  22.  
  23.     var duplicatesNotes = notes.filter((note) => note.title === title);
  24.     //console.log( "If you get message your input object was duplicated");
  25.     if(duplicatesNotes.length === 0) {
  26.         notes.push(note);
  27.         saveNotes(notes);
  28.         return note;
  29.     }
  30. };
  31.  
  32. var getAll = () => {
  33.     return fetchNotes();
  34.     console.log(' You Getting all notes from getAll() function');
  35. };
  36.  
  37. var getNote = (title) => {
  38.     var notes = fetchNotes();
  39.     var filteredNotes = notes.filter((note) => note.title === title);
  40.     return filteredNotes[0];
  41.  
  42.     console.log(' You calling a getNote() function to fetch the notes', title);
  43. }
  44.  
  45. var getRemove = (title) => {
  46.     var notes = fetchNotes();
  47.     var filteredNotes = notes.filter((note) => note.title !== title);
  48.     saveNotes(filteredNotes);
  49.  
  50.     // this return statement is evaluating,
  51.     // cause notes and flteredNotes came with a new values
  52.     // and we wanna print out the message in 'remove' command in app.js.
  53.     // if not equal is return to a new filteredNotes
  54.     // if is equal is return a notes.length
  55.     return notes.length !== filteredNotes.length;
  56. }
  57.  
  58. var logNote = (note) => {
  59.     debugger;
  60.     console.log(' -- which is');
  61.     console.log(` Title: ${note.title}`);
  62.     console.log(` body: ${note.body}`);
  63. };
  64.  
  65. module.exports = {
  66.     addNote,
  67.     getAll,
  68.     getNote,
  69.     getRemove,
  70.     logNote
  71. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement