Skip to content

Instantly share code, notes, and snippets.

@mdwheele
Created October 26, 2013 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwheele/7171422 to your computer and use it in GitHub Desktop.
Save mdwheele/7171422 to your computer and use it in GitHub Desktop.
Simple Sublime Text-esque JS Filter
var items = [
'/home/user/code/folder/whatever/something/nothing/neat.js',
'/home/user/code/folder/whatever/something/nothing/great.js',
'/home/user/code/folder/whatever/something/nothing/stuff.html',
'/home/user/code/folder/whatever/something/nothing/elmo.css',
'/home/user/code/folder/whatever/something/nothing/animal.css',
'/usr/local/lib/blah',
'/foo/bar/baz/goo/blue'
];
$('#search').keyup(function() {
var search = $(this).val();
/* Matching logic */
var matches = items.filter(function(item) {
var j = 0; // remembers position of last found character
// consider each search character one at a time
for (var i = 0; i < search.length; i++) {
var l = search[i];
if (l == ' ') continue; // ignore spaces
j = item.indexOf(l, j); // search for character & update position
if (j == -1) return false; // if it's not found, exclude this item
}
return true;
});
/* End matching logic */
$('#results').empty();
matches.forEach(function(match) {
$('#results').append($('<li>').text(match));
});
}).keyup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment