Three Monkeys, Three Typewriters, Two Days

April 16, 2008

Array.map + destructuring assignment = win

It's nice to write code sometimes unburdened by either C++ or performance considerations. It can just look so nice as a result!

function stringToURL(str) {
  return (new StandardURL(nsIStandardURL.URLTYPE_AUTHORITY, 80,
                         str, "UTF-8", null))
         .QueryInterface(Components.interfaces.nsIURL);
}

function pairToURLs(pair) {
  do_check_eq(pair.length, 2);
  return pair.map(stringToURL);
}

function test_setQuery()
{
  var pairs =
    [
     ["http://example.com", "http://example.com/?foo"],
     /* more pairs here */
    ].map(pairToURLs);
  for each (var [provided, target] in pairs) {
    /* Set the query and test for equality here */
  }
}
Posted by bzbarsky at April 16, 2008 12:34 AM