prague-fluent
Advanced tools
Comparing version 0.17.3 to 0.17.4
@@ -6,3 +6,4 @@ import { Observable } from 'rxjs'; | ||
private _route; | ||
static is<VALUE>(route: any): route is Route<VALUE>; | ||
do$(): Observable<boolean>; | ||
do(): Promise<boolean>; | ||
} | ||
@@ -13,5 +14,4 @@ export declare class ScoredRoute<VALUE = any> extends Route<VALUE> { | ||
static normalizedScore(score?: number): number; | ||
static is(route: any): route is ScoredRoute; | ||
static combinedScore(score: any, otherScore: any): number; | ||
clone(score?: number): this; | ||
cloneWithScore(score?: number): this; | ||
cloneWithCombinedScore(score?: number): this; | ||
@@ -23,3 +23,2 @@ } | ||
constructor(action: ACTION, args: ARGS, score?: number); | ||
static is<ACTION, ARGS>(route: any): route is TemplateRoute<ACTION, ARGS>; | ||
} | ||
@@ -39,5 +38,5 @@ export declare type TemplateActions<TEMPLATES> = keyof TEMPLATES; | ||
export declare class DoRoute extends ScoredRoute<undefined> { | ||
action: Action; | ||
private action; | ||
constructor(action: Action, score?: number); | ||
static is(route: any): route is DoRoute; | ||
do$(): Observable<boolean>; | ||
} | ||
@@ -49,3 +48,2 @@ export declare function _do<ARG = any>(action: (arg?: ARG) => Observableable<any>, score?: number): Router<ARG, DoRoute>; | ||
constructor(value: VALUE, score?: number); | ||
static is<VALUE = any>(route: any): route is MatchRoute<VALUE>; | ||
} | ||
@@ -61,3 +59,3 @@ export declare function match<VALUE = any>(value: VALUE, score?: number): Router<{}, MatchRoute<VALUE>>; | ||
static router: () => Observable<NoRoute<any>>; | ||
static is<VALUE = any>(route: any): route is NoRoute<VALUE>; | ||
do$(): Observable<boolean>; | ||
} | ||
@@ -69,10 +67,9 @@ declare function _no<VALUE>(reason?: string, value?: VALUE): Router<{}, NoRoute<VALUE>>; | ||
export declare class Router<ARG = undefined, VALUE = any> { | ||
getRoute$: (arg?: ARG) => Observable<Route<VALUE>>; | ||
getRoute(arg?: ARG): Promise<Route<VALUE>>; | ||
route$: (arg?: ARG) => Observable<Route<VALUE>>; | ||
route(arg?: ARG): Promise<Route<VALUE>>; | ||
constructor(router?: AnyRouter<ARG, VALUE>); | ||
private static normalizedRoute<VALUE>(route); | ||
static from<ARG, VALUE>(router?: AnyRouter<ARG, VALUE>): Router<ARG, VALUE>; | ||
static is<ARG, VALUE>(router: any): router is Router<ARG, VALUE>; | ||
route$(arg?: ARG): Observable<boolean>; | ||
route(arg?: ARG): Promise<boolean>; | ||
do$(arg?: ARG): Observable<boolean>; | ||
do(arg?: ARG): Promise<boolean>; | ||
map(mapRoute: AnyRouter<Route<VALUE>>): Router<ARG, any>; | ||
@@ -103,7 +100,8 @@ mapByType(mapTypeToRouter: Partial<MapTypeToRouter<VALUE>>): Router<ARG, any>; | ||
}; | ||
export declare function typeRouter<VALUE>(mapTypeToRouter: Partial<MapTypeToRouter<VALUE>>): Router<Route<VALUE>, any>; | ||
export declare function ifGet<VALUE>(getMatch: AnyRouter<undefined, VALUE>, mapMatchRoute: AnyRouter<MatchRoute<VALUE>>, mapNoRoute?: AnyRouter<NoRoute<VALUE>>): Router<undefined, any>; | ||
export declare function _if(predicate: AnyRouter<undefined, boolean>, mapMatchRoute: AnyRouter<MatchRoute<boolean>>, mapNoRoute?: AnyRouter<NoRoute<boolean>>): Router<undefined, any>; | ||
export { _if as if }; | ||
export declare const filterForDoRoute: <VALUE>(route: Route<VALUE>) => DoRoute | NoRoute<VALUE>; | ||
export declare const doable: <VALUE>(route: Route<VALUE>) => void; | ||
export declare function _switch(getKey: GetRoute<undefined, string>, mapKeyToRouter: Record<string, GetRoute>): Router<undefined, any>; | ||
export { _switch as switch }; |
@@ -59,8 +59,12 @@ "use strict"; | ||
exports.toObservable = toObservable; | ||
var routeDoError = new Error('route is not a doRoute or noRoute, cannot do()'); | ||
var Route = /** @class */ (function () { | ||
function Route() { | ||
} | ||
Route.is = function (route) { | ||
return route instanceof Route; | ||
Route.prototype.do$ = function () { | ||
throw routeDoError; | ||
}; | ||
Route.prototype.do = function () { | ||
return this.do$().toPromise(); | ||
}; | ||
return Route; | ||
@@ -81,9 +85,6 @@ }()); | ||
}; | ||
ScoredRoute.is = function (route) { | ||
return route instanceof ScoredRoute; | ||
}; | ||
ScoredRoute.combinedScore = function (score, otherScore) { | ||
return score * otherScore; | ||
}; | ||
ScoredRoute.prototype.clone = function (score) { | ||
ScoredRoute.prototype.cloneWithScore = function (score) { | ||
score = ScoredRoute.normalizedScore(score); | ||
@@ -95,3 +96,3 @@ return score === this.score | ||
ScoredRoute.prototype.cloneWithCombinedScore = function (score) { | ||
return this.clone(ScoredRoute.combinedScore(this.score, ScoredRoute.normalizedScore(score))); | ||
return this.cloneWithScore(ScoredRoute.combinedScore(this.score, ScoredRoute.normalizedScore(score))); | ||
}; | ||
@@ -109,5 +110,2 @@ return ScoredRoute; | ||
} | ||
TemplateRoute.is = function (route) { | ||
return route instanceof TemplateRoute; | ||
}; | ||
return TemplateRoute; | ||
@@ -135,3 +133,3 @@ }(ScoredRoute)); | ||
.from(router) | ||
.getRoute$(route.args) | ||
.route$(route.args) | ||
: route; | ||
@@ -149,4 +147,4 @@ }; | ||
} | ||
DoRoute.is = function (route) { | ||
return route instanceof DoRoute; | ||
DoRoute.prototype.do$ = function () { | ||
return toObservable(this.action()).mapTo(true); | ||
}; | ||
@@ -168,5 +166,2 @@ return DoRoute; | ||
} | ||
MatchRoute.is = function (route) { | ||
return route instanceof MatchRoute; | ||
}; | ||
return MatchRoute; | ||
@@ -188,4 +183,4 @@ }(ScoredRoute)); | ||
} | ||
NoRoute.is = function (route) { | ||
return route instanceof NoRoute; | ||
NoRoute.prototype.do$ = function () { | ||
return rxjs_1.Observable.of(false); | ||
}; | ||
@@ -203,3 +198,3 @@ NoRoute.defaultReason = "none"; | ||
exports.no = _no; | ||
var getRouteError = new Error('router must be a function'); | ||
var routerNotFunctionError = new Error('router must be a function'); | ||
var route$Error = new Error('route must be a DoRoute or a NoRoute'); | ||
@@ -209,19 +204,19 @@ var Router = /** @class */ (function () { | ||
if (router == null) | ||
this.getRoute$ = NoRoute.router; | ||
else if (Router.is(router)) | ||
this.getRoute$ = router.getRoute$; | ||
this.route$ = NoRoute.router; | ||
else if (router instanceof Router) | ||
this.route$ = router.route$; | ||
else if (typeof router !== 'function') | ||
throw getRouteError; | ||
throw routerNotFunctionError; | ||
else { | ||
this.getRoute$ = function (arg) { return rxjs_1.Observable | ||
this.route$ = function (arg) { return rxjs_1.Observable | ||
.of(router) | ||
.map(function (router) { return router(arg); }) | ||
.flatMap(toObservable) | ||
.flatMap(function (result) { return Router.is(result) | ||
? result.getRoute$() | ||
.flatMap(function (result) { return result instanceof Router | ||
? result.route$() | ||
: rxjs_1.Observable.of(Router.normalizedRoute(result)); }); }; | ||
} | ||
} | ||
Router.prototype.getRoute = function (arg) { | ||
return this.getRoute$(arg).toPromise(); | ||
Router.prototype.route = function (arg) { | ||
return this.route$(arg).toPromise(); | ||
}; | ||
@@ -231,3 +226,3 @@ Router.normalizedRoute = function (route) { | ||
return NoRoute.default; | ||
if (Route.is(route)) | ||
if (route instanceof Route) | ||
return route; | ||
@@ -238,23 +233,14 @@ return new MatchRoute(route); | ||
Router.from = function (router) { | ||
return Router.is(router) | ||
return router instanceof Router | ||
? router | ||
: new Router(router); | ||
}; | ||
Router.is = function (router) { | ||
return router instanceof Router; | ||
}; | ||
Router.prototype.route$ = function (arg) { | ||
Router.prototype.do$ = function (arg) { | ||
return this | ||
.getRoute$(arg) | ||
.flatMap(function (route) { | ||
if (DoRoute.is(route)) | ||
return toObservable(route.action()).mapTo(true); | ||
if (NoRoute.is(route)) | ||
return rxjs_1.Observable.of(false); | ||
throw route$Error; | ||
}); | ||
.route$(arg) | ||
.flatMap(function (route) { return route.do$(); }); | ||
}; | ||
Router.prototype.route = function (arg) { | ||
Router.prototype.do = function (arg) { | ||
return this | ||
.route$(arg) | ||
.do$(arg) | ||
.toPromise(); | ||
@@ -265,4 +251,4 @@ }; | ||
return new Router(function (arg) { return _this | ||
.getRoute$(arg) | ||
.flatMap(Router.from(mapRoute).getRoute$); }); | ||
.route$(arg) | ||
.flatMap(Router.from(mapRoute).route$); }); | ||
}; | ||
@@ -287,6 +273,6 @@ Router.prototype.mapByType = function (mapTypeToRouter) { | ||
return this | ||
.map(exports.filterForDoRoute) | ||
.tap(exports.doable) | ||
.mapByType({ | ||
do: function (route) { return new DoRoute(function () { return toObservable(action()) | ||
.flatMap(function (_) { return toObservable(route.action()); }); }, route.score); } | ||
.flatMap(function (_) { return route.do$(); }); }, route.score); } | ||
}); | ||
@@ -296,5 +282,6 @@ }; | ||
return this | ||
.map(exports.filterForDoRoute) | ||
.tap(exports.doable) | ||
.mapByType({ | ||
do: function (route) { return new DoRoute(function () { return toObservable(route.action()) | ||
do: function (route) { return new DoRoute(function () { return route | ||
.do$() | ||
.flatMap(function (_) { return toObservable(action()); }); }, route.score); } | ||
@@ -308,5 +295,5 @@ }); | ||
function strictlyFilterScored(route) { | ||
if (ScoredRoute.is(route)) | ||
if (route instanceof ScoredRoute) | ||
return true; | ||
if (NoRoute.is(route)) | ||
if (route instanceof NoRoute) | ||
return false; | ||
@@ -324,3 +311,3 @@ throw strictlyFilterError; | ||
.from(router) | ||
.getRoute$(); }) | ||
.route$(); }) | ||
.filter(strictlyFilterScored) | ||
@@ -345,3 +332,3 @@ .take(1) // so that we don't keep going through routers after we find one that matches | ||
.from(router) | ||
.getRoute$(); }) | ||
.route$(); }) | ||
.takeWhile(function (_) { return bestRoute.score < 1; }) | ||
@@ -375,2 +362,3 @@ .filter(strictlyFilterScored) | ||
exports.noop = noop; | ||
var notRouteError = new Error('expecting a route'); | ||
function getTypesFromRoute(route) { | ||
@@ -380,3 +368,3 @@ return __generator(this, function (_a) { | ||
case 0: | ||
if (!NoRoute.is(route)) return [3 /*break*/, 2]; | ||
if (!(route instanceof NoRoute)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, 'no']; | ||
@@ -387,3 +375,3 @@ case 1: | ||
case 2: | ||
if (!DoRoute.is(route)) return [3 /*break*/, 4]; | ||
if (!(route instanceof DoRoute)) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, 'do']; | ||
@@ -394,3 +382,3 @@ case 3: | ||
case 4: | ||
if (!MatchRoute.is(route)) return [3 /*break*/, 6]; | ||
if (!(route instanceof MatchRoute)) return [3 /*break*/, 6]; | ||
return [4 /*yield*/, 'match']; | ||
@@ -401,3 +389,3 @@ case 5: | ||
case 6: | ||
if (!TemplateRoute.is(route)) return [3 /*break*/, 8]; | ||
if (!(route instanceof TemplateRoute)) return [3 /*break*/, 8]; | ||
return [4 /*yield*/, 'template']; | ||
@@ -408,3 +396,3 @@ case 7: | ||
case 8: | ||
if (!ScoredRoute.is(route)) return [3 /*break*/, 10]; | ||
if (!(route instanceof ScoredRoute)) return [3 /*break*/, 10]; | ||
return [4 /*yield*/, 'scored']; | ||
@@ -414,10 +402,7 @@ case 9: | ||
_a.label = 10; | ||
case 10: | ||
if (!Route.is(route)) return [3 /*break*/, 12]; | ||
return [4 /*yield*/, 'route']; | ||
case 10: return [4 /*yield*/, 'route']; | ||
case 11: | ||
_a.sent(); | ||
_a.label = 12; | ||
case 12: return [4 /*yield*/, 'default']; | ||
case 13: | ||
return [4 /*yield*/, 'default']; | ||
case 12: | ||
_a.sent(); | ||
@@ -436,3 +421,3 @@ return [2 /*return*/]; | ||
if (router) | ||
return Router.from(router).getRoute$(route); | ||
return Router.from(router).route$(route); | ||
} | ||
@@ -451,2 +436,3 @@ } | ||
} | ||
exports.typeRouter = typeRouter; | ||
var mapRouteIdentity = function (route) { return route; }; | ||
@@ -486,6 +472,5 @@ var getMatchError = new Error("ifGet's matchRouter should only return MatchRoute or NoRoute"); | ||
var doError = new Error("this router must only return DoRoute or NoRoute"); | ||
exports.filterForDoRoute = function (route) { | ||
if (DoRoute.is(route) || NoRoute.is(route)) | ||
return route; | ||
throw doError; | ||
exports.doable = function (route) { | ||
if (!(route instanceof DoRoute) && !(route instanceof NoRoute)) | ||
throw doError; | ||
}; | ||
@@ -492,0 +477,0 @@ function _switch(getKey, mapKeyToRouter) { |
{ | ||
"name": "prague-fluent", | ||
"version": "0.17.3", | ||
"version": "0.17.4", | ||
"description": "fluent API for Prague", | ||
@@ -5,0 +5,0 @@ "main": "dist/fluent.js", |
import { Observable } from 'rxjs'; | ||
import { inspect } from 'util'; | ||
@@ -13,8 +14,14 @@ export type Observableable <T> = T | Observable<T> | Promise<T>; | ||
const routeDoError = new Error('route is not a doRoute or noRoute, cannot do()'); | ||
export abstract class Route <VALUE = any> { | ||
private _route: undefined; // hack so that TypeScript will do better type checking on Routes | ||
static is <VALUE> (route: any): route is Route<VALUE> { | ||
return route instanceof Route; | ||
do$(): Observable<boolean> { | ||
throw routeDoError; | ||
} | ||
do() { | ||
return this.do$().toPromise(); | ||
} | ||
} | ||
@@ -39,6 +46,2 @@ | ||
static is (route: any): route is ScoredRoute { | ||
return route instanceof ScoredRoute; | ||
} | ||
static combinedScore(score, otherScore) { | ||
@@ -48,3 +51,3 @@ return score * otherScore | ||
clone(score?: number): this { | ||
cloneWithScore(score?: number): this { | ||
score = ScoredRoute.normalizedScore(score); | ||
@@ -58,3 +61,3 @@ | ||
cloneWithCombinedScore(score?: number) { | ||
return this.clone(ScoredRoute.combinedScore(this.score, ScoredRoute.normalizedScore(score))); | ||
return this.cloneWithScore(ScoredRoute.combinedScore(this.score, ScoredRoute.normalizedScore(score))); | ||
} | ||
@@ -71,6 +74,2 @@ } | ||
} | ||
static is <ACTION, ARGS> (route: any): route is TemplateRoute<ACTION, ARGS> { | ||
return route instanceof TemplateRoute; | ||
} | ||
} | ||
@@ -115,3 +114,3 @@ | ||
.from(router) | ||
.getRoute$(route.args) | ||
.route$(route.args) | ||
: route; | ||
@@ -125,3 +124,3 @@ } | ||
constructor ( | ||
public action: Action, | ||
private action: Action, | ||
score?: number | ||
@@ -132,4 +131,4 @@ ) { | ||
static is (route: any): route is DoRoute { | ||
return route instanceof DoRoute; | ||
do$() { | ||
return toObservable(this.action()).mapTo(true); | ||
} | ||
@@ -154,6 +153,2 @@ } | ||
} | ||
static is <VALUE = any> (route: any): route is MatchRoute<VALUE> { | ||
return route instanceof MatchRoute; | ||
} | ||
} | ||
@@ -184,5 +179,6 @@ | ||
static is <VALUE = any> (route: any): route is NoRoute<VALUE> { | ||
return route instanceof NoRoute; | ||
do$() { | ||
return Observable.of(false); | ||
} | ||
} | ||
@@ -201,3 +197,3 @@ | ||
const getRouteError = new Error('router must be a function'); | ||
const routerNotFunctionError = new Error('router must be a function'); | ||
const route$Error = new Error('route must be a DoRoute or a NoRoute'); | ||
@@ -208,6 +204,6 @@ | ||
export class Router <ARG = undefined, VALUE = any> { | ||
getRoute$: (arg?: ARG) => Observable<Route<VALUE>>; | ||
route$: (arg?: ARG) => Observable<Route<VALUE>>; | ||
getRoute (arg?: ARG) { | ||
return this.getRoute$(arg).toPromise(); | ||
route (arg?: ARG) { | ||
return this.route$(arg).toPromise(); | ||
} | ||
@@ -217,14 +213,14 @@ | ||
if (router == null) | ||
this.getRoute$ = NoRoute.router; | ||
else if (Router.is(router)) | ||
this.getRoute$ = router.getRoute$; | ||
this.route$ = NoRoute.router; | ||
else if (router instanceof Router) | ||
this.route$ = router.route$; | ||
else if (typeof router !== 'function') | ||
throw getRouteError; | ||
throw routerNotFunctionError; | ||
else { | ||
this.getRoute$ = (arg: ARG) => Observable | ||
this.route$ = arg => Observable | ||
.of(router) | ||
.map(router => router(arg)) | ||
.flatMap(toObservable) | ||
.flatMap(result => Router.is(result) | ||
? result.getRoute$() | ||
.flatMap(result => result instanceof Router | ||
? result.route$() | ||
: Observable.of(Router.normalizedRoute(result)) | ||
@@ -241,3 +237,3 @@ ); | ||
if (Route.is(route)) | ||
if (route instanceof Route) | ||
return route; | ||
@@ -249,3 +245,3 @@ | ||
static from <ARG, VALUE> (router?: AnyRouter<ARG, VALUE>) { | ||
return Router.is(router) | ||
return router instanceof Router | ||
? router | ||
@@ -255,21 +251,11 @@ : new Router(router); | ||
static is <ARG, VALUE> (router: any): router is Router<ARG, VALUE> { | ||
return router instanceof Router; | ||
} | ||
route$ (arg?: ARG) { | ||
do$ (arg?: ARG) { | ||
return this | ||
.getRoute$(arg) | ||
.flatMap(route => { | ||
if (DoRoute.is(route)) | ||
return toObservable(route.action()).mapTo(true); | ||
if (NoRoute.is(route)) | ||
return Observable.of(false); | ||
throw route$Error; | ||
}); | ||
.route$(arg) | ||
.flatMap(route => route.do$()); | ||
} | ||
route (arg?: ARG) { | ||
do (arg?: ARG) { | ||
return this | ||
.route$(arg) | ||
.do$(arg) | ||
.toPromise(); | ||
@@ -280,4 +266,4 @@ } | ||
return new Router((arg?: ARG) => this | ||
.getRoute$(arg) | ||
.flatMap(Router.from(mapRoute).getRoute$) | ||
.route$(arg) | ||
.flatMap(Router.from(mapRoute).route$) | ||
); | ||
@@ -316,7 +302,7 @@ } | ||
return this | ||
.map(filterForDoRoute) | ||
.tap(doable) | ||
.mapByType({ | ||
do: route => new DoRoute( | ||
() => toObservable(action()) | ||
.flatMap(_ => toObservable(route.action())), | ||
.flatMap(_ => route.do$()), | ||
route.score | ||
@@ -331,6 +317,7 @@ ) | ||
return this | ||
.map(filterForDoRoute) | ||
.tap(doable) | ||
.mapByType({ | ||
do: route => new DoRoute( | ||
() => toObservable(route.action()) | ||
() => route | ||
.do$() | ||
.flatMap(_ => toObservable(action())), | ||
@@ -346,6 +333,6 @@ route.score | ||
function strictlyFilterScored(route: Route): route is ScoredRoute { | ||
if (ScoredRoute.is(route)) | ||
if (route instanceof ScoredRoute) | ||
return true; | ||
if (NoRoute.is(route)) | ||
if (route instanceof NoRoute) | ||
return false; | ||
@@ -364,3 +351,3 @@ | ||
.from(router) | ||
.getRoute$() | ||
.route$() | ||
) | ||
@@ -392,3 +379,3 @@ .filter(strictlyFilterScored) | ||
.from(router) | ||
.getRoute$() | ||
.route$() | ||
) | ||
@@ -443,22 +430,23 @@ // early exit if we've already found a winner (score === 1) | ||
const notRouteError = new Error('expecting a route'); | ||
export function* getTypesFromRoute( | ||
route: Route | ||
) { | ||
if (NoRoute.is(route)) | ||
if (route instanceof NoRoute) | ||
yield 'no'; | ||
if (DoRoute.is(route)) | ||
if (route instanceof DoRoute) | ||
yield 'do'; | ||
if (MatchRoute.is(route)) | ||
if (route instanceof MatchRoute) | ||
yield 'match'; | ||
if (TemplateRoute.is(route)) | ||
if (route instanceof TemplateRoute) | ||
yield 'template'; | ||
if (ScoredRoute.is(route)) | ||
if (route instanceof ScoredRoute) | ||
yield 'scored'; | ||
if (Route.is(route)) | ||
yield 'route'; | ||
yield 'route'; | ||
@@ -470,3 +458,3 @@ yield 'default'; | ||
function typeRouter <VALUE> ( | ||
export function typeRouter <VALUE> ( | ||
mapTypeToRouter: Partial<MapTypeToRouter<VALUE>> | ||
@@ -478,3 +466,3 @@ ) { | ||
if (router) | ||
return Router.from(router).getRoute$(route); | ||
return Router.from(router).route$(route); | ||
} | ||
@@ -540,7 +528,5 @@ | ||
export const filterForDoRoute = <VALUE> (route: Route<VALUE>) => { | ||
if (DoRoute.is(route) || NoRoute.is<VALUE>(route)) | ||
return route; | ||
throw doError; | ||
export const doable = <VALUE> (route: Route<VALUE>) => { | ||
if (!(route instanceof DoRoute) && !(route instanceof NoRoute)) | ||
throw doError; | ||
} | ||
@@ -547,0 +533,0 @@ |
Sorry, the diff of this file is not supported yet
57327
9
1025