From be7040b4a0de9c58d4e1717f0d3697c2b19e6564 Mon Sep 17 00:00:00 2001 From: Jake Harding Date: Sun, 24 Mar 2013 00:34:07 -0700 Subject: [PATCH] Add support for minLength option. Closes #120. --- src/dataset.js | 13 ++++++++++--- test/dataset_spec.js | 16 ++++++++++++++++ test/playground.html | 1 + 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/dataset.js b/src/dataset.js index 9fc271a4..dc433320 100644 --- a/src/dataset.js +++ b/src/dataset.js @@ -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'; @@ -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); diff --git a/test/dataset_spec.js b/test/dataset_spec.js index e0beb9c3..b6c28a8d 100644 --- a/test/dataset_spec.js +++ b/test/dataset_spec.js @@ -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' }); diff --git a/test/playground.html b/test/playground.html index b91df35b..78c62b19 100644 --- a/test/playground.html +++ b/test/playground.html @@ -52,6 +52,7 @@