Comparing version 0.14.0 to 0.14.1
@@ -165,3 +165,3 @@ import { konsole } from './Konsole'; | ||
export class IfDoRouter <M extends Routable> extends Router<M> { | ||
export class ifTrueRouter <M extends Routable> extends Router<M> { | ||
constructor ( | ||
@@ -179,3 +179,2 @@ predicate: Predicate<M>, | ||
: elseRouter.getRoute(m) | ||
.map(route => routeWithCombinedScore(route, m.score)) | ||
) | ||
@@ -186,8 +185,8 @@ ); | ||
export function ifDo <M extends Routable> ( | ||
export function ifTrue <M extends Routable> ( | ||
predicate: Predicate<M>, | ||
ifRouterOrHandler: RouterOrHandler<M>, | ||
elseRouterOrHandler?: RouterOrHandler<M> | ||
): IfDoRouter<M> { | ||
return new IfDoRouter(predicate, ifRouterOrHandler, elseRouterOrHandler); | ||
): ifTrueRouter<M> { | ||
return new ifTrueRouter(predicate, ifRouterOrHandler, elseRouterOrHandler); | ||
} | ||
@@ -199,3 +198,3 @@ | ||
export class IfMatchRouter <M extends Routable, N extends Routable> extends Router<M> { | ||
export class ifMatchesRouter <M extends Routable, N extends Routable> extends Router<M> { | ||
constructor ( | ||
@@ -214,3 +213,2 @@ matcher: Matcher<M, N>, | ||
: elseRouter.getRoute(m) | ||
.map(route => routeWithCombinedScore(route, m.score)) | ||
) | ||
@@ -221,8 +219,8 @@ ); | ||
export function ifMatch <M extends Routable, N extends Routable> ( | ||
export function ifMatches <M extends Routable, N extends Routable> ( | ||
matcher: Matcher<M, N>, | ||
ifRouterOrHandler: RouterOrHandler<N>, | ||
elseRouterOrHandler?: RouterOrHandler<M> | ||
): IfMatchRouter<M, N> { | ||
return new IfMatchRouter(matcher, ifRouterOrHandler, elseRouterOrHandler); | ||
): ifMatchesRouter<M, N> { | ||
return new ifMatchesRouter(matcher, ifRouterOrHandler, elseRouterOrHandler); | ||
} | ||
@@ -229,0 +227,0 @@ |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
@@ -22,201 +12,31 @@ for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
var Router_1 = require("./Router"); | ||
function isBotContext(contextOrBoolean) { | ||
return contextOrBoolean.say !== undefined; | ||
} | ||
function ifMessage(routerOrHandler) { | ||
return Router_1.nullRouter; | ||
} | ||
function ifRegEx(regex, routerOrHandler) { | ||
return Router_1.nullRouter; | ||
} | ||
var Validator = (function () { | ||
function Validator(validate) { | ||
this.validate = Validator.toValidate(validate); | ||
} | ||
// create a new validator by composing this one with another | ||
Validator.prototype.and = function (validateOrValidator) { | ||
var _this = this; | ||
return new Validator(function (context) { return _this.validate(context) | ||
.flatMap(function (newContext) { return Validator.isValidator(validateOrValidator) | ||
? validateOrValidator.validate(newContext) | ||
: Validator.toValidate(validateOrValidator)(newContext); }); }); | ||
exports.matchChoice = function (choices) { | ||
return function (message) { | ||
var choice = choices.find(function (choice) { return choice.toLowerCase() === message.text.toLowerCase(); }); | ||
return choice && __assign({}, message, { // remove "as any" when TypeScript fixes this bug | ||
choice: choice }); | ||
}; | ||
Validator.toValidate = function (validate) { | ||
return function (context) { return Router_1.toObservable(validate(context)) | ||
.map(function (result) { return isBotContext(result) | ||
? result | ||
: result | ||
? context | ||
: Validator.addError(context); }); }; | ||
}; | ||
Validator.isValidator = function (validateOrValidator) { | ||
return validateOrValidator.and !== undefined; | ||
}; | ||
Validator.addEntity = function (context, entity) { | ||
// stub - should actually return a new context with the entity added to it | ||
return context; | ||
}; | ||
Validator.addError = function (context, error) { | ||
if (error === void 0) { error = "generic"; } | ||
// stub - should actually return a new context with the error added to it | ||
return context; | ||
}; | ||
return Validator; | ||
}()); | ||
var isText = new Validator(function (context) { | ||
if (context.request.type !== 'message') | ||
return Validator.addError(context, "wrong activity type"); | ||
if (context.request.text.length === 0) | ||
return Validator.addError(context, "empty string"); | ||
return Validator.addEntity(context, context.request.text); | ||
}); | ||
var isPassword = isText.and(function (context) { | ||
if (context.getEntity('string').length < 8) | ||
return Validator.addError(context, "too short"); | ||
return true; | ||
}); | ||
var passwordPrompt = new Prompt('name', isPassword, Router_1.ifMatch(c.error, function (c) { | ||
if (c.error === "too short") | ||
c.say("Passwords must be over 8 characters."); | ||
else | ||
c.say("Please enter a password."); | ||
c.prompt(passwordPrompt); | ||
}, function (c) { | ||
c.state.conversation.password = aHashingFunction(c.getEntity('string')); | ||
})); | ||
var passwordPrompt = new Prompt('name', isText.and(function (context) { | ||
if (context.getEntity('string').length < 8) | ||
return Validator.addError(context, "too short"); | ||
// other password rules here | ||
return context; | ||
}), function (c) { | ||
c.state.conversation.password = aHashingFunction(c.getEntity('string')); | ||
}, function (c) { | ||
if (c.error === "too short") | ||
c.say("Passwords must be over 8 characters."); | ||
else | ||
c.say("Please enter a password."); | ||
c.prompt(passwordPrompt); | ||
}); | ||
// Prompts is all statics - it contains the prompt registry, the active prompt, and some helper methods | ||
var Prompts = (function () { | ||
function Prompts() { | ||
}; | ||
// export const promptChoice = <M extends ITextMatch> ( | ||
// choices: string[], | ||
// routerOrHandler: RouterOrHandler<M & IChatPromptChoiceMatch> | ||
// ): Router<M> => { | ||
// return prependMatcher(matchChoice(choices), routerOrHandler); | ||
// } | ||
exports.matchConfirm = function () { | ||
return Router_1.matchAll(exports.matchChoice(['Yes', 'No']), function (m) { return m.choice === 'Yes'; }); | ||
}; | ||
var parseTime = function (text) { | ||
var now = new Date(); | ||
var groups = /^(\d{1,2}):(\d{2})(am|pm)$/i.exec(text); | ||
if (groups) { | ||
return new Date(now.getFullYear(), now.getMonth(), now.getDate(), parseInt(groups[1]) + (groups[3] === 'pm' ? 12 : 0), parseInt(groups[2])); | ||
} | ||
Prompts.add = function (name, prompt) { | ||
this.prompts[name] = prompt; | ||
}; | ||
exports.matchTime = function () { | ||
return function (m) { | ||
var time = parseTime(m.text); | ||
return time && __assign({}, m, { time: time }); | ||
}; | ||
// Here's the method called by context.prompt() | ||
Prompts.invokePrompt = function (context, prompt) { | ||
prompt._say(context); | ||
context.state.conversation.activePrompt = { | ||
name: prompt._name, | ||
params: prompt.params | ||
}; | ||
}; | ||
// Here's the method called by context.cancelPrompt() | ||
Prompts.cancelPrompt = function (context) { | ||
context.state.conversation.activePrompt = undefined; | ||
}; | ||
// if there's an active prompt, route to it | ||
Prompts.routeTo = function () { | ||
return ifMessage(new Router_1.Router(function (context) { return Router_1.toFilteredObservable(context.state.conversation.activePrompt) | ||
.flatMap(function (_a) { | ||
var name = _a.name, params = _a.params; | ||
return Router_1.toFilteredObservable(Prompts.prompts[name]) | ||
.do(function (_) { return context.cancelPrompt(); }) | ||
.flatMap(function (prompt) { return prompt._getRouter(params).getRoute(context); }); | ||
}); })); | ||
}; | ||
Prompts.prompts = {}; | ||
return Prompts; | ||
}()); | ||
var Prompt = (function () { | ||
function Prompt(name, router) { | ||
var _this = this; | ||
this.params = {}; | ||
Prompts.add(name, this); | ||
this._name = name; | ||
var _router = Router_1.toRouter(router); | ||
this._getRouter = function (params) { return Router_1.ifMatch(function (context) { return (__assign({}, context, { thisPrompt: _this._cloneWithParams(params) })); }, _router); }; | ||
} | ||
Prompt.prototype._say = function (context) { | ||
if (this.params.say) | ||
context.say(this.params.say); | ||
}; | ||
Prompt.prototype._cloneWithParams = function (params) { | ||
return Object.assign(Object.create(Object.getPrototypeOf(this)), this, { params: params }); | ||
}; | ||
Prompt.prototype._cloneWithParam = function (param) { | ||
return this._cloneWithParams(__assign({}, this.params, { param: param })); | ||
}; | ||
Prompt.prototype.say = function (say) { | ||
return this._cloneWithParam({ say: say }); | ||
}; | ||
Prompt.prototype.retries = function (retries) { | ||
return this._cloneWithParam({ retries: retries }); | ||
}; | ||
Prompt.prototype.retry = function () { | ||
return this.params.retries > 0 | ||
? this._cloneWithParam({ retries: this.params.retries - 1 }) | ||
: this; | ||
}; | ||
Prompt.prototype.with = function (withArgs) { | ||
return this._cloneWithParam({ with: withArgs }); | ||
}; | ||
return Prompt; | ||
}()); | ||
function parse(parser, parsedRouterOrHandler, errorRouterOrHandler) { | ||
var parsedRouter = Router_1.toRouter(parsedRouterOrHandler); | ||
var errorRouter = errorRouterOrHandler ? Router_1.toRouter(errorRouterOrHandler) : Router_1.nullRouter; | ||
return new Router_1.Router(function (context) { | ||
var parseResult = parser(context); | ||
return typeof parseResult === 'string' | ||
? errorRouter | ||
.getRoute(__assign({}, context, { error: parseResult })) | ||
: parsedRouter | ||
.getRoute(parseResult); | ||
}); | ||
} | ||
function parseText(context) { | ||
if (context.request.text.length === 0) | ||
return "Empty String"; | ||
return __assign({}, context); | ||
} | ||
var TextPrompt = (function (_super) { | ||
__extends(TextPrompt, _super); | ||
function TextPrompt(name, promptRouterOrHandler, errorRouterOrHandler) { | ||
return _super.call(this, name, parse(parseText, promptRouterOrHandler, errorRouterOrHandler)) || this; | ||
} | ||
return TextPrompt; | ||
}(Prompt)); | ||
var choicesToSuggestedActions = function (choices) { }; | ||
function parseChoices(context, choices) { | ||
if (context.request.text.length === 0) | ||
return "Empty String"; | ||
var choice = choices.find(function (choice) { return choice === context.request.text; }); | ||
if (!choice) | ||
return "Not one of the listed choices"; | ||
return __assign({}, context); | ||
} | ||
var ChoicePrompt = (function (_super) { | ||
__extends(ChoicePrompt, _super); | ||
function ChoicePrompt(name, promptRouterOrHandler, errorRouterOrHandler) { | ||
return _super.call(this, name, parse(function (context) { return parseChoices(context, context.params.choices); }, promptRouterOrHandler, errorRouterOrHandler)) || this; | ||
} | ||
ChoicePrompt.prototype._say = function (context) { | ||
if (!this.params.choices) { | ||
console.warn("ChoicePrompt must have choices"); | ||
return; | ||
} | ||
context.say({ | ||
type: 'message', | ||
text: this.params.say, | ||
suggestedActions: choicesToSuggestedActions(this.params.choices) | ||
}); | ||
}; | ||
ChoicePrompt.prototype.choices = function (choices) { | ||
return this._cloneWithParam({ choices: choices }); | ||
}; | ||
return ChoicePrompt; | ||
}(Prompt)); | ||
}; | ||
//# sourceMappingURL=Prompts.js.map |
@@ -43,13 +43,13 @@ import { Observable } from 'rxjs'; | ||
export declare function routeWithCombinedScore(route: Route, newScore: number): Route; | ||
export declare class IfDoRouter<M extends Routable> extends Router<M> { | ||
export declare class ifTrueRouter<M extends Routable> extends Router<M> { | ||
constructor(predicate: Predicate<M>, ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler?: RouterOrHandler<M>); | ||
} | ||
export declare function ifDo<M extends Routable>(predicate: Predicate<M>, ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler?: RouterOrHandler<M>): IfDoRouter<M>; | ||
export declare function ifTrue<M extends Routable>(predicate: Predicate<M>, ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler?: RouterOrHandler<M>): ifTrueRouter<M>; | ||
export interface Matcher<M extends Routable = {}, Z extends Routable = {}> { | ||
(m: M): Observableable<Z>; | ||
} | ||
export declare class IfMatchRouter<M extends Routable, N extends Routable> extends Router<M> { | ||
export declare class ifMatchesRouter<M extends Routable, N extends Routable> extends Router<M> { | ||
constructor(matcher: Matcher<M, N>, ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler?: RouterOrHandler<M>); | ||
} | ||
export declare function ifMatch<M extends Routable, N extends Routable>(matcher: Matcher<M, N>, ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler?: RouterOrHandler<M>): IfMatchRouter<M, N>; | ||
export declare function ifMatches<M extends Routable, N extends Routable>(matcher: Matcher<M, N>, ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler?: RouterOrHandler<M>): ifMatchesRouter<M, N>; | ||
export declare function throwRoute<M extends Routable>(): Router<M>; | ||
@@ -56,0 +56,0 @@ export declare function catchRoute<M extends Routable>(routerOrHandler: RouterOrHandler<M>): Router<M>; |
@@ -173,5 +173,5 @@ "use strict"; | ||
exports.routeWithCombinedScore = routeWithCombinedScore; | ||
var IfDoRouter = (function (_super) { | ||
__extends(IfDoRouter, _super); | ||
function IfDoRouter(predicate, ifRouterOrHandler, elseRouterOrHandler) { | ||
var ifTrueRouter = (function (_super) { | ||
__extends(ifTrueRouter, _super); | ||
function ifTrueRouter(predicate, ifRouterOrHandler, elseRouterOrHandler) { | ||
var _this = this; | ||
@@ -183,16 +183,15 @@ var ifRouter = Router.from(ifRouterOrHandler); | ||
? ifRouter.getRoute(m) | ||
: elseRouter.getRoute(m) | ||
.map(function (route) { return routeWithCombinedScore(route, m.score); }); }); }) || this; | ||
: elseRouter.getRoute(m); }); }) || this; | ||
return _this; | ||
} | ||
return IfDoRouter; | ||
return ifTrueRouter; | ||
}(Router)); | ||
exports.IfDoRouter = IfDoRouter; | ||
function ifDo(predicate, ifRouterOrHandler, elseRouterOrHandler) { | ||
return new IfDoRouter(predicate, ifRouterOrHandler, elseRouterOrHandler); | ||
exports.ifTrueRouter = ifTrueRouter; | ||
function ifTrue(predicate, ifRouterOrHandler, elseRouterOrHandler) { | ||
return new ifTrueRouter(predicate, ifRouterOrHandler, elseRouterOrHandler); | ||
} | ||
exports.ifDo = ifDo; | ||
var IfMatchRouter = (function (_super) { | ||
__extends(IfMatchRouter, _super); | ||
function IfMatchRouter(matcher, ifRouterOrHandler, elseRouterOrHandler) { | ||
exports.ifTrue = ifTrue; | ||
var ifMatchesRouter = (function (_super) { | ||
__extends(ifMatchesRouter, _super); | ||
function ifMatchesRouter(matcher, ifRouterOrHandler, elseRouterOrHandler) { | ||
var _this = this; | ||
@@ -205,13 +204,12 @@ var ifRouter = Router.from(ifRouterOrHandler); | ||
.map(function (route) { return routeWithCombinedScore(route, n.score); }) | ||
: elseRouter.getRoute(m) | ||
.map(function (route) { return routeWithCombinedScore(route, m.score); }); }); }) || this; | ||
: elseRouter.getRoute(m); }); }) || this; | ||
return _this; | ||
} | ||
return IfMatchRouter; | ||
return ifMatchesRouter; | ||
}(Router)); | ||
exports.IfMatchRouter = IfMatchRouter; | ||
function ifMatch(matcher, ifRouterOrHandler, elseRouterOrHandler) { | ||
return new IfMatchRouter(matcher, ifRouterOrHandler, elseRouterOrHandler); | ||
exports.ifMatchesRouter = ifMatchesRouter; | ||
function ifMatches(matcher, ifRouterOrHandler, elseRouterOrHandler) { | ||
return new ifMatchesRouter(matcher, ifRouterOrHandler, elseRouterOrHandler); | ||
} | ||
exports.ifMatch = ifMatch; | ||
exports.ifMatches = ifMatches; | ||
var thrownRoute = { | ||
@@ -218,0 +216,0 @@ thrown: true, |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.14.0", | ||
"version": "0.14.1", | ||
"description": "rules-based app engine", | ||
@@ -10,0 +10,0 @@ "main": "dist/prague.js", |
@@ -6,3 +6,3 @@ "use strict"; | ||
const expect = chai.expect; | ||
const { toObservable, toFilteredObservable, Router, first, best, run, toScore, routeWithCombinedScore, ifDo, ifMatch, throwRoute, catchRoute, before, after } = require('../dist/prague.js'); | ||
const { toObservable, toFilteredObservable, Router, first, best, run, toScore, routeWithCombinedScore, ifTrue, ifMatches, throwRoute, catchRoute, before, after } = require('../dist/prague.js'); | ||
const { Observable } = require('rxjs'); | ||
@@ -700,5 +700,5 @@ | ||
describe('ifDo', () => { | ||
describe('ifTrue', () => { | ||
it("should complete and never emit on false when 'else' router doesn't exist", (done) => | ||
ifMatch( | ||
ifTrue( | ||
m => false, | ||
@@ -712,3 +712,3 @@ throwErr | ||
it("should complete and never emit on true when 'else' router doesn't route", (done) => | ||
ifDo( | ||
ifTrue( | ||
m => false, | ||
@@ -723,3 +723,3 @@ throwErr, | ||
it("should complete and never emit on true when 'if' router doesn't route and 'else' router doesn't exist", (done) => | ||
ifDo( | ||
ifTrue( | ||
m => true, | ||
@@ -733,3 +733,3 @@ Router.null | ||
it("should complete and never emit on true when 'if' router doesn't route and 'else' router exists", (done) => | ||
ifDo( | ||
ifTrue( | ||
m => true, | ||
@@ -746,3 +746,3 @@ Router.null, | ||
ifDo( | ||
ifTrue( | ||
m => true, | ||
@@ -763,3 +763,3 @@ m => { | ||
ifDo( | ||
ifTrue( | ||
m => true, | ||
@@ -781,3 +781,3 @@ m => { | ||
ifDo( | ||
ifTrue( | ||
m => true, | ||
@@ -798,3 +798,3 @@ Router.fromHandler(m => { | ||
ifDo( | ||
ifTrue( | ||
m => true, | ||
@@ -816,3 +816,3 @@ Router.fromHandler(m => { | ||
ifDo( | ||
ifTrue( | ||
m => false, | ||
@@ -834,3 +834,3 @@ throwErr, | ||
ifDo( | ||
ifTrue( | ||
m => false, | ||
@@ -850,3 +850,3 @@ throwErr, | ||
it("should return score=1 on true predicate when 'if' score undefined", (done) => { | ||
ifDo( | ||
ifTrue( | ||
m => true, | ||
@@ -863,3 +863,3 @@ m => {} | ||
it("should return route score on true predicate", (done) => { | ||
ifDo( | ||
ifTrue( | ||
m => true, | ||
@@ -876,3 +876,3 @@ makeRouter(0.25, () => {}) | ||
it("should return score=1 on false predicate when 'else' score undefined", (done) => { | ||
ifDo( | ||
ifTrue( | ||
m => false, | ||
@@ -890,3 +890,3 @@ m => {}, | ||
it("should return 'else' route score on false predicate", (done) => { | ||
ifDo( | ||
ifTrue( | ||
m => false, | ||
@@ -905,5 +905,5 @@ throwErr, | ||
describe('ifMatch', () => { | ||
describe('ifMatches', () => { | ||
it("should complete and never emit on no match when 'else' router doesn't exist", (done) => | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -917,3 +917,3 @@ throwErr | ||
it("should complete and never emit on no match when 'else' router doesn't route", (done) => | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -928,3 +928,3 @@ throwErr, | ||
it("should complete and never emit on match when 'if' router doesn't route and 'else' router doesn't exist", (done) => | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -939,3 +939,3 @@ Router.null | ||
it("should complete and never emit on match when 'if' router doesn't route and 'else' router exists", (done) => | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -952,3 +952,3 @@ Router.null, | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -969,3 +969,3 @@ m => { | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -987,3 +987,3 @@ m => { | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -1004,3 +1004,3 @@ Router.fromHandler(m => { | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -1022,3 +1022,3 @@ Router.fromHandler(m => { | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -1040,3 +1040,3 @@ throwErr, | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -1056,3 +1056,3 @@ throwErr, | ||
it("should return score=1 on scoreless match when 'if' score undefined", (done) => { | ||
ifMatch( | ||
ifMatches( | ||
m => ({}), | ||
@@ -1069,3 +1069,3 @@ m => {} | ||
it("should return supplied score when 'if' score undefined", (done) => { | ||
ifMatch( | ||
ifMatches( | ||
m => ({ | ||
@@ -1084,3 +1084,3 @@ score: 0.4 | ||
it("should return route score on scoreless match", (done) => { | ||
ifMatch( | ||
ifMatches( | ||
m => ({}), | ||
@@ -1097,3 +1097,3 @@ makeRouter(0.25, () => {}) | ||
it("should return combined score when both scores supplied", (done) => { | ||
ifMatch( | ||
ifMatches( | ||
m => ({ | ||
@@ -1112,3 +1112,3 @@ score: 0.4 | ||
it("should return score=1 on scoreless match when 'else' score undefined", (done) => { | ||
ifMatch( | ||
ifMatches( | ||
m => ({}), | ||
@@ -1126,3 +1126,3 @@ m => {}, | ||
it("should return 'else' route score on no match", (done) => { | ||
ifMatch( | ||
ifMatches( | ||
addBar, | ||
@@ -1129,0 +1129,0 @@ throwErr, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
181932
42
2833
1