mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-11 19:35:32 +00:00
refactor(common/lmlayer): factor out compileWordBreaker()
This commit is contained in:
parent
c850419e22
commit
53416a5515
1 changed files with 22 additions and 15 deletions
|
|
@ -29,21 +29,7 @@ export default class LexicalModelCompiler {
|
|||
|
||||
// Figure out what word breaker the model is using, if any.
|
||||
let wordBreakerSpec = getWordBreakerSpec();
|
||||
let wordBreakerSourceCode: string;
|
||||
if (wordBreakerSpec) {
|
||||
if (typeof wordBreakerSpec === "string") {
|
||||
// It must be a builtin word breaker, so just instantiate it.
|
||||
wordBreakerSourceCode = `wordBreakers['${wordBreakerSpec}']`;
|
||||
} else if (typeof wordBreakerSpec === "function") {
|
||||
// The word breaker was passed as a literal function; use its source code.
|
||||
wordBreakerSourceCode = wordBreakerSpec.toString()
|
||||
// Note: the .toString() might just be the property name, but we want a
|
||||
// plain function:
|
||||
.replace(/^wordBreak(ing|er)\b/, 'function');
|
||||
}
|
||||
} else {
|
||||
wordBreakerSourceCode = `wordBreakers['default']`;
|
||||
}
|
||||
let wordBreakerSourceCode = compileWordBreaker(wordBreakerSpec);
|
||||
|
||||
function getWordBreakerSpec() {
|
||||
if (modelSource.wordBreaker) {
|
||||
|
|
@ -112,3 +98,24 @@ export default class LexicalModelCompiler {
|
|||
|
||||
export class ModelSourceError extends Error {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JavaScript expression (as a string) that can serve as a word
|
||||
* breaking function.
|
||||
*/
|
||||
function compileWordBreaker(wordBreakerSpec: string | WordBreakingFunction | undefined) {
|
||||
if (wordBreakerSpec) {
|
||||
if (typeof wordBreakerSpec === "string") {
|
||||
// It must be a builtin word breaker, so just instantiate it.
|
||||
return `wordBreakers['${wordBreakerSpec}']`;
|
||||
} else {
|
||||
// The word breaker was passed as a literal function; use its source code.
|
||||
return wordBreakerSpec.toString()
|
||||
// Note: the .toString() might just be the property name, but we want a
|
||||
// plain function:
|
||||
.replace(/^wordBreak(ing|er)\b/, 'function');
|
||||
}
|
||||
} else {
|
||||
return `wordBreakers['default']`;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue