refactor(common/lmlayer): factor out compileWordBreaker()

This commit is contained in:
Eddie Antonio Santos 2020-04-21 14:58:51 -06:00
parent c850419e22
commit 53416a5515
No known key found for this signature in database
GPG key ID: DD411EA4D9509D87

View file

@ -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']`;
}
}