Skip to content
This repository has been archived by the owner on Jul 17, 2022. It is now read-only.

Nelson homework week2 #16

Open
wants to merge 10 commits into
base: class-5
Choose a base branch
from

Conversation

nelnaj
Copy link

@nelnaj nelnaj commented Sep 3, 2019

I finished the two assignments:

  1. Refactor your homework from last week
  2. Write a command line application with a todo list.
    • I used commander in order to learn others libraries but I tried to use the process.argv at least one time.
    • I worked with json but write cvs file (I made functions to transform the data).
    • I tried to split the actions.

@BadgerBadgerBadgerBadger
Copy link
Collaborator

@NelsonNaja
image
Think of why I might be getting this error.

@nelnaj
Copy link
Author

nelnaj commented Sep 4, 2019

@BadgerBadgerBadgerBadger:
A little problem with the package :-)

https://docs.npmjs.com/specifying-dependencies-and-devdependencies-in-a-package-json-file)

I solved it. Sorry

@BadgerBadgerBadgerBadger
Copy link
Collaborator

@NelsonNaja comments from using the program:

  • When I type node . hel, I get no response.
➜  homework git:(nelson-homework-week2) ✗ node . add required activity

You activity was added to the list.!

➜  homework git:(nelson-homework-week2) ✗ node . l


     ID                      Activity
   ══════════════════════════════════════════════════════════════
     1                required
   ══════════════════════════════════════════════════════════════


  • how could you have handled this case?
➜  homework git:(nelson-homework-week2) ✗ node . re

WARNING!! - If you reset the file, you will lost all your information !!!!
write yes or YES to continue or type any to cancel

/Users/shak/projects/others/hyf/Nelson/Node.js/week2/homework/src/storeOperations.js:36
    let answerFromUser = process.stdin.read().split('').splice(0, 3).join('');
                                             ^

TypeError: Cannot read property 'split' of null
    at ReadStream.process.stdin.on (/Users/shak/projects/others/hyf/Nelson/Node.js/week2/homework/src/storeOperations.js:36:46)
    at emitNone (events.js:106:13)
    at ReadStream.emit (events.js:208:7)
    at emitReadable_ (_stream_readable.js:513:10)
    at emitReadable (_stream_readable.js:507:7)
    at ReadStream.Readable.read (_stream_readable.js:384:7)
    at nReadingNextTick (_stream_readable.js:797:8)
    at _combinedTickCallback (internal/process/next_tick.js:136:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)
    at Function.Module.runMain (module.js:696:11)
  • the warning is nice, but why the extra error?

I'll have a look at the code once the above issues are fixed.

@@ -0,0 +1,37 @@
'use strict';
const sendResponse = require('./sendResponse');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code from the sendResponse module is used directly in getStore to send a JSON response. As a result if you wanted to use the getStore module to access the state from the command line (instead of via http) you'd have to rewrite that module.

This beats the purpose of splitting code in modules: the idea is that the getStore module is only concerned with the state and the sendResponse only with sending a response. If you need to both access state and send a response a third module can require these two modules and combine their functionalities: in your solution that would be the handleRequest module.

This way you leave the possibility open that if tomorrow you wanted to write a CLI you could reuse getStore without altering any code there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Buccaneer Thanks for your suggestion. I modified my code and I already understand the idea.. :-) thanks a lot.

@nelnaj
Copy link
Author

nelnaj commented Sep 4, 2019

@BadgerBadgerBadgerBadger :

So sorry I swear that before I sent my homework I believed I had thought in everything. :-)

For the first one I found this tj/commander.js#531 but It doesn't work instead I made my own functions, maybe there is another way easier. I dont know.

@BadgerBadgerBadgerBadger
Copy link
Collaborator

@BadgerBadgerBadgerBadger :

So sorry I swear that before I sent my homework I believed I had thought in everything. :-)

For the first one I found this tj/commander.js#531 but It doesn't work instead I made my own functions, maybe there is another way easier. I dont know.

