-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: fix add a timeout to generating suggestions. (#1662)
- Loading branch information
Showing
16 changed files
with
384 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { CompoundWordsMethod } from './walker'; | ||
|
||
export interface GenSuggestionOptionsStrict { | ||
/** | ||
* Controls forcing compound words. | ||
* @default CompoundWordsMethod.NONE | ||
*/ | ||
compoundMethod?: CompoundWordsMethod; | ||
/** | ||
* ignore case when searching. | ||
*/ | ||
ignoreCase: boolean; | ||
/** | ||
* Maximum number of "edits" allowed. | ||
* 3 is a good number. Above 5 can be very slow. | ||
*/ | ||
maxNumChanges: number; | ||
} | ||
|
||
export type GenSuggestionOptions = Partial<GenSuggestionOptionsStrict>; | ||
|
||
export interface SuggestionOptionsStrict extends GenSuggestionOptionsStrict { | ||
/** | ||
* Maximum number of suggestions to make. | ||
*/ | ||
numSuggestions: number; | ||
/** | ||
* Allow ties when making suggestions. | ||
* if `true` it is possible to have more than `numSuggestions`. | ||
*/ | ||
allowTies: boolean; | ||
/** | ||
* Time alloted in milliseconds to generate suggestions. | ||
*/ | ||
timeout: number; | ||
} | ||
|
||
export type SuggestionOptions = Partial<SuggestionOptionsStrict>; | ||
|
||
export const defaultGenSuggestionOptions: GenSuggestionOptionsStrict = { | ||
compoundMethod: CompoundWordsMethod.NONE, | ||
ignoreCase: true, | ||
maxNumChanges: 5, | ||
}; | ||
|
||
export const defaultSuggestionOptions: SuggestionOptionsStrict = { | ||
...defaultGenSuggestionOptions, | ||
numSuggestions: 8, | ||
allowTies: true, | ||
timeout: 5000, | ||
}; | ||
|
||
/** | ||
* Create suggestion options using composition. | ||
* @param opts - partial options. | ||
* @returns Options - with defaults. | ||
*/ | ||
export function createSuggestionOptions(...opts: SuggestionOptions[]): SuggestionOptionsStrict { | ||
const options = { ...defaultSuggestionOptions }; | ||
const keys = Object.keys(defaultSuggestionOptions) as (keyof SuggestionOptions)[]; | ||
for (const opt of opts) { | ||
for (const key of keys) { | ||
assign(options, opt, key); | ||
} | ||
} | ||
return options; | ||
} | ||
|
||
function assign<T, K extends keyof T>(dest: T, src: Partial<T>, k: K) { | ||
dest[k] = src[k] ?? dest[k]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.