Skip to content

Commit

Permalink
add pattern named captures
Browse files Browse the repository at this point in the history
  • Loading branch information
vunb committed Oct 29, 2019
1 parent 02f6f6b commit 5715fa0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/engine/botscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Context } from './context';
import { Request } from './request';
import { Struct, TYPES } from './struct';
import { Logger } from '../lib/logger';
import { transform } from './pattern';
import { transform, execPattern } from './pattern';

/**
* BotScript dialogue engine
Expand Down Expand Up @@ -91,9 +91,17 @@ export class BotScript {
* @param req
*/
buildResponse(dialog: Struct, trigger: string, req: Request) {
const result = this.getActivators(dialog).filter(() => true).some(pattern => {
this.logger.info('Pattern: ', pattern);
});
const result = this.getActivators(dialog)
.filter((x) => RegExp(x.source, x.flags).test(req.text))
.some(pattern => {
this.logger.info('Found: ', dialog.name, pattern);

const captures = execPattern(req.text, pattern);
req.parameters.$ = captures.$1;
Object.keys(captures).forEach(varName => {
req.parameters[varName] = captures[varName];
});
});

if (result) {
this.logger.info('Handle request ok!', 123);
Expand Down
12 changes: 12 additions & 0 deletions src/engine/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ export function transform(pattern: string, definitions: Map<string, Struct>, not
? XRegExp(`^((?!^${pattern}$).)+(?!\\w)`, 'ig')
: XRegExp(`(?:^|[\\s,;—])${pattern}(?!\\w)`, 'ig');
}

/**
* Extract and captures named variables
* @param input
* @param pattern
*/
export function execPattern(input: string, pattern: RegExp | any) {
let captures = !pattern.label ? XRegExp.exec(input, pattern) : pattern.exec(input);
const keys = Object.keys(captures).filter(key => !['index', 'input', 'groups'].includes(key));
captures = keys.map(key => ({ [key.match(/^\d+$/) ? `$${parseInt(key)}` : key]: captures[key] })).splice(1);
return captures.length > 0 ? captures.reduce((a: any, b: any) => Object.assign(a, b)) : [];
}
2 changes: 2 additions & 0 deletions src/engine/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export class Request {
public text: string;
public contexts: string[];
public complete: boolean;
public parameters: any;

constructor(text?: string) {
this.contexts = [];
this.parameters = {};

if (text) {
this.text = text.toLowerCase();
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"object-literal-sort-keys": false,
"one-variable-per-declaration": false,
"variable-name": false,
"max-classes-per-file": false
"max-classes-per-file": false,
"radix": false
},
"rulesDirectory": []
}

0 comments on commit 5715fa0

Please sign in to comment.