_.merge in lodash as a means to recursively merge down objects.

These days I have been exploring all the options out there when it comes to merging down two or more objects into a single object. There are many ways to go about doing it that have different effects, there is the idea of just copying over key values, or just referencing them even. However it most cases I often want to merge them down like that of a bunch of sheets of acetate rather than that of paper. That is if a certain value is in one object, but not any other, it is the value that will end up in the final object. That is the effect that is achieved when using the lodash _.merge method.

Read More

Making a path that may or may not be there in node.js thanks to mkdirp

When making some kind of CLI tool in node.js there is often a need to test if a certain path exists, and if it does do nothing, else make the path. There are ways of doing so with just plain old vanilla javaScript in node.js itself, there are also many user land projects that bring this functionality along with much more to node.js such as with fs-extra. However if you are looking for a user land npm package that just provides this kind of functionality to node.js then there is the npm package mkdirp.

Read More

Merging two or more objects down on top of each other in node.js using object-merge

When making any kind of CLI tool, or project with node.js I am often faced with a situation in witch I need to merge down two or more objects into one, and do so recursively. There are many options for doing so, but there is also doing so it a way in which I do not alter the source objects, and also clone the objects rather than just simple copying references.

Although there might be a method in a framework I am using such as with the lodash merge method, or maybe even something native that is getting added in, to help with this, I have found another stand alone package for this of course. That package is called object-merge, and it works great for this.

Read More

Getting started making a CLI tool with node.js

So because I have been messing around with node.js a whole lot more lately, and have written a number of posts on nodejs it including the various built in modules as well as many npm packages. The thing about nodejs is that it is very much sever side javaScript, or general programing style javaScript if you prefer, and as such it is important to know how to make a script file into something that can be used as a stand alone command that can be executed in a command line such as bash. So I thought I would put a post together to help remind me of the few steps to make in order to make a global CLI tool in javaScript with node.js.

The basic process is that I just need to have a nodejs shebang at the top of a javaScript file that will be the main file that will run to start the program. In addition to this I just need to have a bin key in my package.json file that will serve as a way to name what the command will be and to point to the file that will run when that command is called. After that I just need to install the nodejs project globally to make it a command that I can run anywhere on my computer.

In this post I will be going over just the basics of making a simple node CLI tool that is just a hello world starting point. However I am sure that I will get into at least a few more things beyond this that are closely related to this sort of thing, such as working with positional arguments when calling the script.

Read More

Copying by Value, and Reference in javaScript.

I have been cranking out posts on lodash as of late, and have come to make a post on the _.cloneDeep method in lodash which can be used to deep clone objects in javaScript if I am using lodash in a project. However I think it is called for to write a post on a subject that has to do with objects in general with javaScript regardless if lodash is used or not when it comes to the subject of referencing vs copying objects in javaScript.

You see objects in javaScript are not the same thing as primitive values like numbers and strings, that are always copied by value. When it comes to making a copy of a number value, that copy is now a whole other value in memory, and I can change that value without changing the other value from which it is copied. However when it comes to copying objects, often you are not making a copy of the object, you are just creating another reference to the same object. So then this is where cloning of objects comes into play, as well as shallow and deep cloning them, any understanding the difference between what a value is and what a reference to a value is.

So in this post I will be touching base on the topic of copying by value, and copying by reference in javaScript. Also in this post I will be covering a bunch of ways to go about making a copy of an object with native javaScript by itself. In addition I will try to do my best to get the core of the situation with objects in general down when it comes to copying and referencing them.

Read More