Perlish JavaScript

John Nowlin, Product Development Consultant for the College Center for Library Automation in Tallahassee, FL, noticed that LibraryLookup doesn't handle ISSNs correctly when used with DRA systems. His fix distinguishes between the ISBN and ISSN cases, hyphenates the latter, and for good measure tosses in a popup confirmation where the original LibraryLookup (in manly Unix fashion) just silently fails. Here's John's version, which I'll also roll into the DRA page and bookmarklet generator:

<a href="javascript:if(/([\/-]|is([bs])n=)(\d{7,9}[\dX])/i.test (location.href) || confirm('Could not find an ISBN or ISSN. Would you like to open LINCCSearch anyway?')) void(window.open('http://www.linccweb.org/linccsearch?index=' + ((RegExp.$2=='s')?'SN&query=' + RegExp.$3.slice(0,4) + '-' + RegExp.$3.slice(4,8) : 'BN&query=' + RegExp.$3), 'LibraryLookup', 'scrollbars=1, resizable=1, width=600, height=500'));">LINCC Catalog</a>

Nicely done! (I've added some whitespace so the text will format here; that whitespace should be stripped out for use.) This is a marvel of Perlish terseness. Extraneous variables are banished. The regular expression is tested directly. Question-mark-colon syntax concisely chooses between the ISBN and ISSN cases. In the latter case, the captured number (RegExp.$3) is directly sliced into four-digit components.

It's cool to see that JavaScript can be as Perlishly terse as this. The tradeoff, of course, is readability. And I must admit that I looked twice at John's revision of LibraryLookup before I grokked it. It does seem, though, that bookmarklets are one of those cases where terseness is especially valuable. There's just something magical about these little packages of code that you can email as paragraphs, bury in links on web pages, and park on the browser's toolbar.

I always thought Perl had a role to play on the desktop. And now that it has transfused its genes into JavaScript, it does.


Former URL: http://weblog.infoworld.com/udell/2003/01/21.html#a579