Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Allow non-us-english languages, respect system language default, test cleanup #23

Closed
wants to merge 9 commits into from
48 changes: 30 additions & 18 deletions lib/spellchecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ var Spellchecker = bindings.Spellchecker;

var defaultSpellcheck = null;

var setDictionary = function(lang, dictPath) {
var ensureDefaultSpellCheck = function() {
if (defaultSpellcheck) {
return;
}
defaultSpellcheck = new Spellchecker();
};

var setLanguage = function(lang) {
ensureDefaultSpellCheck();
setDictionary(lang, getDictionaryPath());
};

var setDictionary = function(lang, dictPath) {
// NB: Windows 8 uses *dashes* to set the language (i.e. en-US), so if we fail
// to set the language, try the Windows 8 way
lang = lang.replace('_', '-');
Expand All @@ -16,12 +27,18 @@ var setDictionary = function(lang, dictPath) {
}
};

var ensureDefaultSpellCheck = function() {
if (defaultSpellcheck) {
return;
var getDictionaryPath = function() {
var dict = path.join(__dirname, '..', 'vendor', 'hunspell_dictionaries');
try {
// HACK: Special case being in an asar archive
var unpacked = dict.replace('.asar' + path.sep, '.asar.unpacked' + path.sep);
if (require('fs').statSyncNoException(unpacked)) {
dict = unpacked;
}
} catch (error) {
}
setDictionary('en_US', getDictionaryPath());
};
return dict;
}

var isMisspelled = function() {
ensureDefaultSpellCheck();
Expand All @@ -47,24 +64,19 @@ var getAvailableDictionaries = function() {
return defaultSpellcheck.getAvailableDictionaries.apply(defaultSpellcheck, arguments);
};

var getDictionaryPath = function() {
var dict = path.join(__dirname, '..', 'vendor', 'hunspell_dictionaries');
try {
// HACK: Special case being in an asar archive
var unpacked = dict.replace('.asar' + path.sep, '.asar.unpacked' + path.sep);
if (require('fs').statSyncNoException(unpacked)) {
dict = unpacked;
}
} catch (error) {
}
return dict;
var getDefaultLanguage = function() {
ensureDefaultSpellCheck();
return getAvailableDictionaries()[0];
}

module.exports = {
setDictionary: setDictionary,
setLanguage: setLanguage,
add: add,
isMisspelled: isMisspelled,
getAvailableDictionaries: getAvailableDictionaries,
getCorrectionsForMisspelling: getCorrectionsForMisspelling,
Spellchecker: Spellchecker
Spellchecker: Spellchecker,
getDefaultLanguage: getDefaultLanguage

};
39 changes: 29 additions & 10 deletions spec/spellchecker-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
SpellChecker = require '../lib/spellchecker'

describe "SpellChecker", ->

describe "getDefaultLanguage()", ->
it "uses the system default dictionary", ->
systemDefaultLanguage = SpellChecker.getDefaultLanguage()
systemDefaultLanguage = systemDefaultLanguage.toLowerCase().replace('-', '_')
switch systemDefaultLanguage
when "en_gb", "en_uk"
expect(SpellChecker.isMisspelled('colour')).toBe false;
expect(SpellChecker.isMisspelled('color')).toBe true;
when "en", "en_us"
expect(SpellChecker.isMisspelled('colour')).toBe true;
expect(SpellChecker.isMisspelled('color')).toBe false;

describe ".isMisspelled(word)", ->
it "returns true if the word is mispelled", ->
SpellChecker.setLanguage('en') # Override the system default.
expect(SpellChecker.isMisspelled('wwoorrdd')).toBe true

it "returns false if the word isn't mispelled", ->
SpellChecker.setLanguage('en')
expect(SpellChecker.isMisspelled('word')).toBe false

it "throws an exception when no word specified", ->
expect(-> SpellChecker.isMisspelled()).toThrow()

describe ".getCorrectionsForMisspelling(word)", ->
it "returns an array of possible corrections", ->
SpellChecker.setLanguage('en')
corrections = SpellChecker.getCorrectionsForMisspelling('worrd')
expect(corrections.length).toBeGreaterThan 0
expect(corrections.indexOf('word')).toBeGreaterThan -1

it "throws an exception when no word specified", ->
expect(-> SpellChecker.getCorrectionsForMisspelling()).toThrow()

describe ".add(word)", ->
it "allows words to be added to the dictionary", ->
expect(SpellChecker.isMisspelled('wwoorrdd')).toBe true
SpellChecker.add('wwoorrdd')
expect(SpellChecker.isMisspelled('wwoorrdd')).toBe false

it "throws an error if no word is specified", ->
errorOccurred = false
try
Expand All @@ -47,7 +57,16 @@ describe "SpellChecker", ->
expect(typeof dictionary).toBe 'string'
expect(diction.length).toBeGreaterThan 0

describe ".setDictionary(lang, dictDirectory)", ->
it "sets the spell checker's language, and dictionary directory", ->
awesome = true
expect(awesome).toBe true
describe ".setLanguage(lang, dictDirectory)", ->
it "sets the spell checker's language", ->
SpellChecker.setLanguage('de')
expect(SpellChecker.isMisspelled('Wortwitz')).toBe false
expect(SpellChecker.isMisspelled('Wortwitzz')).toBe true
expect(SpellChecker.isMisspelled('Schnabeltier')).toBe false
expect(SpellChecker.isMisspelled('Platypus')).toBe true
SpellChecker.setLanguage('en_uk')
expect(SpellChecker.isMisspelled('colour')).toBe false
expect(SpellChecker.isMisspelled('color')).toBe true
SpellChecker.setLanguage('en_us')
expect(SpellChecker.isMisspelled('color')).toBe false
expect(SpellChecker.isMisspelled('colour')).toBe true