From da550b45aedbcc79a940f0c3e78a0760caffa3dc Mon Sep 17 00:00:00 2001 From: Andreas Holstenson Date: Sat, 28 Sep 2019 17:20:24 +0200 Subject: [PATCH] refactor: Restoring value matcher functionality --- src/actions.ts | 4 ++-- src/intents.ts | 6 +++--- src/resolver/builder.ts | 2 +- src/resolver/parser.ts | 4 ++-- src/values/base.ts | 32 +++++++++++++++----------------- src/values/index.ts | 2 ++ src/values/options.ts | 6 +++--- test/value-matchers.test.js | 25 ------------------------- test/value-matchers.test.ts | 18 ++++++++++++++++++ 9 files changed, 46 insertions(+), 53 deletions(-) delete mode 100644 test/value-matchers.test.js create mode 100644 test/value-matchers.test.ts diff --git a/src/actions.ts b/src/actions.ts index 612fbf6..17fb3cb 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -1,6 +1,6 @@ import { IntentsBuilder } from './intents'; import { Language } from './language/language'; -import { LanguageSpecificValue, NodeConvertable } from './values/base'; +import { Value } from './values/base'; import { Matcher, EncounterOptions } from './graph/matching'; import { ResolvedIntent } from './resolver/resolved-intent'; import { ResolvedIntents } from './resolver/ResolvedIntents'; @@ -54,7 +54,7 @@ export class ActionsBuilder { } export interface ActionBuilder { - value(id: string, type: LanguageSpecificValue | NodeConvertable): this; + value(id: string, type: Value): this; add(...args: string[]): this; diff --git a/src/intents.ts b/src/intents.ts index 1eeda2f..de9397d 100644 --- a/src/intents.ts +++ b/src/intents.ts @@ -1,6 +1,6 @@ import { ResolverBuilder } from './resolver/builder'; import { Language } from './language/language'; -import { NodeConvertable, LanguageSpecificValue } from './values/base'; +import { Value } from './values/base'; export class IntentsBuilder { private language: Language; @@ -24,7 +24,7 @@ export class IntentsBuilder { const self = this; const instance = new ResolverBuilder(this.language, id); return { - value(id: string, type: LanguageSpecificValue | NodeConvertable) { + value(id: string, type: Value) { instance.value(id, type); return this; }, @@ -47,7 +47,7 @@ export class IntentsBuilder { } export interface IntentBuilder { - value(id: string, type: LanguageSpecificValue | NodeConvertable): this; + value(id: string, type: Value): this; add(...args: string[]): this; diff --git a/src/resolver/builder.ts b/src/resolver/builder.ts index 84f45e2..6d329f3 100644 --- a/src/resolver/builder.ts +++ b/src/resolver/builder.ts @@ -46,7 +46,7 @@ export class ResolverBuilder { }; } - public value(id: string, type: LanguageSpecificValue | NodeConvertable) { + public value(id: string, type: LanguageSpecificValue | NodeConvertable) { this.parser.value(id, type); return this; } diff --git a/src/resolver/parser.ts b/src/resolver/parser.ts index b3a88da..487fc9f 100644 --- a/src/resolver/parser.ts +++ b/src/resolver/parser.ts @@ -4,7 +4,7 @@ import { Matcher, MatcherOptions, EncounterOptions } from '../graph/matching'; import { TokenNode } from '../graph/token'; import { ValueNode } from './value'; -import { LanguageSpecificValue, NodeConvertable } from '../values/base'; +import { LanguageSpecificValue, NodeConvertable, Value } from '../values/base'; import { isDeepEqual } from '../utils/equality'; import { Node } from '../graph/node'; import { Language } from '../language/language'; @@ -29,7 +29,7 @@ export class ResolverParser extends GraphBuilder { this.allowPartial(); } - public value(id: string, type: LanguageSpecificValue | NodeConvertable): this { + public value(id: string, type: Value): this { let factory = type; if(factory instanceof LanguageSpecificValue) { factory = factory.create(this.language); diff --git a/src/values/base.ts b/src/values/base.ts index 38ef174..36ae4fc 100644 --- a/src/values/base.ts +++ b/src/values/base.ts @@ -2,7 +2,7 @@ import { ValueParserNode, ValueParserOptions } from '../resolver/value-parser'; import { ValueNode, ValueNodeOptions } from '../resolver/value'; import { Language } from '../language/language'; import { Node } from '../graph/node'; -import { Matcher } from '../graph/matching'; +import { Matcher, EncounterOptions } from '../graph/matching'; /** * Object that can be converted into a node within a graph. @@ -14,12 +14,14 @@ export interface NodeConvertable { /** * Function that creates a convertable item for the given language. */ -export type LanguageSpecificFactory = (language: Language) => NodeConvertable; +export type LanguageSpecificFactory = (language: Language) => ParsingValue; -export class LanguageSpecificValue { - private factory: LanguageSpecificFactory; +export type Value = LanguageSpecificValue | NodeConvertable; - constructor(factory: LanguageSpecificFactory) { +export class LanguageSpecificValue { + private factory: LanguageSpecificFactory; + + constructor(factory: LanguageSpecificFactory) { this.factory = factory; } @@ -33,29 +35,25 @@ export class LanguageSpecificValue { * @param {Language} language */ public matcher(language: Language) { - /* - const parser = this.factory(language).parser; - - return function(text, options) { - if(typeof text !== 'string') return Promise.resolve(null); + const value = this.factory(language); - return parser.match(text, options); + return function(text: string, options?: EncounterOptions) { + return value.matcher.match(text, options); }; - */ } } -export class ParsingValue { - private parser: Matcher; +export class ParsingValue { + public matcher: Matcher; private options: ValueParserOptions; - constructor(parser: Matcher, options?: ValueParserOptions) { - this.parser = parser; + constructor(parser: Matcher, options?: ValueParserOptions) { + this.matcher = parser; this.options = options || {}; } public toNode(id: string) { - return new ValueParserNode(id, this.parser, this.options); + return new ValueParserNode(id, this.matcher, this.options); } } diff --git a/src/values/index.ts b/src/values/index.ts index 940e0f9..2203499 100644 --- a/src/values/index.ts +++ b/src/values/index.ts @@ -1,6 +1,8 @@ /** * Re-export all of the types of values available. */ +export * from './base'; + export * from './any'; export * from './boolean'; export * from './custom'; diff --git a/src/values/options.ts b/src/values/options.ts index 92b1023..b1d327a 100644 --- a/src/values/options.ts +++ b/src/values/options.ts @@ -1,7 +1,7 @@ import { GraphBuilder } from '../graph/builder'; import { ResolverBuilder } from '../resolver/builder'; -import { LanguageSpecificValue, ParsingValue, NodeConvertable } from './base'; +import { LanguageSpecificValue, ParsingValue, NodeConvertable, Value } from './base'; import { ValueParserOptions } from '../resolver/value-parser'; import { ResolvedIntent } from '../resolver/resolved-intent'; import { ExpressionPart } from '../resolver/expression/ExpressionPart'; @@ -96,7 +96,7 @@ export function optionsValue(options: OptionBuilderOptions={}) { } export interface OptionBuilder { - value(id: string, type: LanguageSpecificValue | NodeConvertable): this; + value(id: string, type: Value): this; add(...args: string[]): this; @@ -104,7 +104,7 @@ export interface OptionBuilder { } interface OptionData { - values: Record; + values: Record; phrases: any[]; } diff --git a/test/value-matchers.test.js b/test/value-matchers.test.js deleted file mode 100644 index b7787be..0000000 --- a/test/value-matchers.test.js +++ /dev/null @@ -1,25 +0,0 @@ -import { expect } from 'chai'; - -import lang from '../src/language/en'; -import { boolean } from '../src/values'; - -describe('Value: Matchers', () => { - - it('Can parse string', () => { - const matcher = boolean().matcher(lang); - return matcher('yes') - .then(v => expect(v).to.equal(true)); - }); - - it('Can handle invalid value', () => { - const matcher = boolean().matcher(lang); - return matcher('cookies') - .then(v => expect(v).to.equal(null)); - }); - - it('Can handle non-string values', () => { - const matcher = boolean().matcher(lang); - return matcher(2029) - .then(v => expect(v).to.equal(null)); - }); -}); diff --git a/test/value-matchers.test.ts b/test/value-matchers.test.ts new file mode 100644 index 0000000..0d4c429 --- /dev/null +++ b/test/value-matchers.test.ts @@ -0,0 +1,18 @@ +import { en } from '../src/language/en'; +import { booleanValue } from '../src/values'; + +describe('Value: Matchers', () => { + + it('Can parse string', () => { + const matcher = booleanValue().matcher(en); + return matcher('yes') + .then(v => expect(v).toEqual(true)); + }); + + it('Can handle invalid value', () => { + const matcher = booleanValue().matcher(en); + return matcher('cookies') + .then(v => expect(v).toEqual(null)); + }); + +});