I'm just doing some research into Typeahead.js
and it's a really cool library. I've managed to get a basic example working with thanks to the documentation which is also very good.
However I'm trying to get my head around what the following block of code is actually doing?
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
In the example it's passed in when initialising the typeahead as the source
option. I can understand that it's taking the input from the textbox and comparing it to the dataset but I'm a little confused as to what q
and cb
are?