refactor(common/lmlayer): extract type guard for simple wordbreaker spec

This commit is contained in:
Eddie Antonio Santos 2020-04-23 13:30:13 -06:00
parent 57f15578d5
commit 3013d2a4fa
No known key found for this signature in database
GPG key ID: DD411EA4D9509D87

View file

@ -114,12 +114,10 @@ function compileWordBreaker(spec: WordBreakerSpec): string {
* normalizes it to a common form that the compiler can deal with.
*/
function normalizeWordBreakerSpec(wordBreakerSpec: LexicalModelSource["wordBreaker"]): WordBreakerSpec {
if (!wordBreakerSpec) {
if (wordBreakerSpec == undefined) {
// Use the default word breaker when it's unspecified
return { use: 'default' };
} else if (wordBreakerSpec === "default" || wordBreakerSpec === 'ascii') {
return { use: wordBreakerSpec };
} else if (typeof wordBreakerSpec === "function") {
} else if (isSimpleWordBreaker(wordBreakerSpec)) {
// The word breaker was passed as a literal function; use its source code.
return { use: wordBreakerSpec };
} else if (wordBreakerSpec.use) {
@ -129,3 +127,6 @@ function normalizeWordBreakerSpec(wordBreakerSpec: LexicalModelSource["wordBreak
}
}
function isSimpleWordBreaker(spec: WordBreakerSpec | SimpleWordBreakerSpec): spec is SimpleWordBreakerSpec {
return typeof spec === "function" || spec === "default" || spec === "ascii";
}