Skip to content

Commit

Permalink
Cache case insensitive results
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko committed May 2, 2015
1 parent a2db2ca commit 71f43b4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
8 changes: 5 additions & 3 deletions dist/Autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ var Autosuggest = (function (_Component) {
value: function showSuggestions(input) {
var _this2 = this;

var cacheKey = input.toLowerCase();

this.lastSuggestionsInputValue = input;

if (!this.props.showWhen(input)) {
this.setSuggestionsState(null);
} else if (this.cache[input]) {
this.setSuggestionsState(this.cache[input]);
} else if (this.cache[cacheKey]) {
this.setSuggestionsState(this.cache[cacheKey]);
} else {
this.suggestionsFn(input, function (error, suggestions) {
// If input value changed, suggestions are not relevant anymore.
Expand All @@ -131,7 +133,7 @@ var Autosuggest = (function (_Component) {
suggestions = null;
}

_this2.cache[input] = suggestions;
_this2.cache[cacheKey] = suggestions;
_this2.setSuggestionsState(suggestions);
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-autosuggest",
"version": "1.12.0",
"version": "1.12.1",
"description": "WAI-ARIA compliant React autosuggest component",
"main": "dist/Autosuggest.js",
"repository": {
Expand Down
10 changes: 6 additions & 4 deletions src/Autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ export default class Autosuggest extends Component {
}

showSuggestions(input) {
const cacheKey = input.toLowerCase();

this.lastSuggestionsInputValue = input;

if (!this.props.showWhen(input)) {
this.setSuggestionsState(null);
} else if (this.cache[input]) {
this.setSuggestionsState(this.cache[input]);
} else if (this.cache[cacheKey]) {
this.setSuggestionsState(this.cache[cacheKey]);
} else {
this.suggestionsFn(input, (error, suggestions) => {
// If input value changed, suggestions are not relevant anymore.
Expand All @@ -103,7 +105,7 @@ export default class Autosuggest extends Component {
suggestions = null;
}

this.cache[input] = suggestions;
this.cache[cacheKey] = suggestions;
this.setSuggestionsState(suggestions);
}
});
Expand Down Expand Up @@ -316,7 +318,7 @@ export default class Autosuggest extends Component {
valueBeforeUpDown: null
}, () => {
// This code executes after the component is re-rendered
setTimeout( () => findDOMNode(this.refs.input).focus() );
setTimeout(() => findDOMNode(this.refs.input).focus());
});
}

Expand Down
27 changes: 19 additions & 8 deletions src/tests/Autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const onSuggestionSelected = jest.genMockFunction();
const onSuggestionFocused = jest.genMockFunction();
const onSuggestionUnfocused = jest.genMockFunction();
const onChange = jest.genMockFunction();
const getSuburbs = jest.genMockFunction().mockImplementation(getSuburbStrings);

function getSuburbStrings(input, callback) {
const regex = new RegExp('^' + input, 'i');
Expand Down Expand Up @@ -970,17 +971,27 @@ describe('Autosuggest', function() {
});
});

describe('Misc', function() {
describe('Caching', function() {
beforeEach(function() {
createAutosuggest(<Autosuggest suggestions={getSuburbStrings} />);
createAutosuggest(<Autosuggest suggestions={getSuburbs} />);
setInputValue('m');
getSuburbs.mockClear();
});

it('should reset sectionIterator when getting cached suggestions', function() {
setInputValue('m');
setInputValue('mz');
setInputValue('m');
clickDown();
expectFocusedSuggestion('Mill Park');
describe('should not call suggestions function if', function() {
it('suggestions function was called before with the same input', function() {
setInputValue('mi');
getSuburbs.mockClear();
setInputValue('m');
expect(getSuburbs).not.toBeCalled();
});

it('suggestions function was called before with the same case insensitive input', function() {
setInputValue('mi');
getSuburbs.mockClear();
setInputValue('M');
expect(getSuburbs).not.toBeCalled();
});
});
});
});

0 comments on commit 71f43b4

Please sign in to comment.