Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ env:

jobs:
Test:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-2022, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: 20
- name: Install dependencies
run: npm i
- name: Run tests
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.DS_Store
build/
node_modules/
.cache
.tool-versions
npm-debug.log

compile_commands.json
package-lock.json
27 changes: 25 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
['OS=="win"', {
'msvs_disabled_warnings': [
4267, # conversion from 'size_t' to 'int', possible loss of data
4530, # C++ exception handler used, but unwind semantics are not enabled
4530, # C++ exception handler used, but unwind semantics are not
# enabled
4506, # no definition for inline function
],
}],
Expand All @@ -33,7 +34,19 @@
'targets': [
{
'target_name': 'spellchecker',
'include_dirs': [ '<!(node -e "require(\'nan\')")' ],
'cflags!': ['-fno-exceptions'],
'cflags_cc!': ['-fno-exceptions'],
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': {'ExceptionHandling': 1},
},
'include_dirs': [
'<!(node -p "require(\'node-addon-api\').include_dir")'
],
'sources': [
'src/main.cc',
'src/worker.cc'
Expand Down Expand Up @@ -78,6 +91,16 @@
'targets': [
{
'target_name': 'hunspell',
'cflags!': ['-fno-exceptions'],
'cflags_cc!': ['-fno-exceptions'],
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': {'ExceptionHandling': 1},
},
'type': 'static_library',
'msvs_guid': 'D5E8DCB2-9C61-446F-8BEE-B18CA0E0936E',
'defines': [
Expand Down
123 changes: 54 additions & 69 deletions lib/spellchecker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var path = require('path');
var Promise = require('any-promise');
var bindings = require('../build/Release/spellchecker.node');
const path = require('path');
const fs = require('fs');
const Promise = require('any-promise');
const bindings = require('../build/Release/spellchecker.node');

var Spellchecker = bindings.Spellchecker;
const Spellchecker = bindings.Spellchecker;

var checkSpellingAsyncCb = Spellchecker.prototype.checkSpellingAsync
const checkSpellingAsyncCb = Spellchecker.prototype.checkSpellingAsync

Spellchecker.prototype.checkSpellingAsync = function (corpus) {
return new Promise(function (resolve, reject) {
Expand All @@ -18,92 +19,76 @@ Spellchecker.prototype.checkSpellingAsync = function (corpus) {
}.bind(this));
};

var defaultSpellcheck = null;
let defaultSpellcheck = null;

var ensureDefaultSpellCheck = function() {
if (defaultSpellcheck) {
return;
}
function ensureDefaultSpellCheck () {
if (!defaultSpellcheck) return;

var lang = process.env.LANG;
lang = lang ? lang.split('.')[0] : 'en_US';
let lang = process.env.LANG || undefined;
lang = lang?.split('.')[0] ?? 'en_US';
defaultSpellcheck = new Spellchecker();

setDictionary(lang, getDictionaryPath());
};

var setDictionary = function(lang, dictPath) {
ensureDefaultSpellCheck();
return defaultSpellcheck.setDictionary(lang, dictPath);
};

var isMisspelled = function() {
ensureDefaultSpellCheck();

return defaultSpellcheck.isMisspelled.apply(defaultSpellcheck, arguments);
};

var checkSpelling = function() {
ensureDefaultSpellCheck();

return defaultSpellcheck.checkSpelling.apply(defaultSpellcheck, arguments);
};

var checkSpellingAsync = function(corpus) {
ensureDefaultSpellCheck();

return defaultSpellcheck.checkSpellingAsync.apply(defaultSpellcheck, arguments);
};
return defaultSpellcheck;
}

var add = function() {
ensureDefaultSpellCheck();
function setDictionary (lang, dictPath) {
return ensureDefaultSpellCheck().setDictionary(lang, dictPath);
}

defaultSpellcheck.add.apply(defaultSpellcheck, arguments);
};
function isMisspelled (...args) {
return ensureDefaultSpellCheck().isMisspelled(...args);
}

var remove = function() {
ensureDefaultSpellCheck();
function checkSpelling (...args) {
return ensureDefaultSpellCheck().checkSpelling(...args);
}

defaultSpellcheck.remove.apply(defaultSpellcheck, arguments);
};
function checkSpellingAsync (...args) {
return ensureDefaultSpellCheck().checkSpellingAsync(...args);
}

var getCorrectionsForMisspelling = function() {
ensureDefaultSpellCheck();
function add (...args) {
return ensureDefaultSpellCheck().add(...args);
}

return defaultSpellcheck.getCorrectionsForMisspelling.apply(defaultSpellcheck, arguments);
};
function remove (...args) {
return ensureDefaultSpellCheck().remove(...args);
}

var getAvailableDictionaries = function() {
ensureDefaultSpellCheck();
function getCorrectionsForMisspelling (...args) {
return ensureDefaultSpellCheck().getCorrectionsForMisspelling(...args);
}

return defaultSpellcheck.getAvailableDictionaries.apply(defaultSpellcheck, arguments);
};
function getAvailableDictionaries (...args) {
return ensureDefaultSpellCheck().getAvailableDictionaries(...args);
}

var getDictionaryPath = function() {
var dict = path.join(__dirname, '..', 'vendor', 'hunspell_dictionaries');
function getDictionaryPath () {
let 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').statSync(unpacked)) {
let unpacked = dict.replace(`.asar${path.sep}`, `.asar.unpacked${path.sep}`);
if (fs.statSync(unpacked)) {
dict = unpacked;
}
} catch (error) {
// When the dictionary isn't contained within an .asar, return the original path.
// When the dictionary isn't contained within an .asar, return the original
// path.
}
return dict;
}


module.exports = {
setDictionary: setDictionary,
add: add,
remove: remove,
isMisspelled: isMisspelled,
checkSpelling: checkSpelling,
checkSpellingAsync: checkSpellingAsync,
getAvailableDictionaries: getAvailableDictionaries,
getCorrectionsForMisspelling: getCorrectionsForMisspelling,
getDictionaryPath: getDictionaryPath,
Spellchecker: Spellchecker,
setDictionary,
add,
remove,
isMisspelled,
checkSpelling,
checkSpellingAsync,
getAvailableDictionaries,
getCorrectionsForMisspelling,
getDictionaryPath,
Spellchecker,
USE_SYSTEM_DEFAULTS: 0,
ALWAYS_USE_SYSTEM: 1,
ALWAYS_USE_HUNSPELL: 2,
Expand Down
Loading
Loading