diff --git a/src/services/dictionary-test/dictionary-test.ts b/src/services/dictionary-test/dictionary-test.ts index 0ceb233a..8598fdd3 100644 --- a/src/services/dictionary-test/dictionary-test.ts +++ b/src/services/dictionary-test/dictionary-test.ts @@ -1,11 +1,14 @@ import * as fs from 'fs' -import { Dictionary } from 'services' +import { Dictionary, IBasicData } from 'services' -import basicData from './basic.json' +import testData from './basic.json' import { deepDiffMapper } from './deepDiffMapper' import testSnapshot from './snapshot.json' +const basicData = testData as unknown as IBasicData + + Dictionary.init(basicData.wordList, basicData.searchIndex) const snapshot = {} diff --git a/src/services/dictionary.ts b/src/services/dictionary.ts index af7789b2..f5b5fa15 100644 --- a/src/services/dictionary.ts +++ b/src/services/dictionary.ts @@ -158,6 +158,9 @@ export interface ITranslateResult { export const ISV_SRC = 'isv-src' export const ISV = 'isv' +export type WordList = string[][] +export type SearchIndex = Record> + class DictionaryClass { public static getInstance(): DictionaryClass { if (!DictionaryClass.instance) { @@ -173,7 +176,7 @@ class DictionaryClass { private langsList: string[] private headerIndexes: Map private percentsOfChecked: {[lang: string]: string} - private words: string[][] + private words: WordList private splittedMap: {[lang: string]: Map} private isvSearchLetters: { from: string[], to: string[] } private isvSearchByWordForms: boolean @@ -188,9 +191,9 @@ class DictionaryClass { } public init( - wordList: string[][], - searchIndex?: any | false, - percentsOfChecked?: any, + wordList: WordList, + searchIndex?: SearchIndex, + percentsOfChecked?: Record, ): number { let startInitTime = 0 @@ -284,7 +287,7 @@ class DictionaryClass { return initTime } - public addLang(wordList: string[], searchIndex?: any) { + public addLang(wordList: string[], searchIndex?: SearchIndex) { const lang = wordList[0] if (this.hasLang(lang)) { @@ -303,7 +306,7 @@ class DictionaryClass { public hasLang(lang): boolean { return this.headerIndexes.has(lang) } - public getWordList(): string[][] { + public getWordList(): WordList { return this.words } public getWord(wordId: string) { @@ -311,7 +314,7 @@ class DictionaryClass { return this.words.filter((line) => this.getField(line, 'id') === wordId)[0] } } - public getIndex() { + public getIndex(): SearchIndex { const searchIndex = {}; [ @@ -326,7 +329,7 @@ class DictionaryClass { return searchIndex } - public translate(translateParams: ITranslateParams, showTime = true): [string[][], number] { + public translate(translateParams: ITranslateParams, showTime = true): [WordList, number] { const { inputText, from, @@ -517,7 +520,7 @@ class DictionaryClass { } public formatTranslate( - results: string[][], + results: WordList, from: string, to: string, flavorisationType: string, diff --git a/src/services/fetchDictionary.ts b/src/services/fetchDictionary.ts index bd90d281..25758efd 100644 --- a/src/services/fetchDictionary.ts +++ b/src/services/fetchDictionary.ts @@ -2,7 +2,7 @@ import { addLangs } from 'consts' import { isLoadingAction, runSearch } from 'actions' -import { Dictionary } from 'services' +import { Dictionary, SearchIndex, WordList } from 'services' async function fetchStat() { return fetch('data/translateStatistic.json').then((res) => res.json()).then((data) => data) @@ -14,7 +14,12 @@ async function fetchLangs(langList: string[]) { ) } -async function fetchBasic() { +export interface IBasicData { + wordList: WordList, + searchIndex?: SearchIndex, +} + +async function fetchBasic(): Promise { return await fetch('data/basic.json').then((res) => res.json()) }