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

Commit 26f6b12

Browse files
Control the use of a system or Hunspell checker via API.
- Introduce a new API call, `setSpellcheckerType` which takes a constant to determine if a system checker should be used, if available (the default), to always use the system checker, or to always use Hunspell. - Duplicates the specs to have a force to Hunspell implementation.
1 parent 81367ae commit 26f6b12

11 files changed

+642
-20
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,42 @@ When using Hunspell, this will not modify the .dic file; new words must be added
5454
`word` - String word to add.
5555

5656
Returns nothing.
57+
58+
### new Spellchecker()
59+
60+
In addition to the above functions that are used on a default instance, a new instance of SpellChecker can be instantiated with the use of the `new` operator. The same methods are available with the instance but the dictionary and underlying API can be changed independently from the default instance.
61+
62+
```javascript
63+
const checker = new SpellChecker.Spellchecker()
64+
```
65+
66+
#### SpellChecker.Spellchecker.setSpellcheckerType(type)
67+
68+
Overrides the library selection for checking. Without this, the checker will use [Hunspell](http://hunspell.github.io/) on Linux, the [Spell Checking API](https://docs.microsoft.com/en-us/windows/desktop/intl/spell-checker-api) for Windows, and [NSSpellChecker](https://developer.apple.com/documentation/appkit/nsspellchecker) on Macs.
69+
70+
If the environment variable `SPELLCHECKER_PREFER_HUNSPELL` is set to any value, the library will fallback to always using the Hunspell implementation.
71+
72+
This is the same behavior as calling `setSpellcheckerType` with the `USE_SYSTEM_DEFAULTS` constant:
73+
74+
```coffeescript
75+
checker = new SpellChecker.Spellchecker
76+
checker.setSpellcheckerType SpellChecker.USE_SYSTEM_DEFAULTS
77+
```
78+
79+
To always use the system API and not fallback to Hunspell regardless of the environment variable, use the `ALWAYS_USE_SYSTEM` constant:
80+
81+
```coffeescript
82+
checker = new SpellChecker.Spellchecker
83+
checker.setSpellcheckerType SpellChecker.ALWAYS_USE_SYSTEM
84+
```
85+
86+
Likewise, Hunspell can be forced with the `ALWAYS_USE_HUNSPELL` constant.
87+
88+
```javascript
89+
const checker = new SpellChecker.Spellchecker();
90+
checker.setSpellcheckerType(SpellChecker.ALWAYS_USE_SYSTEM);
91+
```
92+
93+
On Linux, Hunspell is always used regardless of the setting. This method must also be called before any spelling is done otherwise it will throw an error.
94+
95+
This returns nothing.

lib/spellchecker.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,8 @@ module.exports = {
103103
getAvailableDictionaries: getAvailableDictionaries,
104104
getCorrectionsForMisspelling: getCorrectionsForMisspelling,
105105
getDictionaryPath: getDictionaryPath,
106-
Spellchecker: Spellchecker
106+
Spellchecker: Spellchecker,
107+
USE_SYSTEM_DEFAULTS: 0,
108+
ALWAYS_USE_SYSTEM: 1,
109+
ALWAYS_USE_HUNSPELL: 2,
107110
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"test": "jasmine-focused --captureExceptions --coffee spec/"
1717
},
1818
"devDependencies": {
19-
"jasmine-focused": "1.x"
19+
"jasmine-focused": "^1.0.7"
2020
},
2121
"dependencies": {
2222
"any-promise": "^1.3.0",

0 commit comments

Comments
 (0)