Using spread operator to split your string

There are two obvious interesting ways to split your string:

If you are interested to use the spread operator, consult the following compatibility tables before you use it in your environment:

Advertisements

Toggle a class value using Element.classList.toggle() method

Instead of using jQuery’s .toggleClass() method to add or remove classes from an element, I can use the Element.classList.toggle() method to achieve the similar goal. The method will return true if the value is successfully added to the element class, otherwise, it will return false.

It’s a perfectly valid choice for a fairly simple application.

What I can do with JavaScript’s Array.filter()?

I wonder what I can do with JavaScript’s Array.filter() and I think there is a lot of things I can do with it.

Filter my list of cars

Filter my list of heroes

Filter my dirty clothes…

Filter my favorite shoes…

Filter my favorite foods…

Filter the candidate to be included in my crew…

I think there is a lot of things we can filter out of an Array. A lot of things…

Have not used Array.filter() before? If yes, then consult the complete reference at MDN.

Using jsinspect to detect duplicate codes

As a JavaScript software developer, we occasionally find ourselves writing duplicate codes and keep moving forward without even considering to clean up the code. We may also find ourselves working in a codebase without knowing how much of duplicate codes have been written to and not sure where to start the cleanup.

There are tools built specifically to help for this purpose, jsinspect. I found this gem when I watched a talk by Elijah Manor: Eliminate JavaScript Code Smells.

Curious enough to give it a try, I installed jsinspect to see how it goes. I ran the tool to inspect several of my JavaScript files and here is the jsinspect returned in my console:

It’s cool, isn’t it? Imagine if we can tap this tool into build process to keep the codebase as clean as possible. But, if you guys also curious enough about jsinspect like myself, try to use it in your daily workflow, see how the tool can help you find the duplicate code easily.

Quote: The most important characteristic of promises

The most important characteristic of promises is that it normalizes into predictable form how we handle the success or failure of any function call, especially if that call may be asynchronous in nature.

And in that normalization, it leaves our program in a position to control and trust how it will proceed, rather than handing that control (via a callback continuation) off to an untrustable third-party.

https://blog.getify.com/promises-part-2/

Break the promise chains flow

I like the idea of chaining the promises in my code so that it looks nice, pretty nice and easier to read. After several months following this pattern, I have a situation where I need to break the chain promise flow, like stopping the flow. See the below snippet code:

From the above code, I am expecting the code to stop chain and move on to the .catch handler, but is not. I learned the code will continue the flow as I return with no value at line 9.

So, here is the way to break the promise chains, instead of returning with no value, I throw an Error to stop the flow:

Oh, yeah I use Bluebird to working with Promise…

Updating to Node v0.12

Today I just noticed that I’m still using the old version of Node, v0.10.36 to be precise. So, I head over to Node blog to see what is happening. It seems to be there is an update for the v0.10.x contained a fix for CVE-2015-0278. I can just update the Node installed on my laptop to v0.10.37 and at the same time I’m curious what if I move forward to Node v0.12.0. I don’t really get into the Node v0.12.0 stuff, so I decided to update it to those version. At this point I’m not quite sure what changes that brought to the Node v0.12.0. There is a few great a places to start digging what’s new in Node v0.12:

  1. API changes between v0.10 and v0.12
  2. What’s New in Node v0.12