prague-fluent
Advanced tools
Comparing version 0.17.2 to 0.17.3
@@ -12,3 +12,3 @@ import { Observable } from 'rxjs'; | ||
static normalizedScore(score?: number): number; | ||
static is(route: Route): route is ScoredRoute; | ||
static is(route: any): route is ScoredRoute; | ||
static combinedScore(score: any, otherScore: any): number; | ||
@@ -18,2 +18,19 @@ clone(score?: number): this; | ||
} | ||
export declare class TemplateRoute<ACTION, ARGS> extends ScoredRoute { | ||
action: ACTION; | ||
args: ARGS; | ||
constructor(action: ACTION, args: ARGS, score?: number); | ||
static is<ACTION, ARGS>(route: any): route is TemplateRoute<ACTION, ARGS>; | ||
} | ||
export declare type TemplateActions<TEMPLATES> = keyof TEMPLATES; | ||
export declare type MapTemplateActionToRouter<TEMPLATES> = { | ||
[P in TemplateActions<TEMPLATES>]: AnyRouter<TEMPLATES[P]>; | ||
}; | ||
export declare class Templates<TEMPLATES> { | ||
private mapActionToRouter; | ||
constructor(mapActionToRouter: Partial<MapTemplateActionToRouter<TEMPLATES>>); | ||
route<ACTION extends keyof TEMPLATES, ARGS extends TEMPLATES[ACTION]>(action: ACTION, args: ARGS): TemplateRoute<ACTION, ARGS>; | ||
router<ACTION extends keyof TEMPLATES, ARGS extends TEMPLATES[ACTION]>(action: ACTION, args: ARGS): () => TemplateRoute<ACTION, ARGS>; | ||
map(route: TemplateRoute<keyof TEMPLATES, TEMPLATES[keyof TEMPLATES]>): TemplateRoute<keyof TEMPLATES, TEMPLATES[keyof TEMPLATES]> | Observable<Route<any>>; | ||
} | ||
export declare type Action = () => Observableable<any>; | ||
@@ -23,3 +40,3 @@ export declare class DoRoute extends ScoredRoute<undefined> { | ||
constructor(action: Action, score?: number); | ||
static is(route: Route): route is DoRoute; | ||
static is(route: any): route is DoRoute; | ||
} | ||
@@ -31,3 +48,3 @@ 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: Route<VALUE>): route is MatchRoute<VALUE>; | ||
static is<VALUE = any>(route: any): route is MatchRoute<VALUE>; | ||
} | ||
@@ -43,3 +60,3 @@ export declare function match<VALUE = any>(value: VALUE, score?: number): Router<{}, MatchRoute<VALUE>>; | ||
static router: () => Observable<NoRoute<any>>; | ||
static is<VALUE = any>(route: Route<VALUE>): route is NoRoute<VALUE>; | ||
static is<VALUE = any>(route: any): route is NoRoute<VALUE>; | ||
} | ||
@@ -56,7 +73,8 @@ declare function _no<VALUE>(reason?: string, value?: VALUE): Router<{}, NoRoute<VALUE>>; | ||
static from<ARG, VALUE>(router?: AnyRouter<ARG, VALUE>): Router<ARG, VALUE>; | ||
static is<ARG, VALUE>(router: any | AnyRouter<ARG, VALUE>): router is Router<ARG, VALUE>; | ||
static is<ARG, VALUE>(router: any): router is Router<ARG, VALUE>; | ||
route$(arg?: ARG): Observable<boolean>; | ||
route(arg?: ARG): Promise<boolean>; | ||
map(mapRoute: AnyRouter<Route<VALUE>>): Router<ARG, any>; | ||
mapByType(mapTypeToFlexRouter: Partial<MapTypeToRouter<VALUE>>): Router<ARG, any>; | ||
mapByType(mapTypeToRouter: Partial<MapTypeToRouter<VALUE>>): Router<ARG, any>; | ||
mapTemplate<TEMPLATES>(templates: Templates<TEMPLATES>): Router<ARG, any>; | ||
tap<VALUE>(fn: (route: Route<VALUE>) => Observableable<any>): Router<ARG, any>; | ||
@@ -75,2 +93,3 @@ default(router: AnyRouter<NoRoute>): Router<ARG, any>; | ||
match: MatchRoute<VALUE>; | ||
template: TemplateRoute<any, any>; | ||
no: NoRoute<VALUE>; | ||
@@ -80,3 +99,3 @@ default: Route; | ||
export declare type RouteTypes<VALUE> = keyof MapTypeToRouteClass<VALUE>; | ||
export declare function getTypesFromRoute(route: Route): IterableIterator<"default" | "no" | "do" | "match" | "scored" | "route">; | ||
export declare function getTypesFromRoute(route: Route): IterableIterator<"default" | "no" | "do" | "match" | "template" | "scored" | "route">; | ||
export declare type MapTypeToRouter<VALUE> = { | ||
@@ -83,0 +102,0 @@ [P in RouteTypes<VALUE>]: AnyRouter<MapTypeToRouteClass<VALUE>[P]>; |
@@ -98,2 +98,41 @@ "use strict"; | ||
exports.ScoredRoute = ScoredRoute; | ||
var TemplateRoute = /** @class */ (function (_super) { | ||
__extends(TemplateRoute, _super); | ||
function TemplateRoute(action, args, score) { | ||
var _this = _super.call(this, score) || this; | ||
_this.action = action; | ||
_this.args = args; | ||
return _this; | ||
} | ||
TemplateRoute.is = function (route) { | ||
return route instanceof TemplateRoute; | ||
}; | ||
return TemplateRoute; | ||
}(ScoredRoute)); | ||
exports.TemplateRoute = TemplateRoute; | ||
var templateError = new Error('action not present in mapActionToRouter'); | ||
var Templates = /** @class */ (function () { | ||
function Templates(mapActionToRouter) { | ||
this.mapActionToRouter = mapActionToRouter; | ||
} | ||
Templates.prototype.route = function (action, args) { | ||
if (!this.mapActionToRouter[action]) | ||
throw templateError; | ||
return new TemplateRoute(action, args); | ||
}; | ||
Templates.prototype.router = function (action, args) { | ||
var _this = this; | ||
return function () { return _this.route(action, args); }; | ||
}; | ||
Templates.prototype.map = function (route) { | ||
var router = this.mapActionToRouter[route.action]; | ||
return router | ||
? Router | ||
.from(router) | ||
.getRoute$(route.args) | ||
: route; | ||
}; | ||
return Templates; | ||
}()); | ||
exports.Templates = Templates; | ||
var DoRoute = /** @class */ (function (_super) { | ||
@@ -158,2 +197,3 @@ __extends(DoRoute, _super); | ||
var getRouteError = new Error('router must be a function'); | ||
var route$Error = new Error('route must be a DoRoute or a NoRoute'); | ||
var Router = /** @class */ (function () { | ||
@@ -199,6 +239,9 @@ function Router(router) { | ||
.getRoute$(arg) | ||
.filter(DoRoute.is) | ||
.flatMap(function (route) { return toObservable(route.action()); }) | ||
.mapTo(true) | ||
.defaultIfEmpty(false); | ||
.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; | ||
}); | ||
}; | ||
@@ -216,5 +259,10 @@ Router.prototype.route = function (arg) { | ||
}; | ||
Router.prototype.mapByType = function (mapTypeToFlexRouter) { | ||
return this.map(typeRouter(mapTypeToFlexRouter)); | ||
Router.prototype.mapByType = function (mapTypeToRouter) { | ||
return this.map(typeRouter(mapTypeToRouter)); | ||
}; | ||
Router.prototype.mapTemplate = function (templates) { | ||
return this.mapByType({ | ||
template: function (route) { return templates.map(route); } | ||
}); | ||
}; | ||
Router.prototype.tap = function (fn) { | ||
@@ -335,4 +383,4 @@ return this.map(function (route) { return toObservable(fn(route)).mapTo(route); }); | ||
case 6: | ||
if (!ScoredRoute.is(route)) return [3 /*break*/, 8]; | ||
return [4 /*yield*/, 'scored']; | ||
if (!TemplateRoute.is(route)) return [3 /*break*/, 8]; | ||
return [4 /*yield*/, 'template']; | ||
case 7: | ||
@@ -342,10 +390,16 @@ _a.sent(); | ||
case 8: | ||
if (!Route.is(route)) return [3 /*break*/, 10]; | ||
return [4 /*yield*/, 'route']; | ||
if (!ScoredRoute.is(route)) return [3 /*break*/, 10]; | ||
return [4 /*yield*/, 'scored']; | ||
case 9: | ||
_a.sent(); | ||
_a.label = 10; | ||
case 10: return [4 /*yield*/, 'default']; | ||
case 10: | ||
if (!Route.is(route)) return [3 /*break*/, 12]; | ||
return [4 /*yield*/, 'route']; | ||
case 11: | ||
_a.sent(); | ||
_a.label = 12; | ||
case 12: return [4 /*yield*/, 'default']; | ||
case 13: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
@@ -356,3 +410,3 @@ } | ||
exports.getTypesFromRoute = getTypesFromRoute; | ||
function typeRouter(mapTypeToFlexRouter) { | ||
function typeRouter(mapTypeToRouter) { | ||
return Router.from(function (route) { | ||
@@ -362,3 +416,3 @@ try { | ||
var type = _b.value; | ||
var router = mapTypeToFlexRouter[type]; | ||
var router = mapTypeToRouter[type]; | ||
if (router) | ||
@@ -365,0 +419,0 @@ return Router.from(router).getRoute$(route); |
{ | ||
"name": "prague-fluent", | ||
"version": "0.17.2", | ||
"version": "0.17.3", | ||
"description": "fluent API for Prague", | ||
@@ -5,0 +5,0 @@ "main": "dist/fluent.js", |
@@ -38,3 +38,3 @@ import { Observable } from 'rxjs'; | ||
static is (route: Route): route is ScoredRoute { | ||
static is (route: any): route is ScoredRoute { | ||
return route instanceof ScoredRoute; | ||
@@ -60,2 +60,58 @@ } | ||
export class TemplateRoute <ACTION, ARGS> extends ScoredRoute { | ||
constructor ( | ||
public action: ACTION, | ||
public args: ARGS, | ||
score?: number | ||
) { | ||
super(score); | ||
} | ||
static is <ACTION, ARGS> (route: any): route is TemplateRoute<ACTION, ARGS> { | ||
return route instanceof TemplateRoute; | ||
} | ||
} | ||
export type TemplateActions <TEMPLATES> = keyof TEMPLATES; | ||
export type MapTemplateActionToRouter <TEMPLATES> = { [P in TemplateActions<TEMPLATES>]: AnyRouter<TEMPLATES[P]> } | ||
const templateError = new Error('action not present in mapActionToRouter') | ||
export class Templates <TEMPLATES> { | ||
constructor( | ||
private mapActionToRouter: Partial<MapTemplateActionToRouter<TEMPLATES>> | ||
) { | ||
} | ||
route <ACTION extends keyof TEMPLATES, ARGS extends TEMPLATES[ACTION]> ( | ||
action: ACTION, | ||
args: ARGS | ||
) { | ||
if (!this.mapActionToRouter[action]) | ||
throw templateError; | ||
return new TemplateRoute(action, args); | ||
} | ||
router <ACTION extends keyof TEMPLATES, ARGS extends TEMPLATES[ACTION]> ( | ||
action: ACTION, | ||
args: ARGS | ||
) { | ||
return () => this.route(action, args); | ||
} | ||
map ( | ||
route: TemplateRoute<keyof TEMPLATES, TEMPLATES[keyof TEMPLATES]> | ||
) { | ||
const router: AnyRouter<TEMPLATES[keyof TEMPLATES], any> = this.mapActionToRouter[route.action]; | ||
return router | ||
? Router | ||
.from(router) | ||
.getRoute$(route.args) | ||
: route; | ||
} | ||
} | ||
export type Action = () => Observableable<any>; | ||
@@ -71,3 +127,3 @@ | ||
static is (route: Route): route is DoRoute { | ||
static is (route: any): route is DoRoute { | ||
return route instanceof DoRoute; | ||
@@ -94,3 +150,3 @@ } | ||
static is <VALUE = any> (route: Route<VALUE>): route is MatchRoute<VALUE> { | ||
static is <VALUE = any> (route: any): route is MatchRoute<VALUE> { | ||
return route instanceof MatchRoute; | ||
@@ -123,3 +179,3 @@ } | ||
static is <VALUE = any> (route: Route<VALUE>): route is NoRoute<VALUE> { | ||
static is <VALUE = any> (route: any): route is NoRoute<VALUE> { | ||
return route instanceof NoRoute; | ||
@@ -141,2 +197,3 @@ } | ||
const getRouteError = new Error('router must be a function'); | ||
const route$Error = new Error('route must be a DoRoute or a NoRoute'); | ||
@@ -189,3 +246,3 @@ export type AnyRouter <ARG = undefined, VALUE = any> = GetRoute<ARG, VALUE> | Router<ARG, VALUE> | ((arg?: ARG) => Observableable<Router<undefined, VALUE>>); | ||
static is <ARG, VALUE> (router: any | AnyRouter<ARG, VALUE>): router is Router<ARG, VALUE> { | ||
static is <ARG, VALUE> (router: any): router is Router<ARG, VALUE> { | ||
return router instanceof Router; | ||
@@ -197,6 +254,9 @@ } | ||
.getRoute$(arg) | ||
.filter(DoRoute.is) | ||
.flatMap(route => toObservable(route.action())) | ||
.mapTo(true) | ||
.defaultIfEmpty(false); | ||
.flatMap(route => { | ||
if (DoRoute.is(route)) | ||
return toObservable(route.action()).mapTo(true); | ||
if (NoRoute.is(route)) | ||
return Observable.of(false); | ||
throw route$Error; | ||
}); | ||
} | ||
@@ -218,7 +278,15 @@ | ||
mapByType ( | ||
mapTypeToFlexRouter: Partial<MapTypeToRouter<VALUE>> | ||
mapTypeToRouter: Partial<MapTypeToRouter<VALUE>> | ||
) { | ||
return this.map(typeRouter(mapTypeToFlexRouter)); | ||
return this.map(typeRouter(mapTypeToRouter)); | ||
} | ||
mapTemplate <TEMPLATES> ( | ||
templates: Templates<TEMPLATES> | ||
) { | ||
return this.mapByType({ | ||
template: route => templates.map(route) | ||
}); | ||
} | ||
tap <VALUE> (fn: (route: Route<VALUE>) => Observableable<any>) { | ||
@@ -346,3 +414,3 @@ return this.map(route => toObservable(fn(route)).mapTo(route)); | ||
return Router | ||
.from() | ||
.from() | ||
.tap(action) | ||
@@ -356,2 +424,3 @@ } | ||
match: MatchRoute<VALUE>, | ||
template: TemplateRoute<any, any>, | ||
no: NoRoute<VALUE>, | ||
@@ -375,2 +444,5 @@ default: Route, | ||
if (TemplateRoute.is(route)) | ||
yield 'template'; | ||
if (ScoredRoute.is(route)) | ||
@@ -388,7 +460,7 @@ yield 'scored'; | ||
function typeRouter <VALUE> ( | ||
mapTypeToFlexRouter: Partial<MapTypeToRouter<VALUE>> | ||
mapTypeToRouter: Partial<MapTypeToRouter<VALUE>> | ||
) { | ||
return Router.from((route: Route<VALUE>) => { | ||
for (let type of getTypesFromRoute(route)) { | ||
const router = mapTypeToFlexRouter[type]; | ||
const router = mapTypeToRouter[type]; | ||
if (router) | ||
@@ -395,0 +467,0 @@ return Router.from(router).getRoute$(route); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53436
1055