Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

hunspell-asm

kwonoj54.4kMIT4.0.2TypeScript support: included

WebAssembly based Javascript bindings for hunspell spellchecker

WebAssembly, Wasm, Spellchecker, Hunspell, Language

readme

Build Status Build status codecov npm node

Hunspell-asm

Hunspell-asm is isomorphic javascript binding to hunspell spellchecker based on WebAssembly hunspell binary. This module aims to provide thin, lightweight interface to hunspell without requiring native modules.

Install

npm install hunspell-asm

Usage

Loading module asynchronously

Hunspell-asm relies on wasm binary of hunspell, which need to be initialized first.

import { loadModule } from 'hunspell-asm';

const hunspellFactory = await loadModule();

loadModule loads wasm binary, initialize it, and returns factory function to create instance of hunspell.

loadModule({ timeout?: number }): Promise<HunspellFactory>

It allows to specify timeout to wait until wasm binary compliation & load.

Mounting files

Wasm binary uses different memory spaces allocated for its own and cannot access plain javascript object / or files directly. HunspellFactory provides few interfaces to interop file contents into hunspell.

  • mountBuffer(contents: ArrayBufferView, fileName?: string): string : Mount contents of file.
  • unmount(mountedFilePath: string) : Unmount path if it's exists in memory. If it's bufferFile created by mountBuffer, unmount will remove those file object in wasm memory as well.

All of virtual paths for mounted filesystem uses unix separator regardless of platform.

Creating spellchecker

Once you mounted dic / aff files you can create hunspell spellchecker instance via HunspellFactory::create. Each path for files are mounted path and should not be actual path or server endpoint.

create(affPath: string, dictPath: string): Hunspell

Hunspell exposes minimal interfaces to spellchecker.

  • spell(word: string): boolean : Check spelling for word. False for misspelled, True otherwise.
  • suggest(word: string): Array<string> : Get suggestion list for misspelled word. Empty if word is not misspelled or no suggestions.
  • dispose(): void : Destroy current instance of hunspell. It is important to note created instance of hunspell will not be destroyed automatically.

There are simple examples for each environments using different apis. In each example directory do npm install && npm start.

Adding words to dictionary in runtime

Hunspell exposes few interfaces allow to add words, or dictionaries in existing dictionary in runtime. This is runtime behavior, so it doesn't persist over once instance is disposed.

  • addWord(word: string): void : add single word to current dictionary.
  • removeWord(word: string): void : remove single word from current dictionary.
  • addWordWithAffix(word: string, affix: string): void: add word with example word having affix flag to be applied. Second param affix is example word, should exists in current dictionary with its own affix flag. Newly added word will have same affix rule as example word.
  • addDictionary(dictPath): boolean : Load addtional dictionary into existing hunspell instance. This cannot load additional affi x. If function returns false, it means internal slot hunspell manages are full and can't add additional dictionaries.

Things to note

  • Ensure all inputs (aff, dic, word for spell / suggest) are UTF-8 encoded correctly. While hunspell itself supports other encodings, all surrounding interfaces passing buffers are plain javascript doesn't detect / converts encodings automatically.

Building / Testing

Few npm scripts are supported for build / test code.

  • build: Transpiles code to ES5 commonjs to dist.
  • test: Run hunspell / hunspell-asm test both. Does not require build before execute test.
  • test:hunspell: Run integration test for actual hunspell wasm binary, using hunspell's test case as-is.
  • test:hunspell-asm: Run unit test against hunspell-asm interface
  • lint: Run lint over all codebases
  • lint:staged: Run lint only for staged changes. This'll be executed automatically with precommit hook.
  • commit: Commit wizard to write commit message

License

changelog

4.0.2 (2019-10-24)

Features

  • hunspell: bump up binary to reduce size using llvm backend

4.0.1 (2019-10-19)

Bug Fixes

  • hunspell: normalize unicodei to NFC where possible (800b79e)

4.0.0 (2019-09-13)

Features

  • hunspell: reenable optimization (06bdc36)

