Skip to content

Commit 7a2da1e

Browse files
committedJul 28, 2019
Small improvement for search term generation
1 parent c7cec20 commit 7a2da1e

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed
 

‎javascripts/background/searchTermGeneration.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ function getSuggestion(term, callback) {
3232
* @param {function} callback Mandatory callback function with suggestion as parameter.
3333
*/
3434
function getSuggestionRecursive(original, current, runs, alreadyDone, callback) {
35+
if (isValid(original, current)) {
36+
callback(current);
37+
return;
38+
}
39+
3540
var words = getAllWords(current);
3641
words = words.filter(w => w.split(' ').length <= original.split(' ').length);
3742
words = words.filter(w => !alreadyDone.includes(w));
@@ -57,11 +62,8 @@ function getSuggestionRecursive(original, current, runs, alreadyDone, callback)
5762
}
5863

5964
alreadyDone.push(suggestion);
60-
61-
var suggestionWords = suggestion.split(' ');
62-
var termWords = original.split(' ');
63-
if (suggestionWords.length == termWords.length &&
64-
!suggestionWords.some(w => termWords.includes(w))) {
65+
66+
if (isValid(original, suggestion)) {
6567
callback(suggestion); // No inCallback() call: break from loop
6668
return;
6769
} else {
@@ -173,4 +175,19 @@ function chooseTerm(suggestions, alreadyChosen) {
173175
}
174176

175177
return '';
178+
}
179+
180+
/**
181+
* Checks if a suggestion is valid, i.e., it contains the same number of words as the original
182+
* term and it contains not a single word of the original term.
183+
*
184+
* @param {string} originalTerm The original term.
185+
* @param {string} suggestionTerm The suggestion to be checked.
186+
*/
187+
function isValid(originalTerm, suggestionTerm) {
188+
var suggestionWords = suggestionTerm.split(' ');
189+
var originalWords = originalTerm.split(' ');
190+
191+
return suggestionWords.length == originalWords.length &&
192+
!suggestionWords.some(w => originalWords.includes(w));
176193
}

‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
## Lines of code
88
|Language |files |blank |comment |code|
99
|-----------------------------|---------------|-------------|------------------|----|
10-
|JavaScript | 21 | 217 | 558 |1066|
10+
|JavaScript | 21 | 220 | 565 |1073|
1111
|HTML | 2 | 10 | 1 | 116|
1212
|JSON | 2 | 0 | 0 | 84|
1313
|Bourne Shell | 1 | 13 | 8 | 38|
1414
|CSS | 2 | 4 | 6 | 34|
1515
|Markdown | 1 | 3 | 0 | 14|
1616
|- |- |- |- |- |
17-
|SUM: | 29 | 247 | 573 |1352|
17+
|SUM: | 29 | 250 | 580 |1359|

0 commit comments

Comments
 (0)
Please sign in to comment.