Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic16x committed Dec 4, 2024
1 parent d3a8b7f commit 9e99b19
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/services/dictionary-test/dictionary-test.ts
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down
21 changes: 12 additions & 9 deletions src/services/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Array<[string, string[]]>>

class DictionaryClass {
public static getInstance(): DictionaryClass {
if (!DictionaryClass.instance) {
Expand All @@ -173,7 +176,7 @@ class DictionaryClass {
private langsList: string[]
private headerIndexes: Map<string, number>
private percentsOfChecked: {[lang: string]: string}
private words: string[][]
private words: WordList
private splittedMap: {[lang: string]: Map<string, string[]>}
private isvSearchLetters: { from: string[], to: string[] }
private isvSearchByWordForms: boolean
Expand All @@ -188,9 +191,9 @@ class DictionaryClass {
}

public init(
wordList: string[][],
searchIndex?: any | false,
percentsOfChecked?: any,
wordList: WordList,
searchIndex?: SearchIndex,
percentsOfChecked?: Record<string, string>,
): number {
let startInitTime = 0

Expand Down Expand Up @@ -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)) {
Expand All @@ -303,15 +306,15 @@ class DictionaryClass {
public hasLang(lang): boolean {
return this.headerIndexes.has(lang)
}
public getWordList(): string[][] {
public getWordList(): WordList {
return this.words
}
public getWord(wordId: string) {
if (this.words && this.words.length) {
return this.words.filter((line) => this.getField(line, 'id') === wordId)[0]
}
}
public getIndex() {
public getIndex(): SearchIndex {
const searchIndex = {};

[
Expand All @@ -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,
Expand Down Expand Up @@ -517,7 +520,7 @@ class DictionaryClass {
}

public formatTranslate(
results: string[][],
results: WordList,
from: string,
to: string,
flavorisationType: string,
Expand Down
9 changes: 7 additions & 2 deletions src/services/fetchDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -14,7 +14,12 @@ async function fetchLangs(langList: string[]) {
)
}

async function fetchBasic() {
export interface IBasicData {
wordList: WordList,
searchIndex?: SearchIndex,
}

async function fetchBasic(): Promise<IBasicData> {
return await fetch('data/basic.json').then((res) => res.json())
}

Expand Down

0 comments on commit 9e99b19

Please sign in to comment.