Nelson homework week2 #16
base: class-5
Are you sure you want to change the base?
Nelson homework week2 #16
Conversation
@BadgerBadgerBadgerBadger: https://docs.npmjs.com/specifying-dependencies-and-devdependencies-in-a-package-json-file) I solved it. Sorry |
@NelsonNaja comments from using the program:
I'll have a look at the code once the above issues are fixed. |
@@ -0,0 +1,37 @@ | |||
'use strict'; | |||
const sendResponse = require('./sendResponse'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 Sorry I'm being so particular, but if you're gonna do it well, might as well do it perfectly. |
There was a problem hiding this 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?
week2/homework/src/writeFile.js
Outdated
} | ||
|
||
function convertJsonToCVS(information) { | ||
let informationCVS = ''; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
week2/homework/src/writeFile.js
Outdated
}); | ||
} | ||
|
||
function convertJsonToCVS(information) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
week2/homework/src/writeFile.js
Outdated
information.forEach((eachline, index) => { | ||
let stringInformation = JSON.stringify(eachline.activity); | ||
if (stringInformation !== '' && stringInformation !== null) { | ||
informationCVS = informationCVS + (index + 1) + ',' + stringInformation + '\r\n'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
week2/homework/src/writeFile.js
Outdated
if (information !== '') | ||
informationToWrite = convertJsonToCVS(information); | ||
|
||
fs.writeFileSync('to-do.csv', informationToWrite, (err) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NelsonNaja Why sync functions?
There was a problem hiding this comment.
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.
week2/homework/src/writeFile.js
Outdated
const fs = require('fs'); | ||
|
||
function createFile() { | ||
fs.appendFileSync('./to-do.csv', '', 'utf8'); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BadgerBadgerBadgerBadger: My error.
process.stdin.on('readable', () => { | ||
let inData = process.stdin.read(); | ||
if (inData !== null) { | ||
let answerFromUser = inData.split('').splice(0, 3).join(''); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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;
}
}
@Buccaneer @BadgerBadgerBadgerBadger: Thanks for your comments. I really apreciate your time and recomendations. |
I finished the two assignments: