jaseew01 / multi-user-file-manager

0 stars 0 forks source link

More Node practice: Line of Code count #9

Closed skiadas closed 10 years ago

skiadas commented 10 years ago

Write a Javascript script that:

jaseew01 commented 10 years ago

Could you take a look at the last commit that I made? I am getting the following error about mime: Type error Object false has no method 'replace'. I installed it on my machine using the command npm install mime.

skiadas commented 10 years ago

Hm I'm confused, why are you using mime? Wouldn't it be simpler to look for the extension?

In any case, I think the argument to mime.lookup needs to be a path to the filename. It looks to me you are feeding it the contents of the files?

Is this the package: https://github.com/broofa/node-mime

jaseew01 commented 10 years ago

I got rid of mime, now I'm just splitting the file name by the '.' and accessing the second item in the list that the .split(',') returns.

jaseew01 commented 10 years ago

Use Regular Expressions to omit commented lines and sections

jaseew01 commented 10 years ago

Could you check out the latest commit for this issue? printJsonObject() is supposed to be getting called after everything has been added to the jsonObjects array. I am using the same eventEmitter that we did for the file traverser, but it seems that done is getting called before the entire process is complete.

skiadas commented 10 years ago

I would try two things:

  1. First, add a console log right before: myTraverser.on("done",printJsonObject); To tell you what printJsonObject is at this point. I suspect it might be undefined. If that is the case, then you will want to move the definition of printJsonObject further up, before its use.
  2. Add a console log into your FileTraversal.prototype.done method to see if it actually fires up. Also add logs in the methods that increment and decrement the counter.

Haris Skiadas Hanover College

On Oct 13, 2014, at 9:38 PM, jaseew01 notifications@github.com wrote:

Could you check out the latest commit for this issue? printJsonObject() is supposed to be getting called after everything has been added to the jsonObjects array. I am using the same eventEmitter that we did for the file traverser, but it seems that done is getting called before the entire process is complete.

— Reply to this email directly or view it on GitHub.

jaseew01 commented 10 years ago

Figured it out, I wasn't incrementing and decrementing inside of the countLines() function.

jaseew01 commented 10 years ago

Could you check out the last commit for this issue? Am I writing the Regular Expressions incorrectly within the countLines() functions?

skiadas commented 10 years ago

You cannot match regular expressions with a simple equality. These are your options:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Working_with_Regular_Expressions

I typically use String.prototype.match, for example:

"hi there".match(/[he]+/)

The object returned would either be null if there was no match, or an object containing all the captured subexpressions, if you used parentheses to capture parts.

So notice first of all, that you typically create regular expressions by putting them between forward slashes. That page, and the corresponding match page documentation, should get you going:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

Haris Skiadas Hanover College

On Oct 14, 2014, at 8:38 PM, jaseew01 notifications@github.com wrote:

Could you check out the last commit for this issue? Am I writing the Regular Expressions incorrectly within the countLines() functions?

— Reply to this email directly or view it on GitHub.

jaseew01 commented 10 years ago

I can't find anything online that mentions how to represent the '*' character within a Regex object. It stands for 0 or more when in the expression, but I just want it to match to the asterisk character so that I may find commented sections.

skiadas commented 10 years ago

Escaping characters is usually done with a blackslash in front, so \* would match a literal asterisk. For example: "ab*c".match(/\*c/);

jaseew01 commented 10 years ago

Also, I am having trouble getting my second regex expression to match blank lines. I tried using this: [\s\t]* but it doesn't seem to be working. I also tried putting in a condition for if the lines length is null or 0, it won't get counted, but that didn't seem to work either. Any ideas?

skiadas commented 10 years ago

So, the problem is that an expression can match if it matches any part of a string. So try this for instance:

"hithere".match(/\s*/);

You would expect this not to match, but it does. Because you are asking it to match “0 or more spaces”, and it can match 0 spaces. In fact that expression would always match. (Btw get in the habit of using those forward slashes to define regular expressions, instead of new RegEx.

What you want is to specify that the entire line, from start to finish, contains only whitespaces. The way to do it would be to add in the regular expression testing for beginning and end of line. So if you use:

/^[\s\t]*$/

The caret indicates that you want to match at the beginning of the string, then you would have any number of whitespaces or tabs, and then the dollar sign indicates matching the end of the string. You might need to also allow for an optional newline character at the end, not sure if the way you have the lines contains it or not.

The tables in: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp have a lot of these and more, admittedly perhaps too much more. The caret and dollar sign are in the “boundaries” table.

Haris Skiadas Hanover College

On Oct 15, 2014, at 2:38 AM, jaseew01 notifications@github.com wrote:

Also, I am having trouble getting my second regex expression to match blank lines. I tried using this: [\s\t]* but it doesn't seem to be working. I also tried putting in a condition for if the lines length is null or 0, it won't get counted, but that didn't seem to work either. Any ideas?

— Reply to this email directly or view it on GitHub.

jaseew01 commented 10 years ago

Concerning the last unchecked item, I am having trouble figuring out how to handle this. One thing that I thought of doing was to add a callback function to the line where I call processFile() within my traverse() function. If this is possible, how would I get the value that the processFile() function returns into a paramter of the callback function?

skiadas commented 10 years ago

Doesn't your process file at some point do an "emit" of file information? You can get a separate part of the program to listen for that, then store the information on some sort of object. Then it will also listen for "done", and then print stuff. Does that make sense? Or did I misunderstand your question?

jaseew01 commented 10 years ago

I was thinking on doing something like that, but I was wondering if it was at all possible to add a callback to a call to a function that I created

skiadas commented 10 years ago

Yep it's your function after all, you can arrange it to take a callback, and for when it should call it.

That way of course there's only one callback, you can't set up multiple listeners like with events. But it's otherwise a very good approach.