"Falsy" Mishaps in JavaScript

I sometimes see web developers check for falsy values incorrectly in JavaScript. It's easy to get wrong. Here are a few common mistakes. Missing Null if (myVariable === undefined) This is probably the most frequent mistake I see. While this catches undefined properly, it doesn't guard against reference errors when myVariable is null. Unless you're explicitly trying to exclude null from your check, this will result in a null reference error if you try to access a property on myVariable. Missing Undefined if(myVariable === null) I don't see this as often, but it does happen. While this catches null properly, it…

read more...

LRI (Least Recently Inserted)

I just recently had the need for a fairly specific piece of functionality in Node, but I couldn't seem to find anything that quite fit the bill. All I needed was a really simple collection that maintained up to a defined number of elements, and once that defined number had been reached, evicted the oldest element in the cache. TL;DR: I've created a library, lri, to solve this if you just want to use it. Use Cases Seems simple right? I couldn't seem to find anything that fit that bill precisely. The rub lies with how "oldest" is defined.…

read more...

Advanced JSON (de)serialization Using Java's Jackson

Jackson is an excellent library for serializing and deserializing JSON data in Java. It's a fairly quick and painless way to get the job done, until you need custom (de)serialization logic. If you get to that point, you'll instantly wish you were using some language that makes JSON work seamless--like Node.js--but that's not always an option, so the next best solution is to write yourself some custom (de)serialization functionality. I guess it's still substantially better than programming in assembly language... TL;DR: There's working demonstration code here if you don't like reading. Advanced (de)serialization So you've…

read more...

Say Goodbye To Relative Paths in Node.js

The story's always the same. You start out developing a simple Node application, but soon you want to start adding a few tidbits of functionality. Suddenly you're stuck in the land of relative path hell. Your paths begin to look like your fingers convulsed on the period and slash keys as you attempted to type. You start to lose track of the periods and the slashes it takes to get to the proper path. Before you know it, you're fed up with Node and ready to punch the first person who raves about the benefits of Node's event-driven model. Who…

read more...