From 63e639fab32c0698e5dcd94c2f0d704387b69c8b Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Sun, 11 Aug 2019 12:56:43 -0600 Subject: [PATCH] Video 22 --- notes-app/app.js | 8 ++++---- notes-app/notes.js | 18 +++++++----------- playground/3-arrow-function.js | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 playground/3-arrow-function.js diff --git a/notes-app/app.js b/notes-app/app.js index ffeecc1..6970465 100644 --- a/notes-app/app.js +++ b/notes-app/app.js @@ -21,7 +21,7 @@ yargs.command({ type: 'string' } }, - handler: function(argv) { + handler(argv) { notes.addNote(argv.title, argv.body); } }); @@ -37,7 +37,7 @@ yargs.command({ type: 'string' } }, - handler: function(argv) { + handler(argv) { notes.removeNote(argv.title); } }); @@ -46,7 +46,7 @@ yargs.command({ yargs.command({ command: 'list', describe: 'List notes', - handler: function() { + handler() { console.log('Listing out all notes'); } }); @@ -55,7 +55,7 @@ yargs.command({ yargs.command({ command: 'read', describe: 'Read a note', - handler: function() { + handler() { console.log('Reading a note'); } }); diff --git a/notes-app/notes.js b/notes-app/notes.js index 1181fd1..976abea 100644 --- a/notes-app/notes.js +++ b/notes-app/notes.js @@ -1,15 +1,13 @@ const fs = require('fs'); const chalk = require('chalk'); -const getNotes = function() { +const getNotes = () => { return 'Your notes...' } -const addNote = function(title, body) { +const addNote = (title, body) => { const notes = loadNotes(); - const duplicateNotes = notes.filter(function(note) { - return note.title === title; - }); + const duplicateNotes = notes.filter((note) => note.title === title); if (duplicateNotes.length === 0) { notes.push({ @@ -24,11 +22,9 @@ const addNote = function(title, body) { } } -const removeNote = function(title) { +const removeNote = (title) => { const notes = loadNotes(); - const notesToKeep = notes.filter(function(note) { - return note.title !== title; - }); + const notesToKeep = notes.filter((note) => note.title !== title); if (notes.length > notesToKeep.length) { console.log(chalk.bgGreen.bold('Note removed!')); @@ -38,12 +34,12 @@ const removeNote = function(title) { } } -const saveNotes = function(notes) { +const saveNotes = (notes) => { const dataJSON = JSON.stringify(notes); fs.writeFileSync('notes.json', dataJSON); } -const loadNotes = function() { +const loadNotes = () => { try { const dataBuffer = fs.readFileSync('notes.json'); const dataJSON = dataBuffer.toString(); diff --git a/playground/3-arrow-function.js b/playground/3-arrow-function.js new file mode 100644 index 0000000..492be7d --- /dev/null +++ b/playground/3-arrow-function.js @@ -0,0 +1,24 @@ +// +// Goal: Create method to get incomplete tasks +// +// 1. Define getTasksToDo method +// 2. Use filter to to return just the incompleted tasks (arrow function) +// 3. Test your work by running the script + +const tasks = { + tasks: [{ + text: 'Grocery shopping', + completed: true + },{ + text: 'Clean yard', + completed: false + }, { + text: 'Film course', + completed: false + }], + getTasksToDo() { + return this.tasks.filter((task) => task.completed === false); + } +} + +console.log(tasks.getTasksToDo())