From 53416a55152e53693336d08bc180e59137e31053 Mon Sep 17 00:00:00 2001 From: Eddie Antonio Santos Date: Tue, 21 Apr 2020 14:58:51 -0600 Subject: [PATCH] refactor(common/lmlayer): factor out compileWordBreaker() --- .../lexical-model-compiler.ts | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/developer/js/source/lexical-model-compiler/lexical-model-compiler.ts b/developer/js/source/lexical-model-compiler/lexical-model-compiler.ts index 6aabbbc155..ca381edd62 100644 --- a/developer/js/source/lexical-model-compiler/lexical-model-compiler.ts +++ b/developer/js/source/lexical-model-compiler/lexical-model-compiler.ts @@ -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']`; + } +}