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

Commit 069bff7

Browse files
committed
Merge pull request #21 from hammerandchisel/master
use bindings to eliminate fixed path search for node module
2 parents 2b78185 + 7db13e7 commit 069bff7

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

lib/spellchecker.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
var path = require('path');
2-
var bindings = require('../build/Release/spellchecker.node');
2+
var bindings = require('bindings')('spellchecker.node');
33

44
var Spellchecker = bindings.Spellchecker;
55

66
var defaultSpellcheck = null;
7-
var ensureDefaultSpellCheck = function() {
8-
if (defaultSpellcheck) return;
97

8+
var setDictionary = function(lang, dictPath) {
109
defaultSpellcheck = new Spellchecker();
11-
1210
// NB: Windows 8 uses *dashes* to set the language (i.e. en-US), so if we fail
1311
// to set the language, try the Windows 8 way
14-
var dict = getDictionaryPath();
15-
if (!defaultSpellcheck.setDictionary('en_US', dict)) {
16-
defaultSpellcheck.setDictionary('en-US', dict);
12+
lang = lang.replace('_', '-');
13+
if (!defaultSpellcheck.setDictionary(lang, dictPath)) {
14+
lang = lang.replace('-', '_');
15+
defaultSpellcheck.setDictionary(lang, dictPath);
16+
}
17+
};
18+
19+
var ensureDefaultSpellCheck = function() {
20+
if (defaultSpellcheck) {
21+
return;
1722
}
23+
setDictionary('en_US', getDictionaryPath());
1824
};
1925

2026
var isMisspelled = function() {
@@ -55,6 +61,7 @@ var getDictionaryPath = function() {
5561
}
5662

5763
module.exports = {
64+
setDictionary: setDictionary,
5865
add: add,
5966
isMisspelled: isMisspelled,
6067
getAvailableDictionaries: getAvailableDictionaries,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"jasmine-focused": "1.x"
2525
},
2626
"dependencies": {
27+
"bindings": "^1.2.1",
2728
"nan": "^2.0.0"
2829
}
2930
}

spec/spellchecker-spec.coffee

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ describe "SpellChecker", ->
4040
expect(Array.isArray(dictionaries)).toBe true
4141

4242
# Dictionaries do not appear to be available on AppVeyor
43-
unless process.platform is 'win32' and process.env.CI
43+
unless process.platform is 'win32' and (process.env.CI or process.env.SPELLCHECKER_PREFER_HUNSPELL)
4444
expect(dictionaries.length).toBeGreaterThan 0
4545

4646
for dictionary in dictionaries.length
4747
expect(typeof dictionary).toBe 'string'
4848
expect(diction.length).toBeGreaterThan 0
49+
50+
describe ".setDictionary(lang, dictDirectory)", ->
51+
it "sets the spell checker's language, and dictionary directory", ->
52+
awesome = true
53+
expect(awesome).toBe true

src/spellchecker_hunspell.cc

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55

66
namespace spellchecker {
77

8-
HunspellSpellchecker::HunspellSpellchecker() {
9-
this->hunspell = NULL;
10-
}
11-
8+
HunspellSpellchecker::HunspellSpellchecker() : hunspell(nullptr) { }
129
HunspellSpellchecker::~HunspellSpellchecker() {
13-
if (!this->hunspell) return;
14-
15-
delete this->hunspell;
10+
if (hunspell) {
11+
delete hunspell;
12+
}
1613
}
1714

1815
bool HunspellSpellchecker::SetDictionary(const std::string& language, const std::string& dirname) {
19-
if (hunspell != NULL) {
20-
delete this->hunspell;
21-
hunspell = NULL;
16+
if (hunspell) {
17+
delete hunspell;
18+
hunspell = nullptr;
2219
}
2320

2421
// NB: Hunspell uses underscore to separate language and locale, and Win8 uses
@@ -34,9 +31,9 @@ bool HunspellSpellchecker::SetDictionary(const std::string& language, const std:
3431
if (!handle) {
3532
return false;
3633
}
37-
3834
fclose(handle);
39-
this->hunspell = new Hunspell(affixpath.c_str(), dpath.c_str());
35+
36+
hunspell = new Hunspell(affixpath.c_str(), dpath.c_str());
4037
return true;
4138
}
4239

@@ -45,24 +42,32 @@ std::vector<std::string> HunspellSpellchecker::GetAvailableDictionaries(const st
4542
}
4643

4744
bool HunspellSpellchecker::IsMisspelled(const std::string& word) {
48-
return this->hunspell->spell(word.c_str()) == 0;
45+
if (!hunspell) {
46+
return false;
47+
}
48+
return hunspell->spell(word.c_str()) == 0;
4949
}
5050

5151
void HunspellSpellchecker::Add(const std::string& word) {
52-
this->hunspell->add(word.c_str());
52+
if (hunspell) {
53+
hunspell->add(word.c_str());
54+
}
5355
}
5456

5557
std::vector<std::string> HunspellSpellchecker::GetCorrectionsForMisspelling(const std::string& word) {
5658
std::vector<std::string> corrections;
57-
char** slist;
58-
int size = hunspell->suggest(&slist, word.c_str());
5959

60-
corrections.reserve(size);
61-
for (int i = 0; i < size; ++i) {
62-
corrections.push_back(slist[i]);
63-
}
60+
if (hunspell) {
61+
char** slist;
62+
int size = hunspell->suggest(&slist, word.c_str());
63+
64+
corrections.reserve(size);
65+
for (int i = 0; i < size; ++i) {
66+
corrections.push_back(slist[i]);
67+
}
6468

65-
this->hunspell->free_list(&slist, size);
69+
hunspell->free_list(&slist, size);
70+
}
6671
return corrections;
6772
}
6873

0 commit comments

Comments
 (0)