diff --git a/src/main/SpellChecker.js b/src/main/SpellChecker.js index 018c0c59..da7f0291 100644 --- a/src/main/SpellChecker.js +++ b/src/main/SpellChecker.js @@ -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; } } diff --git a/test/specs/spellchecker_test.js b/test/specs/spellchecker_test.js index fd3853ba..6b85eb0e 100644 --- a/test/specs/spellchecker_test.js +++ b/test/specs/spellchecker_test.js @@ -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'); + } + }); }); });