Created
October 26, 2013 16:23
-
-
Save mdwheele/7171422 to your computer and use it in GitHub Desktop.
Simple Sublime Text-esque JS Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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