Search with underscore and jQuery

Super simple array filtering with _.js and jQuery.

// up to you to get this from somewhere
document.search = [
  "Apples growing guide",
  "Orange growing",
  "Apple repair guide",
  "ruby on rails setup guide",
  "ruby vps install",
  "rails to sinatra"
];

$("input.omnibar").on('keyup', function(e){
  // clear our results list
  var $ul = $("ul.omnibar_results").html("");
  if($(this).val() == ""){
    // dont show anything if the query is empty
    return;
  }
  // get our query, remove excess white space, down case 
  // and split on white space
  var query = $(this).val().trim().toLowerCase().split(/[^\w]/);
  var results = _.select(document.search, function(d){
    return _.all(query, function(q){
      return d.toLowerCase().match(q);
    });
  })
  _.map(results, function(r){
    this.append("<li>"+r+"</li>");
  }, $ul);
});

Written 26th of August, 2012