| 1 |
interface SpellCheckTerms { |
| 2 |
mode: 'INCLUDE' | 'EXCLUDE'; |
| 3 |
dictionary: string; |
| 4 |
} |
| 5 |
|
| 6 |
interface SpellCheckOptions { |
| 7 |
DISTANCE?: number; |
| 8 |
TERMS?: SpellCheckTerms | Array<SpellCheckTerms>; |
| 9 |
DIALECT?: number; |
| 10 |
} |
| 11 |
|
| 12 |
export function transformArguments(index: string, query: string, options?: SpellCheckOptions): Array<string> { |
| 13 |
const args = ['FT.SPELLCHECK', index, query]; |
| 14 |
|
| 15 |
if (options?.DISTANCE) { |
| 16 |
args.push('DISTANCE', options.DISTANCE.toString()); |
| 17 |
} |
| 18 |
|
| 19 |
if (options?.TERMS) { |
| 20 |
if (Array.isArray(options.TERMS)) { |
| 21 |
for (const term of options.TERMS) { |
| 22 |
pushTerms(args, term); |
| 23 |
} |
| 24 |
} else { |
| 25 |
pushTerms(args, options.TERMS); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
if (options?.DIALECT) { |
| 30 |
args.push('DIALECT', options.DIALECT.toString()); |
| 31 |
} |
| 32 |
|
| 33 |
return args; |
| 34 |
} |
| 35 |
|
| 36 |
function pushTerms(args: Array<string>, { mode, dictionary }: SpellCheckTerms): void { |
| 37 |
args.push('TERMS', mode, dictionary); |
| 38 |
} |
| 39 |
|
| 40 |
type SpellCheckRawReply = Array<[ |
| 41 |
_: string, |
| 42 |
term: string, |
| 43 |
suggestions: Array<[score: string, suggestion: string]> |
| 44 |
]>; |
| 45 |
|
| 46 |
type SpellCheckReply = Array<{ |
| 47 |
term: string, |
| 48 |
suggestions: Array<{ |
| 49 |
score: number, |
| 50 |
suggestion: string |
| 51 |
}> |
| 52 |
}>; |
| 53 |
|
| 54 |
export function transformReply(rawReply: SpellCheckRawReply): SpellCheckReply { |
| 55 |
return rawReply.map(([, term, suggestions]) => ({ |
| 56 |
term, |
| 57 |
suggestions: suggestions.map(([score, suggestion]) => ({ |
| 58 |
score: Number(score), |
| 59 |
suggestion |
| 60 |
})) |
| 61 |
})); |
| 62 |
} |