Copying an Array in JavaScript

Of course when you assign an existing object to a variable in JavaScript, you actually receive a reference to the object.  So modifications on the reference, cause changes on the original.  This shouldn’t be a surprise at all, and just to illustrate:

var a = [1, 2, 3];
var b = a;
b.push(4);
a; // [1, 2, 3, 4]

It’s common to see code for making a copy of an array by iterating over the elements:

var a = [1, 2, 3];
var b = new Array(a.length);
for (var i = 0; i < a.length; i++) {
  b[i] = a[i];
}

After which we’re safe since we have an element-by-element copy of the original (safe if the elements are primitives!):

b.push(4);
a; // [1, 2, 3]

Pretty ugly code for such a simple, common operation.  Luckily JavaScript has a nifty method on Array called “slice”, which is for selecting a segment of an existing Array.  We can abuse slice to make the copy for us:

var a = [1, 2, 3];
var b = a.slice(0, a.length);

b.push(4);
a; // [1, 2, 3]

And wouldn’t you know it – the second argument is optional:

var a = [1, 2, 3];
var b = a.slice(0);

AND, don’t tell the old-school JavaScript developers but if you’re programming in a decently new version of JavaScript (hey node.js guys) – that first argument is also optional:

var a = [1, 2, 3];
var b = a.slice();

Mocha v1.0

My favorite JavaScript testing framework, Mocha – just hit v1.0 a few days ago.

No breaking changes (exactly how I think a 1.0 should be), compiler support, some new reporters.  If you’re using Mocha for JavaScript testing – time to upgrade.

Great Job TJ!

Testing Subdomains Locally with Ease

When you test subdomains locally using `localhost`, it can be a pain since it won’t work out of the box for subdomains you haven’t added to /etc/hosts (since there is no support for wildcard subdomains).  You can verify this easily enough:

~ $ ping john.localhost
ping: cannot resolve john.localhost: Unknown host

So, when you want something more flexible – there is another easy answer.  You can use “lvh.me”, a domain which resolves to 127.0.0.1 (for all subdomains):

~ $ ping lvh.me
PING lvh.me (127.0.0.1): 56 data bytes

So, you’re actually set up for this right now:

~ $ ping john.lvh.me
PING john.lvh.me (127.0.0.1): 56 data bytes

Pretty awesome!  You need an internet connection to use it, so what I do normally is throw the subdomains that I know I’ll use into /etc/hosts – that way they work offline too!

JSON IP

A lot of times, when I’m trying to get the IP address of a computer, I use a small site called jsonip.com, which presents a nice (ad-free) JSON formatted IP address for viewing.

You can use it programmatically obviously, and it even supports JSONP (see: http://jsonip.com/func) – but I find I use it more often for a clear, easy-to-read IP address site (which far surpasses the ad-loaded junk you’ll find elsewhere).

Natural Language Processing in JavaScript

If you’re doing anything with natural language processing in Node.js, natural is a great library that has some common algorithms all in one place.  They have everything from distance algorithms to phonetic-similarity algorithms like Soundex & Metaphone.

I just added a Dice’s Coefficient implementation today, and its really easy to use:

var natural = require('natural');
console.log(natural.DiceCoefficient('thing', 'thing')); // 1.0
console.log(natural.DiceCoefficient('not', 'the same')); // 0.0
console.log(natural.DiceCoefficient('night', 'nacht')); // 0.25