Skip to content

Commit

Permalink
refactor: Restoring value matcher functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Sep 28, 2019
1 parent 9407759 commit da550b4
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/intents.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
},
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/resolver/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ResolverBuilder {
};
}

public value(id: string, type: LanguageSpecificValue | NodeConvertable) {
public value(id: string, type: LanguageSpecificValue<any> | NodeConvertable) {
this.parser.value(id, type);
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/resolver/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -29,7 +29,7 @@ export class ResolverParser<V, M=V[]> extends GraphBuilder<V, M> {
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);
Expand Down
32 changes: 15 additions & 17 deletions src/values/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<V> = (language: Language) => ParsingValue<V>;

export class LanguageSpecificValue {
private factory: LanguageSpecificFactory;
export type Value = LanguageSpecificValue<any> | NodeConvertable;

constructor(factory: LanguageSpecificFactory) {
export class LanguageSpecificValue<V> {
private factory: LanguageSpecificFactory<V>;

constructor(factory: LanguageSpecificFactory<V>) {
this.factory = factory;
}

Expand All @@ -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<any>;
export class ParsingValue<V> {
public matcher: Matcher<V>;
private options: ValueParserOptions;

constructor(parser: Matcher<any>, options?: ValueParserOptions) {
this.parser = parser;
constructor(parser: Matcher<V>, 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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/values/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 3 additions & 3 deletions src/values/options.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -96,15 +96,15 @@ 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;

done(): OptionsBuilder;
}

interface OptionData {
values: Record<string, LanguageSpecificValue | NodeConvertable>;
values: Record<string, Value>;
phrases: any[];
}

Expand Down
25 changes: 0 additions & 25 deletions test/value-matchers.test.js

This file was deleted.

18 changes: 18 additions & 0 deletions test/value-matchers.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});

});

0 comments on commit da550b4

Please sign in to comment.