Flagger now at 0.9.4, and a new JS library in the works

See, I knew I wasn’t cut out for blogging. I keep forgetting to post code updates, when that’s precisely the reason I set this thing up. Anyway. Flagger is now at version 0.9.4. Most of the changes are bug fixes. 0.9.4 closes a bug where the wrong callbacks got called in some situations. Since 0.9.1 there’s also been a functional change, in that after_mark_as_* methods are only called if their corresponding attribute is changed by a mark_as_* call.

In other news, I’m working on a JavaScript library. Not a Prototype/jQuery competitor, oh my no. I saw this the other day and decided I’d have a go at writing something similar, as an exercise in not completely forgetting how to do maths. I’ve noticed that much JavaScript that attempts to to anything geometrical does so by throwing piles of arrays around rather than having any real classes doing the work. That is to say, they favour things like

var c = vector_add([0,3,7], [5,2,8]);

whereas I’d rather write

var a = Vector.create([0,3,7]);
var b = Vector.create([5,2,8]);
var c = a.add(b);

Obviously, for this trivial example that’s a lot more work. The point of what I’m working on is that a and b are fully-fledged Vector objects with loads of useful methods attached to them for letting them interact with other vectors, matrices, lines, planes, etc. Also, a more object-oriented approach leads to better code legibility and lets you write code that’s closer to the maths it represents. As a quick sneak-preview, here’s the method for finding the intersection of two lines in 3D space:

this.intersectionWith = function(obj) {
  if (!this.intersects(obj)) { return null; }
  var P = this.anchor, X = this.direction,
      Q = obj.anchor, Y = obj.direction;
  var a = (X.dot(Q.subtract(P)) * Y.dot(Y) / X.dot(X)) +
      (X.dot(Y) * Y.dot(P.subtract(Q)));
  var s = a / (Y.dot(Y) - (X.dot(Y) * X.dot(Y)));
  return P.add(X.x(s));

I guarantee that would be a lot harder to follow if we were passing around lots of arrays and for loops. You might not understand what lines 5 and 6 do, but the point is that it reads almost exactly like the maths would on paper. More news on a release date soon (hopefully).

If you’ve enjoyed this article, you might enjoy my recently published book JavaScript Testing Recipes. It’s full of simple techniques for writing modular, maintainable JavaScript apps in the browser and on the server.