@@ -32,6 +32,11 @@ function getSuggestion(term, callback) {
32
32
* @param {function } callback Mandatory callback function with suggestion as parameter.
33
33
*/
34
34
function getSuggestionRecursive ( original , current , runs , alreadyDone , callback ) {
35
+ if ( isValid ( original , current ) ) {
36
+ callback ( current ) ;
37
+ return ;
38
+ }
39
+
35
40
var words = getAllWords ( current ) ;
36
41
words = words . filter ( w => w . split ( ' ' ) . length <= original . split ( ' ' ) . length ) ;
37
42
words = words . filter ( w => ! alreadyDone . includes ( w ) ) ;
@@ -57,11 +62,8 @@ function getSuggestionRecursive(original, current, runs, alreadyDone, callback)
57
62
}
58
63
59
64
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 ) ) {
65
67
callback ( suggestion ) ; // No inCallback() call: break from loop
66
68
return ;
67
69
} else {
@@ -173,4 +175,19 @@ function chooseTerm(suggestions, alreadyChosen) {
173
175
}
174
176
175
177
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 ) ) ;
176
193
}
0 commit comments