Comparing version 0.13.1 to 0.13.2
@@ -38,7 +38,5 @@ import { konsole } from './Konsole'; | ||
export function nullRouter <M extends Match = {}> (): Router<M> { | ||
return { | ||
getRoute: (m) => Observable.empty() | ||
} | ||
} | ||
export const nullRouter = { | ||
getRoute: (m) => Observable.empty() | ||
} as Router<any>; | ||
@@ -159,46 +157,23 @@ export function routeMessage <M extends Match> (router: Router<M>, m: M) { | ||
export function ifMatch <L extends Match, M extends Match> ( | ||
matcher: Matcher<L, M>, | ||
routerOrHandler: RouterOrHandler<M> | ||
): Router<L>; | ||
export function ifMatch <M extends Match> ( | ||
predicate: Predicate<M>, | ||
routerOrHandler: RouterOrHandler<M> | ||
): Router<M>; | ||
export function ifMatch ( | ||
predicateOrMatcher: Matcher | Predicate, | ||
routerOrHandler: RouterOrHandler | ||
): Router<any> { | ||
const router = toRouter(routerOrHandler); | ||
return { | ||
getRoute: (m) => | ||
tryMatch(predicateOrMatcher, m) | ||
.flatMap((n: Match) => | ||
router.getRoute(n) | ||
.map(route => routeWithCombinedScore(route, n.score)) | ||
) | ||
}; | ||
} | ||
export function ifMatchElse <M extends Match> ( | ||
predicate: Predicate<M>, | ||
ifRouterOrHandler: RouterOrHandler<M>, | ||
elseRouterOrHandler: RouterOrHandler<M>, | ||
elseRouterOrHandler?: RouterOrHandler<M>, | ||
): Router<M>; | ||
export function ifMatchElse <M extends Match, N extends Match> ( | ||
export function ifMatch <M extends Match, N extends Match> ( | ||
matcher: Matcher<M, N>, | ||
ifRouterOrHandler: RouterOrHandler<N>, | ||
elseRouterOrHandler: RouterOrHandler<M>, | ||
elseRouterOrHandler?: RouterOrHandler<M>, | ||
): Router<M>; | ||
export function ifMatchElse <M extends Match> ( | ||
export function ifMatch <M extends Match> ( | ||
predicateOrMatcher: Predicate<M> | Matcher<M>, | ||
ifRouterOrHandler: RouterOrHandler, | ||
elseRouterOrHandler: RouterOrHandler, | ||
elseRouterOrHandler?: RouterOrHandler, | ||
): Router<M> { | ||
const ifRouter = toRouter(ifRouterOrHandler); | ||
const elseRouter = toRouter(elseRouterOrHandler); | ||
const elseRouter = elseRouterOrHandler | ||
? toRouter(elseRouterOrHandler) | ||
: nullRouter; | ||
return { | ||
@@ -205,0 +180,0 @@ getRoute: (m) => |
@@ -16,3 +16,3 @@ import { Observable } from 'rxjs'; | ||
} | ||
export declare function nullRouter<M extends Match = {}>(): Router<M>; | ||
export declare const nullRouter: Router<any>; | ||
export declare function routeMessage<M extends Match>(router: Router<M>, m: M): Observable<any>; | ||
@@ -43,7 +43,5 @@ export interface Handler<Z extends Match = {}> { | ||
}; | ||
export declare function ifMatch<L extends Match, M extends Match>(matcher: Matcher<L, M>, routerOrHandler: RouterOrHandler<M>): Router<L>; | ||
export declare function ifMatch<M extends Match>(predicate: Predicate<M>, routerOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatchElse<M extends Match>(predicate: Predicate<M>, ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatchElse<M extends Match, N extends Match>(matcher: Matcher<M, N>, ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatch<M extends Match>(predicate: Predicate<M>, ifRouterOrHandler: RouterOrHandler<M>, elseRouterOrHandler?: RouterOrHandler<M>): Router<M>; | ||
export declare function ifMatch<M extends Match, N extends Match>(matcher: Matcher<M, N>, ifRouterOrHandler: RouterOrHandler<N>, elseRouterOrHandler?: RouterOrHandler<M>): Router<M>; | ||
export declare function throwRoute<M extends Match>(): Router<M>; | ||
export declare function catchRoute<M extends Match>(routerOrHandler: RouterOrHandler<M>): Router<M>; |
@@ -31,8 +31,5 @@ "use strict"; | ||
exports.toFilteredObservable = toFilteredObservable; | ||
function nullRouter() { | ||
return { | ||
getRoute: function (m) { return rxjs_1.Observable.empty(); } | ||
}; | ||
} | ||
exports.nullRouter = nullRouter; | ||
exports.nullRouter = { | ||
getRoute: function (m) { return rxjs_1.Observable.empty(); } | ||
}; | ||
function routeMessage(router, m) { | ||
@@ -136,18 +133,7 @@ return router.getRoute(m) | ||
exports.routeWithCombinedScore = routeWithCombinedScore; | ||
function ifMatch(predicateOrMatcher, routerOrHandler) { | ||
var router = toRouter(routerOrHandler); | ||
return { | ||
getRoute: function (m) { | ||
return tryMatch(predicateOrMatcher, m) | ||
.flatMap(function (n) { | ||
return router.getRoute(n) | ||
.map(function (route) { return routeWithCombinedScore(route, n.score); }); | ||
}); | ||
} | ||
}; | ||
} | ||
exports.ifMatch = ifMatch; | ||
function ifMatchElse(predicateOrMatcher, ifRouterOrHandler, elseRouterOrHandler) { | ||
function ifMatch(predicateOrMatcher, ifRouterOrHandler, elseRouterOrHandler) { | ||
var ifRouter = toRouter(ifRouterOrHandler); | ||
var elseRouter = toRouter(elseRouterOrHandler); | ||
var elseRouter = elseRouterOrHandler | ||
? toRouter(elseRouterOrHandler) | ||
: exports.nullRouter; | ||
return { | ||
@@ -165,3 +151,3 @@ getRoute: function (m) { | ||
} | ||
exports.ifMatchElse = ifMatchElse; | ||
exports.ifMatch = ifMatch; | ||
var thrownRoute = { | ||
@@ -168,0 +154,0 @@ thrown: true, |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.13.1", | ||
"version": "0.13.2", | ||
"description": "rules-based app engine", | ||
@@ -10,0 +10,0 @@ "main": "dist/prague.js", |
@@ -5,3 +5,3 @@ "use strict"; | ||
const expect = chai.expect; | ||
const { toObservable, toFilteredObservable, isRouter, simpleRouter, toRouter, routeMessage, first, best, run, tryMatch, routeWithCombinedScore, ifMatch, ifMatchElse, nullRouter, throwRoute, catchRoute } = require('../dist/prague.js'); | ||
const { toObservable, toFilteredObservable, isRouter, simpleRouter, toRouter, routeMessage, first, best, run, tryMatch, routeWithCombinedScore, ifMatch, nullRouter, throwRoute, catchRoute } = require('../dist/prague.js'); | ||
const { Observable } = require('rxjs'); | ||
@@ -279,3 +279,3 @@ | ||
it('should not route', (done) => { | ||
nullRouter() | ||
nullRouter | ||
.getRoute(foo) | ||
@@ -309,3 +309,3 @@ .subscribe(throwErr, passErr, done); | ||
routeMessage( | ||
nullRouter(), | ||
nullRouter, | ||
foo | ||
@@ -338,3 +338,3 @@ ) | ||
it('should return true for a router', () => { | ||
expect(isRouter(nullRouter())).to.be.true; | ||
expect(isRouter(nullRouter)).to.be.true; | ||
}); | ||
@@ -420,3 +420,3 @@ | ||
first( | ||
nullRouter(), | ||
nullRouter, | ||
null, | ||
@@ -433,3 +433,3 @@ undefined | ||
first( | ||
nullRouter() | ||
nullRouter | ||
), | ||
@@ -498,3 +498,3 @@ foo | ||
first( | ||
nullRouter(), | ||
nullRouter, | ||
simpleRouter(m => { | ||
@@ -544,3 +544,3 @@ routed = true; | ||
best( | ||
nullRouter(), | ||
nullRouter, | ||
null, | ||
@@ -557,3 +557,3 @@ undefined | ||
best( | ||
nullRouter() | ||
nullRouter | ||
), | ||
@@ -622,3 +622,3 @@ foo | ||
best( | ||
nullRouter(), | ||
nullRouter, | ||
simpleRouter(m => { | ||
@@ -797,3 +797,3 @@ routed = true; | ||
describe('ifMatch', () => { | ||
it('should complete and never emit on false predicate', (done) => | ||
it("should complete and never emit on false predicate when 'else' router doesn't exist", (done) => | ||
routeMessage( | ||
@@ -809,5 +809,17 @@ ifMatch( | ||
it('should complete and never emit on no match', (done) => | ||
it("should complete and never emit on false predicate when 'else' router doesn't route", (done) => | ||
routeMessage( | ||
ifMatch( | ||
m => false, | ||
throwErr, | ||
nullRouter | ||
), | ||
foo | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it("should complete and never emit on no match when 'else' router doesn't exist", (done) => | ||
routeMessage( | ||
ifMatch( | ||
addBar, | ||
@@ -821,21 +833,62 @@ throwErr | ||
it('should route message to handler on true predicate', (done) => { | ||
let routed; | ||
it("should complete and never emit on no match when 'else' router doesn't route", (done) => | ||
routeMessage( | ||
ifMatch( | ||
addBar, | ||
throwErr, | ||
nullRouter | ||
), | ||
notFoo | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it("should complete and never emit on true predicate when 'if' router doesn't route and 'else' router doesn't exist", (done) => | ||
routeMessage( | ||
ifMatch( | ||
m => true, | ||
m => { | ||
routed = true; | ||
} | ||
nullRouter | ||
), | ||
foo | ||
) | ||
.subscribe(n => { | ||
expect(routed).to.be.true; | ||
done(); | ||
}) | ||
}); | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it('should route message to router on true predicate', (done) => { | ||
it("should complete and never emit on true predicate when 'if' router doesn't route and 'else' router exists", (done) => | ||
routeMessage( | ||
ifMatch( | ||
m => true, | ||
nullRouter, | ||
throwErr | ||
), | ||
foo | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it("should complete and never emit on match when 'if' router doesn't route and 'else' router doesn't exist", (done) => | ||
routeMessage( | ||
ifMatch( | ||
addBar, | ||
nullRouter | ||
), | ||
foo | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it("should complete and never emit on match when 'if' router doesn't route and 'else' router exists", (done) => | ||
routeMessage( | ||
ifMatch( | ||
addBar, | ||
nullRouter, | ||
throwErr | ||
), | ||
foo | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it("should route message to 'if' handler on true predicate when 'else' router doesn't exist", (done) => { | ||
let routed; | ||
@@ -846,5 +899,5 @@ | ||
m => true, | ||
simpleRouter(m => { | ||
m => { | ||
routed = true; | ||
}) | ||
} | ||
), | ||
@@ -859,3 +912,3 @@ foo | ||
it('should route message to handler on match', (done) => { | ||
it("should route message to 'if' handler on true predicate when 'else' router exists", (done) => { | ||
let routed; | ||
@@ -865,6 +918,7 @@ | ||
ifMatch( | ||
addBar, | ||
m => true, | ||
m => { | ||
routed = true; | ||
} | ||
}, | ||
throwErr | ||
), | ||
@@ -879,3 +933,3 @@ foo | ||
it('should route message to router on match', (done) => { | ||
it("should route message to 'if' router on true predicate when 'else' router doesn't exist", (done) => { | ||
let routed; | ||
@@ -885,3 +939,3 @@ | ||
ifMatch( | ||
addBar, | ||
m => true, | ||
simpleRouter(m => { | ||
@@ -899,62 +953,11 @@ routed = true; | ||
it("should return score=1 with both scores undefined", (done) => { | ||
ifMatch( | ||
m => true, | ||
m => {} | ||
) | ||
.getRoute(foo) | ||
.subscribe(route => { | ||
expect(route.score).to.eql(1); | ||
done(); | ||
}) | ||
}); | ||
it("should route message to 'if' router on true predicate when 'else' router exists", (done) => { | ||
let routed; | ||
it("should return supplied score when route score undefined", (done) => { | ||
ifMatch( | ||
m => ({ | ||
score: 0.4 | ||
}), | ||
() => {} | ||
) | ||
.getRoute(foo) | ||
.subscribe(route => { | ||
expect(route.score).to.eql(.4); | ||
done(); | ||
}) | ||
}); | ||
it("should return route score when supplied score undefined", (done) => { | ||
ifMatch( | ||
m => true, | ||
makeRouter(0.25, () => {}) | ||
) | ||
.getRoute(foo) | ||
.subscribe(route => { | ||
expect(route.score).to.eql(.25); | ||
done(); | ||
}) | ||
}); | ||
it("should return combined score when both scores supplied", (done) => { | ||
ifMatch( | ||
m => ({ | ||
score: 0.4 | ||
}), | ||
makeRouter(0.25, () => {}) | ||
) | ||
.getRoute(foo) | ||
.subscribe(route => { | ||
expect(route.score).to.eql(.1); | ||
done(); | ||
}) | ||
}); | ||
}); | ||
describe('ifMatchElse', () => { | ||
it("should complete and never emit on true predicate when first router doesn't route", (done) => | ||
routeMessage( | ||
ifMatchElse( | ||
ifMatch( | ||
m => true, | ||
nullRouter(), | ||
simpleRouter(m => { | ||
routed = true; | ||
}), | ||
throwErr | ||
@@ -964,51 +967,17 @@ ), | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
.subscribe(n => { | ||
expect(routed).to.be.true; | ||
done(); | ||
}) | ||
}); | ||
it("should complete and never emit on match when first router doesn't route", (done) => | ||
routeMessage( | ||
ifMatchElse( | ||
addBar, | ||
nullRouter(), | ||
throwErr | ||
), | ||
foo | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it("should route message to 'if' handler on match when 'else' router doesn't exist", (done) => { | ||
let routed; | ||
it("should complete and never emit on false predicate when second router doesn't route", (done) => | ||
routeMessage( | ||
ifMatchElse( | ||
m => false, | ||
throwErr, | ||
nullRouter() | ||
), | ||
foo | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it("should complete and never emit on no match when second router doesn't route", (done) => | ||
routeMessage( | ||
ifMatchElse( | ||
ifMatch( | ||
addBar, | ||
throwErr, | ||
nullRouter() | ||
), | ||
notFoo | ||
) | ||
.subscribe(throwErr, passErr, done) | ||
); | ||
it('should route message to first handler on true predicate', (done) => { | ||
let routed; | ||
routeMessage( | ||
ifMatchElse( | ||
m => true, | ||
m => { | ||
routed = true; | ||
}, | ||
throwErr | ||
} | ||
), | ||
@@ -1023,11 +992,11 @@ foo | ||
it('should route message to first router on true predicate', (done) => { | ||
it("should route message to 'if' handler on match when 'else' router exists", (done) => { | ||
let routed; | ||
routeMessage( | ||
ifMatchElse( | ||
m => true, | ||
simpleRouter(m => { | ||
ifMatch( | ||
addBar, | ||
m => { | ||
routed = true; | ||
}), | ||
}, | ||
throwErr | ||
@@ -1043,12 +1012,11 @@ ), | ||
it('should route message to first handler on match', (done) => { | ||
it("should route message to 'if' router on match when 'else' router doesn't exist", (done) => { | ||
let routed; | ||
routeMessage( | ||
ifMatchElse( | ||
ifMatch( | ||
addBar, | ||
m => { | ||
simpleRouter(m => { | ||
routed = true; | ||
}, | ||
throwErr | ||
}) | ||
), | ||
@@ -1063,7 +1031,7 @@ foo | ||
it('should route message to first router on no match', (done) => { | ||
it("should route message to 'if' router on match when 'else' router exists", (done) => { | ||
let routed; | ||
routeMessage( | ||
ifMatchElse( | ||
ifMatch( | ||
addBar, | ||
@@ -1083,7 +1051,8 @@ simpleRouter(m => { | ||
it('should route message to second handler on false predicate', (done) => { | ||
it("should route message to 'else' handler on false predicate", (done) => { | ||
let routed; | ||
routeMessage( | ||
ifMatchElse( | ||
ifMatch( | ||
m => false, | ||
@@ -1103,7 +1072,7 @@ throwErr, | ||
it('should route message to second router on false predicate', (done) => { | ||
it("should route message to 'else' router on false predicate", (done) => { | ||
let routed; | ||
routeMessage( | ||
ifMatchElse( | ||
ifMatch( | ||
m => false, | ||
@@ -1123,7 +1092,7 @@ throwErr, | ||
it('should route message to second handler on no match', (done) => { | ||
it("should route message to 'else' handler on no match", (done) => { | ||
let routed; | ||
routeMessage( | ||
ifMatchElse( | ||
ifMatch( | ||
addBar, | ||
@@ -1143,7 +1112,7 @@ throwErr, | ||
it('should route message to second router on no match', (done) => { | ||
it("should route message to 'else' router on no match", (done) => { | ||
let routed; | ||
routeMessage( | ||
ifMatchElse( | ||
ifMatch( | ||
addBar, | ||
@@ -1163,6 +1132,5 @@ throwErr, | ||
it("should return score=1 on true predicate when route score undefined", (done) => { | ||
ifMatchElse( | ||
it("should return score=1 on true predicate when 'if' score undefined", (done) => { | ||
ifMatch( | ||
m => true, | ||
m => {}, | ||
m => {} | ||
@@ -1177,6 +1145,5 @@ ) | ||
it("should return score=1 on scoreless match when route score undefined", (done) => { | ||
ifMatchElse( | ||
it("should return score=1 on scoreless match when 'if' score undefined", (done) => { | ||
ifMatch( | ||
m => ({}), | ||
m => {}, | ||
m => {} | ||
@@ -1191,9 +1158,8 @@ ) | ||
it("should return supplied score on match when 'if' route score undefined", (done) => { | ||
ifMatchElse( | ||
it("should return supplied score when 'if' score undefined", (done) => { | ||
ifMatch( | ||
m => ({ | ||
score: 0.4 | ||
}), | ||
m => {}, | ||
m => {} | ||
() => {} | ||
) | ||
@@ -1207,7 +1173,6 @@ .getRoute(foo) | ||
it("should return 'if' route score when match score undefined", (done) => { | ||
ifMatchElse( | ||
it("should return route score on true predicate", (done) => { | ||
ifMatch( | ||
m => true, | ||
makeRouter(0.25, () => {}), | ||
makeRouter(0.5, () => {}) | ||
makeRouter(0.25, () => {}) | ||
) | ||
@@ -1221,9 +1186,20 @@ .getRoute(foo) | ||
it("should combine supplied score and 'if' route score on match", (done) => { | ||
ifMatchElse( | ||
it("should return route score on scoreless match", (done) => { | ||
ifMatch( | ||
m => ({}), | ||
makeRouter(0.25, () => {}) | ||
) | ||
.getRoute(foo) | ||
.subscribe(route => { | ||
expect(route.score).to.eql(.25); | ||
done(); | ||
}) | ||
}); | ||
it("should return combined score when both scores supplied", (done) => { | ||
ifMatch( | ||
m => ({ | ||
score: 0.4 | ||
}), | ||
makeRouter(0.25, () => {}), | ||
makeRouter(0.5, () => {}) | ||
makeRouter(0.25, () => {}) | ||
) | ||
@@ -1237,6 +1213,32 @@ .getRoute(foo) | ||
it("should return score=1 on false predicate when 'else' score undefined", (done) => { | ||
ifMatch( | ||
m => false, | ||
m => {}, | ||
m => {} | ||
) | ||
.getRoute(foo) | ||
.subscribe(route => { | ||
expect(route.score).to.eql(1); | ||
done(); | ||
}) | ||
}); | ||
it("should return score=1 on scoreless match when 'else' score undefined", (done) => { | ||
ifMatch( | ||
m => ({}), | ||
m => {}, | ||
m => {} | ||
) | ||
.getRoute(foo) | ||
.subscribe(route => { | ||
expect(route.score).to.eql(1); | ||
done(); | ||
}) | ||
}); | ||
it("should return 'else' route score on false predicate", (done) => { | ||
ifMatchElse( | ||
ifMatch( | ||
m => false, | ||
makeRouter(0.25, () => {}), | ||
throwErr, | ||
makeRouter(0.5, () => {}) | ||
@@ -1252,5 +1254,5 @@ ) | ||
it("should return 'else' route score on no match", (done) => { | ||
ifMatchElse( | ||
ifMatch( | ||
addBar, | ||
makeRouter(0.25, () => {}), | ||
throwErr, | ||
makeRouter(0.5, () => {}) | ||
@@ -1257,0 +1259,0 @@ ) |
Sorry, the diff of this file is not supported yet
90115
1580