-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.js
78 lines (61 loc) · 2.13 KB
/
autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var DEFINITIONS_URL = "https://quizlet.com/create-set/autodefine";
var WORD_LANGUAGE = "en";
var DEFINITION_LANGUAGE = "en";
var Autocomplete = function() {
var wordToDefinitions = {};
var wordToSuccessFunction = {};
// TODO: Handle error.
this.getDefinitions = function(word, onSuccess) {
var safeWord = safenWord(word);
if (wordToDefinitions.hasOwnProperty(safeWord)) {
onSuccess(wordToDefinitions[safeWord]);
return;
}
// Necessary if typing quickly and multiple calls are made before the result is cached.
if (wordToSuccessFunction.hasOwnProperty(safeWord)) {
return;
}
wordToSuccessFunction[safeWord] = onSuccess;
$.getJSON(
DEFINITIONS_URL,
{
text: word,
wordLang: WORD_LANGUAGE,
defLang: DEFINITION_LANGUAGE
},
function(data) {
var definitions = new Definitions(data.definitions);
wordToDefinitions[safeWord] = definitions;
var successFunction = wordToSuccessFunction[safeWord];
delete wordToSuccessFunction[safeWord];
successFunction(definitions);
});
};
var safenWord = function(word) {
return word.trim().toLowerCase();
};
};
var Definitions = function(definitions) {
var safeDefinitions = [];
var init = function() {
for (var i = 0; i < definitions.length; i++) {
safeDefinitions.push(safenDefinition(definitions[i]));
}
};
this.findAutocompleteMatch = function(definitionTyped) {
var safeTypedDefinition = safenDefinition(definitionTyped);
// TODO: Could be more efficient (e.g., trie). Quizlet only returns about thirty definitions, so
// it might not be worth it.
for (var i = 0; i < safeDefinitions.length; i++) {
var safeDefinition = safeDefinitions[i];
if (safeDefinition.substr(0, safeTypedDefinition.length) == safeTypedDefinition) {
return definitions[i];
}
}
return null;
};
var safenDefinition = function(definition) {
return definition.toLowerCase().replace(/[^\w ]/g, '');
};
init();
};