converting csv spreadsheets to json using node.js with the csvtojson npm package

I thought about maybe making some projects where I work with some data from my google analytics statistics. Grabbing the data is simple enough, but there is no option to download the data in *.json format, but there is an option for *.csv. So all I need is some kind of tool to help me convert data in a *.csv to *.json. I prefer to do just about everything with node.js, and as such I was able to come across something called csvtojson which is one user space solution for converting a csv file to json.

Of course it would also be nice to have some easy to use solution for going in the other direction also, that is converting JSON to csv. As much as I like json it does have its draw backs compared to some other options such as with yaml that supports comments when makes it a better option for config files. However when it comes to the nature of this post it is nice to create csv files rather than json files for some things because that is a good standard to allow for data to be opened up in spread sheet programs.

Read More

The node.js process global for positional arguments environment variables and much more

The process global in node.js is helpful for getting information on, and control over, the current process of a script. When making node.js applications chances are you are going to use at least some of it’s properties to grab at environment variables, and positional arguments given from the command line when a script is called with node in the command line.

However there is even more to it on top of just getting positional arguments, and environment variables, such as things like piping data into a nodejs script from another application in the command line when calling the script. On top of piping data into a script from the standard input, there is also having better control over the standard output of a script beyond that of using the console.log method. The console.log method will work okay, but one little drawback of console.log is that it will always append a line break to output each time it is used.

In addition it can also be used to set some event handlers to give control over the process. For example I can set up an event handler that will do something each time some data is given to a script from the standard output. So then it is called for to write not just one post, but a few posts actually on the topic of the nodejs process global. In this post I will be just doing a general overview of the global without getting into any one feature in depth.

Read More

The lodash findIndex array method and vanilla javaScript alternatives

The _.findIndex array method in lodash can be used to find the first index of an element in an Array that meets a specific condition. In modern browsers there is now Array.prototype.findIndex that works in very much the same manor as _.findIndex. So that would make the lodash find index method yet another one of those lodash methods that you might only bother with for the sake of backward compatibility with older browsers, or just for the sake of consistency if you are using lodash in a project. Yet again maybe not, it seems that the lodash alternatives often do have a little more going on with them, in addition there are additional helper methods that can be used with _.findIndex that come in handy. So maybe I should not be to quick to judge a lodash method such as the lodash find index method, as many of these methods are not just referencing native methods, although some of them are.

In this post then I will be going over a few quick examples of the lodash find index method, and then get into some additional examples that have to do with using native javaScript for getting one or more index values in an array that meet a given condition.

Read More

Working with the file system module in node.js

Working with files is a big part of most node.js projects. I have written a post on fs-extra a while back, but so far never got around to the core file system module in node.js itself.

Depending on the version range of node you wish to support, you might not need to make a user space module that extends the file system module part of the stack. For example if you want file system methods that return promises there is the nodejs build in util promisify method that can be used to get that effect fairly quick, and easy. If you want more than just that maybe you still do need to bother with something more, in any case in this post I will be going vanilla javaScript style when it comes to file io tasks.

This post will also serve as a general overview of the file system module, and I will link to additional posts on more specific topics where doing so is called for.

Read More