Skip to content

Commit

Permalink
Support arbitrary datum types. Closes #967.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Harding committed Feb 9, 2015
1 parent e6a6aeb commit 7ed93f9
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 145 deletions.
2 changes: 1 addition & 1 deletion src/bloodhound/options_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var oParser = (function() {

defaults = {
initialize: true,
identify: _.getIdGenerator(),
identify: _.stringify,
datumTokenizer: null,
queryTokenizer: null,
sufficient: 5,
Expand Down
21 changes: 14 additions & 7 deletions src/bloodhound/search_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* Copyright 2013-2014 Twitter, Inc. and other contributors; Licensed MIT
*/

var SearchIndex = (function() {
var SearchIndex = window.SearchIndex = (function() {
'use strict';

var CHILDREN = 'c', IDS = 'i';

// constructor
// -----------

Expand All @@ -17,7 +19,7 @@ var SearchIndex = (function() {
$.error('datumTokenizer and queryTokenizer are both required');
}

this.identify = o.identify || _.getIdGenerator();
this.identify = o.identify || _.stringify;
this.datumTokenizer = o.datumTokenizer;
this.queryTokenizer = o.queryTokenizer;

Expand Down Expand Up @@ -54,8 +56,8 @@ var SearchIndex = (function() {
chars = token.split('');

while (ch = chars.shift()) {
node = node.children[ch] || (node.children[ch] = newNode());
node.ids.push(id);
node = node[CHILDREN][ch] || (node[CHILDREN][ch] = newNode());
node[IDS].push(id);
}
});
});
Expand Down Expand Up @@ -84,11 +86,11 @@ var SearchIndex = (function() {
chars = token.split('');

while (node && (ch = chars.shift())) {
node = node.children[ch];
node = node[CHILDREN][ch];
}

if (node && chars.length === 0) {
ids = node.ids.slice(0);
ids = node[IDS].slice(0);
matches = matches ? getIntersection(matches, ids) : ids;
}

Expand Down Expand Up @@ -139,7 +141,12 @@ var SearchIndex = (function() {
}

function newNode() {
return { ids: [], children: {} };
var node = {};

node[IDS] = [];
node[CHILDREN] = {};

return node;
}

function unique(array) {
Expand Down
4 changes: 4 additions & 0 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ var _ = (function() {
};
},

stringify: function(val) {
return _.isString(val) ? val : JSON.stringify(val);
},

noop: function() {}
};
})();
2 changes: 1 addition & 1 deletion src/typeahead/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ var Dataset = (function() {
// ----------------

function getDisplayFn(display) {
display = display || 'value';
display = display || _.stringify;

return _.isFunction(display) ? display : displayFn;

Expand Down
4 changes: 2 additions & 2 deletions test/bloodhound/search_index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('SearchIndex', function() {
it('#reset should empty the search index', function() {
this.index.reset();
expect(this.index.datums).toEqual([]);
expect(this.index.trie.ids).toEqual([]);
expect(this.index.trie.children).toEqual({});
expect(this.index.trie.i).toEqual([]);
expect(this.index.trie.c).toEqual({});
});
});
172 changes: 92 additions & 80 deletions test/fixtures/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,99 +18,111 @@ fixtures.data = {

fixtures.serialized = {
simple: {
"datums": [
{ "value": "big" },
{ "value": "bigger" },
{ "value": "biggest" },
{ "value": "small" },
{ "value": "smaller" },
{ "value": "smallest" }
],
"datums": {
"{\"value\":\"big\"}": {
"value": "big"
},
"{\"value\":\"bigger\"}": {
"value": "bigger"
},
"{\"value\":\"biggest\"}": {
"value": "biggest"
},
"{\"value\":\"small\"}": {
"value": "small"
},
"{\"value\":\"smaller\"}": {
"value": "smaller"
},
"{\"value\":\"smallest\"}": {
"value": "smallest"
}
},
"trie": {
"ids": [],
"children": {
"b": {
"ids": [0, 1, 2],
"children": {
"i": {
"ids": [0, 1, 2],
"children": {
"g": {
"ids": [0, 1, 2],
"children": {
"g": {
"ids": [1, 2],
"children": {
"e": {
"ids": [1, 2],
"children": {
"r": {
"ids": [1],
"children": {}
},
"s": {
"ids": [2],
"children": {
"t": {
"ids": [2],
"children": {}
"i": [],
"c": {
"b": {
"i": ["{\"value\":\"big\"}", "{\"value\":\"bigger\"}", "{\"value\":\"biggest\"}"],
"c": {
"i": {
"i": ["{\"value\":\"big\"}", "{\"value\":\"bigger\"}", "{\"value\":\"biggest\"}"],
"c": {
"g": {
"i": ["{\"value\":\"big\"}", "{\"value\":\"bigger\"}", "{\"value\":\"biggest\"}"],
"c": {
"g": {
"i": ["{\"value\":\"bigger\"}", "{\"value\":\"biggest\"}"],
"c": {
"e": {
"i": ["{\"value\":\"bigger\"}", "{\"value\":\"biggest\"}"],
"c": {
"r": {
"i": ["{\"value\":\"bigger\"}"],
"c": {}
},
"s": {
"i": ["{\"value\":\"biggest\"}"],
"c": {
"t": {
"i": ["{\"value\":\"biggest\"}"],
"c": {}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
},
"s": {
"ids": [3, 4, 5],
"children": {
"m": {
"ids": [3, 4, 5],
"children": {
"a": {
"ids": [3, 4, 5],
"children": {
"l": {
"ids": [3, 4, 5],
"children": {
"l": {
"ids": [3, 4, 5],
"children": {
"e": {
"ids": [4, 5],
"children": {
"r": {
"ids": [4],
"children": {}
},
"s": {
"ids": [5],
"children": {
"t": {
"ids": [5],
"children": {}
},
"s": {
"i": ["{\"value\":\"small\"}", "{\"value\":\"smaller\"}", "{\"value\":\"smallest\"}"],
"c": {
"m": {
"i": ["{\"value\":\"small\"}", "{\"value\":\"smaller\"}", "{\"value\":\"smallest\"}"],
"c": {
"a": {
"i": ["{\"value\":\"small\"}", "{\"value\":\"smaller\"}", "{\"value\":\"smallest\"}"],
"c": {
"l": {
"i": ["{\"value\":\"small\"}", "{\"value\":\"smaller\"}", "{\"value\":\"smallest\"}"],
"c": {
"l": {
"i": ["{\"value\":\"small\"}", "{\"value\":\"smaller\"}", "{\"value\":\"smallest\"}"],
"c": {
"e": {
"i": ["{\"value\":\"smaller\"}", "{\"value\":\"smallest\"}"],
"c": {
"r": {
"i": ["{\"value\":\"smaller\"}"],
"c": {}
},
"s": {
"i": ["{\"value\":\"smallest\"}"],
"c": {
"t": {
"i": ["{\"value\":\"smallest\"}"],
"c": {}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

1 comment on commit 7ed93f9

@zenithtekla
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Root of the issue with Trie data structure is that it only checks node at first character in the string.

California, type 'c' and retrieves the state with typeahead (54 states). but typing 'l', 'i', 'for', 'li',... does not include California as a selective option or even nothing found.

In the current(Outdated) implementation of this stale Typeahead.js (look for more maintained version yourself), the search does not result in matches for query of middle characters.

It would be great if someone can fix this issue, so the Typeahead plugin can be more useful

Please sign in to comment.