Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for minLength option #131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -288,6 +288,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