Comparing version 0.12.3 to 0.13.0
@@ -6,3 +6,3 @@ import { konsole } from './Konsole'; | ||
export const toObservable = <T> (t: Observableable<T>) => { | ||
export function toObservable <T> (t: Observableable<T>) { | ||
if (t instanceof Observable) | ||
@@ -15,3 +15,3 @@ return t; | ||
export const toFilteredObservable = <T> (t: Observableable<T>) => { | ||
export function toFilteredObservable <T> (t: Observableable<T>) { | ||
if (!t) | ||
@@ -36,2 +36,15 @@ return Observable.empty<T>(); | ||
export function nullRouter <M extends object = {}> (): Router<M> { | ||
return { | ||
getRoute: (m) => Observable.empty() | ||
} | ||
} | ||
export function routeMessage <M extends object> (router: Router<M>, message: M) { | ||
return router.getRoute(message) | ||
.do(route => konsole.log("handle: matched a route", route)) | ||
.flatMap(route => toObservable(route.action())) | ||
.do(_ => konsole.log("handle: called action")); | ||
} | ||
export interface Handler <Z extends object = {}> { | ||
@@ -47,63 +60,72 @@ (message: Z): Observableable<any>; | ||
export const toRouter = <M extends object> (routerOrHandler: RouterOrHandler<M>) => { | ||
export function simpleRouter <M extends object> (handler: Handler<M>): Router<M> { | ||
return { | ||
getRoute: (m) => Observable.of({ | ||
action: () => handler(m) | ||
} as Route) | ||
} | ||
} | ||
export function toRouter <M extends object> (routerOrHandler: RouterOrHandler<M>): Router<M> { | ||
return isRouter(routerOrHandler) ? routerOrHandler : simpleRouter(routerOrHandler); | ||
} | ||
export const routeMessage = <M extends object> (router: Router<M>, message: M) => | ||
router.getRoute(message) | ||
.do(route => konsole.log("handle: matched a route", route)) | ||
.flatMap(route => toObservable(route.action())) | ||
.do(_ => konsole.log("handle: called action")); | ||
export const simpleRouter = <M extends object> (handler: Handler<M>): Router<M> => ({ | ||
getRoute: (m) => Observable.of({ | ||
action: () => handler(m) | ||
} as Route) | ||
}); | ||
const filteredRouter$ = <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]) => | ||
Observable.from(routersOrHandlers) | ||
function filteredRouter$ <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]) { | ||
return Observable.from(routersOrHandlers) | ||
.filter(routerOrHandler => !!routerOrHandler) | ||
.map(routerOrHandler => toRouter(routerOrHandler)); | ||
} | ||
export const first = <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]): Router<M> => ({ | ||
getRoute: (m) => | ||
filteredRouter$(... routersOrHandlers).flatMap( | ||
(router, i) => { | ||
konsole.log(`first: trying router #${i}`); | ||
return router.getRoute(m) | ||
.do(m => konsole.log(`first: router #${i} succeeded`, m)); | ||
}, | ||
1 | ||
) | ||
.take(1) // so that we don't keep going through routers after we find one that matches | ||
}); | ||
export function first <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]): Router<M> { | ||
const routers = filteredRouter$(... routersOrHandlers); | ||
return { | ||
getRoute: (m) => | ||
routers.flatMap( | ||
(router, i) => { | ||
konsole.log(`first: trying router #${i}`); | ||
return router.getRoute(m) | ||
.do(m => konsole.log(`first: router #${i} succeeded`, m)); | ||
}, | ||
1 | ||
) | ||
.take(1) // so that we don't keep going through routers after we find one that matches | ||
} | ||
} | ||
const minRoute: Route = { | ||
score: 0, | ||
action: () => console.log("This should never be called") | ||
action: () => console.warn("This should never be called") | ||
} | ||
export const best = <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]): Router<M> => ({ | ||
getRoute: (m) => | ||
filteredRouter$(... routersOrHandlers).flatMap( | ||
(router, i) => { | ||
konsole.log(`best: trying router #${i}`); | ||
return router.getRoute(m) | ||
.do(m => konsole.log(`best: router #${i} succeeded`, m)); | ||
} | ||
) | ||
.reduce( | ||
(prev, current) => Math.min(prev.score === undefined ? 1 : prev.score) > Math.min(current.score === undefined ? 1 : current.score) ? prev : current, | ||
minRoute | ||
) | ||
}); | ||
function toScore (score: number) { | ||
return score == null ? 1 : score; | ||
} | ||
export const run = <M extends object> (handler: Handler<M>): Router<M> => ({ | ||
getRoute: (m) => | ||
toObservable(handler(m)) | ||
.filter(_ => false) | ||
}); | ||
export function best <M extends object> (... routersOrHandlers: RouterOrHandler<M>[]): Router<M> { | ||
const routers = filteredRouter$(... routersOrHandlers); | ||
return { | ||
getRoute: (m) => | ||
filteredRouter$(... routersOrHandlers).flatMap( | ||
(router, i) => { | ||
konsole.log(`best: trying router #${i}`); | ||
return router.getRoute(m) | ||
.do(m => konsole.log(`best: router #${i} succeeded`, m)); | ||
} | ||
) | ||
.reduce( | ||
(prev, current) => toScore(prev.score) >= toScore(current.score) ? prev : current, | ||
minRoute | ||
) | ||
.filter(route => toScore(route.score) > 0) | ||
} | ||
} | ||
export function run <M extends object> (handler: Handler<M>): Router<M> { | ||
return { | ||
getRoute: (m) => | ||
toObservable(handler(m)) | ||
.filter(_ => false) | ||
} | ||
} | ||
export interface Matcher <M extends object = {}, Z extends object = {}> { | ||
@@ -120,99 +142,34 @@ (message: M): Observableable<Z>; | ||
export function tryMatch <M extends object> (predicateOrMatcher: Predicate | Matcher, message: M): Observable<any>; | ||
export function tryMatch(matcherOrPredicate: Matcher | Predicate, message): Observable<any> { | ||
export function tryMatch(predicateOrMatcher: Matcher | Predicate, message): Observable<any> { | ||
// we want to allow any matcher to be a predicate (return a boolean) | ||
// if so, the 'falsey' case will be filtered out by toFilteredObservable, | ||
// so we just need to catch the case where it is precisely true | ||
konsole.log("tryMatch", matcherOrPredicate, message); | ||
return toFilteredObservable(matcherOrPredicate(message)) | ||
konsole.log("tryMatch", predicateOrMatcher, message); | ||
return toFilteredObservable(predicateOrMatcher(message)) | ||
.map(m => typeof m === 'boolean' ? message : m); | ||
} | ||
export function matchAll <M extends object, Z extends object> (m1: Matcher<M, Z>): Matcher<M, Z> | ||
export function matchAll <M extends object> (p1: Predicate<M>): Matcher<M, M> | ||
export function ifMatch <L extends object, M extends object> ( | ||
matcher: Matcher<L, M>, | ||
routerOrHandler: RouterOrHandler<M> | ||
): Router<L>; | ||
export function matchAll <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (p1: Predicate<M>, m2: Matcher<M, Z>,): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (m1: Matcher<M, Z>, p2: Predicate<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object> (p1: Predicate<M>, p2: Predicate<M>): Matcher<M, M> | ||
export function ifMatch <M extends object> ( | ||
predicate: Predicate<M>, | ||
routerOrHandler: RouterOrHandler<M> | ||
): Router<M>; | ||
export function matchAll <M extends object, N extends object, O extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, N extends object, Z extends object> (p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object, Z extends object> (p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z> | ||
export function matchAll <M extends object> (p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>): Matcher<M, M> | ||
export function matchAll <M extends object> (... matchersOrPredicates: (Matcher|Predicate)[]): Matcher<M> | ||
export function matchAll <M extends object> (... args: Matcher[]): Matcher<M> { | ||
return m => { | ||
konsole.log("matchAll", args, m) | ||
return Observable.from(args) | ||
.reduce<Matcher, Observable<any>> ( | ||
(prevObservable, currentMatcher, i) => | ||
prevObservable | ||
.flatMap(prevMatch => { | ||
konsole.log(`calling matcher #${i}`, currentMatcher); | ||
return tryMatch(currentMatcher, prevMatch) | ||
.do(result => konsole.log("result", result)); | ||
}), | ||
Observable.of(m) | ||
) | ||
.mergeAll(); | ||
} | ||
} | ||
export function matchAny <M extends object> (... predicatesOrMatchers: (Predicate<M> | Matcher<M>)[]): Matcher<M> { | ||
konsole.log("matchAny", predicatesOrMatchers); | ||
return m => | ||
Observable.from(predicatesOrMatchers) | ||
.flatMap(predicateOrMatcher => tryMatch(predicateOrMatcher, m), 1) | ||
.take(1); | ||
} | ||
export function prependMatcher <L extends object, M extends object> (matcher: Matcher<L, M>, routerOrHandler: RouterOrHandler<M>): Router<L>; | ||
export function prependMatcher <M extends object> (predicate: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export function prependMatcher (matcherOrPredicate: Matcher | Predicate, routerOrHandler: RouterOrHandler): Router<any> { | ||
export function ifMatch ( | ||
predicateOrMatcher: Matcher | Predicate, | ||
routerOrHandler: RouterOrHandler | ||
): Router<any> { | ||
const router = toRouter(routerOrHandler); | ||
return { | ||
getRoute: (m) => | ||
tryMatch(matcherOrPredicate, m) | ||
.flatMap(m => toRouter(routerOrHandler).getRoute(m)) | ||
tryMatch(predicateOrMatcher, m) | ||
.flatMap(m => router.getRoute(m)) | ||
}; | ||
} | ||
export function ifMatch <M extends object> (routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (m1: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object> (p1: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (p1: Predicate<M>, m2: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (m1: Matcher<M, Z>, p2: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object> (p1: Predicate<M>, p2: Matcher<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch <M extends object, N extends object, O extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, Z extends object> (p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, N extends object, Z extends object> (p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object, N extends object, Z extends object> (m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M> | ||
export function ifMatch <M extends object> (p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M> | ||
export function ifMatch <M extends object> (... args: (Predicate | Matcher | RouterOrHandler)[]): Router<M> { | ||
konsole.log("ifMatch", args); | ||
const router = toRouter(args[args.length - 1] as RouterOrHandler); | ||
switch (args.length) { | ||
case 1: | ||
return router; | ||
case 2: | ||
return prependMatcher(args[0] as Matcher, router); | ||
default: | ||
return prependMatcher(matchAll(... args.slice(0, args.length - 1) as Matcher[]), router); | ||
} | ||
} | ||
export function branchMatch <M extends object> ( | ||
export function ifMatchElse <M extends object> ( | ||
predicate: Predicate<M>, | ||
@@ -223,3 +180,3 @@ ifRouterOrHandler: RouterOrHandler<M>, | ||
export function branchMatch <M extends object, N extends object> ( | ||
export function ifMatchElse <M extends object, N extends object> ( | ||
matcher: Matcher<M, N>, | ||
@@ -230,17 +187,20 @@ ifRouterOrHandler: RouterOrHandler<N>, | ||
export function branchMatch <M extends object> ( | ||
predicateOrMatcher: Predicate | Matcher, | ||
export function ifMatchElse <M extends object> ( | ||
predicateOrMatcher: Predicate<M> | Matcher<M>, | ||
ifRouterOrHandler: RouterOrHandler, | ||
elseRouterOrHandler: RouterOrHandler, | ||
): Router<M> { | ||
return first( | ||
ifMatch(predicateOrMatcher, ifRouterOrHandler), | ||
elseRouterOrHandler | ||
) | ||
const ifRouter = toRouter(ifRouterOrHandler); | ||
const elseRouter = toRouter(elseRouterOrHandler); | ||
return { | ||
getRoute: (m) => | ||
tryMatch(predicateOrMatcher, m) | ||
.defaultIfEmpty(null) | ||
.flatMap(n => n | ||
? ifRouter.getRoute(n) | ||
: elseRouter.getRoute(m) | ||
) | ||
}; | ||
} | ||
export const nullRouter = <M extends object = {}> (): Router<M> => ({ | ||
getRoute: (m) => Observable.empty() | ||
}) | ||
const thrownRoute: Route = { | ||
@@ -251,9 +211,13 @@ thrown: true, | ||
export const throwRoute = <M extends object> (): Router<M> => ({ | ||
getRoute: (m) => Observable.of(thrownRoute) | ||
}); | ||
export function throwRoute <M extends object> (): Router<M> { | ||
return { | ||
getRoute: (m) => Observable.of(thrownRoute) | ||
} | ||
} | ||
export const catchRoute = <M extends object> (router: Router<M>): Router<M> => ({ | ||
getRoute: (m) => router.getRoute(m) | ||
.filter(route => !route.thrown) | ||
}) | ||
export function catchRoute <M extends object> (routerOrHandler: RouterOrHandler<M>): Router<M> { | ||
return { | ||
getRoute: (m) => toRouter(routerOrHandler).getRoute(m) | ||
.filter(route => !route.thrown) | ||
} | ||
} |
import { Observable } from 'rxjs'; | ||
export declare type Observableable<T> = T | Observable<T> | Promise<T>; | ||
export declare const toObservable: <T>(t: Observableable<T>) => Observable<T>; | ||
export declare const toFilteredObservable: <T>(t: Observableable<T>) => Observable<T>; | ||
export declare function toObservable<T>(t: Observableable<T>): Observable<T>; | ||
export declare function toFilteredObservable<T>(t: Observableable<T>): Observable<T>; | ||
export interface Route { | ||
@@ -13,2 +13,4 @@ score?: number; | ||
} | ||
export declare function nullRouter<M extends object = {}>(): Router<M>; | ||
export declare function routeMessage<M extends object>(router: Router<M>, message: M): Observable<any>; | ||
export interface Handler<Z extends object = {}> { | ||
@@ -19,8 +21,7 @@ (message: Z): Observableable<any>; | ||
export declare function isRouter<M extends object>(routerOrHandler: RouterOrHandler<M>): routerOrHandler is Router<M>; | ||
export declare const toRouter: <M extends object>(routerOrHandler: RouterOrHandler<M>) => Router<M>; | ||
export declare const routeMessage: <M extends object>(router: Router<M>, message: M) => Observable<any>; | ||
export declare const simpleRouter: <M extends object>(handler: Handler<M>) => Router<M>; | ||
export declare const first: <M extends object>(...routersOrHandlers: RouterOrHandler<M>[]) => Router<M>; | ||
export declare const best: <M extends object>(...routersOrHandlers: RouterOrHandler<M>[]) => Router<M>; | ||
export declare const run: <M extends object>(handler: Handler<M>) => Router<M>; | ||
export declare function simpleRouter<M extends object>(handler: Handler<M>): Router<M>; | ||
export declare function toRouter<M extends object>(routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function first<M extends object>(...routersOrHandlers: RouterOrHandler<M>[]): Router<M>; | ||
export declare function best<M extends object>(...routersOrHandlers: RouterOrHandler<M>[]): Router<M>; | ||
export declare function run<M extends object>(handler: Handler<M>): Router<M>; | ||
export interface Matcher<M extends object = {}, Z extends object = {}> { | ||
@@ -35,39 +36,7 @@ (message: M): Observableable<Z>; | ||
export declare function tryMatch<M extends object>(predicateOrMatcher: Predicate | Matcher, message: M): Observable<any>; | ||
export declare function matchAll<M extends object, Z extends object>(m1: Matcher<M, Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object>(p1: Predicate<M>): Matcher<M, M>; | ||
export declare function matchAll<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object, Z extends object>(p1: Predicate<M>, m2: Matcher<M, Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object, Z extends object>(m1: Matcher<M, Z>, p2: Predicate<Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object>(p1: Predicate<M>, p2: Predicate<M>): Matcher<M, M>; | ||
export declare function matchAll<M extends object, N extends object, O extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object, N extends object, Z extends object>(p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object, Z extends object>(m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object, Z extends object>(p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object, Z extends object>(p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Matcher<M, Z>; | ||
export declare function matchAll<M extends object>(p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>): Matcher<M, M>; | ||
export declare function matchAll<M extends object>(...matchersOrPredicates: (Matcher | Predicate)[]): Matcher<M>; | ||
export declare function matchAny<M extends object>(...predicatesOrMatchers: (Predicate<M> | Matcher<M>)[]): Matcher<M>; | ||
export declare function prependMatcher<L extends object, M extends object>(matcher: Matcher<L, M>, routerOrHandler: RouterOrHandler<M>): Router<L>; | ||
export declare function prependMatcher<M extends object>(predicate: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatch<M extends object>(routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatch<M extends object, Z extends object>(m1: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object>(p1: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatch<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object, Z extends object>(p1: Predicate<M>, m2: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object, Z extends object>(m1: Matcher<M, Z>, p2: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object>(p1: Predicate<M>, p2: Matcher<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatch<M extends object, N extends object, O extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, O>, m3: Matcher<O, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object, Z extends object>(m1: Matcher<M, Z>, p2: Predicate<Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object, Z extends object>(p1: Matcher<M>, m2: Matcher<M, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object, Z extends object>(p1: Predicate<M>, p2: Predicate<M>, m3: Matcher<M, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object, N extends object, Z extends object>(p1: Predicate<M>, m2: Matcher<M, N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, p2: Predicate<N>, m3: Matcher<N, Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object, N extends object, Z extends object>(m1: Matcher<M, N>, m2: Matcher<N, Z>, p3: Predicate<Z>, routerOrHandler: RouterOrHandler<Z>): Router<M>; | ||
export declare function ifMatch<M extends object>(p1: Predicate<M>, p2: Predicate<M>, p3: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function branchMatch<M extends object>(predicate: Predicate<M>, ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function branchMatch<M extends object, N extends object>(matcher: Matcher<M, N>, ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare const nullRouter: <M extends object = {}>() => Router<M>; | ||
export declare const throwRoute: <M extends object>() => Router<M>; | ||
export declare const catchRoute: <M extends object>(router: Router<M>) => Router<M>; | ||
export declare function ifMatch<L extends object, M extends object>(matcher: Matcher<L, M>, routerOrHandler: RouterOrHandler<M>): Router<L>; | ||
export declare function ifMatch<M extends object>(predicate: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatchElse<M extends object>(predicate: Predicate<M>, ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatchElse<M extends object, N extends object>(matcher: Matcher<M, N>, ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function throwRoute<M extends object>(): Router<M>; | ||
export declare function catchRoute<M extends object>(routerOrHandler: RouterOrHandler<M>): Router<M>; |
@@ -5,3 +5,3 @@ "use strict"; | ||
var rxjs_1 = require("rxjs"); | ||
exports.toObservable = function (t) { | ||
function toObservable(t) { | ||
if (t instanceof rxjs_1.Observable) | ||
@@ -12,4 +12,5 @@ return t; | ||
return rxjs_1.Observable.of(t); | ||
}; | ||
exports.toFilteredObservable = function (t) { | ||
} | ||
exports.toObservable = toObservable; | ||
function toFilteredObservable(t) { | ||
if (!t) | ||
@@ -22,3 +23,17 @@ return rxjs_1.Observable.empty(); | ||
return rxjs_1.Observable.of(t); | ||
}; | ||
} | ||
exports.toFilteredObservable = toFilteredObservable; | ||
function nullRouter() { | ||
return { | ||
getRoute: function (m) { return rxjs_1.Observable.empty(); } | ||
}; | ||
} | ||
exports.nullRouter = nullRouter; | ||
function routeMessage(router, message) { | ||
return router.getRoute(message) | ||
.do(function (route) { return Konsole_1.konsole.log("handle: matched a route", route); }) | ||
.flatMap(function (route) { return toObservable(route.action()); }) | ||
.do(function (_) { return Konsole_1.konsole.log("handle: called action"); }); | ||
} | ||
exports.routeMessage = routeMessage; | ||
function isRouter(routerOrHandler) { | ||
@@ -28,17 +43,15 @@ return (routerOrHandler.getRoute !== undefined); | ||
exports.isRouter = isRouter; | ||
exports.toRouter = function (routerOrHandler) { | ||
return isRouter(routerOrHandler) ? routerOrHandler : exports.simpleRouter(routerOrHandler); | ||
}; | ||
exports.routeMessage = function (router, message) { | ||
return router.getRoute(message) | ||
.do(function (route) { return Konsole_1.konsole.log("handle: matched a route", route); }) | ||
.flatMap(function (route) { return exports.toObservable(route.action()); }) | ||
.do(function (_) { return Konsole_1.konsole.log("handle: called action"); }); | ||
}; | ||
exports.simpleRouter = function (handler) { return ({ | ||
getRoute: function (m) { return rxjs_1.Observable.of({ | ||
action: function () { return handler(m); } | ||
}); } | ||
}); }; | ||
var filteredRouter$ = function () { | ||
function simpleRouter(handler) { | ||
return { | ||
getRoute: function (m) { return rxjs_1.Observable.of({ | ||
action: function () { return handler(m); } | ||
}); } | ||
}; | ||
} | ||
exports.simpleRouter = simpleRouter; | ||
function toRouter(routerOrHandler) { | ||
return isRouter(routerOrHandler) ? routerOrHandler : simpleRouter(routerOrHandler); | ||
} | ||
exports.toRouter = toRouter; | ||
function filteredRouter$() { | ||
var routersOrHandlers = []; | ||
@@ -50,5 +63,5 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
.filter(function (routerOrHandler) { return !!routerOrHandler; }) | ||
.map(function (routerOrHandler) { return exports.toRouter(routerOrHandler); }); | ||
}; | ||
exports.first = function () { | ||
.map(function (routerOrHandler) { return toRouter(routerOrHandler); }); | ||
} | ||
function first() { | ||
var routersOrHandlers = []; | ||
@@ -58,5 +71,6 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
} | ||
return ({ | ||
var routers = filteredRouter$.apply(void 0, routersOrHandlers); | ||
return { | ||
getRoute: function (m) { | ||
return filteredRouter$.apply(void 0, routersOrHandlers).flatMap(function (router, i) { | ||
return routers.flatMap(function (router, i) { | ||
Konsole_1.konsole.log("first: trying router #" + i); | ||
@@ -68,9 +82,13 @@ return router.getRoute(m) | ||
} // so that we don't keep going through routers after we find one that matches | ||
}); | ||
}; | ||
}; | ||
} | ||
exports.first = first; | ||
var minRoute = { | ||
score: 0, | ||
action: function () { return console.log("This should never be called"); } | ||
action: function () { return console.warn("This should never be called"); } | ||
}; | ||
exports.best = function () { | ||
function toScore(score) { | ||
return score == null ? 1 : score; | ||
} | ||
function best() { | ||
var routersOrHandlers = []; | ||
@@ -80,3 +98,4 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
} | ||
return ({ | ||
var routers = filteredRouter$.apply(void 0, routersOrHandlers); | ||
return { | ||
getRoute: function (m) { | ||
@@ -88,87 +107,50 @@ return filteredRouter$.apply(void 0, routersOrHandlers).flatMap(function (router, i) { | ||
}) | ||
.reduce(function (prev, current) { return Math.min(prev.score === undefined ? 1 : prev.score) > Math.min(current.score === undefined ? 1 : current.score) ? prev : current; }, minRoute); | ||
.reduce(function (prev, current) { return toScore(prev.score) >= toScore(current.score) ? prev : current; }, minRoute) | ||
.filter(function (route) { return toScore(route.score) > 0; }); | ||
} | ||
}); | ||
}; | ||
exports.run = function (handler) { return ({ | ||
getRoute: function (m) { | ||
return exports.toObservable(handler(m)) | ||
.filter(function (_) { return false; }); | ||
} | ||
}); }; | ||
function tryMatch(matcherOrPredicate, message) { | ||
}; | ||
} | ||
exports.best = best; | ||
function run(handler) { | ||
return { | ||
getRoute: function (m) { | ||
return toObservable(handler(m)) | ||
.filter(function (_) { return false; }); | ||
} | ||
}; | ||
} | ||
exports.run = run; | ||
function tryMatch(predicateOrMatcher, message) { | ||
// we want to allow any matcher to be a predicate (return a boolean) | ||
// if so, the 'falsey' case will be filtered out by toFilteredObservable, | ||
// so we just need to catch the case where it is precisely true | ||
Konsole_1.konsole.log("tryMatch", matcherOrPredicate, message); | ||
return exports.toFilteredObservable(matcherOrPredicate(message)) | ||
Konsole_1.konsole.log("tryMatch", predicateOrMatcher, message); | ||
return toFilteredObservable(predicateOrMatcher(message)) | ||
.map(function (m) { return typeof m === 'boolean' ? message : m; }); | ||
} | ||
exports.tryMatch = tryMatch; | ||
function matchAll() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return function (m) { | ||
Konsole_1.konsole.log("matchAll", args, m); | ||
return rxjs_1.Observable.from(args) | ||
.reduce(function (prevObservable, currentMatcher, i) { | ||
return prevObservable | ||
.flatMap(function (prevMatch) { | ||
Konsole_1.konsole.log("calling matcher #" + i, currentMatcher); | ||
return tryMatch(currentMatcher, prevMatch) | ||
.do(function (result) { return Konsole_1.konsole.log("result", result); }); | ||
}); | ||
}, rxjs_1.Observable.of(m)) | ||
.mergeAll(); | ||
function ifMatch(predicateOrMatcher, routerOrHandler) { | ||
var router = toRouter(routerOrHandler); | ||
return { | ||
getRoute: function (m) { | ||
return tryMatch(predicateOrMatcher, m) | ||
.flatMap(function (m) { return router.getRoute(m); }); | ||
} | ||
}; | ||
} | ||
exports.matchAll = matchAll; | ||
function matchAny() { | ||
var predicatesOrMatchers = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
predicatesOrMatchers[_i] = arguments[_i]; | ||
} | ||
Konsole_1.konsole.log("matchAny", predicatesOrMatchers); | ||
return function (m) { | ||
return rxjs_1.Observable.from(predicatesOrMatchers) | ||
.flatMap(function (predicateOrMatcher) { return tryMatch(predicateOrMatcher, m); }, 1) | ||
.take(1); | ||
}; | ||
} | ||
exports.matchAny = matchAny; | ||
function prependMatcher(matcherOrPredicate, routerOrHandler) { | ||
exports.ifMatch = ifMatch; | ||
function ifMatchElse(predicateOrMatcher, ifRouterOrHandler, elseRouterOrHandler) { | ||
var ifRouter = toRouter(ifRouterOrHandler); | ||
var elseRouter = toRouter(elseRouterOrHandler); | ||
return { | ||
getRoute: function (m) { | ||
return tryMatch(matcherOrPredicate, m) | ||
.flatMap(function (m) { return exports.toRouter(routerOrHandler).getRoute(m); }); | ||
return tryMatch(predicateOrMatcher, m) | ||
.defaultIfEmpty(null) | ||
.flatMap(function (n) { return n | ||
? ifRouter.getRoute(n) | ||
: elseRouter.getRoute(m); }); | ||
} | ||
}; | ||
} | ||
exports.prependMatcher = prependMatcher; | ||
function ifMatch() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
Konsole_1.konsole.log("ifMatch", args); | ||
var router = exports.toRouter(args[args.length - 1]); | ||
switch (args.length) { | ||
case 1: | ||
return router; | ||
case 2: | ||
return prependMatcher(args[0], router); | ||
default: | ||
return prependMatcher(matchAll.apply(void 0, args.slice(0, args.length - 1)), router); | ||
} | ||
} | ||
exports.ifMatch = ifMatch; | ||
function branchMatch(predicateOrMatcher, ifRouterOrHandler, elseRouterOrHandler) { | ||
return exports.first(ifMatch(predicateOrMatcher, ifRouterOrHandler), elseRouterOrHandler); | ||
} | ||
exports.branchMatch = branchMatch; | ||
exports.nullRouter = function () { return ({ | ||
getRoute: function (m) { return rxjs_1.Observable.empty(); } | ||
}); }; | ||
exports.ifMatchElse = ifMatchElse; | ||
var thrownRoute = { | ||
@@ -178,9 +160,15 @@ thrown: true, | ||
}; | ||
exports.throwRoute = function () { return ({ | ||
getRoute: function (m) { return rxjs_1.Observable.of(thrownRoute); } | ||
}); }; | ||
exports.catchRoute = function (router) { return ({ | ||
getRoute: function (m) { return router.getRoute(m) | ||
.filter(function (route) { return !route.thrown; }); } | ||
}); }; | ||
function throwRoute() { | ||
return { | ||
getRoute: function (m) { return rxjs_1.Observable.of(thrownRoute); } | ||
}; | ||
} | ||
exports.throwRoute = throwRoute; | ||
function catchRoute(routerOrHandler) { | ||
return { | ||
getRoute: function (m) { return toRouter(routerOrHandler).getRoute(m) | ||
.filter(function (route) { return !route.thrown; }); } | ||
}; | ||
} | ||
exports.catchRoute = catchRoute; | ||
//# sourceMappingURL=Router.js.map |
export * from './core/Router'; | ||
export * from './core/Text'; | ||
export * from './core/LUIS'; | ||
export * from './core/RegExp'; | ||
export * from './core/State'; | ||
export * from './core/ChatState'; | ||
export * from './core/Prompts'; | ||
export * from './core/Konsole'; | ||
export * from './core/Dialogs'; |
@@ -7,7 +7,3 @@ "use strict"; | ||
__export(require("./core/Router")); | ||
__export(require("./core/LUIS")); | ||
__export(require("./core/RegExp")); | ||
__export(require("./core/Prompts")); | ||
__export(require("./core/Konsole")); | ||
__export(require("./core/Dialogs")); | ||
//# sourceMappingURL=prague.js.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.12.3", | ||
"version": "0.13.0", | ||
"description": "rules-based app engine", | ||
@@ -15,3 +15,3 @@ "main": "dist/prague.js", | ||
"watch": "tsc -w", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha test" | ||
}, | ||
@@ -22,2 +22,5 @@ "author": "Bill Barnes", | ||
"@types/node": "^7.0.31", | ||
"chai": "^4.1.1", | ||
"chai-subset": "^1.5.0", | ||
"mocha": "^3.5.0", | ||
"typescript": "2.4.2" | ||
@@ -24,0 +27,0 @@ }, |
export * from './core/Router'; | ||
export * from './core/Text'; | ||
export * from './core/LUIS'; | ||
export * from './core/RegExp'; | ||
export * from './core/State'; | ||
export * from './core/ChatState'; | ||
export * from './core/Prompts'; | ||
export * from './core/Konsole'; | ||
export * from './core/Dialogs'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
2
85028
5
21
1408