Skip to content

Commit

Permalink
Merge pull request #131 from jharding/120-support-min-length-option
Browse files Browse the repository at this point in the history
Add support for minLength option
  • Loading branch information
jharding committed Mar 30, 2013
2 parents 0064ad6 + be7040b commit 1c18506
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var Dataset = (function() {

this.name = o.name || utils.getUniqueId();
this.limit = o.limit || 5;
this.minLength = o.minLength || 1;
this.header = o.header;
this.footer = o.footer;
this.valueKey = o.valueKey || 'value';
Expand Down Expand Up @@ -246,9 +247,15 @@ var Dataset = (function() {
},

getSuggestions: function(query, cb) {
var that = this,
terms = utils.tokenizeQuery(query),
suggestions = this._getLocalSuggestions(terms).slice(0, this.limit);
var that = this, terms, suggestions;

// don't do anything until the minLength constraint is met
if (query.length < this.minLength) {
return;
}

terms = utils.tokenizeQuery(query);
suggestions = this._getLocalSuggestions(terms).slice(0, this.limit);

cb && cb(suggestions);

Expand Down
16 changes: 16 additions & 0 deletions test/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ describe('Dataset', function() {
});
});

describe('#getSuggestions', function() {
describe('when length of query is less than minLength', function() {
beforeEach(function() {
this.spy = jasmine.createSpy();

this.dataset = new Dataset({ local: fixtureStrings, minLength: 3 });
this.dataset.initialize();
});

it('should be a noop', function() {
this.dataset.getSuggestions('co', this.spy);
expect(this.spy).not.toHaveBeenCalled();
});
});
});

describe('Matching, combining, returning results', function() {
beforeEach(function() {
this.dataset = new Dataset({ local: fixtureStrings, remote: '/remote' });
Expand Down
1 change: 1 addition & 0 deletions test/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<script>
$('.states').typeahead({
valueKey: 'state',
minLength: 3,
local: [
"Alabama",
"Alaska",
Expand Down

0 comments on commit 1c18506

Please sign in to comment.