Merge pull request #869 from torlenor/MM-562

Preserve case of first letter in spell check, fixes #562
This commit is contained in:
Yuya Ochiai
2018-10-15 23:51:16 +09:00
committed by GitHub
2 changed files with 54 additions and 1 deletions

View File

@@ -70,7 +70,24 @@ export default class SpellChecker extends EventEmitter {
}
getSuggestions(word, maxSuggestions) {
return this.dict.getSuggestions(word, maxSuggestions);
const suggestions = this.dict.getSuggestions(word, maxSuggestions);
const firstCharWord = word.charAt(0);
let i;
for (i = 0; i < suggestions.length; i++) {
if (suggestions[i].charAt(0).toUpperCase() === firstCharWord.toUpperCase()) {
suggestions[i] = firstCharWord + suggestions[i].slice(1);
}
}
const uniqueSuggestions = suggestions.reduce((a, b) => {
if (a.indexOf(b) < 0) {
a.push(b);
}
return a;
}, []);
return uniqueSuggestions;
}
}

View File

@@ -67,6 +67,27 @@ describe('main/Spellchecker.js', function() {
spellchecker.spellCheck('Mattermost').should.equal(true);
spellchecker.spellCheck('mattermost').should.equal(true);
});
it('should give at most the requested number of suggestions', function() {
// helllo known to give at least 4 suggestions
spellchecker.getSuggestions('helllo', 4).length.should.be.equal(4);
spellchecker.getSuggestions('helllo', 1).length.should.be.equal(1);
});
it('should give suggestions which preserve case of first letter', function() {
let suggestions = spellchecker.getSuggestions('carr', 4);
suggestions.length.should.not.be.equal(0);
let i;
for (i = 0; i < suggestions.length; i++) {
suggestions[i].charAt(0).should.be.equal('c');
}
suggestions = spellchecker.getSuggestions('Carr', 4);
suggestions.length.should.not.be.equal(0);
for (i = 0; i < suggestions.length; i++) {
suggestions[i].charAt(0).should.be.equal('C');
}
});
});
describe('en-GB', function() {
@@ -107,5 +128,20 @@ describe('main/Spellchecker.js', function() {
spellchecker.spellCheck('-100').should.equal(true);
spellchecker.spellCheck('3.14').should.equal(true);
});
it('should give suggestions which preserve case of first letter', function() {
let suggestions = spellchecker.getSuggestions('gutenn', 4);
suggestions.length.should.not.be.equal(0);
let i;
for (i = 0; i < suggestions.length; i++) {
suggestions[i].charAt(0).should.be.equal('g');
}
suggestions = spellchecker.getSuggestions('Gutenn', 4);
suggestions.length.should.not.be.equal(0);
for (i = 0; i < suggestions.length; i++) {
suggestions[i].charAt(0).should.be.equal('G');
}
});
});
});