If you’ve spent much time with cljs, you probably know that cljs uses the Closure Compiler for code compilation. The Closure Compiler provides significant features such as dead code removal and minimization. (For more details, check out Luke VanderHart’s old but relevant post. Also make sure to check out Maria Neise’s awesome blog about her cljs compiler work through Google’s Summer of Code).

One other well-known, though often forgotten, benefit from Google Closure is the Closure Library. The Closure Library provides a huge set of functionality for free.

By all accounts, the code is high quality and covers lots of edge cases that would be difficult and painful to find. For example, what’s the regular expression to determine if a string is all whitespace? If, like me, you said ^[\s]*$… Well, thanks for playing, because that’s not 100% correct. Here’s what you want:

//http://google.github.io/closure-library/api/source/closure/goog/string/string.js.src.html#l154
goog.string.isEmptyOrWhitespace = function(str) {
  // testing length == 0 first is actually slower in all browsers (about the
  // same in Opera).
  // Since IE doesn't include non-breaking-space (0xa0) in their \s character
  // class (as required by section 7.2 of the ECMAScript spec), we explicitly
  // include it in the regexp to enforce consistent cross-browser behavior.
  return /^[\s\xa0]*$/.test(str);
};

Good luck figuring out that IE’s handling of 0xa0 is broken before you get to production. And you get a nice little performance optimization thrown in for free.

The Closure Library is large, and I’ve only used a small portion of it. As I’ve suggested before, we could find worse ways to spend time than reading through the Closure api to get a feel for what’s available.

Bonus: Derek Slager wrote a great post about Google Closure that you should definitely read. He even includes a cljs shout-out.

Get access to new content

New posts are at bostonou.com. Go check it out, or you can just subscribe from here.

    Reminder: You're subscribing to bostonou.com