Flattening an array in JavaScript

If you have an mutidementional array(or nested array) you want to flattern, you can do it this way.

So now you can run it

Making directory along with missing parents in Node.js

Note: By the time this post is written, the Node.js is on version 0.6.10. Therefore anything in this post may or may not valid in the newer versions.

Node.js file system API provide a bunch of functions to read/write files. One of them is the fs.mkdir(path, [mode], [callback]). However, this function itself does not provide any option for you to create a directory if the parents folder is missing, it’ll trigger an exception if you trying to do so. In another word, you can’t do something like “mkdir -p DIR”.

However, the callback function is an extreamly useful feature to extend this function. In case the fs.mkdir failed, it passes an error object into the callback function. Like this:

You’ll see it print an object looks like:

{ [Error: ENOENT, no such file or directory ‘parent/child’] errno: 34, code: ‘ENOENT’, path: ‘parent/child’ }

Good, now we know in the case of missing directory, we’ll get error code 34. What if we catch the error and try to recursively create it’s parent, then create the directory again? Everybody deserve a second chance, right?

Now you can run it like this;

JavaScript is like socialism

  • JavaScript is like socialism, there are too many bad examples out there make this awesome idea unfairly blamed.
  • JavaScript is like socialism, it has a lot of good things in it but they are hard to get. Whoever don’t get it blame it.
  • JavaScript is like socialism, the theory is always looks better those practices.
  • JavaScript is like socialism, if you get it right, it will gives you more benefits than you can imagine.
  • JavaScript is like socialism, it’s dark side is so easy to be abused to create evil.
  • JavaScript is like socialism, most of the people swear on it out loud are the people don’t really know about it.
  • JavaScript is like socialism, it’s hard to prove it will works well in large scale because we don’t have that productivity yet.
  • JavaScript is like socialism, the older generation think it’s awful while the younger think it’s cool.
  • JavaScript is like socialism, if you believe in it’s philosophy, you will think our current system sucks.
  • JavaScript is like socialism, it’s not a proud thing to admit you are a practitioner in a traditional system.
  • JavaScript is like socialism, whoever exercise it correctly are the hell smart geniuses.
  • JavaScript is like socialism, you can argue it is a religious, however, there aren’t that much preachers, but hardcore practitioners.
  • JavaScript is like socialism, you can see almost everywhere use a little of it, but people don’t take the whole thing seriously.
  • JavaScript is like socialism, you may use it’s features to fix you system or rethink the fundamental of your system in it’s way.
  • JavaScript is like socialism, people won’t believe such an idea from so many years ago will works in the future.