Still something missing. I see that when I type node . hel I get the help screen. But what I was trying to imply is, what happens if I type in random commands? If i type in node . lil, again, no response. Your application should never not respond. Think of what you can do about that.

Sorry I'm being so particular, but if you're gonna do it well, might as well do it perfectly.

@nelnaj
Copy link
Author

nelnaj commented Sep 4, 2019

I really appreciate your comments to me it is a good challenge. ;-)

Copy link
Collaborator

@BadgerBadgerBadgerBadger BadgerBadgerBadgerBadger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I noticed is that you're storing your data as a csv, correct? Then why bother having a json step at all?

}

function convertJsonToCVS(information) {
let informationCVS = '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja Don't forget those newlines.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BadgerBadgerBadgerBadger: I thought in divide the data in two levels of abstractions, in the first level work with an array of Json objects to work in the different operations into the system and second level write a file csv like a storage (because if someone wants to see his data he can open excel or other similar program and he can see the data in a table).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your though of using CSV is valid, but in that case there is not need for the JSON abstraction. It doesn't serve any purpose, right?

});
}

function convertJsonToCVS(information) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja Do you mean, perhaps, CSV?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see :-) , Typing error. Done.

information.forEach((eachline, index) => {
let stringInformation = JSON.stringify(eachline.activity);
if (stringInformation !== '' && stringInformation !== null) {
informationCVS = informationCVS + (index + 1) + ',' + stringInformation + '\r\n';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja Prefer the informationCVS += style, here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes It's better. Done

if (information !== '')
informationToWrite = convertJsonToCVS(information);

fs.writeFileSync('to-do.csv', informationToWrite, (err) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja Why sync functions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My error I only copied the function from the documentation, I forgot the asyncronous one. But with reader async I couldn't. But the write it's done.

const fs = require('fs');

function createFile() {
fs.appendFileSync('./to-do.csv', '', 'utf8');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja Why append and not write?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BadgerBadgerBadgerBadger: Sorry I thought that append functions was for create files and write only to write. Done.

}

function add(informationToAdd) {
let information = readFile.read();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja readFile already sounds like an action. Hence I expected it to be callable. But if you're gonna do readFile.read(), then shouldn't it be fileReader.read()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

function add(informationToAdd) {
let information = readFile.read();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja Any reason it's let instead of const?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.stdin.on('readable', () => {
let inData = process.stdin.read();
if (inData !== null) {
let answerFromUser = inData.split('').splice(0, 3).join('');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja Add a comment to explain what you're doing here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BadgerBadgerBadgerBadger: inData I have a string, with split I make an array as a result my array is [ 'y', 'e', 's', '\r', '\n' ], with splice (0,3) I get [ 'y', 'e', 's'] and finally with join() I return a new string to use.

let inData = process.stdin.read();
if (inData !== null) {
let answerFromUser = inData.split('').splice(0, 3).join('');
if (answerFromUser === 'YES' || answerFromUser === 'yes') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja a better way of doing this is to lowercase it first and then just compare it against that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BadgerBadgerBadgerBadger: Yes it is better. Done


function update(index, newUpdatedActivity) {
let information = readFile.read();
if (messages.isAValidIndex(Number(index), information.length)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NelsonNaja What happens if it's not a valid index?

Copy link
Author

@nelnaj nelnaj Sep 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BadgerBadgerBadgerBadger: The function show the next messages:

function isAValidIndex(index, sizeArray) {
if (typeof (index) === 'number' && !isNaN(index)) {
if (index > 0 && index <= sizeArray) {
return true;
}
else {
console.log('\x1b[31m');
console.log(You activity doens't exist, please write 'list' command);
console.log('\x1b[0m');
return false;
}
}
else {
console.log('\x1b[31m');
console.log('You must write a number');
console.log('\x1b[0m');
return false;
}
}

@nelnaj
Copy link
Author

nelnaj commented Sep 6, 2019

@Buccaneer @BadgerBadgerBadgerBadger: Thanks for your comments. I really apreciate your time and recomendations.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants