Skip to content

Commit

Permalink
make default filter case-insensitive. update version to 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rniemeyer committed Dec 12, 2013
1 parent e79ed7b commit a03538f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-jqAutocomplete",
"version": "0.1.0",
"version": "0.1.1",
"main": "build/knockout-jqAutocomplete.min.js",
"ignore": [
"examples",
Expand Down
8 changes: 5 additions & 3 deletions build/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-jqAutocomplete 0.1.0 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-jqAutocomplete 0.1.1 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD anonymous module
Expand Down Expand Up @@ -63,6 +63,7 @@

//if dealing with local data, the default filtering function
this.defaultFilter = function(item, term) {
term = term && term.toLowerCase();
return (item || item === 0) && ko.toJSON(item).toLowerCase().indexOf(term) > -1;
};

Expand All @@ -81,7 +82,8 @@
results.push({
label: props.label ? item[props.label] : item.toString(),
value: props.input ? item[props.input] : item.toString(),
actual: props.value ? item[props.value] : item
actual: props.value ? item[props.value] : item,
data: item
});
}
}
Expand All @@ -93,7 +95,7 @@
//if specified, use a template to render an item
this.renderItem = function(templateName, context, ul, item) {
var $li = $("<li></li>").appendTo(ul),
itemContext = context.createChildContext(item.actual);
itemContext = context.createChildContext(item.data);

//apply the template binding
ko.applyBindingsToNode($li[0], { template: templateName }, itemContext);
Expand Down
4 changes: 2 additions & 2 deletions build/knockout-jqAutocomplete.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-jqAutocomplete",
"version": "0.1.0",
"version": "0.1.1",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "0.x.x",
Expand Down
23 changes: 23 additions & 0 deletions spec/knockout-jqAutocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,29 @@ describe("knockout-jqAutocomplete", function(){
expect(JSON.stringify(responseData[1])).toEqual('{"label":"three","value":"three","actual":"three","data":"three"}');
});

it("should apply the default filter in a case-insensitve manner to the results", function() {
var responseData,
response = jasmine.createSpy(),
valueAccessor = function() {
return {};
},
data = [
"one",
"two",
"three"
],
request = { term: "T" };


instance.processOptions(valueAccessor, instance.defaultFilter, data, request, response);

responseData = response.mostRecentCall.args[0];

expect(responseData.length).toEqual(2);
expect(JSON.stringify(responseData[0])).toEqual('{"label":"two","value":"two","actual":"two","data":"two"}');
expect(JSON.stringify(responseData[1])).toEqual('{"label":"three","value":"three","actual":"three","data":"three"}');
});

it("should not error when data is empty", function() {
var responseData,
response = jasmine.createSpy(),
Expand Down
1 change: 1 addition & 0 deletions src/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

//if dealing with local data, the default filtering function
this.defaultFilter = function(item, term) {
term = term && term.toLowerCase();
return (item || item === 0) && ko.toJSON(item).toLowerCase().indexOf(term) > -1;
};

Expand Down

0 comments on commit a03538f

Please sign in to comment.