BREAKING CHANGES

  • hunspell: Unsupport mobile safari version have bugs (#187)

3.0.3 (2019-07-19)

  • bump up dependencies

3.0.2 (2019-06-20)

Bug Fixes

  • hunspell: bump to non-optimized hunspell (97df1f6), closes #187

3.0.1 (2019-06-13)

Bug Fixes

  • mountbuffer: fix dependencies (a434cd8)

3.0.0 (2019-06-13)

Features

  • build: target es2018 for output (27f6400)
  • hunspell: load env specific binary (53df646)
  • hunspellloader: deprecate mountdirectory (cb96561)

BREAKING CHANGES

  • hunspellloader: dictionary should be mounted via buffer
  • build: requires runtime for es2018, or need downlevel transpiling

2.0.0 (2019-01-31)

  • Bump up beta.8 to official release

2.0.0-beta.8 (2019-01-31)

Bug Fixes

  • suggest: free allocated ptr for list (6708573)

2.0.0-beta.7 (2019-01-30)

Features

  • hunspell: bump up hunspell binary (9ed737d)
  • loadmodule: deprecate locatebinary, back to single file (4b8f5b0)

2.0.0-beta.6 (2019-01-27)

Features

  • loadmodule: deprecate asm (b49b731)

2.0.0-beta.5 (2019-01-26)

Bug Fixes

  • createmoduleloader: fix non treeshakable import (a77a149)

2.0.0-beta.4 (2019-01-26)

Bug Fixes

  • createmoduleloader: apply overridden env (22645b6)

2.0.0-beta.3 (2019-01-26)

Features

  • hunspell: bump up hunspell with asmjs (845ee54)
  • loadasmmodule: asmjs loader (a620219)
  • loadmodule: reintroduce env override (d2f651d)

2.0.0-beta.2 (2018-11-02)

Bug Fixes

  • allocstring: use emscripten method, explicitly free (09a1a56)

Features

  • hunspell: update interfaces (c937dec)
  • hunspellloader: implement addword interfaces (9a56a35)
  • wraphunspellinterface: expose add interfaces (73e5d1c)

2.0.0-beta.1 (2018-11-01)

BREAKING CHANGES:

  • ENVIRONMENT override deprecated
  • loadModule accepts timeout option as InitOptions object instead of single argument, like loadModule({timeout: 3000});

Features

  • hunspell: support separate binary, expose locatebinary (978e1a8)

1.1.2 (2018-10-19)

Features

  • loadmodule: Change timeout option in loadModule to auto-destructure (925362e)
  • loadmodule: Expose timeout option to loadModule (71c29af)

1.0.2 (2018-03-07)

Features

  • hunspell: bump up hunspell binary (e931003)

1.0.1 (2018-02-04)

Bug Fixes

  • hunspellasmmodule: update runtime module signature (cbf45f9)

1.0.0 (2017-11-30)

Bug Fixes

  • package: update nanoid to version 1.0.0 (f69df27)

Features

  • hunspell: update hunspell into single file binary (8b9e6ea)
  • loadmodule: support single file binary load (16ff979)
  • logger: enablelogger appends scope (36c429c)

BREAKING CHANGES

  • loadmodule: now runs on native-wasm supported runtime only

0.0.17 (2017-10-10)

Features

  • hunspell: bump up hunspell (482cfde)

0.0.16 (2017-09-22)

0.0.15 (2017-09-21)

Bug Fixes

  • mountdirectory: accept environment instead of internal detect (b24dcee)

Features

  • loadmodule: fallback to asm.js when wasm load fail (bac803c)

0.0.14 (2017-09-18)

Bug Fixes

  • preamble: support electron without require (cced45a)

0.0.13 (2017-09-16)

Features

  • hunspell: bump up hunspell (a38c5dc)

0.0.12 (2017-09-15)

Bug Fixes

  • environment: fix browser environment module loading (b2e16d6)

0.0.11 (2017-09-15)

Features

  • hunspell: bump up hunspell (4e7b6d6)
  • loadmodule: support environment override (b79f7cf)
  • logger: wire wasm loader logger (ba59165)

0.0.10 (2017-09-12)

0.0.9 (2017-09-10)

Features

  • mount: replace mount id into nanoid (dfd630e)

0.0.8 (2017-09-10)

Features

  • index: export interfaces (39ec725)

0.0.7 (2017-09-08)

0.0.6 (2017-08-26)

0.0.5 (2017-08-23)

Features

  • hunspell: bump up hunspell (77367b9)

0.0.4 (2017-07-30)

0.0.3 (2017-07-29)

0.0.2 (2017-07-28)

0.0.1 (2017-07-28)

Bug Fixes

  • hunspellLoader: support relative current path (76dad41)
  • isNode: detect node.js via process.versions (19872ef)
  • loadModule: update init logic (575e43a)
  • mountDirectory: support cross-platform path mount (4c81044)
  • suggest: return empty array when there isn't suggestions (c679edc)
  • unmount: unlink memory file correctly (dd12c63)

Features

  • hunspell: bump up hunspell (5640dd9)
  • hunspell: bump up hunspell (a0f2c22)
  • hunspell: bump up hunspell (be7895a)
  • hunspell: bump up hunspell (8fab8f1)
  • hunspell: bump up hunspell (262ebca)
  • hunspell: bump up hunspell (daf0cbb)
  • hunspell: bump up hunspell binary (b5aec3f)
  • hunspell: bump up hunspell binary from local build (cd31a6c)
  • hunspell: initial wasm binary (d2acfc1)
  • Hunspell: define initial interface (d1008bc)
  • hunspellLoader: implement initial interface (f0ef545)
  • hunspellLoader: implement spell logic (376ce73)
  • hunspellLoader: split file mount, support ArrayBuffer loading (ac2e7e6)
  • loadModule: support binaryEndpoint for browser (3b77e4f)
  • logger: implement logger function (8f0ea01)
  • mountBuffer: accept ArrayBufferView directly (be3863d)
  • suggest: implement suggestion interface (35b2bbf)
  • util: add utility functions (a696a98)