From c52b655134afcd571f2642da9fee94ce259191fe Mon Sep 17 00:00:00 2001 From: Dale Fenton Date: Fri, 1 Feb 2019 10:27:31 -0500 Subject: [PATCH] add types for spellchecker package --- types/spellchecker/index.d.ts | 63 ++++++++++++++++++++++++ types/spellchecker/spellchecker-tests.ts | 41 +++++++++++++++ types/spellchecker/tsconfig.json | 23 +++++++++ types/spellchecker/tslint.json | 1 + 4 files changed, 128 insertions(+) create mode 100644 types/spellchecker/index.d.ts create mode 100644 types/spellchecker/spellchecker-tests.ts create mode 100644 types/spellchecker/tsconfig.json create mode 100644 types/spellchecker/tslint.json diff --git a/types/spellchecker/index.d.ts b/types/spellchecker/index.d.ts new file mode 100644 index 0000000000..0863e19646 --- /dev/null +++ b/types/spellchecker/index.d.ts @@ -0,0 +1,63 @@ +// Type definitions for spellchecker 3.5 +// Project: http://atom.github.io/node-spellchecker +// Definitions by: Dale Fenton +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +export as namespace Spellchecker; + +/* --------------------------------------------------------------------------- + Custom Types / Interfaces +--------------------------------------------------------------------------- */ +/** + * MisspelledLocation - shape of an object returned by checkSpelling to + * identify locations of misspelled words in a corpus. + * @description a misspelled word can be found by corpus.slice(start, end) + * start - start index of a misspelled word in a corpus + * end - end index of a misspelled word in a corpus + */ +export interface MisspelledLocation { + start: number; + end: number; +} + +/* --------------------------------------------------------------------------- + Methods +--------------------------------------------------------------------------- */ +/** + * Spellchecker.isMisspelled - Check if a word is misspelled. + * @param word - String word to check. + * @returns boolean - true if the word is misspelled, false otherwise. + */ +export function isMisspelled(word: string): boolean; + +/** + * Spellchecker.getCorrectionsForMisspelling - Get the corrections for a misspelled word. + * @param word - String word to get corrections for. + * @returns array - Returns a non-null but possibly empty array of string corrections. + */ +export function getCorrectionsForMisspelling(word: string): string[]; + +/** + * Spellchecker.checkSpelling - Identify misspelled words in a corpus of text. + * @param corpus - String corpus of text to spellcheck. + * @returns array - Returns an Array containing {start, end} objects that describe an + * index range within the original String that contains a misspelled word. + */ +export function checkSpelling(corpus: string): MisspelledLocation[]; + +/** + * Spellchecker.checkSpellingAsync - Asynchronously identify misspelled words. + * @param corpus - String corpus of text to spellcheck. + * @returns array - Returns a Promise that resolves with the Array described by checkSpelling(). + */ +export function checkSpellingAsync(corpus: string): Promise; + +/** + * Spellchecker.add - Adds a word to the dictionary. + * When using Hunspell, this will not modify the .dic file; + * new words must be added each time the spellchecker is created. Use a custom dictionary file. + * @param word - String word to add. + * @returns void + */ +export function add(word: string): void; diff --git a/types/spellchecker/spellchecker-tests.ts b/types/spellchecker/spellchecker-tests.ts new file mode 100644 index 0000000000..7f53b26ab0 --- /dev/null +++ b/types/spellchecker/spellchecker-tests.ts @@ -0,0 +1,41 @@ +import * as Spellchecker from 'spellchecker'; + +const additionalDictionary = ['Xynoronmalade']; + +// add words to the dictionary +additionalDictionary.forEach(word => Spellchecker.add(word)); + +let testWord = 'kat'; +let testCorpus = 'The queeck brown fox jumped over the lazy dog, from Xynoronmalade'; +let testCorpusAsync = 'The queeck brown fox jumped over the lazy dog, from Xynoronmalade'; + +// check and correct a single word +if (Spellchecker.isMisspelled(testWord)) { + const corrections = Spellchecker.getCorrectionsForMisspelling(testWord); + if (corrections.length > 0) { + testWord = corrections[0]; + } +} + +// check and correct words in a body of text +const misspellings = Spellchecker.checkSpelling(testCorpus); +misspellings.forEach(location => { + const misspelled = testCorpus.slice(location.start, location.end); + const corrections = Spellchecker.getCorrectionsForMisspelling(misspelled); + if (corrections.length > 0) { + testCorpus = testCorpus.replace(misspelled, corrections[0]); + } +}); + +// check for corrections asynchronously +const asyncSpellCheck = async () => { + // check and correct words in a body of text + const misspellings = await Spellchecker.checkSpellingAsync(testCorpusAsync); + misspellings.forEach(location => { + const misspelled = testCorpusAsync.slice(location.start, location.end); + const corrections = Spellchecker.getCorrectionsForMisspelling(misspelled); + if (corrections.length > 0) { + testCorpusAsync = testCorpusAsync.replace(misspelled, corrections[0]); + } + }); +}; diff --git a/types/spellchecker/tsconfig.json b/types/spellchecker/tsconfig.json new file mode 100644 index 0000000000..6817057d8c --- /dev/null +++ b/types/spellchecker/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "spellchecker-tests.ts" + ] +} diff --git a/types/spellchecker/tslint.json b/types/spellchecker/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/spellchecker/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }